diff options
author | Abseil Team <absl-team@google.com> | 2020-08-20 08:54:55 -0700 |
---|---|---|
committer | Gennadiy Rozental <rogeeff@google.com> | 2020-08-20 15:09:31 -0400 |
commit | fbf0fdab62ac6fcc72b935d505f6837e0884fdc3 (patch) | |
tree | bec3225c3c50143e0f3b4e2b2ff72a931de6c245 /absl/flags | |
parent | dc969f34a79d019497abb61c2a3f79b5b4be2ea9 (diff) |
Export of internal Abseil changes
--
9f746b79e16d36dba908ed9e2a586d890fe54acc by Derek Mauro <dmauro@google.com>:
Remove the Bazel mirror URL that we use in our WORKSPACE file
This doesn't appear to be supported. We use --distdir caching in our tests.
PiperOrigin-RevId: 327634738
--
e1f6f54830c415fc8bb8dc14483fae4cf9713d75 by Abseil Team <absl-team@google.com>:
Removing comments following license() rules as they are rarely useful, redundant with LICENSE files, and tend to fall out of date.
PiperOrigin-RevId: 327504063
--
b59c076638a57a74a3f8475f1dee8b536e32a15f by Gennadiy Rozental <rogeeff@google.com>:
Change GetAllFlags API to return a collection of mutable flag handles.
This will make this interface on par with FindCommandLineFlag and allow to call
CommandLineFlag::ParseFrom on the handle returned by GetAllFlags.
PiperOrigin-RevId: 327499084
--
bbf56b3c4a1d908d95e5a52aba38b1984151efff by Gennadiy Rozental <rogeeff@google.com>:
Make raw_hash_set compile when AllocTraits::propagate_on_container_swap is false.
PiperOrigin-RevId: 327371107
--
837f62c85ea65c1d6b847a75816198c625fe62ff by Abseil Team <absl-team@google.com>:
On macOS, `mem_alloc` is defined like this:
rpc/types.h:86:#define mem_alloc(bsize) calloc(1, bsize)
So if that file is included before container_memory.h, the variable mem_alloc
may never get defined. This is fixed by using a different name.
PiperOrigin-RevId: 327360224
--
aa8f5528382c0d01239ce75b645723aaf7e1ef46 by Gennadiy Rozental <rogeeff@google.com>:
Release of absl::GetAllFlags API.
PiperOrigin-RevId: 327275943
GitOrigin-RevId: 9f746b79e16d36dba908ed9e2a586d890fe54acc
Change-Id: I99c5c87dd1712bf8df9a52397b0c1e400a3c3447
Diffstat (limited to 'absl/flags')
-rw-r--r-- | absl/flags/BUILD.bazel | 4 | ||||
-rw-r--r-- | absl/flags/CMakeLists.txt | 4 | ||||
-rw-r--r-- | absl/flags/reflection.cc | 10 | ||||
-rw-r--r-- | absl/flags/reflection.h | 7 | ||||
-rw-r--r-- | absl/flags/reflection_test.cc | 31 |
5 files changed, 53 insertions, 3 deletions
diff --git a/absl/flags/BUILD.bazel b/absl/flags/BUILD.bazel index 92bd4f15..62fb9a8b 100644 --- a/absl/flags/BUILD.bazel +++ b/absl/flags/BUILD.bazel @@ -24,7 +24,7 @@ load( package(default_visibility = ["//visibility:public"]) -licenses(["notice"]) # Apache 2.0 +licenses(["notice"]) cc_library( name = "path_util", @@ -179,6 +179,7 @@ cc_library( ":private_handle_accessor", "//absl/base:config", "//absl/base:core_headers", + "//absl/container:flat_hash_map", "//absl/strings", "//absl/synchronization", ], @@ -464,6 +465,7 @@ cc_test( ":flag", ":marshalling", ":reflection", + ":usage_internal", "//absl/memory", "//absl/strings", "@com_google_googletest//:gtest_main", diff --git a/absl/flags/CMakeLists.txt b/absl/flags/CMakeLists.txt index 343774de..28bd5a85 100644 --- a/absl/flags/CMakeLists.txt +++ b/absl/flags/CMakeLists.txt @@ -165,6 +165,7 @@ absl_cc_library( absl::flags_config absl::strings absl::synchronization + absl::flat_hash_map ) # Internal-only target, do not depend on directly. @@ -407,9 +408,10 @@ absl_cc_test( absl::flags_commandlineflag_internal absl::flags absl::flags_reflection + absl::flags_usage absl::memory absl::strings - gtest_main + gmock_main ) absl_cc_test( diff --git a/absl/flags/reflection.cc b/absl/flags/reflection.cc index e4145b34..d7060221 100644 --- a/absl/flags/reflection.cc +++ b/absl/flags/reflection.cc @@ -303,5 +303,15 @@ CommandLineFlag* FindCommandLineFlag(absl::string_view name) { return registry.FindFlagLocked(name); } +// -------------------------------------------------------------------- + +absl::flat_hash_map<absl::string_view, absl::CommandLineFlag*> GetAllFlags() { + absl::flat_hash_map<absl::string_view, absl::CommandLineFlag*> res; + flags_internal::ForEachFlag([&](CommandLineFlag& flag) { + res.insert({flag.Name(), &flag}); + }); + return res; +} + ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/flags/reflection.h b/absl/flags/reflection.h index 045f9784..4ce0ab6c 100644 --- a/absl/flags/reflection.h +++ b/absl/flags/reflection.h @@ -26,6 +26,7 @@ #include <string> #include "absl/base/config.h" +#include "absl/container/flat_hash_map.h" #include "absl/flags/commandlineflag.h" #include "absl/flags/internal/commandlineflag.h" @@ -40,7 +41,11 @@ class FlagSaverImpl; // Returns the reflection handle of an Abseil flag of the specified name, or // `nullptr` if not found. This function will emit a warning if the name of a // 'retired' flag is specified. -CommandLineFlag* FindCommandLineFlag(absl::string_view name); +absl::CommandLineFlag* FindCommandLineFlag(absl::string_view name); + +// Returns current state of the Flags registry in a form of mapping from flag +// name to a flag reflection handle. +absl::flat_hash_map<absl::string_view, absl::CommandLineFlag*> GetAllFlags(); //------------------------------------------------------------------------------ // FlagSaver diff --git a/absl/flags/reflection_test.cc b/absl/flags/reflection_test.cc index 2da0a0ec..1a1dcb4a 100644 --- a/absl/flags/reflection_test.cc +++ b/absl/flags/reflection_test.cc @@ -18,7 +18,9 @@ #include <memory> #include <string> +#include "gmock/gmock.h" #include "gtest/gtest.h" +#include "absl/flags/declare.h" #include "absl/flags/flag.h" #include "absl/flags/internal/commandlineflag.h" #include "absl/flags/marshalling.h" @@ -30,6 +32,8 @@ ABSL_FLAG(int, int_flag, 1, "int_flag help"); ABSL_FLAG(std::string, string_flag, "dflt", "string_flag help"); ABSL_RETIRED_FLAG(bool, bool_retired_flag, false, "bool_retired_flag help"); +ABSL_DECLARE_FLAG(bool, help); + namespace { namespace flags = absl::flags_internal; @@ -61,6 +65,33 @@ TEST_F(ReflectionTest, TestFindCommandLineFlag) { // -------------------------------------------------------------------- +TEST_F(ReflectionTest, TestGetAllFlags) { + (void)absl::GetFlag(FLAGS_help); // Force linking of usage flags. + + auto all_flags = absl::GetAllFlags(); + EXPECT_NE(all_flags.find("int_flag"), all_flags.end()); + EXPECT_NE(all_flags.find("bool_retired_flag"), all_flags.end()); + EXPECT_NE(all_flags.find("help"), all_flags.end()); + EXPECT_EQ(all_flags.find("some_undefined_flag"), all_flags.end()); + + std::vector<absl::string_view> flag_names_first_attempt; + auto all_flags_1 = absl::GetAllFlags(); + for (auto f : all_flags_1) { + flag_names_first_attempt.push_back(f.first); + } + + std::vector<absl::string_view> flag_names_second_attempt; + auto all_flags_2 = absl::GetAllFlags(); + for (auto f : all_flags_2) { + flag_names_second_attempt.push_back(f.first); + } + + EXPECT_THAT(flag_names_first_attempt, + ::testing::UnorderedElementsAreArray(flag_names_second_attempt)); +} + +// -------------------------------------------------------------------- + struct CustomUDT { CustomUDT() : a(1), b(1) {} CustomUDT(int a_, int b_) : a(a_), b(b_) {} |