about summary refs log tree commit diff
path: root/absl/flags/internal/flag.cc
diff options
context:
space:
mode:
authorAbseil Team <absl-team@google.com>2020-02-14T17·41-0800
committerMark Barolak <mbar@google.com>2020-02-14T17·54-0500
commit3c814105108680997d0821077694f663693b5382 (patch)
treef414158f000b24926b4110dd1c3a640106fe630c /absl/flags/internal/flag.cc
parentc44657f55692eddf5504156645d1f4ec7b3acabd (diff)
Export of internal Abseil changes
--
97faa5fdfa4cd5d7a74cd9332cddd8a7c1e67b89 by Abseil Team <absl-team@google.com>:

Internal changes

PiperOrigin-RevId: 295164378

--
74990f100b3f4172c770ef8c76c05c8e99febdde by Xiaoyi Zhang <zhangxy@google.com>:

Release `absl::Cord`.

PiperOrigin-RevId: 295161959

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

Introduce independent notion of FlagStaticTypeID.

This change separates static flag value type identification from the type specific "vtable" with all the operations specific to value type. This change allows us to do the following:
* We can move most of "vtable" implementation from handle header, which will become public soon, into implementation details of Abseil Flag.
* We can combine back marshalling ops and general ops into a single vtable routine. They were split previously to facilitate type identification without requiring marshalling routines to be exposed in header.
* We do not need to store two vtable pointers. We can now store only one. The static type id can be deduced on request.

Overall we are saving 24 bytes per flag according to size_tester run.

PiperOrigin-RevId: 295149687

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

Update internal comments.

PiperOrigin-RevId: 295030681

--
825412b29fd6015027bbc3e5f802706eee0d2837 by Matthew Brown <matthewbr@google.com>:

Change str_format_internal::ConversionChar to an enum (from a struct-wrapped enum).

PiperOrigin-RevId: 294987462

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

Use more precise wording in the question on live-at-head

