diff options
| author | 2022-12-12 19:30:44 +0000 | |
|---|---|---|
| committer | 2023-07-19 17:52:58 +0100 | |
| commit | a6507b5c18c54b77dad169b37fc3b6ec3c0bc608 (patch) | |
| tree | 38dd20f4aa09870dd49933a6a47a40a19bf3d366 | |
| parent | 414018e27d82661ec718a8641c7fa7c36a747018 (diff) | |
Add `Concat` to `CmdlineBuilder`.
(partially cherry picked from commit 7b6aef7770375817cfc245ed5f03c1c55218a7a1)
Bug: 291903090
Test: m test-art-host-gtest-art_libarttools_tests
Merged-In: Ia8068d804294debe0de6c947f3878da4dbf8c8ca
Change-Id: I4dd0b59fda1f3416b8d17a4715030d92db70b5b0
| -rw-r--r-- | libarttools/tools/cmdline_builder.h | 11 | ||||
| -rw-r--r-- | libarttools/tools/cmdline_builder_test.cc | 15 |
2 files changed, 26 insertions, 0 deletions
diff --git a/libarttools/tools/cmdline_builder.h b/libarttools/tools/cmdline_builder.h index 13b79cacd0..fd11ee87ba 100644 --- a/libarttools/tools/cmdline_builder.h +++ b/libarttools/tools/cmdline_builder.h @@ -17,6 +17,8 @@ #ifndef ART_LIBARTTOOLS_TOOLS_CMDLINE_BUILDER_H_ #define ART_LIBARTTOOLS_TOOLS_CMDLINE_BUILDER_H_ +#include <algorithm> +#include <iterator> #include <string> #include <string_view> #include <vector> @@ -135,6 +137,15 @@ class CmdlineBuilder { return *this; } + // Concatenates this builder with another. Returns the concatenated result and nullifies the input + // builder. + CmdlineBuilder& Concat(CmdlineBuilder&& other) { + elements_.reserve(elements_.size() + other.elements_.size()); + std::move(other.elements_.begin(), other.elements_.end(), std::back_inserter(elements_)); + other.elements_.clear(); + return *this; + } + private: std::vector<std::string> elements_; }; diff --git a/libarttools/tools/cmdline_builder_test.cc b/libarttools/tools/cmdline_builder_test.cc index 8509f73e66..55518607db 100644 --- a/libarttools/tools/cmdline_builder_test.cc +++ b/libarttools/tools/cmdline_builder_test.cc @@ -16,6 +16,8 @@ #include "cmdline_builder.h" +#include <utility> + #include "gmock/gmock.h" #include "gtest/gtest.h" @@ -115,6 +117,19 @@ TEST_F(CmdlineBuilderTest, AddRuntimeIfFalse) { EXPECT_THAT(args_.Get(), IsEmpty()); } +TEST_F(CmdlineBuilderTest, Concat) { + args_.Add("--flag1"); + args_.Add("--flag2"); + + CmdlineBuilder other; + other.Add("--flag3"); + other.Add("--flag4"); + + args_.Concat(std::move(other)); + EXPECT_THAT(args_.Get(), ElementsAre("--flag1", "--flag2", "--flag3", "--flag4")); + EXPECT_THAT(other.Get(), IsEmpty()); +} + } // namespace } // namespace tools } // namespace art |