aboutsummaryrefslogtreecommitdiffhomepage
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.h89
1 files changed, 79 insertions, 10 deletions
diff --git a/absl/flags/internal/flag.h b/absl/flags/internal/flag.h
index ce0ccf2..d799025 100644
--- a/absl/flags/internal/flag.h
+++ b/absl/flags/internal/flag.h
@@ -61,6 +61,68 @@ class FlagState : public flags_internal::FlagStateInterface {
int64_t counter_;
};
+// This is help argument for absl::Flag encapsulating the string literal pointer
+// or pointer to function generating it as well as enum descriminating two
+// cases.
+using HelpGenFunc = std::string (*)();
+
+union FlagHelpSrc {
+ constexpr explicit FlagHelpSrc(const char* help_msg) : literal(help_msg) {}
+ constexpr explicit FlagHelpSrc(HelpGenFunc help_gen) : gen_func(help_gen) {}
+
+ const char* literal;
+ HelpGenFunc gen_func;
+};
+
+enum class FlagHelpSrcKind : int8_t { kLiteral, kGenFunc };
+
+struct HelpInitArg {
+ FlagHelpSrc source;
+ FlagHelpSrcKind kind;
+};
+
+// 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*.
+// If help message expression is indeed constexpr const char* HelpConstexprWrap
+// is just a trivial identity function.
+template <typename T>
+const char* HelpConstexprWrap(const T&) {
+ return nullptr;
+}
+constexpr const char* HelpConstexprWrap(const char* p) { return p; }
+constexpr const char* HelpConstexprWrap(char* p) { return p; }
+
+// These two HelpArg overloads allows us to select at compile time one of two
+// way to pass Help argument to absl::Flag. We'll be passing
+// AbslFlagHelpGenFor##name as T and integer 0 as a single argument to prefer
+// first overload if possible. If T::Const is evaluatable on constexpr
+// context (see non template int parameter below) we'll choose first overload.
+// In this case the help message expression is immediately evaluated and is used
+// to construct the absl::Flag. No additionl code is generated by ABSL_FLAG.
+// Otherwise SFINAE kicks in and first overload is dropped from the
+// consideration, in which case the second overload will be used. The second
+// overload does not attempt to evaluate the help message expression
+// immediately and instead delays the evaluation by returing the function
+// pointer (&T::NonConst) genering the help message when necessary. This is
+// evaluatable in constexpr context, but the cost is an extra function being
+// generated in the ABSL_FLAG code.
+template <typename T, int = (T::Const(), 1)>
+constexpr flags_internal::HelpInitArg HelpArg(int) {
+ return {flags_internal::FlagHelpSrc(T::Const()),
+ flags_internal::FlagHelpSrcKind::kLiteral};
+}
+
+template <typename T>
+constexpr flags_internal::HelpInitArg HelpArg(char) {
+ return {flags_internal::FlagHelpSrc(&T::NonConst),
+ flags_internal::FlagHelpSrcKind::kGenFunc};
+}
+
+// Signature for the function generating the initial flag value based (usually
+// based on default value supplied in flag's definition)
+using InitialValGenFunc = void* (*)();
+
// Signature for the mutation callback used by watched Flags
// The callback is noexcept.
// TODO(rogeeff): add noexcept after C++17 support is added.
@@ -74,15 +136,19 @@ class FlagImpl {
public:
constexpr FlagImpl(const flags_internal::FlagOpFn op,
const flags_internal::FlagMarshallingOpFn marshalling_op,
- const flags_internal::InitialValGenFunc initial_value_gen)
+ const flags_internal::InitialValGenFunc initial_value_gen,
+ const HelpInitArg help)
: op_(op),
marshalling_op_(marshalling_op),
- initial_value_gen_(initial_value_gen) {}
+ initial_value_gen_(initial_value_gen),
+ help_(help.source),
+ help_source_kind_(help.kind) {}
// Forces destruction of the Flag's data.
void Destroy() const;
// Constant access methods
+ std::string Help() const;
bool IsModified() const ABSL_LOCKS_EXCLUDED(locks_->primary_mu);
bool IsSpecifiedOnCommandLine() const ABSL_LOCKS_EXCLUDED(locks_->primary_mu);
std::string DefaultValue() const ABSL_LOCKS_EXCLUDED(locks_->primary_mu);
@@ -149,9 +215,12 @@ class FlagImpl {
absl::Mutex* DataGuard() const ABSL_LOCK_RETURNED(locks_->primary_mu);
// Immutable Flag's data.
- const FlagOpFn op_; // Type-specific handler
- const FlagMarshallingOpFn marshalling_op_; // Marshalling ops handler
- const InitialValGenFunc initial_value_gen_; // Makes flag's initial value
+ const FlagOpFn op_; // Type-specific handler.
+ const FlagMarshallingOpFn marshalling_op_; // Marshalling ops handler.
+ const InitialValGenFunc initial_value_gen_; // Makes flag's initial value.
+ const FlagHelpSrc help_; // Help message literal or function to generate it.
+ // Indicates if help message was supplied as literal or generator func.
+ const FlagHelpSrcKind help_source_kind_;
// Mutable Flag's data. (guarded by locks_->primary_mu).
// Indicates that locks_, cur_ and def_ fields have been lazily initialized.
@@ -191,14 +260,13 @@ class FlagImpl {
template <typename T>
class Flag final : public flags_internal::CommandLineFlag {
public:
- constexpr Flag(const char* name, const flags_internal::HelpGenFunc help_gen,
+ constexpr Flag(const char* name, const flags_internal::HelpInitArg help,
const char* filename,
const flags_internal::FlagMarshallingOpFn marshalling_op,
const flags_internal::InitialValGenFunc initial_value_gen)
- : flags_internal::CommandLineFlag(
- name, flags_internal::HelpText::FromFunctionPointer(help_gen),
- filename),
- impl_(&flags_internal::FlagOps<T>, marshalling_op, initial_value_gen) {}
+ : flags_internal::CommandLineFlag(name, filename),
+ impl_(&flags_internal::FlagOps<T>, marshalling_op, initial_value_gen,
+ help) {}
T Get() const {
// See implementation notes in CommandLineFlag::Get().
@@ -222,6 +290,7 @@ class Flag final : public flags_internal::CommandLineFlag {
}
// CommandLineFlag interface
+ std::string Help() const override { return impl_.Help(); }
bool IsModified() const override { return impl_.IsModified(); }
bool IsSpecifiedOnCommandLine() const override {
return impl_.IsSpecifiedOnCommandLine();