PiperOrigin-RevId: 294957679
GitOrigin-RevId: 97faa5fdfa4cd5d7a74cd9332cddd8a7c1e67b89
Change-Id: I081e70d148ffac7296d65e2a2f775f643eaf70bf
Diffstat (limited to 'absl/flags/internal/flag.cc')
-rw-r--r--absl/flags/internal/flag.cc76
1 files changed, 40 insertions, 36 deletions
diff --git a/absl/flags/internal/flag.cc b/absl/flags/internal/flag.cc
index 721e411e0875..83ec8df19149 100644
--- a/absl/flags/internal/flag.cc
+++ b/absl/flags/internal/flag.cc
@@ -47,23 +47,15 @@ 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(FlagOpFn flag_type_id) {
+bool ShouldValidateFlagValue(FlagStaticTypeId flag_type_id) {
 #define DONT_VALIDATE(T) \
-  if (flag_type_id == &flags_internal::FlagOps<T>) return false;
+  if (flag_type_id == &FlagStaticTypeIdGen<T>) return false;
   ABSL_FLAGS_INTERNAL_BUILTIN_TYPES(DONT_VALIDATE)
 #undef DONT_VALIDATE
 
   return true;
 }
 
-#if defined(ABSL_FLAGS_INTERNAL_HAS_RTTI)
-bool MatchRuntimeTypeId(FlagOpFn lhs_type_id, FlagOpFn rhs_type_id) {
-  return RuntimeTypeId(lhs_type_id) == RuntimeTypeId(rhs_type_id);
-}
-#else
-bool MatchRuntimeTypeId(FlagOpFn, FlagOpFn) { return true; }
-#endif
-
 // RAII helper used to temporarily unlock and relock `absl::Mutex`.
 // This is used when we need to ensure that locks are released while
 // invoking user supplied callbacks and then reacquired, since callbacks may
@@ -101,22 +93,35 @@ absl::Mutex* FlagImpl::DataGuard() const {
   return reinterpret_cast<absl::Mutex*>(&data_guard_);
 }
 
-void FlagImpl::AssertValidType(const flags_internal::FlagOpFn op) const {
-  // `op` is the unmarshaling operation corresponding to the declaration
-  // visibile at the call site. `op_` is the Flag's defined unmarshalling
-  // operation. They must match for this operation to be well-defined.
-  if (ABSL_PREDICT_FALSE(op != op_) && !MatchRuntimeTypeId(op, op_)) {
-    ABSL_INTERNAL_LOG(
-        FATAL,
-        absl::StrCat("Flag '", Name(),
-                     "' is defined as one type and declared as another"));
-  }
+void FlagImpl::AssertValidType(FlagStaticTypeId type_id) const {
+  FlagStaticTypeId this_type_id = flags_internal::StaticTypeId(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;
+
+  void* lhs_runtime_type_id = type_id();
+  void* rhs_runtime_type_id = this_type_id();
+
+  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;
+#endif
+
+  ABSL_INTERNAL_LOG(
+      FATAL, absl::StrCat("Flag '", Name(),
+                          "' is defined as one type and declared as another"));
 }
 
 std::unique_ptr<void, DynValueDeleter> FlagImpl::MakeInitValue() const {
   void* res = nullptr;
   if (DefaultKind() == FlagDefaultKind::kDynamicValue) {
-    res = Clone(op_, default_src_.dynamic_value);
+    res = flags_internal::Clone(op_, default_src_.dynamic_value);
   } else {
     res = (*default_src_.gen_func)();
   }
@@ -148,13 +153,13 @@ std::string FlagImpl::DefaultValue() const {
   absl::MutexLock l(DataGuard());
 
   auto obj = MakeInitValue();
-  return Unparse(marshalling_op_, obj.get());
+  return flags_internal::Unparse(op_, obj.get());
 }
 
 std::string FlagImpl::CurrentValue() const {
   absl::MutexLock l(DataGuard());
 
-  return Unparse(marshalling_op_, value_.dynamic);
+  return flags_internal::Unparse(op_, value_.dynamic);
 }
 
 void FlagImpl::SetCallback(const FlagCallbackFunc mutation_callback) {
@@ -220,7 +225,7 @@ bool FlagImpl::TryParse(void** dst, absl::string_view value,
   auto tentative_value = MakeInitValue();
 
   std::string parse_err;
-  if (!Parse(marshalling_op_, value, tentative_value.get(), &parse_err)) {
+  if (!flags_internal::Parse(op_, value, tentative_value.get(), &parse_err)) {
     absl::string_view err_sep = parse_err.empty() ? "" : "; ";
     *err = absl::StrCat("Illegal value '", value, "' specified for flag '",
                         Name(), "'", err_sep, parse_err);
@@ -237,11 +242,11 @@ bool FlagImpl::TryParse(void** dst, absl::string_view value,
 void FlagImpl::Read(void* dst) const {
   absl::ReaderMutexLock l(DataGuard());
 
-  CopyConstruct(op_, value_.dynamic, dst);
+  flags_internal::CopyConstruct(op_, value_.dynamic, dst);
 }
 
 void FlagImpl::StoreAtomic() {
-  size_t data_size = Sizeof(op_);
+  size_t data_size = flags_internal::Sizeof(op_);
 
   if (data_size <= sizeof(int64_t)) {
     int64_t t = 0;
@@ -260,20 +265,20 @@ void FlagImpl::StoreAtomic() {
 void FlagImpl::Write(const void* src) {
   absl::MutexLock l(DataGuard());
 
-  if (ShouldValidateFlagValue(op_)) {
-    void* obj = Clone(op_, src);
+  if (ShouldValidateFlagValue(flags_internal::StaticTypeId(op_))) {
+    void* obj = flags_internal::Clone(op_, src);
     std::string ignored_error;
-    std::string src_as_str = Unparse(marshalling_op_, src);
-    if (!Parse(marshalling_op_, src_as_str, obj, &ignored_error)) {
+    std::string src_as_str = flags_internal::Unparse(op_, src);
+    if (!flags_internal::Parse(op_, src_as_str, obj, &ignored_error)) {
       ABSL_INTERNAL_LOG(ERROR, absl::StrCat("Attempt to set flag '", Name(),
                                             "' to invalid value ", src_as_str));
     }
-    Delete(op_, obj);
+    flags_internal::Delete(op_, obj);
   }
 
   modified_ = true;
   counter_++;
-  Copy(op_, src, value_.dynamic);
+  flags_internal::Copy(op_, src, value_.dynamic);
 
   StoreAtomic();
   InvokeCallback();
@@ -341,7 +346,7 @@ bool FlagImpl::SetFromString(absl::string_view value, FlagSettingMode set_mode,
 
       if (!modified_) {
         // Need to set both default value *and* current, in this case
-        Copy(op_, default_src_.dynamic_value, value_.dynamic);
+        flags_internal::Copy(op_, default_src_.dynamic_value, value_.dynamic);
         StoreAtomic();
         InvokeCallback();
       }
@@ -359,7 +364,7 @@ void FlagImpl::CheckDefaultValueParsingRoundtrip() const {
 
   auto dst = MakeInitValue();
   std::string error;
-  if (!flags_internal::Parse(marshalling_op_, v, dst.get(), &error)) {
+  if (!flags_internal::Parse(op_, v, dst.get(), &error)) {
     ABSL_INTERNAL_LOG(
         FATAL,
         absl::StrCat("Flag ", Name(), " (from ", Filename(),
@@ -376,8 +381,7 @@ bool FlagImpl::ValidateInputValue(absl::string_view value) const {
 
   auto obj = MakeInitValue();
   std::string ignored_error;
-  return flags_internal::Parse(marshalling_op_, value, obj.get(),
-                               &ignored_error);
+  return flags_internal::Parse(op_, value, obj.get(), &ignored_error);
 }
 
 }  // namespace flags_internal