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