summaryrefslogtreecommitdiff
path: root/absl/flags/internal/flag.h
diff options
context:
space:
mode:
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 21a10c7f..7d5271c4 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