]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
enhance: Add Args initializer list constructor
authorJoel Rosdahl <joel@rosdahl.net>
Mon, 21 Jul 2025 19:00:27 +0000 (21:00 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 22 Jul 2025 12:45:52 +0000 (14:45 +0200)
src/ccache/args.cpp
src/ccache/args.hpp
unittest/test_args.cpp

index d1dcf16fcbc51bfdabff48a8c221c75a43b40c5d..bfdf1b368bb6fda840681b2564dfe2001711e8a0 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2020-2024 Joel Rosdahl and other contributors
+// Copyright (C) 2020-2025 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -30,6 +30,11 @@ Args::Args(Args&& other) noexcept
 {
 }
 
+Args::Args(std::initializer_list<std::string> init) noexcept
+{
+  m_args.assign(init.begin(), init.end());
+}
+
 Args
 Args::from_argv(int argc, const char* const* argv)
 {
index 4f7b10efe18989fa4e9ab85b683d859cd4ef6d2c..6b46fa507ffce040a3844ab5addc69a0c149b9c1 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2020-2024 Joel Rosdahl and other contributors
+// Copyright (C) 2020-2025 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -20,6 +20,7 @@
 
 #include <deque>
 #include <filesystem>
+#include <initializer_list>
 #include <optional>
 #include <string>
 #include <string_view>
@@ -35,6 +36,7 @@ public:
   };
 
   Args() = default;
+  Args(std::initializer_list<std::string>) noexcept;
   Args(const Args& other) = default;
   Args(Args&& other) noexcept;
 
index 650e2bac396319d224b2cf1cb60ba45ae8f7c41c..549a106c57ef21958e708c83a4ee782211a47f4d 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2020-2024 Joel Rosdahl and other contributors
+// Copyright (C) 2020-2025 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -34,21 +34,24 @@ TEST_CASE("Args default constructor")
   CHECK(args.size() == 0);
 }
 
-TEST_CASE("Args copy constructor")
+TEST_CASE("Args initializer list constructor")
 {
-  Args args1;
-  args1.push_back("foo");
-  args1.push_back("bar");
+  Args args{"foo", "bar"};
+  CHECK(args.size() == 2);
+  CHECK(args[0] == "foo");
+  CHECK(args[1] == "bar");
+}
 
+TEST_CASE("Args copy constructor")
+{
+  Args args1{"foo", "bar"};
   Args args2(args1);
   CHECK(args1 == args2);
 }
 
 TEST_CASE("Args move constructor")
 {
-  Args args1;
-  args1.push_back("foo");
-  args1.push_back("bar");
+  Args args1{"foo", "bar"};
   const char* foo_pointer = args1[0].c_str();
   const char* bar_pointer = args1[1].c_str();