about summary refs log tree commit diff
path: root/absl/flags/internal
diff options
context:
space:
mode:
Diffstat (limited to 'absl/flags/internal')
-rw-r--r--absl/flags/internal/commandlineflag.h17
-rw-r--r--absl/flags/internal/flag.cc36
-rw-r--r--absl/flags/internal/flag.h37
3 files changed, 18 insertions, 72 deletions
diff --git a/absl/flags/internal/commandlineflag.h b/absl/flags/internal/commandlineflag.h
index 0a7197b7bd96..fa050209b0d7 100644
--- a/absl/flags/internal/commandlineflag.h
+++ b/absl/flags/internal/commandlineflag.h
@@ -178,23 +178,6 @@ class CommandLineFlag {
   virtual void CheckDefaultValueParsingRoundtrip() const = 0;
 };
 
-// This macro is the "source of truth" for the list of supported flag built-in
-// types.
-#define ABSL_FLAGS_INTERNAL_BUILTIN_TYPES(A) \
-  A(bool)                                    \
-  A(short)                                   \
-  A(unsigned short)                          \
-  A(int)                                     \
-  A(unsigned int)                            \
-  A(long)                                    \
-  A(unsigned long)                           \
-  A(long long)                               \
-  A(unsigned long long)                      \
-  A(double)                                  \
-  A(float)                                   \
-  A(std::string)                             \
-  A(std::vector<std::string>)
-
 }  // namespace flags_internal
 ABSL_NAMESPACE_END
 }  // namespace absl
