about summary refs log tree commit diff
path: root/absl/flags/internal/flag.h
diff options
context:
space:
mode:
authorAbseil Team <absl-team@google.com>2020-01-03T16·41-0800
committerAndy Soffer <asoffer@google.com>2020-01-03T18·41-0500
commit1de0166368e2ae67347f92099d6dca3ab3a4a496 (patch)
treecad7784fab3a1d673d140143f0d6eb70b16dde96 /absl/flags/internal/flag.h
parentad904b6cd3906ddf79878003d92b7bc08d7786ae (diff)
Export of internal Abseil changes
--
330051e00cd57ee516b4eaf656965656ffbcd0bc by Abseil Team <absl-team@google.com>:

Fix indentation in comment.

PiperOrigin-RevId: 287997504

--
35fb1a893e708031ba4fc0db460875eb0d31820e by Abseil Team <absl-team@google.com>:

Enable compile-time enforcement that absl::Substitute patterns to not contain unescaped $ symbols.

absl::Substitute already considers unescaped $ symbols undefined behavior and crashes when it's passed them in debug builds.  Some code isn't ever built in debug mode, though, and inadvertently used some unescaped $ symbols, which led to surprising results.  This change will prevent that problem from happening in the future.
PiperOrigin-RevId: 287906643

--
c5762833ebde6d7110bf68041a823b571c238e9e by Gennadiy Rozental <rogeeff@google.com>:

Move all the flag data into a single place instead of being split between handle and flag object.

After this change CommandLineFlag will not hold any data anymore. And we also do not need to pass the CommandLineFlag around in Abseil Flag implementation to report flag name and location.

PiperOrigin-RevId: 287899076

--
8b5fb644f1e3d9267b7a75106fe9a72c886db786 by Derek Mauro <dmauro@google.com>:

Upgrade CI testing to Bazel 2.0.0 and Clang 407ac2eb5f13

-fno-sanitize-blacklist is to workaround
https://github.com/bazelbuild/bazel/issues/10510

PiperOrigin-RevId: 287875363

--
a20cc1d58895de2babc3748a6c79d1d6813734ef by Abseil Team <absl-team@google.com>:

Make ABSL_RETIRED_FLAG behave consistently with ABSL_FLAG.

Before the change:
ABSL_RETIRED_FLAG does not compile when there are competing ctors in the type, even when ABSL_FLAG does.

After the change:
ABSL_RETIRED_FLAG compiles when ABSL_FLAG does.

PiperOrigin-RevId: 286483183

--
1cff7e67329d2be9e50bee1f2e76ef9ffd2edde5 by Abseil Team <absl-team@google.com>:

Support C++20 erase_if API in unordered associative containers

See [unord.set.erasure]: https://eel.is/c++draft/unord.set.erasure
See [unord.map.erasure]: https://eel.is/c++draft/unord.map.erasure

PiperOrigin-RevId: 286461140
GitOrigin-RevId: 330051e00cd57ee516b4eaf656965656ffbcd0bc
Change-Id: I5513110b41c2af08a44da54612cff341ac5c6607
Diffstat (limited to 'absl/flags/internal/flag.h')
-rw-r--r--absl/flags/internal/flag.h85
1 files changed, 39 insertions, 46 deletions
diff --git a/absl/flags/internal/flag.h b/absl/flags/internal/flag.h
index 21a10c7f85f8..7d5271c40de6 100644
--- a/absl/flags/internal/flag.h
+++ b/absl/flags/internal/flag.h
@@ -82,6 +82,8 @@ struct HelpInitArg {
   FlagHelpSrcKind kind;
 };
 
+extern const char kStrippedFlagHelp[];
+
 // HelpConstexprWrap is used by struct AbslFlagHelpGenFor##name generated by
 // ABSL_FLAG macro. It is only used to silence the compiler in the case where
 // help message expression is not constexpr and does not have type const char*.
