From: Joel Rosdahl Date: Mon, 21 Jul 2025 19:00:27 +0000 (+0200) Subject: enhance: Add Args initializer list constructor X-Git-Tag: v4.12~74 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=4c2d2d2a34de3f8002195af4ef69bdb32187f356;p=thirdparty%2Fccache.git enhance: Add Args initializer list constructor --- diff --git a/src/ccache/args.cpp b/src/ccache/args.cpp index d1dcf16f..bfdf1b36 100644 --- a/src/ccache/args.cpp +++ b/src/ccache/args.cpp @@ -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 init) noexcept +{ + m_args.assign(init.begin(), init.end()); +} + Args Args::from_argv(int argc, const char* const* argv) { diff --git a/src/ccache/args.hpp b/src/ccache/args.hpp index 4f7b10ef..6b46fa50 100644 --- a/src/ccache/args.hpp +++ b/src/ccache/args.hpp @@ -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 #include +#include #include #include #include @@ -35,6 +36,7 @@ public: }; Args() = default; + Args(std::initializer_list) noexcept; Args(const Args& other) = default; Args(Args&& other) noexcept; diff --git a/unittest/test_args.cpp b/unittest/test_args.cpp index 650e2bac..549a106c 100644 --- a/unittest/test_args.cpp +++ b/unittest/test_args.cpp @@ -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();