00001 // 00002 // Copyright 2019 The Abseil Authors. 00003 // 00004 // Licensed under the Apache License, Version 2.0 (the "License"); 00005 // you may not use this file except in compliance with the License. 00006 // You may obtain a copy of the License at 00007 // 00008 // https://www.apache.org/licenses/LICENSE-2.0 00009 // 00010 // Unless required by applicable law or agreed to in writing, software 00011 // distributed under the License is distributed on an "AS IS" BASIS, 00012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 // See the License for the specific language governing permissions and 00014 // limitations under the License. 00015 // 00016 // ----------------------------------------------------------------------------- 00017 // File: flag.h 00018 // ----------------------------------------------------------------------------- 00019 // 00020 // This header file defines the `absl::Flag<T>` type for holding command-line 00021 // flag data, and abstractions to create, get and set such flag data. 00022 // 00023 // It is important to note that this type is **unspecified** (an implementation 00024 // detail) and you do not construct or manipulate actual `absl::Flag<T>` 00025 // instances. Instead, you define and declare flags using the 00026 // `ABSL_FLAG()` and `ABSL_DECLARE_FLAG()` macros, and get and set flag values 00027 // using the `absl::GetFlag()` and `absl::SetFlag()` functions. 00028 00029 #ifndef ABSL_FLAGS_FLAG_H_ 00030 #define ABSL_FLAGS_FLAG_H_ 00031 00032 #include "absl/base/attributes.h" 00033 #include "absl/base/casts.h" 00034 #include "absl/flags/config.h" 00035 #include "absl/flags/declare.h" 00036 #include "absl/flags/internal/commandlineflag.h" 00037 #include "absl/flags/internal/flag.h" 00038 #include "absl/flags/marshalling.h" 00039 00040 namespace absl { 00041 00042 // Flag 00043 // 00044 // An `absl::Flag` holds a command-line flag value, providing a runtime 00045 // parameter to a binary. Such flags should be defined in the global namespace 00046 // and (preferably) in the module containing the binary's `main()` function. 00047 // 00048 // You should not construct and cannot use the `absl::Flag` type directly; 00049 // instead, you should declare flags using the `ABSL_DECLARE_FLAG()` macro 00050 // within a header file, and define your flag using `ABSL_FLAG()` within your 00051 // header's associated `.cc` file. Such flags will be named `FLAGS_name`. 00052 // 00053 // Example: 00054 // 00055 // .h file 00056 // 00057 // // Declares usage of a flag named "FLAGS_count" 00058 // ABSL_DECLARE_FLAG(int, count); 00059 // 00060 // .cc file 00061 // 00062 // // Defines a flag named "FLAGS_count" with a default `int` value of 0. 00063 // ABSL_FLAG(int, count, 0, "Count of items to process"); 00064 // 00065 // No public methods of `absl::Flag<T>` are part of the Abseil Flags API. 00066 template <typename T> 00067 using Flag = flags_internal::Flag<T>; 00068 00069 // GetFlag() 00070 // 00071 // Returns the value (of type `T`) of an `absl::Flag<T>` instance, by value. Do 00072 // not construct an `absl::Flag<T>` directly and call `absl::GetFlag()`; 00073 // instead, refer to flag's constructed variable name (e.g. `FLAGS_name`). 00074 // Because this function returns by value and not by reference, it is 00075 // thread-safe, but note that the operation may be expensive; as a result, avoid 00076 // `absl::GetFlag()` within any tight loops. 00077 // 00078 // Example: 00079 // 00080 // // FLAGS_count is a Flag of type `int` 00081 // int my_count = absl::GetFlag(FLAGS_count); 00082 // 00083 // // FLAGS_firstname is a Flag of type `std::string` 00084 // std::string first_name = absl::GetFlag(FLAGS_firstname); 00085 template <typename T> 00086 T GetFlag(const absl::Flag<T>& flag) { 00087 #define ABSL_FLAGS_INTERNAL_LOCK_FREE_VALIDATE(BIT) \ 00088 static_assert( \ 00089 !std::is_same<T, BIT>::value, \ 00090 "Do not specify explicit template parameters to absl::GetFlag"); 00091 ABSL_FLAGS_INTERNAL_FOR_EACH_LOCK_FREE(ABSL_FLAGS_INTERNAL_LOCK_FREE_VALIDATE) 00092 #undef ABSL_FLAGS_INTERNAL_LOCK_FREE_VALIDATE 00093 00094 // Implementation notes: 00095 // 00096 // We are wrapping a union around the value of `T` to serve three purposes: 00097 // 00098 // 1. `U.value` has correct size and alignment for a value of type `T` 00099 // 2. The `U.value` constructor is not invoked since U's constructor does not 00100 // do it explicitly. 00101 // 3. The `U.value` destructor is invoked since U's destructor does it 00102 // explicitly. This makes `U` a kind of RAII wrapper around non default 00103 // constructible value of T, which is destructed when we leave the scope. 00104 // We do need to destroy U.value, which is constructed by 00105 // CommandLineFlag::Read even though we left it in a moved-from state 00106 // after std::move. 00107 // 00108 // All of this serves to avoid requiring `T` being default constructible. 00109 union U { 00110 T value; 00111 U() {} 00112 ~U() { value.~T(); } 00113 }; 00114 U u; 00115 00116 flag.internal.Read(&u.value, &flags_internal::FlagOps<T>); 00117 return std::move(u.value); 00118 } 00119 00120 // Overload for `GetFlag()` for types that support lock-free reads. 00121 #define ABSL_FLAGS_INTERNAL_LOCK_FREE_EXPORT(T) \ 00122 extern T GetFlag(const absl::Flag<T>& flag); 00123 ABSL_FLAGS_INTERNAL_FOR_EACH_LOCK_FREE(ABSL_FLAGS_INTERNAL_LOCK_FREE_EXPORT) 00124 #undef ABSL_FLAGS_INTERNAL_LOCK_FREE_EXPORT 00125 00126 // SetFlag() 00127 // 00128 // Sets the value of an `absl::Flag` to the value `v`. Do not construct an 00129 // `absl::Flag<T>` directly and call `absl::SetFlag()`; instead, use the 00130 // flag's variable name (e.g. `FLAGS_name`). This function is 00131 // thread-safe, but is potentially expensive. Avoid setting flags in general, 00132 // but especially within performance-critical code. 00133 template <typename T> 00134 void SetFlag(absl::Flag<T>* flag, const T& v) { 00135 flag->internal.Write(&v, &flags_internal::FlagOps<T>); 00136 } 00137 00138 // Overload of `SetFlag()` to allow callers to pass in a value that is 00139 // convertible to `T`. E.g., use this overload to pass a "const char*" when `T` 00140 // is `std::string`. 00141 template <typename T, typename V> 00142 void SetFlag(absl::Flag<T>* flag, const V& v) { 00143 T value(v); 00144 SetFlag<T>(flag, value); 00145 } 00146 00147 } // namespace absl 00148 00149 00150 // ABSL_FLAG() 00151 // 00152 // This macro defines an `absl::Flag<T>` instance of a specified type `T`: 00153 // 00154 // ABSL_FLAG(T, name, default_value, help); 00155 // 00156 // where: 00157 // 00158 // * `T` is a supported flag type (See below), 00159 // * `name` designates the name of the flag (as a global variable 00160 // `FLAGS_name`), 00161 // * `default_value` is an expression holding the default value for this flag 00162 // (which must be implicitly convertible to `T`), 00163 // * `help` is the help text, which can also be an expression. 00164 // 00165 // This macro expands to a flag named 'FLAGS_name' of type 'T': 00166 // 00167 // absl::Flag<T> FLAGS_name = ...; 00168 // 00169 // Note that all such instances are created as global variables. 00170 // 00171 // For `ABSL_FLAG()` values that you wish to expose to other translation units, 00172 // it is recommended to define those flags within the `.cc` file associated with 00173 // the header where the flag is declared. 00174 // 00175 // Note: do not construct objects of type `absl::Flag<T>` directly. Only use the 00176 // `ABSL_FLAG()` macro for such construction. 00177 #define ABSL_FLAG(Type, name, default_value, help) \ 00178 ABSL_FLAG_IMPL(Type, name, default_value, help) 00179 00180 // ABSL_FLAG().OnUpdate() 00181 // 00182 // Defines a flag of type `T` with a callback attached: 00183 // 00184 // ABSL_FLAG(T, name, default_value, help).OnUpdate(callback); 00185 // 00186 // After any setting of the flag value, the callback will be called at least 00187 // once. A rapid sequence of changes may be merged together into the same 00188 // callback. No concurrent calls to the callback will be made for the same 00189 // flag. Callbacks are allowed to read the current value of the flag but must 00190 // not mutate that flag. 00191 // 00192 // The update mechanism guarantees "eventual consistency"; if the callback 00193 // derives an auxiliary data structure from the flag value, it is guaranteed 00194 // that eventually the flag value and the derived data structure will be 00195 // consistent. 00196 // 00197 // Note: ABSL_FLAG.OnUpdate() does not have a public definition. Hence, this 00198 // comment serves as its API documentation. 00199 00200 00201 // ----------------------------------------------------------------------------- 00202 // Implementation details below this section 00203 // ----------------------------------------------------------------------------- 00204 00205 // ABSL_FLAG_IMPL macro definition conditional on ABSL_FLAGS_STRIP_NAMES 00206 00207 #if ABSL_FLAGS_STRIP_NAMES 00208 #define ABSL_FLAG_IMPL_FLAGNAME(txt) "" 00209 #define ABSL_FLAG_IMPL_FILENAME() "" 00210 #define ABSL_FLAG_IMPL_REGISTRAR(T, flag) \ 00211 absl::flags_internal::FlagRegistrar<T, false>(&flag) 00212 #else 00213 #define ABSL_FLAG_IMPL_FLAGNAME(txt) txt 00214 #define ABSL_FLAG_IMPL_FILENAME() __FILE__ 00215 #define ABSL_FLAG_IMPL_REGISTRAR(T, flag) \ 00216 absl::flags_internal::FlagRegistrar<T, true>(&flag) 00217 #endif 00218 00219 // ABSL_FLAG_IMPL macro definition conditional on ABSL_FLAGS_STRIP_HELP 00220 00221 #if ABSL_FLAGS_STRIP_HELP 00222 #define ABSL_FLAG_IMPL_FLAGHELP(txt) absl::flags_internal::kStrippedFlagHelp 00223 #else 00224 #define ABSL_FLAG_IMPL_FLAGHELP(txt) txt 00225 #endif 00226 00227 #define ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, txt) \ 00228 static std::string AbslFlagsWrapHelp##name() { \ 00229 return ABSL_FLAG_IMPL_FLAGHELP(txt); \ 00230 } 00231 00232 #define ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value) \ 00233 static void* AbslFlagsInitFlag##name() { \ 00234 return absl::flags_internal::MakeFromDefaultValue<Type>(default_value); \ 00235 } 00236 00237 // ABSL_FLAG_IMPL 00238 // 00239 // Note: Name of registrar object is not arbitrary. It is used to "grab" 00240 // global name for FLAGS_no<flag_name> symbol, thus preventing the possibility 00241 // of defining two flags with names foo and nofoo. 00242 #define ABSL_FLAG_IMPL(Type, name, default_value, help) \ 00243 namespace absl {} \ 00244 ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value) \ 00245 ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, help) \ 00246 absl::Flag<Type> FLAGS_##name( \ 00247 ABSL_FLAG_IMPL_FLAGNAME(#name), \ 00248 &AbslFlagsWrapHelp##name, \ 00249 ABSL_FLAG_IMPL_FILENAME(), \ 00250 &absl::flags_internal::FlagMarshallingOps<Type>, \ 00251 &AbslFlagsInitFlag##name); \ 00252 extern bool FLAGS_no##name; \ 00253 bool FLAGS_no##name = ABSL_FLAG_IMPL_REGISTRAR(Type, FLAGS_##name) 00254 00255 // ABSL_RETIRED_FLAG 00256 // 00257 // Designates the flag (which is usually pre-existing) as "retired." A retired 00258 // flag is a flag that is now unused by the program, but may still be passed on 00259 // the command line, usually by production scripts. A retired flag is ignored 00260 // and code can't access it at runtime. 00261 // 00262 // This macro registers a retired flag with given name and type, with a name 00263 // identical to the name of the original flag you are retiring. The retired 00264 // flag's type can change over time, so that you can retire code to support a 00265 // custom flag type. 00266 // 00267 // This macro has the same signature as `ABSL_FLAG`. To retire a flag, simply 00268 // replace an `ABSL_FLAG` definition with `ABSL_RETIRED_FLAG`, leaving the 00269 // arguments unchanged (unless of course you actually want to retire the flag 00270 // type at this time as well). 00271 // 00272 // `default_value` is only used as a double check on the type. `explanation` is 00273 // unused. 00274 // TODO(rogeeff): Return an anonymous struct instead of bool, and place it into 00275 // the unnamed namespace. 00276 #define ABSL_RETIRED_FLAG(type, flagname, default_value, explanation) \ 00277 ABSL_ATTRIBUTE_UNUSED static const bool ignored_##flagname = \ 00278 ([] { return type(default_value); }, \ 00279 absl::flags_internal::RetiredFlag<type>(#flagname)) 00280 00281 #endif // ABSL_FLAGS_FLAG_H_