@@ -148,11 +150,14 @@ struct DynValueDeleter {
 // The class encapsulates the Flag's data and safe access to it.
 class FlagImpl {
  public:
-  constexpr FlagImpl(const flags_internal::FlagOpFn op,
+  constexpr FlagImpl(const char* name, const char* filename,
+                     const flags_internal::FlagOpFn op,
                      const flags_internal::FlagMarshallingOpFn marshalling_op,
-                     const flags_internal::FlagDfltGenFunc default_value_gen,
-                     const HelpInitArg help)
-      : op_(op),
+                     const HelpInitArg help,
+                     const flags_internal::FlagDfltGenFunc default_value_gen)
+      : name_(name),
+        filename_(filename),
+        op_(op),
         marshalling_op_(marshalling_op),
         help_(help.source),
         help_source_kind_(help.kind),
@@ -164,18 +169,18 @@ class FlagImpl {
   void Destroy();
 
   // Constant access methods
+  absl::string_view Name() const;
+  std::string Filename() const;
   std::string Help() const;
   bool IsModified() const ABSL_LOCKS_EXCLUDED(*DataGuard());
   bool IsSpecifiedOnCommandLine() const ABSL_LOCKS_EXCLUDED(*DataGuard());
   std::string DefaultValue() const ABSL_LOCKS_EXCLUDED(*DataGuard());
   std::string CurrentValue() const ABSL_LOCKS_EXCLUDED(*DataGuard());
-  void Read(const CommandLineFlag& flag, void* dst,
-            const flags_internal::FlagOpFn dst_op) const
+  void Read(void* dst, const flags_internal::FlagOpFn dst_op) const
       ABSL_LOCKS_EXCLUDED(*DataGuard());
   // Attempts to parse supplied `value` std::string. If parsing is successful, then
   // it replaces `dst` with the new value.
-  bool TryParse(const CommandLineFlag& flag, void** dst,
-                absl::string_view value, std::string* err) const
+  bool TryParse(void** dst, absl::string_view value, std::string* err) const
       ABSL_EXCLUSIVE_LOCKS_REQUIRED(*DataGuard());
   template <typename T>
   bool AtomicGet(T* v) const {
@@ -189,12 +194,11 @@ class FlagImpl {
   }
 
   // Mutating access methods
-  void Write(const CommandLineFlag& flag, const void* src,
-             const flags_internal::FlagOpFn src_op)
+  void Write(const void* src, const flags_internal::FlagOpFn src_op)
+      ABSL_LOCKS_EXCLUDED(*DataGuard());
+  bool SetFromString(absl::string_view value, FlagSettingMode set_mode,
+                     ValueSource source, std::string* err)
       ABSL_LOCKS_EXCLUDED(*DataGuard());
-  bool SetFromString(const CommandLineFlag& flag, absl::string_view value,
-                     FlagSettingMode set_mode, ValueSource source,
-                     std::string* err) ABSL_LOCKS_EXCLUDED(*DataGuard());
   // If possible, updates copy of the Flag's value that is stored in an
   // atomic word.
   void StoreAtomic() ABSL_EXCLUSIVE_LOCKS_REQUIRED(*DataGuard());
@@ -214,12 +218,11 @@ class FlagImpl {
     return absl::make_unique<flags_internal::FlagState<T>>(
         flag, std::move(cur_value), modified_, on_command_line_, counter_);
   }
-  bool RestoreState(const CommandLineFlag& flag, const void* value,
-                    bool modified, bool on_command_line, int64_t counter)
-      ABSL_LOCKS_EXCLUDED(*DataGuard());
+  bool RestoreState(const void* value, bool modified, bool on_command_line,
+                    int64_t counter) ABSL_LOCKS_EXCLUDED(*DataGuard());
 
   // Value validation interfaces.
-  void CheckDefaultValueParsingRoundtrip(const CommandLineFlag& flag) const
+  void CheckDefaultValueParsingRoundtrip() const
       ABSL_LOCKS_EXCLUDED(*DataGuard());
   bool ValidateInputValue(absl::string_view value) const
       ABSL_LOCKS_EXCLUDED(*DataGuard());
@@ -235,7 +238,10 @@ class FlagImpl {
       ABSL_EXCLUSIVE_LOCKS_REQUIRED(*DataGuard());
 
   // Immutable Flag's data.
-  const FlagOpFn op_;                         // Type-specific handler.
+  // Constant configuration for a particular flag.
+  const char* const name_;      // Flags name passed to ABSL_FLAG as second arg.
+  const char* const filename_;  // The file name where ABSL_FLAG resides.
+  const FlagOpFn op_;           // Type-specific handler.
   const FlagMarshallingOpFn marshalling_op_;  // Marshalling ops handler.
   const FlagHelpSrc help_;  // Help message literal or function to generate it.
   // Indicates if help message was supplied as literal or generator func.
@@ -286,13 +292,12 @@ class FlagImpl {
 template <typename T>
 class Flag final : public flags_internal::CommandLineFlag {
  public:
-  constexpr Flag(const char* name, const flags_internal::HelpInitArg help,
-                 const char* filename,
+  constexpr Flag(const char* name, const char* filename,
                  const flags_internal::FlagMarshallingOpFn marshalling_op,
-                 const flags_internal::FlagDfltGenFunc initial_value_gen)
-      : flags_internal::CommandLineFlag(name, filename),
-        impl_(&flags_internal::FlagOps<T>, marshalling_op, initial_value_gen,
-              help) {}
+                 const flags_internal::HelpInitArg help,
+                 const flags_internal::FlagDfltGenFunc default_value_gen)
+      : impl_(name, filename, &flags_internal::FlagOps<T>, marshalling_op, help,
+              default_value_gen) {}
 
   T Get() const {
     // See implementation notes in CommandLineFlag::Get().
@@ -303,19 +308,22 @@ class Flag final : public flags_internal::CommandLineFlag {
     };
     U u;
 
-    impl_.Read(*this, &u.value, &flags_internal::FlagOps<T>);
+    impl_.Read(&u.value, &flags_internal::FlagOps<T>);
     return std::move(u.value);
   }
 
   bool AtomicGet(T* v) const { return impl_.AtomicGet(v); }
 
-  void Set(const T& v) { impl_.Write(*this, &v, &flags_internal::FlagOps<T>); }
+  void Set(const T& v) { impl_.Write(&v, &flags_internal::FlagOps<T>); }
 
   void SetCallback(const flags_internal::FlagCallback mutation_callback) {
     impl_.SetCallback(mutation_callback);
   }
 
   // CommandLineFlag interface
+  absl::string_view Name() const override { return impl_.Name(); }
+  std::string Filename() const override { return impl_.Filename(); }
+  absl::string_view Typename() const override { return ""; }
   std::string Help() const override { return impl_.Help(); }
   bool IsModified() const override { return impl_.IsModified(); }
   bool IsSpecifiedOnCommandLine() const override {
@@ -338,20 +346,19 @@ class Flag final : public flags_internal::CommandLineFlag {
   // Restores the flag state to the supplied state object. If there is
   // nothing to restore returns false. Otherwise returns true.
   bool RestoreState(const flags_internal::FlagState<T>& flag_state) {
-    return impl_.RestoreState(*this, &flag_state.cur_value_,
-                              flag_state.modified_, flag_state.on_command_line_,
-                              flag_state.counter_);
+    return impl_.RestoreState(&flag_state.cur_value_, flag_state.modified_,
+                              flag_state.on_command_line_, flag_state.counter_);
   }
 
   bool SetFromString(absl::string_view value,
                      flags_internal::FlagSettingMode set_mode,
                      flags_internal::ValueSource source,
                      std::string* error) override {
-    return impl_.SetFromString(*this, value, set_mode, source, error);
+    return impl_.SetFromString(value, set_mode, source, error);
   }
 
   void CheckDefaultValueParsingRoundtrip() const override {
-    impl_.CheckDefaultValueParsingRoundtrip(*this);
+    impl_.CheckDefaultValueParsingRoundtrip();
   }
 
  private:
@@ -360,7 +367,7 @@ class Flag final : public flags_internal::CommandLineFlag {
   void Destroy() override { impl_.Destroy(); }
 
   void Read(void* dst) const override {
-    impl_.Read(*this, dst, &flags_internal::FlagOps<T>);
+    impl_.Read(dst, &flags_internal::FlagOps<T>);
   }
   flags_internal::FlagOpFn TypeId() const override {
     return &flags_internal::FlagOps<T>;
@@ -416,20 +423,6 @@ T* MakeFromDefaultValue(EmptyBraces) {
   return new T;
 }
 
-// MakeFromDefaultValueOrEmpty is basically the same as MakeFromDefaultValue. It
-// also allows for empty macro parameter (hence no argument), which was somehow
-// widely used for ABSL_RETIRED_FLAG().
-
-template <typename T>
-T* MakeFromDefaultValueOrEmpty(T t) {
-  return MakeFromDefaultValue(std::move(t));
-}
-
-template <typename T>
-T* MakeFromDefaultValueOrEmpty() {
-  return MakeFromDefaultValue<T>(EmptyBraces());
-}
-
 }  // namespace flags_internal
 ABSL_NAMESPACE_END
 }  // namespace absl