diff --git a/absl/flags/internal/flag.cc b/absl/flags/internal/flag.cc
index 8f0777fa1dc1..96c026dcb5d2 100644
--- a/absl/flags/internal/flag.cc
+++ b/absl/flags/internal/flag.cc
@@ -49,9 +49,9 @@ namespace {
 
 // Currently we only validate flag values for user-defined flag types.
 bool ShouldValidateFlagValue(FlagFastTypeId flag_type_id) {
-#define DONT_VALIDATE(T) \
+#define DONT_VALIDATE(T, _) \
   if (flag_type_id == base_internal::FastTypeId<T>()) return false;
-  ABSL_FLAGS_INTERNAL_BUILTIN_TYPES(DONT_VALIDATE)
+  ABSL_FLAGS_INTERNAL_SUPPORTED_TYPES(DONT_VALIDATE)
 #undef DONT_VALIDATE
 
   return true;
@@ -150,23 +150,11 @@ void FlagImpl::Init() {
       break;
     case FlagValueStorageKind::kOneWordAtomic: {
       alignas(int64_t) std::array<char, sizeof(int64_t)> buf{};
-      switch (def_kind) {
-        case FlagDefaultKind::kOneWord:
-          std::memcpy(buf.data(), &default_value_.one_word,
-                      sizeof(default_value_.one_word));
-          break;
-        case FlagDefaultKind::kFloat:
-          std::memcpy(buf.data(), &default_value_.float_value,
-                      sizeof(default_value_.float_value));
-          break;
-        case FlagDefaultKind::kDouble:
-          std::memcpy(buf.data(), &default_value_.double_value,
-                      sizeof(default_value_.double_value));
-          break;
-        default:
-          assert(def_kind == FlagDefaultKind::kGenFunc);
-          (*default_value_.gen_func)(buf.data());
-          break;
+      if (def_kind == FlagDefaultKind::kGenFunc) {
+        (*default_value_.gen_func)(buf.data());
+      } else {
+        assert(def_kind != FlagDefaultKind::kDynamicValue);
+        std::memcpy(buf.data(), &default_value_, Sizeof(op_));
       }
       OneWordValue().store(absl::bit_cast<int64_t>(buf),
                            std::memory_order_release);
@@ -228,14 +216,8 @@ std::unique_ptr<void, DynValueDeleter> FlagImpl::MakeInitValue() const {
       res = flags_internal::Alloc(op_);
       (*default_value_.gen_func)(res);
       break;
-    case FlagDefaultKind::kOneWord:
-      res = flags_internal::Clone(op_, &default_value_.one_word);
-      break;
-    case FlagDefaultKind::kFloat:
-      res = flags_internal::Clone(op_, &default_value_.float_value);
-      break;
-    case FlagDefaultKind::kDouble:
-      res = flags_internal::Clone(op_, &default_value_.double_value);
+    default:
+      res = flags_internal::Clone(op_, &default_value_);
       break;
   }
   return {res, DynValueDeleter{op_}};
diff --git a/absl/flags/internal/flag.h b/absl/flags/internal/flag.h
index 146c3efc2af3..e374ecde3d95 100644
--- a/absl/flags/internal/flag.h
+++ b/absl/flags/internal/flag.h
@@ -231,25 +231,21 @@ using FlagDfltGenFunc = void (*)(void*);
 union FlagDefaultSrc {
   constexpr explicit FlagDefaultSrc(FlagDfltGenFunc gen_func_arg)
       : gen_func(gen_func_arg) {}
-  template <typename T>
-  constexpr explicit FlagDefaultSrc(T one_word_value)
-      : one_word(static_cast<int64_t>(one_word_value)) {}
-  constexpr explicit FlagDefaultSrc(float f) : float_value(f) {}
-  constexpr explicit FlagDefaultSrc(double d) : double_value(d) {}
+
+#define ABSL_FLAGS_INTERNAL_DFLT_FOR_TYPE(T, name) \
+  T name##_value;                                  \
+  constexpr explicit FlagDefaultSrc(T value) : name##_value(value) {}  // NOLINT
+  ABSL_FLAGS_INTERNAL_BUILTIN_TYPES(ABSL_FLAGS_INTERNAL_DFLT_FOR_TYPE)
+#undef ABSL_FLAGS_INTERNAL_DFLT_FOR_TYPE
 
   void* dynamic_value;
   FlagDfltGenFunc gen_func;
-  int64_t one_word;
-  float float_value;
-  double double_value;
 };
 
 enum class FlagDefaultKind : uint8_t {
   kDynamicValue = 0,
   kGenFunc = 1,
-  kOneWord = 2,
-  kFloat = 3,
-  kDouble = 4
+  kOneWord = 2  // for default values UP to one word in size
 };
 
 struct FlagDefaultArg {
@@ -279,20 +275,6 @@ constexpr FlagDefaultArg DefaultArg(int) {
   return {FlagDefaultSrc(GenT{}.value), FlagDefaultKind::kOneWord};
 }
 
-template <typename ValueT, typename GenT,
-          typename std::enable_if<std::is_same<ValueT, float>::value,
-                                  int>::type = (GenT{}, 0)>
-constexpr FlagDefaultArg DefaultArg(int) {
-  return {FlagDefaultSrc(GenT{}.value), FlagDefaultKind::kFloat};
-}
-
-template <typename ValueT, typename GenT,
-          typename std::enable_if<std::is_same<ValueT, double>::value,
-                                  int>::type = (GenT{}, 0)>
-constexpr FlagDefaultArg DefaultArg(int) {
-  return {FlagDefaultSrc(GenT{}.value), FlagDefaultKind::kDouble};
-}
-
 template <typename ValueT, typename GenT>
 constexpr FlagDefaultArg DefaultArg(char) {
   return {FlagDefaultSrc(&GenT::Gen), FlagDefaultKind::kGenFunc};
@@ -576,9 +558,8 @@ class FlagImpl final : public flags_internal::CommandLineFlag {
   // Mutable flag's state (guarded by `data_guard_`).
 
   // def_kind_ is not guard by DataGuard() since it is accessed in Init without
-  // locks. If necessary we can decrease number of bits used to 2 by folding
-  // one_word storage cases.
-  uint8_t def_kind_ : 3;
+  // locks.
+  uint8_t def_kind_ : 2;
   // Has this flag's value been modified?
   bool modified_ : 1 ABSL_GUARDED_BY(*DataGuard());
   // Has this flag been specified on command line.