about summary refs log tree commit diff
path: root/absl/flags
diff options
context:
space:
mode:
Diffstat (limited to 'absl/flags')
-rw-r--r--absl/flags/BUILD.bazel1
-rw-r--r--absl/flags/CMakeLists.txt1
-rw-r--r--absl/flags/internal/commandlineflag.h22
-rw-r--r--absl/flags/internal/flag.cc34
-rw-r--r--absl/flags/internal/flag.h46
-rw-r--r--absl/flags/internal/registry.cc8
-rw-r--r--absl/flags/internal/registry.h4
7 files changed, 64 insertions, 52 deletions
diff --git a/absl/flags/BUILD.bazel b/absl/flags/BUILD.bazel
index 908e77612be6..4b51d9d45207 100644
--- a/absl/flags/BUILD.bazel
+++ b/absl/flags/BUILD.bazel
@@ -147,6 +147,7 @@ cc_library(
         ":marshalling",
         "//absl/base:config",
         "//absl/base:core_headers",
+        "//absl/base:fast_type_id",
         "//absl/strings",
         "//absl/types:optional",
     ],
diff --git a/absl/flags/CMakeLists.txt b/absl/flags/CMakeLists.txt
index 01cf09b1b52a..2204b0ff2dd8 100644
--- a/absl/flags/CMakeLists.txt
+++ b/absl/flags/CMakeLists.txt
@@ -128,6 +128,7 @@ absl_cc_library(
     ${ABSL_DEFAULT_LINKOPTS}
   DEPS
     absl::config
+    absl::fast_type_id
     absl::flags_config
     absl::flags_marshalling
     absl::core_headers
diff --git a/absl/flags/internal/commandlineflag.h b/absl/flags/internal/commandlineflag.h
index 338a1228dacb..ef992f7f478d 100644
--- a/absl/flags/internal/commandlineflag.h
+++ b/absl/flags/internal/commandlineflag.h
@@ -24,6 +24,7 @@
 #include <typeinfo>
 
 #include "absl/base/config.h"
+#include "absl/base/internal/fast_type_id.h"
 #include "absl/base/macros.h"
 #include "absl/flags/config.h"
 #include "absl/flags/marshalling.h"
@@ -34,23 +35,12 @@ namespace absl {
 ABSL_NAMESPACE_BEGIN
 namespace flags_internal {
 
-// An alias for flag static type id. Values of type identify the flag value type
-// simialarly to typeid(T), but without relying on RTTI being available. In most
+// An alias for flag fast type id. This value identifies the flag value type
+// simialarly to typeid(T), without relying on RTTI being available. In most
 // cases this id is enough to uniquely identify the flag's value type. In a few
 // cases we'll have to resort to using actual RTTI implementation if it is
 // available.
-using FlagStaticTypeId = void* (*)();
-
-// Address of this function template is used in current implementation as a flag
-// static type id.
-template <typename T>
-void* FlagStaticTypeIdGen() {
-#if defined(ABSL_FLAGS_INTERNAL_HAS_RTTI)
-  return const_cast<std::type_info*>(&typeid(T));
-#else
-  return nullptr;
-#endif
-}
+using FlagFastTypeId = base_internal::FastTypeIdType;
 
 // Options that control SetCommandLineOptionWithMode.
 enum FlagSettingMode {
@@ -97,7 +87,7 @@ class CommandLineFlag {
   // Return true iff flag has type T.
   template <typename T>
   inline bool IsOfType() const {
-    return TypeId() == &flags_internal::FlagStaticTypeIdGen<T>;
+    return TypeId() == base_internal::FastTypeId<T>();
   }
 
   // Attempts to retrieve the flag value. Returns value on success,
@@ -150,7 +140,7 @@ class CommandLineFlag {
   // Returns true iff this is a handle to an Abseil Flag.
   virtual bool IsAbseilFlag() const;
   // Returns id of the flag's value type.
-  virtual FlagStaticTypeId TypeId() const = 0;
+  virtual FlagFastTypeId TypeId() const = 0;
   virtual bool IsModified() const = 0;
   virtual bool IsSpecifiedOnCommandLine() const = 0;
   virtual std::string DefaultValue() const = 0;
diff --git a/absl/flags/internal/flag.cc b/absl/flags/internal/flag.cc
index 5b4499abe47e..f3c424ad83d8 100644
--- a/absl/flags/internal/flag.cc
+++ b/absl/flags/internal/flag.cc
@@ -48,9 +48,9 @@ const char kStrippedFlagHelp[] = "\001\002\003\004 (unknown) \004\003\002\001";
 namespace {
 
 // Currently we only validate flag values for user-defined flag types.
-bool ShouldValidateFlagValue(FlagStaticTypeId flag_type_id) {
+bool ShouldValidateFlagValue(FlagFastTypeId flag_type_id) {
 #define DONT_VALIDATE(T) \
-  if (flag_type_id == &FlagStaticTypeIdGen<T>) return false;
+  if (flag_type_id == base_internal::FastTypeId<T>()) return false;
   ABSL_FLAGS_INTERNAL_BUILTIN_TYPES(DONT_VALIDATE)
 #undef DONT_VALIDATE
 
@@ -161,24 +161,24 @@ absl::Mutex* FlagImpl::DataGuard() const {
   return reinterpret_cast<absl::Mutex*>(&data_guard_);
 }
 
-void FlagImpl::AssertValidType(FlagStaticTypeId type_id) const {
-  FlagStaticTypeId this_type_id = flags_internal::StaticTypeId(op_);
+void FlagImpl::AssertValidType(FlagFastTypeId rhs_type_id,
+                               const std::type_info* (*gen_rtti)()) const {
+  FlagFastTypeId lhs_type_id = flags_internal::FastTypeId(op_);
 
-  // `type_id` is the type id corresponding to the declaration visibile at the
-  // call site. `this_type_id` is the type id corresponding to the type stored
-  // during flag definition. They must match for this operation to be
-  // well-defined.
-  if (ABSL_PREDICT_TRUE(type_id == this_type_id)) return;
+  // `rhs_type_id` is the fast type id corresponding to the declaration
+  // visibile at the call site. `lhs_type_id` is the fast type id
+  // corresponding to the type specified in flag definition. They must match
+  //  for this operation to be well-defined.
+  if (ABSL_PREDICT_TRUE(lhs_type_id == rhs_type_id)) return;
 
-  void* lhs_runtime_type_id = type_id();
-  void* rhs_runtime_type_id = this_type_id();
+  const std::type_info* lhs_runtime_type_id =
+      flags_internal::RuntimeTypeId(op_);
+  const std::type_info* rhs_runtime_type_id = (*gen_rtti)();
 
   if (lhs_runtime_type_id == rhs_runtime_type_id) return;
 
 #if defined(ABSL_FLAGS_INTERNAL_HAS_RTTI)
-  if (*reinterpret_cast<std::type_info*>(lhs_runtime_type_id) ==
-      *reinterpret_cast<std::type_info*>(rhs_runtime_type_id))
-    return;
+  if (*lhs_runtime_type_id == *rhs_runtime_type_id) return;
 #endif
 
   ABSL_INTERNAL_LOG(
@@ -233,8 +233,8 @@ std::string FlagImpl::Help() const {
                                                     : help_.gen_func();
 }
 
-FlagStaticTypeId FlagImpl::TypeId() const {
-  return flags_internal::StaticTypeId(op_);
+FlagFastTypeId FlagImpl::TypeId() const {
+  return flags_internal::FastTypeId(op_);
 }
 
 bool FlagImpl::IsModified() const {
@@ -429,7 +429,7 @@ void FlagImpl::Read(void* dst) const {
 void FlagImpl::Write(const void* src) {
   absl::MutexLock l(DataGuard());
 
-  if (ShouldValidateFlagValue(flags_internal::StaticTypeId(op_))) {
+  if (ShouldValidateFlagValue(flags_internal::FastTypeId(op_))) {
     std::unique_ptr<void, DynValueDeleter> obj{flags_internal::Clone(op_, src),
                                                DynValueDeleter{op_}};
     std::string ignored_error;
diff --git a/absl/flags/internal/flag.h b/absl/flags/internal/flag.h
index 19119bbb6193..c1bf8652815d 100644
--- a/absl/flags/internal/flag.h
+++ b/absl/flags/internal/flag.h
@@ -23,6 +23,7 @@
 #include <memory>
 #include <string>
 #include <type_traits>
+#include <typeinfo>
 
 #include "absl/base/call_once.h"
 #include "absl/base/config.h"
@@ -50,7 +51,8 @@ enum class FlagOp {
   kCopy,
   kCopyConstruct,
   kSizeof,
-  kStaticTypeId,
+  kFastTypeId,
+  kRuntimeTypeId,
   kParse,
   kUnparse,
   kValueOffset,
@@ -96,10 +98,15 @@ inline size_t Sizeof(FlagOpFn op) {
   return static_cast<size_t>(reinterpret_cast<intptr_t>(
       op(FlagOp::kSizeof, nullptr, nullptr, nullptr)));
 }
-// Returns static type id coresponding to the value type.
-inline FlagStaticTypeId StaticTypeId(FlagOpFn op) {
-  return reinterpret_cast<FlagStaticTypeId>(
-      op(FlagOp::kStaticTypeId, nullptr, nullptr, nullptr));
+// Returns fast type id coresponding to the value type.
+inline FlagFastTypeId FastTypeId(FlagOpFn op) {
+  return reinterpret_cast<FlagFastTypeId>(
+      op(FlagOp::kFastTypeId, nullptr, nullptr, nullptr));
+}
+// Returns fast type id coresponding to the value type.
+inline const std::type_info* RuntimeTypeId(FlagOpFn op) {
+  return reinterpret_cast<const std::type_info*>(
+      op(FlagOp::kRuntimeTypeId, nullptr, nullptr, nullptr));
 }
 // Returns offset of the field value_ from the field impl_ inside of
 // absl::Flag<T> data. Given FlagImpl pointer p you can get the
@@ -112,6 +119,16 @@ inline ptrdiff_t ValueOffset(FlagOpFn op) {
       op(FlagOp::kValueOffset, nullptr, nullptr, nullptr)));
 }
 
+// Returns an address of RTTI's typeid(T).
+template <typename T>
+inline const std::type_info* GenRuntimeTypeId() {
+#if defined(ABSL_FLAGS_INTERNAL_HAS_RTTI)
+  return &typeid(T);
+#else
+  return nullptr;
+#endif
+}
+
 ///////////////////////////////////////////////////////////////////////////////
 // Flag help auxiliary structs.
 
@@ -374,9 +391,10 @@ class FlagImpl final : public flags_internal::CommandLineFlag {
   // For example if flag is declared as absl::Flag<int> FLAGS_foo, a call to
   // absl::GetFlag(FLAGS_foo) validates that the type of FLAGS_foo is indeed
   // int. To do that we pass the "assumed" type id (which is deduced from type
-  // int) as an argument `op`, which is in turn is validated against the type id
-  // stored in flag object by flag definition statement.
-  void AssertValidType(FlagStaticTypeId type_id) const;
+  // int) as an argument `type_id`, which is in turn is validated against the
+  // type id stored in flag object by flag definition statement.
+  void AssertValidType(FlagFastTypeId type_id,
+                       const std::type_info* (*gen_rtti)()) const;
 
  private:
   template <typename T>
@@ -433,7 +451,7 @@ class FlagImpl final : public flags_internal::CommandLineFlag {
   std::string Filename() const override;
   absl::string_view Typename() const override;
   std::string Help() const override;
-  FlagStaticTypeId TypeId() const override;
+  FlagFastTypeId TypeId() const override;
   bool IsModified() const override ABSL_LOCKS_EXCLUDED(*DataGuard());
   bool IsSpecifiedOnCommandLine() const override
       ABSL_LOCKS_EXCLUDED(*DataGuard());
@@ -539,14 +557,14 @@ class Flag {
     U u;
 
 #if !defined(NDEBUG)
-    impl_.AssertValidType(&flags_internal::FlagStaticTypeIdGen<T>);
+    impl_.AssertValidType(base_internal::FastTypeId<T>(), &GenRuntimeTypeId<T>);
 #endif
 
     if (!value_.Get(&u.value)) impl_.Read(&u.value);
     return std::move(u.value);
   }
   void Set(const T& v) {
-    impl_.AssertValidType(&flags_internal::FlagStaticTypeIdGen<T>);
+    impl_.AssertValidType(base_internal::FastTypeId<T>(), &GenRuntimeTypeId<T>);
     impl_.Write(&v);
   }
   void SetCallback(const FlagCallbackFunc mutation_callback) {
@@ -595,8 +613,10 @@ void* FlagOps(FlagOp op, const void* v1, void* v2, void* v3) {
       return nullptr;
     case FlagOp::kSizeof:
       return reinterpret_cast<void*>(static_cast<uintptr_t>(sizeof(T)));
-    case FlagOp::kStaticTypeId:
-      return reinterpret_cast<void*>(&FlagStaticTypeIdGen<T>);
+    case FlagOp::kFastTypeId:
+      return const_cast<void*>(base_internal::FastTypeId<T>());
+    case FlagOp::kRuntimeTypeId:
+      return const_cast<std::type_info*>(GenRuntimeTypeId<T>());
     case FlagOp::kParse: {
       // Initialize the temporary instance of type T based on current value in
       // destination (which is going to be flag's default value).
diff --git a/absl/flags/internal/registry.cc b/absl/flags/internal/registry.cc
index 9ed912140ee7..eb619c7086ad 100644
--- a/absl/flags/internal/registry.cc
+++ b/absl/flags/internal/registry.cc
@@ -284,14 +284,14 @@ namespace {
 
 class RetiredFlagObj final : public flags_internal::CommandLineFlag {
  public:
-  constexpr RetiredFlagObj(const char* name, FlagStaticTypeId type_id)
+  constexpr RetiredFlagObj(const char* name, FlagFastTypeId type_id)
       : name_(name), type_id_(type_id) {}
 
  private:
   absl::string_view Name() const override { return name_; }
   std::string Filename() const override { return "RETIRED"; }
   absl::string_view Typename() const override { return ""; }
-  FlagStaticTypeId TypeId() const override { return type_id_; }
+  FlagFastTypeId TypeId() const override { return type_id_; }
   std::string Help() const override { return ""; }
   bool IsRetired() const override { return true; }
   bool IsModified() const override { return false; }
@@ -317,7 +317,7 @@ class RetiredFlagObj final : public flags_internal::CommandLineFlag {
 
   // Data members
   const char* const name_;
-  const FlagStaticTypeId type_id_;
+  const FlagFastTypeId type_id_;
 };
 
 void DestroyRetiredFlag(flags_internal::CommandLineFlag* flag) {
@@ -327,7 +327,7 @@ void DestroyRetiredFlag(flags_internal::CommandLineFlag* flag) {
 
 }  // namespace
 
-bool Retire(const char* name, FlagStaticTypeId type_id) {
+bool Retire(const char* name, FlagFastTypeId type_id) {
   auto* flag = new flags_internal::RetiredFlagObj(name, type_id);
   FlagRegistry::GlobalRegistry()->RegisterFlag(flag);
   return true;
diff --git a/absl/flags/internal/registry.h b/absl/flags/internal/registry.h
index 69ff889fb100..af8ed6b99b78 100644
--- a/absl/flags/internal/registry.h
+++ b/absl/flags/internal/registry.h
@@ -79,12 +79,12 @@ bool RegisterCommandLineFlag(CommandLineFlag*);
 //
 
 // Retire flag with name "name" and type indicated by ops.
-bool Retire(const char* name, FlagStaticTypeId type_id);
+bool Retire(const char* name, FlagFastTypeId type_id);
 
 // Registered a retired flag with name 'flag_name' and type 'T'.
 template <typename T>
 inline bool RetiredFlag(const char* flag_name) {
-  return flags_internal::Retire(flag_name, &FlagStaticTypeIdGen<T>);
+  return flags_internal::Retire(flag_name, base_internal::FastTypeId<T>());
 }
 
 // If the flag is retired, returns true and indicates in |*type_is_bool|