flag.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //
  2. // Copyright 2019 The Abseil Authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // https://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. // -----------------------------------------------------------------------------
  17. // File: flag.h
  18. // -----------------------------------------------------------------------------
  19. //
  20. // This header file defines the `absl::Flag<T>` type for holding command-line
  21. // flag data, and abstractions to create, get and set such flag data.
  22. //
  23. // It is important to note that this type is **unspecified** (an implementation
  24. // detail) and you do not construct or manipulate actual `absl::Flag<T>`
  25. // instances. Instead, you define and declare flags using the
  26. // `ABSL_FLAG()` and `ABSL_DECLARE_FLAG()` macros, and get and set flag values
  27. // using the `absl::GetFlag()` and `absl::SetFlag()` functions.
  28. #ifndef ABSL_FLAGS_FLAG_H_
  29. #define ABSL_FLAGS_FLAG_H_
  30. #include <cstdint>
  31. #include <string>
  32. #include <type_traits>
  33. #include "absl/base/attributes.h"
  34. #include "absl/base/config.h"
  35. #include "absl/base/optimization.h"
  36. #include "absl/flags/commandlineflag.h"
  37. #include "absl/flags/config.h"
  38. #include "absl/flags/internal/flag.h"
  39. #include "absl/flags/internal/registry.h"
  40. #include "absl/strings/string_view.h"
  41. namespace absl {
  42. ABSL_NAMESPACE_BEGIN
  43. // Flag
  44. //
  45. // An `absl::Flag` holds a command-line flag value, providing a runtime
  46. // parameter to a binary. Such flags should be defined in the global namespace
  47. // and (preferably) in the module containing the binary's `main()` function.
  48. //
  49. // You should not construct and cannot use the `absl::Flag` type directly;
  50. // instead, you should declare flags using the `ABSL_DECLARE_FLAG()` macro
  51. // within a header file, and define your flag using `ABSL_FLAG()` within your
  52. // header's associated `.cc` file. Such flags will be named `FLAGS_name`.
  53. //
  54. // Example:
  55. //
  56. // .h file
  57. //
  58. // // Declares usage of a flag named "FLAGS_count"
  59. // ABSL_DECLARE_FLAG(int, count);
  60. //
  61. // .cc file
  62. //
  63. // // Defines a flag named "FLAGS_count" with a default `int` value of 0.
  64. // ABSL_FLAG(int, count, 0, "Count of items to process");
  65. //
  66. // No public methods of `absl::Flag<T>` are part of the Abseil Flags API.
  67. //
  68. // For type support of Abseil Flags, see the marshalling.h header file, which
  69. // discusses supported standard types, optional flags, and additional Abseil
  70. // type support.
  71. template <typename T>
  72. using Flag = flags_internal::Flag<T>;
  73. // GetFlag()
  74. //
  75. // Returns the value (of type `T`) of an `absl::Flag<T>` instance, by value. Do
  76. // not construct an `absl::Flag<T>` directly and call `absl::GetFlag()`;
  77. // instead, refer to flag's constructed variable name (e.g. `FLAGS_name`).
  78. // Because this function returns by value and not by reference, it is
  79. // thread-safe, but note that the operation may be expensive; as a result, avoid
  80. // `absl::GetFlag()` within any tight loops.
  81. //
  82. // Example:
  83. //
  84. // // FLAGS_count is a Flag of type `int`
  85. // int my_count = absl::GetFlag(FLAGS_count);
  86. //
  87. // // FLAGS_firstname is a Flag of type `std::string`
  88. // std::string first_name = absl::GetFlag(FLAGS_firstname);
  89. template <typename T>
  90. ABSL_MUST_USE_RESULT T GetFlag(const absl::Flag<T>& flag) {
  91. return flags_internal::FlagImplPeer::InvokeGet<T>(flag);
  92. }
  93. // SetFlag()
  94. //
  95. // Sets the value of an `absl::Flag` to the value `v`. Do not construct an
  96. // `absl::Flag<T>` directly and call `absl::SetFlag()`; instead, use the
  97. // flag's variable name (e.g. `FLAGS_name`). This function is
  98. // thread-safe, but is potentially expensive. Avoid setting flags in general,
  99. // but especially within performance-critical code.
  100. template <typename T>
  101. void SetFlag(absl::Flag<T>* flag, const T& v) {
  102. flags_internal::FlagImplPeer::InvokeSet(*flag, v);
  103. }
  104. // Overload of `SetFlag()` to allow callers to pass in a value that is
  105. // convertible to `T`. E.g., use this overload to pass a "const char*" when `T`
  106. // is `std::string`.
  107. template <typename T, typename V>
  108. void SetFlag(absl::Flag<T>* flag, const V& v) {
  109. T value(v);
  110. flags_internal::FlagImplPeer::InvokeSet(*flag, value);
  111. }
  112. // GetFlagReflectionHandle()
  113. //
  114. // Returns the reflection handle corresponding to specified Abseil Flag
  115. // instance. Use this handle to access flag's reflection information, like name,
  116. // location, default value etc.
  117. //
  118. // Example:
  119. //
  120. // std::string = absl::GetFlagReflectionHandle(FLAGS_count).DefaultValue();
  121. template <typename T>
  122. const CommandLineFlag& GetFlagReflectionHandle(const absl::Flag<T>& f) {
  123. return flags_internal::FlagImplPeer::InvokeReflect(f);
  124. }
  125. ABSL_NAMESPACE_END
  126. } // namespace absl
  127. // ABSL_FLAG()
  128. //
  129. // This macro defines an `absl::Flag<T>` instance of a specified type `T`:
  130. //
  131. // ABSL_FLAG(T, name, default_value, help);
  132. //
  133. // where:
  134. //
  135. // * `T` is a supported flag type (see the list of types in `marshalling.h`),
  136. // * `name` designates the name of the flag (as a global variable
  137. // `FLAGS_name`),
  138. // * `default_value` is an expression holding the default value for this flag
  139. // (which must be implicitly convertible to `T`),
  140. // * `help` is the help text, which can also be an expression.
  141. //
  142. // This macro expands to a flag named 'FLAGS_name' of type 'T':
  143. //
  144. // absl::Flag<T> FLAGS_name = ...;
  145. //
  146. // Note that all such instances are created as global variables.
  147. //
  148. // For `ABSL_FLAG()` values that you wish to expose to other translation units,
  149. // it is recommended to define those flags within the `.cc` file associated with
  150. // the header where the flag is declared.
  151. //
  152. // Note: do not construct objects of type `absl::Flag<T>` directly. Only use the
  153. // `ABSL_FLAG()` macro for such construction.
  154. #define ABSL_FLAG(Type, name, default_value, help) \
  155. ABSL_FLAG_IMPL(Type, name, default_value, help)
  156. // ABSL_FLAG().OnUpdate()
  157. //
  158. // Defines a flag of type `T` with a callback attached:
  159. //
  160. // ABSL_FLAG(T, name, default_value, help).OnUpdate(callback);
  161. //
  162. // `callback` should be convertible to `void (*)()`.
  163. //
  164. // After any setting of the flag value, the callback will be called at least
  165. // once. A rapid sequence of changes may be merged together into the same
  166. // callback. No concurrent calls to the callback will be made for the same
  167. // flag. Callbacks are allowed to read the current value of the flag but must
  168. // not mutate that flag.
  169. //
  170. // The update mechanism guarantees "eventual consistency"; if the callback
  171. // derives an auxiliary data structure from the flag value, it is guaranteed
  172. // that eventually the flag value and the derived data structure will be
  173. // consistent.
  174. //
  175. // Note: ABSL_FLAG.OnUpdate() does not have a public definition. Hence, this
  176. // comment serves as its API documentation.
  177. // -----------------------------------------------------------------------------
  178. // Implementation details below this section
  179. // -----------------------------------------------------------------------------
  180. // ABSL_FLAG_IMPL macro definition conditional on ABSL_FLAGS_STRIP_NAMES
  181. #define ABSL_FLAG_IMPL_FLAG_PTR(flag) flag
  182. #define ABSL_FLAG_IMPL_HELP_ARG(name) \
  183. absl::flags_internal::HelpArg<AbslFlagHelpGenFor##name>( \
  184. FLAGS_help_storage_##name)
  185. #define ABSL_FLAG_IMPL_DEFAULT_ARG(Type, name) \
  186. absl::flags_internal::DefaultArg<Type, AbslFlagDefaultGenFor##name>(0)
  187. #if ABSL_FLAGS_STRIP_NAMES
  188. #define ABSL_FLAG_IMPL_FLAGNAME(txt) ""
  189. #define ABSL_FLAG_IMPL_FILENAME() ""
  190. #define ABSL_FLAG_IMPL_REGISTRAR(T, flag) \
  191. absl::flags_internal::FlagRegistrar<T, false>(ABSL_FLAG_IMPL_FLAG_PTR(flag), \
  192. nullptr)
  193. #else
  194. #define ABSL_FLAG_IMPL_FLAGNAME(txt) txt
  195. #define ABSL_FLAG_IMPL_FILENAME() __FILE__
  196. #define ABSL_FLAG_IMPL_REGISTRAR(T, flag) \
  197. absl::flags_internal::FlagRegistrar<T, true>(ABSL_FLAG_IMPL_FLAG_PTR(flag), \
  198. __FILE__)
  199. #endif
  200. // ABSL_FLAG_IMPL macro definition conditional on ABSL_FLAGS_STRIP_HELP
  201. #if ABSL_FLAGS_STRIP_HELP
  202. #define ABSL_FLAG_IMPL_FLAGHELP(txt) absl::flags_internal::kStrippedFlagHelp
  203. #else
  204. #define ABSL_FLAG_IMPL_FLAGHELP(txt) txt
  205. #endif
  206. // AbslFlagHelpGenFor##name is used to encapsulate both immediate (method Const)
  207. // and lazy (method NonConst) evaluation of help message expression. We choose
  208. // between the two via the call to HelpArg in absl::Flag instantiation below.
  209. // If help message expression is constexpr evaluable compiler will optimize
  210. // away this whole struct.
  211. // TODO(rogeeff): place these generated structs into local namespace and apply
  212. // ABSL_INTERNAL_UNIQUE_SHORT_NAME.
  213. // TODO(rogeeff): Apply __attribute__((nodebug)) to FLAGS_help_storage_##name
  214. #define ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, txt) \
  215. struct AbslFlagHelpGenFor##name { \
  216. /* The expression is run in the caller as part of the */ \
  217. /* default value argument. That keeps temporaries alive */ \
  218. /* long enough for NonConst to work correctly. */ \
  219. static constexpr absl::string_view Value( \
  220. absl::string_view absl_flag_help = ABSL_FLAG_IMPL_FLAGHELP(txt)) { \
  221. return absl_flag_help; \
  222. } \
  223. static std::string NonConst() { return std::string(Value()); } \
  224. }; \
  225. constexpr auto FLAGS_help_storage_##name ABSL_INTERNAL_UNIQUE_SMALL_NAME() \
  226. ABSL_ATTRIBUTE_SECTION_VARIABLE(flags_help_cold) = \
  227. absl::flags_internal::HelpStringAsArray<AbslFlagHelpGenFor##name>( \
  228. 0);
  229. #define ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value) \
  230. struct AbslFlagDefaultGenFor##name { \
  231. Type value = absl::flags_internal::InitDefaultValue<Type>(default_value); \
  232. static void Gen(void* absl_flag_default_loc) { \
  233. new (absl_flag_default_loc) Type(AbslFlagDefaultGenFor##name{}.value); \
  234. } \
  235. };
  236. // ABSL_FLAG_IMPL
  237. //
  238. // Note: Name of registrar object is not arbitrary. It is used to "grab"
  239. // global name for FLAGS_no<flag_name> symbol, thus preventing the possibility
  240. // of defining two flags with names foo and nofoo.
  241. #define ABSL_FLAG_IMPL(Type, name, default_value, help) \
  242. extern ::absl::Flag<Type> FLAGS_##name; \
  243. namespace absl /* block flags in namespaces */ {} \
  244. ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value) \
  245. ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, help) \
  246. ABSL_CONST_INIT absl::Flag<Type> FLAGS_##name{ \
  247. ABSL_FLAG_IMPL_FLAGNAME(#name), ABSL_FLAG_IMPL_FILENAME(), \
  248. ABSL_FLAG_IMPL_HELP_ARG(name), ABSL_FLAG_IMPL_DEFAULT_ARG(Type, name)}; \
  249. extern absl::flags_internal::FlagRegistrarEmpty FLAGS_no##name; \
  250. absl::flags_internal::FlagRegistrarEmpty FLAGS_no##name = \
  251. ABSL_FLAG_IMPL_REGISTRAR(Type, FLAGS_##name)
  252. // ABSL_RETIRED_FLAG
  253. //
  254. // Designates the flag (which is usually pre-existing) as "retired." A retired
  255. // flag is a flag that is now unused by the program, but may still be passed on
  256. // the command line, usually by production scripts. A retired flag is ignored
  257. // and code can't access it at runtime.
  258. //
  259. // This macro registers a retired flag with given name and type, with a name
  260. // identical to the name of the original flag you are retiring. The retired
  261. // flag's type can change over time, so that you can retire code to support a
  262. // custom flag type.
  263. //
  264. // This macro has the same signature as `ABSL_FLAG`. To retire a flag, simply
  265. // replace an `ABSL_FLAG` definition with `ABSL_RETIRED_FLAG`, leaving the
  266. // arguments unchanged (unless of course you actually want to retire the flag
  267. // type at this time as well).
  268. //
  269. // `default_value` is only used as a double check on the type. `explanation` is
  270. // unused.
  271. // TODO(rogeeff): replace RETIRED_FLAGS with FLAGS once forward declarations of
  272. // retired flags are cleaned up.
  273. #define ABSL_RETIRED_FLAG(type, name, default_value, explanation) \
  274. static absl::flags_internal::RetiredFlag<type> RETIRED_FLAGS_##name; \
  275. ABSL_ATTRIBUTE_UNUSED static const auto RETIRED_FLAGS_REG_##name = \
  276. (RETIRED_FLAGS_##name.Retire(#name), \
  277. ::absl::flags_internal::FlagRegistrarEmpty{})
  278. #endif // ABSL_FLAGS_FLAG_H_