OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2006, Google Inc. |
| 2 // All rights reserved. |
| 3 // |
| 4 // Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted provided that the following conditions are |
| 6 // met: |
| 7 // |
| 8 // * Redistributions of source code must retain the above copyright |
| 9 // notice, this list of conditions and the following disclaimer. |
| 10 // * Redistributions in binary form must reproduce the above |
| 11 // copyright notice, this list of conditions and the following disclaimer |
| 12 // in the documentation and/or other materials provided with the |
| 13 // distribution. |
| 14 // * Neither the name of Google Inc. nor the names of its |
| 15 // contributors may be used to endorse or promote products derived from |
| 16 // this software without specific prior written permission. |
| 17 // |
| 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
| 30 // --- |
| 31 // Author: Ray Sidney |
| 32 // Revamped and reorganized by Craig Silverstein |
| 33 // |
| 34 // This is the file that should be included by any file which declares |
| 35 // or defines a command line flag or wants to parse command line flags |
| 36 // or print a program usage message (which will include information about |
| 37 // flags). Executive summary, in the form of an example foo.cc file: |
| 38 // |
| 39 // #include "foo.h" // foo.h has a line "DECLARE_int32(start);" |
| 40 // |
| 41 // DEFINE_int32(end, 1000, "The last record to read"); |
| 42 // DECLARE_bool(verbose); // some other file has a DEFINE_bool(verbose, ...
) |
| 43 // |
| 44 // void MyFunc() { |
| 45 // if (FLAGS_verbose) printf("Records %d-%d\n", FLAGS_start, FLAGS_end); |
| 46 // } |
| 47 // |
| 48 // Then, at the command-line: |
| 49 // ./foo --noverbose --start=5 --end=100 |
| 50 // |
| 51 // For more details, see |
| 52 // doc/gflags.html |
| 53 // |
| 54 // --- A note about thread-safety: |
| 55 // |
| 56 // We describe many functions in this routine as being thread-hostile, |
| 57 // thread-compatible, or thread-safe. Here are the meanings we use: |
| 58 // |
| 59 // thread-safe: it is safe for multiple threads to call this routine |
| 60 // (or, when referring to a class, methods of this class) |
| 61 // concurrently. |
| 62 // thread-hostile: it is not safe for multiple threads to call this |
| 63 // routine (or methods of this class) concurrently. In gflags, |
| 64 // most thread-hostile routines are intended to be called early in, |
| 65 // or even before, main() -- that is, before threads are spawned. |
| 66 // thread-compatible: it is safe for multiple threads to read from |
| 67 // this variable (when applied to variables), or to call const |
| 68 // methods of this class (when applied to classes), as long as no |
| 69 // other thread is writing to the variable or calling non-const |
| 70 // methods of this class. |
| 71 |
| 72 #ifndef GOOGLE_GFLAGS_H_ |
| 73 #define GOOGLE_GFLAGS_H_ |
| 74 |
| 75 #include <string> |
| 76 #include <vector> |
| 77 |
| 78 // We care a lot about number of bits things take up. Unfortunately, |
| 79 // systems define their bit-specific ints in a lot of different ways. |
| 80 // We use our own way, and have a typedef to get there. |
| 81 // Note: these commands below may look like "#if 1" or "#if 0", but |
| 82 // that's because they were constructed that way at ./configure time. |
| 83 // Look at gflags.h.in to see how they're calculated (based on your config). |
| 84 #if @ac_cv_have_stdint_h@ |
| 85 #include <stdint.h> // the normal place uint16_t is defined |
| 86 #endif |
| 87 #if @ac_cv_have_systypes_h@ |
| 88 #include <sys/types.h> // the normal place u_int16_t is defined |
| 89 #endif |
| 90 #if @ac_cv_have_inttypes_h@ |
| 91 #include <inttypes.h> // a third place for uint16_t or u_int16_t |
| 92 #endif |
| 93 |
| 94 @ac_google_start_namespace@ |
| 95 |
| 96 #if @ac_cv_have_uint16_t@ // the C99 format |
| 97 typedef int32_t int32; |
| 98 typedef uint32_t uint32; |
| 99 typedef int64_t int64; |
| 100 typedef uint64_t uint64; |
| 101 #elif @ac_cv_have_u_int16_t@ // the BSD format |
| 102 typedef int32_t int32; |
| 103 typedef u_int32_t uint32; |
| 104 typedef int64_t int64; |
| 105 typedef u_int64_t uint64; |
| 106 #elif @ac_cv_have___int16@ // the windows (vc7) format |
| 107 typedef __int32 int32; |
| 108 typedef unsigned __int32 uint32; |
| 109 typedef __int64 int64; |
| 110 typedef unsigned __int64 uint64; |
| 111 #else |
| 112 #error Do not know how to define a 32-bit integer quantity on your system |
| 113 #endif |
| 114 |
| 115 // -------------------------------------------------------------------- |
| 116 // To actually define a flag in a file, use DEFINE_bool, |
| 117 // DEFINE_string, etc. at the bottom of this file. You may also find |
| 118 // it useful to register a validator with the flag. This ensures that |
| 119 // when the flag is parsed from the commandline, or is later set via |
| 120 // SetCommandLineOption, we call the validation function. The |
| 121 // validation function should return true if the flag value is valid, |
| 122 // and false otherwise. |
| 123 // |
| 124 // This function is safe to call at global construct time (as in the |
| 125 // example below). |
| 126 // |
| 127 // Example use: |
| 128 // static bool ValidatePort(const char* flagname, int32 value) { |
| 129 // if (value > 0 && value < 32768) // value is ok |
| 130 // return true; |
| 131 // printf("Invalid value for --%s: %d\n", flagname, (int)value); |
| 132 // return false; |
| 133 // } |
| 134 // DEFINE_int32(port, 0, "What port to listen on"); |
| 135 // static bool dummy = RegisterFlagValidator(&FLAGS_port, &ValidatePort); |
| 136 |
| 137 // Returns true if successfully registered, false if not (because the |
| 138 // first argument doesn't point to a command-line flag, or because a |
| 139 // validator is already registered for this flag). |
| 140 bool RegisterFlagValidator(const bool* flag, |
| 141 bool (*validate_fn)(const char*, bool)); |
| 142 bool RegisterFlagValidator(const int32* flag, |
| 143 bool (*validate_fn)(const char*, int32)); |
| 144 bool RegisterFlagValidator(const int64* flag, |
| 145 bool (*validate_fn)(const char*, int64)); |
| 146 bool RegisterFlagValidator(const uint64* flag, |
| 147 bool (*validate_fn)(const char*, uint64)); |
| 148 bool RegisterFlagValidator(const double* flag, |
| 149 bool (*validate_fn)(const char*, double)); |
| 150 bool RegisterFlagValidator(const std::string* flag, |
| 151 bool (*validate_fn)(const char*, const std::string&))
; |
| 152 |
| 153 |
| 154 // -------------------------------------------------------------------- |
| 155 // These methods are the best way to get access to info about the |
| 156 // list of commandline flags. Note that these routines are pretty slow. |
| 157 // GetAllFlags: mostly-complete info about the list, sorted by file. |
| 158 // ShowUsageWithFlags: pretty-prints the list to stdout (what --help does) |
| 159 // ShowUsageWithFlagsRestrict: limit to filenames with restrict as a substr |
| 160 // |
| 161 // In addition to accessing flags, you can also access argv[0] (the program |
| 162 // name) and argv (the entire commandline), which we sock away a copy of. |
| 163 // These variables are static, so you should only set them once. |
| 164 |
| 165 struct CommandLineFlagInfo { |
| 166 std::string name; // the name of the flag |
| 167 std::string type; // the type of the flag: int32, etc |
| 168 std::string description; // the "help text" associated with the flag |
| 169 std::string current_value; // the current value, as a string |
| 170 std::string default_value; // the default value, as a string |
| 171 std::string filename; // 'cleaned' version of filename holding the flag |
| 172 bool has_validator_fn; // true if RegisterFlagValidator called on flag |
| 173 bool is_default; // true if the flag has default value |
| 174 }; |
| 175 |
| 176 extern void GetAllFlags(std::vector<CommandLineFlagInfo>* OUTPUT); |
| 177 // These two are actually defined in commandlineflags_reporting.cc. |
| 178 extern void ShowUsageWithFlags(const char *argv0); // what --help does |
| 179 extern void ShowUsageWithFlagsRestrict(const char *argv0, const char *restrict); |
| 180 |
| 181 // Create a descriptive string for a flag. |
| 182 // Goes to some trouble to make pretty line breaks. |
| 183 extern std::string DescribeOneFlag(const CommandLineFlagInfo& flag); |
| 184 |
| 185 // Thread-hostile; meant to be called before any threads are spawned. |
| 186 extern void SetArgv(int argc, const char** argv); |
| 187 // The following functions are thread-safe as long as SetArgv() is |
| 188 // only called before any threads start. |
| 189 extern const std::vector<std::string>& GetArgvs(); // all of argv as a vector |
| 190 extern const char* GetArgv(); // all of argv as a string |
| 191 extern const char* GetArgv0(); // only argv0 |
| 192 extern uint32 GetArgvSum(); // simple checksum of argv |
| 193 extern const char* ProgramInvocationName(); // argv0, or "UNKNOWN" if not set |
| 194 extern const char* ProgramInvocationShortName(); // basename(argv0) |
| 195 // ProgramUsage() is thread-safe as long as SetUsageMessage() is only |
| 196 // called before any threads start. |
| 197 extern const char* ProgramUsage(); // string set by SetUsageMessage() |
| 198 |
| 199 |
| 200 // -------------------------------------------------------------------- |
| 201 // Normally you access commandline flags by just saying "if (FLAGS_foo)" |
| 202 // or whatever, and set them by calling "FLAGS_foo = bar" (or, more |
| 203 // commonly, via the DEFINE_foo macro). But if you need a bit more |
| 204 // control, we have programmatic ways to get/set the flags as well. |
| 205 // These programmatic ways to access flags are thread-safe, but direct |
| 206 // access is only thread-compatible. |
| 207 |
| 208 // Return true iff the flagname was found. |
| 209 // OUTPUT is set to the flag's value, or unchanged if we return false. |
| 210 extern bool GetCommandLineOption(const char* name, std::string* OUTPUT); |
| 211 |
| 212 // Return true iff the flagname was found. OUTPUT is set to the flag's |
| 213 // CommandLineFlagInfo or unchanged if we return false. |
| 214 extern bool GetCommandLineFlagInfo(const char* name, |
| 215 CommandLineFlagInfo* OUTPUT); |
| 216 |
| 217 // Return the CommandLineFlagInfo of the flagname. exit() if name not found. |
| 218 // Example usage, to check if a flag's value is currently the default value: |
| 219 // if (GetCommandLineFlagInfoOrDie("foo").is_default) ... |
| 220 extern CommandLineFlagInfo GetCommandLineFlagInfoOrDie(const char* name); |
| 221 |
| 222 enum FlagSettingMode { |
| 223 // update the flag's value (can call this multiple times). |
| 224 SET_FLAGS_VALUE, |
| 225 // update the flag's value, but *only if* it has not yet been updated |
| 226 // with SET_FLAGS_VALUE, SET_FLAG_IF_DEFAULT, or "FLAGS_xxx = nondef". |
| 227 SET_FLAG_IF_DEFAULT, |
| 228 // set the flag's default value to this. If the flag has not yet updated |
| 229 // yet (via SET_FLAGS_VALUE, SET_FLAG_IF_DEFAULT, or "FLAGS_xxx = nondef") |
| 230 // change the flag's current value to the new default value as well. |
| 231 SET_FLAGS_DEFAULT |
| 232 }; |
| 233 |
| 234 // Set a particular flag ("command line option"). Returns a string |
| 235 // describing the new value that the option has been set to. The |
| 236 // return value API is not well-specified, so basically just depend on |
| 237 // it to be empty if the setting failed for some reason -- the name is |
| 238 // not a valid flag name, or the value is not a valid value -- and |
| 239 // non-empty else. |
| 240 |
| 241 // SetCommandLineOption uses set_mode == SET_FLAGS_VALUE (the common case) |
| 242 extern std::string SetCommandLineOption(const char* name, const char* value); |
| 243 extern std::string SetCommandLineOptionWithMode(const char* name, const char* va
lue, |
| 244 FlagSettingMode set_mode); |
| 245 |
| 246 |
| 247 // -------------------------------------------------------------------- |
| 248 // Saves the states (value, default value, whether the user has set |
| 249 // the flag, registered validators, etc) of all flags, and restores |
| 250 // them when the FlagSaver is destroyed. This is very useful in |
| 251 // tests, say, when you want to let your tests change the flags, but |
| 252 // make sure that they get reverted to the original states when your |
| 253 // test is complete. |
| 254 // |
| 255 // Example usage: |
| 256 // void TestFoo() { |
| 257 // FlagSaver s1; |
| 258 // FLAG_foo = false; |
| 259 // FLAG_bar = "some value"; |
| 260 // |
| 261 // // test happens here. You can return at any time |
| 262 // // without worrying about restoring the FLAG values. |
| 263 // } |
| 264 // |
| 265 // Note: This class is marked with __attribute__((unused)) because all the |
| 266 // work is done in the constructor and destructor, so in the standard |
| 267 // usage example above, the compiler would complain that it's an |
| 268 // unused variable. |
| 269 // |
| 270 // This class is thread-safe. |
| 271 |
| 272 class FlagSaver { |
| 273 public: |
| 274 FlagSaver(); |
| 275 ~FlagSaver(); |
| 276 |
| 277 private: |
| 278 class FlagSaverImpl* impl_; // we use pimpl here to keep API steady |
| 279 |
| 280 FlagSaver(const FlagSaver&); // no copying! |
| 281 void operator=(const FlagSaver&); |
| 282 } @ac_cv___attribute__unused@; |
| 283 |
| 284 // -------------------------------------------------------------------- |
| 285 // Some deprecated or hopefully-soon-to-be-deprecated functions. |
| 286 |
| 287 // This is often used for logging. TODO(csilvers): figure out a better way |
| 288 extern std::string CommandlineFlagsIntoString(); |
| 289 // Usually where this is used, a FlagSaver should be used instead. |
| 290 extern bool ReadFlagsFromString(const std::string& flagfilecontents, |
| 291 const char* prog_name, |
| 292 bool errors_are_fatal); // uses SET_FLAGS_VALUE |
| 293 |
| 294 // These let you manually implement --flagfile functionality. |
| 295 // DEPRECATED. |
| 296 extern bool AppendFlagsIntoFile(const std::string& filename, const char* prog_na
me); |
| 297 extern bool SaveCommandFlags(); // actually defined in google.cc ! |
| 298 extern bool ReadFromFlagsFile(const std::string& filename, const char* prog_name
, |
| 299 bool errors_are_fatal); // uses SET_FLAGS_VALUE |
| 300 |
| 301 |
| 302 // -------------------------------------------------------------------- |
| 303 // Useful routines for initializing flags from the environment. |
| 304 // In each case, if 'varname' does not exist in the environment |
| 305 // return defval. If 'varname' does exist but is not valid |
| 306 // (e.g., not a number for an int32 flag), abort with an error. |
| 307 // Otherwise, return the value. NOTE: for booleans, for true use |
| 308 // 't' or 'T' or 'true' or '1', for false 'f' or 'F' or 'false' or '0'. |
| 309 |
| 310 extern bool BoolFromEnv(const char *varname, bool defval); |
| 311 extern int32 Int32FromEnv(const char *varname, int32 defval); |
| 312 extern int64 Int64FromEnv(const char *varname, int64 defval); |
| 313 extern uint64 Uint64FromEnv(const char *varname, uint64 defval); |
| 314 extern double DoubleFromEnv(const char *varname, double defval); |
| 315 extern const char *StringFromEnv(const char *varname, const char *defval); |
| 316 |
| 317 |
| 318 // -------------------------------------------------------------------- |
| 319 // The next two functions parse commandlineflags from main(): |
| 320 |
| 321 // Set the "usage" message for this program. For example: |
| 322 // string usage("This program does nothing. Sample usage:\n"); |
| 323 // usage += argv[0] + " <uselessarg1> <uselessarg2>"; |
| 324 // SetUsageMessage(usage); |
| 325 // Do not include commandline flags in the usage: we do that for you! |
| 326 // Thread-hostile; meant to be called before any threads are spawned. |
| 327 extern void SetUsageMessage(const std::string& usage); |
| 328 |
| 329 // Looks for flags in argv and parses them. Rearranges argv to put |
| 330 // flags first, or removes them entirely if remove_flags is true. |
| 331 // If a flag is defined more than once in the command line or flag |
| 332 // file, the last definition is used. |
| 333 // See top-of-file for more details on this function. |
| 334 #ifndef SWIG // In swig, use ParseCommandLineFlagsScript() instead. |
| 335 extern uint32 ParseCommandLineFlags(int *argc, char*** argv, |
| 336 bool remove_flags); |
| 337 #endif |
| 338 |
| 339 |
| 340 // Calls to ParseCommandLineNonHelpFlags and then to |
| 341 // HandleCommandLineHelpFlags can be used instead of a call to |
| 342 // ParseCommandLineFlags during initialization, in order to allow for |
| 343 // changing default values for some FLAGS (via |
| 344 // e.g. SetCommandLineOptionWithMode calls) between the time of |
| 345 // command line parsing and the time of dumping help information for |
| 346 // the flags as a result of command line parsing. |
| 347 // If a flag is defined more than once in the command line or flag |
| 348 // file, the last definition is used. |
| 349 extern uint32 ParseCommandLineNonHelpFlags(int *argc, char*** argv, |
| 350 bool remove_flags); |
| 351 // This is actually defined in commandlineflags_reporting.cc. |
| 352 // This function is misnamed (it also handles --version, etc.), but |
| 353 // it's too late to change that now. :-( |
| 354 extern void HandleCommandLineHelpFlags(); // in commandlineflags_reporting.cc |
| 355 |
| 356 // Allow command line reparsing. Disables the error normally |
| 357 // generated when an unknown flag is found, since it may be found in a |
| 358 // later parse. Thread-hostile; meant to be called before any threads |
| 359 // are spawned. |
| 360 extern void AllowCommandLineReparsing(); |
| 361 |
| 362 // Reparse the flags that have not yet been recognized. |
| 363 // Only flags registered since the last parse will be recognized. |
| 364 // Any flag value must be provided as part of the argument using "=", |
| 365 // not as a separate command line argument that follows the flag argument. |
| 366 // Intended for handling flags from dynamically loaded libraries, |
| 367 // since their flags are not registered until they are loaded. |
| 368 extern uint32 ReparseCommandLineNonHelpFlags(); |
| 369 |
| 370 |
| 371 // -------------------------------------------------------------------- |
| 372 // Now come the command line flag declaration/definition macros that |
| 373 // will actually be used. They're kind of hairy. A major reason |
| 374 // for this is initialization: we want people to be able to access |
| 375 // variables in global constructors and have that not crash, even if |
| 376 // their global constructor runs before the global constructor here. |
| 377 // (Obviously, we can't guarantee the flags will have the correct |
| 378 // default value in that case, but at least accessing them is safe.) |
| 379 // The only way to do that is have flags point to a static buffer. |
| 380 // So we make one, using a union to ensure proper alignment, and |
| 381 // then use placement-new to actually set up the flag with the |
| 382 // correct default value. In the same vein, we have to worry about |
| 383 // flag access in global destructors, so FlagRegisterer has to be |
| 384 // careful never to destroy the flag-values it constructs. |
| 385 // |
| 386 // Note that when we define a flag variable FLAGS_<name>, we also |
| 387 // preemptively define a junk variable, FLAGS_no<name>. This is to |
| 388 // cause a link-time error if someone tries to define 2 flags with |
| 389 // names like "logging" and "nologging". We do this because a bool |
| 390 // flag FLAG can be set from the command line to true with a "-FLAG" |
| 391 // argument, and to false with a "-noFLAG" argument, and so this can |
| 392 // potentially avert confusion. |
| 393 // |
| 394 // We also put flags into their own namespace. It is purposefully |
| 395 // named in an opaque way that people should have trouble typing |
| 396 // directly. The idea is that DEFINE puts the flag in the weird |
| 397 // namespace, and DECLARE imports the flag from there into the current |
| 398 // namespace. The net result is to force people to use DECLARE to get |
| 399 // access to a flag, rather than saying "extern bool FLAGS_whatever;" |
| 400 // or some such instead. We want this so we can put extra |
| 401 // functionality (like sanity-checking) in DECLARE if we want, and |
| 402 // make sure it is picked up everywhere. |
| 403 // |
| 404 // We also put the type of the variable in the namespace, so that |
| 405 // people can't DECLARE_int32 something that they DEFINE_bool'd |
| 406 // elsewhere. |
| 407 |
| 408 class FlagRegisterer { |
| 409 public: |
| 410 FlagRegisterer(const char* name, const char* type, |
| 411 const char* help, const char* filename, |
| 412 void* current_storage, void* defvalue_storage); |
| 413 }; |
| 414 |
| 415 #ifndef SWIG // In swig, ignore the main flag declarations |
| 416 |
| 417 // If your application #defines STRIP_FLAG_HELP to a non-zero value |
| 418 // before #including this file, we remove the help message from the |
| 419 // binary file. This can reduce the size of the resulting binary |
| 420 // somewhat, and may also be useful for security reasons. |
| 421 |
| 422 extern const char kStrippedFlagHelp[]; |
| 423 |
| 424 #if defined(STRIP_FLAG_HELP) && STRIP_FLAG_HELP > 0 |
| 425 // Need this construct to avoid the 'defined but not used' warning. |
| 426 #define MAYBE_STRIPPED_HELP(txt) (false ? (txt) : kStrippedFlagHelp) |
| 427 #else |
| 428 #define MAYBE_STRIPPED_HELP(txt) txt |
| 429 #endif |
| 430 |
| 431 // Each command-line flag has two variables associated with it: one |
| 432 // with the current value, and one with the default value. However, |
| 433 // we have a third variable, which is where value is assigned; it's a |
| 434 // constant. This guarantees that FLAG_##value is initialized at |
| 435 // static initialization time (e.g. before program-start) rather than |
| 436 // than global construction time (which is after program-start but |
| 437 // before main), at least when 'value' is a compile-time constant. We |
| 438 // use a small trick for the "default value" variable, and call it |
| 439 // FLAGS_no<name>. This serves the second purpose of assuring a |
| 440 // compile error if someone tries to define a flag named no<name> |
| 441 // which is illegal (--foo and --nofoo both affect the "foo" flag). |
| 442 #define DEFINE_VARIABLE(type, shorttype, name, value, help) \ |
| 443 namespace fL##shorttype { \ |
| 444 static const type FLAGS_nono##name = value; \ |
| 445 type FLAGS_##name = FLAGS_nono##name; \ |
| 446 type FLAGS_no##name = FLAGS_nono##name; \ |
| 447 static @ac_google_namespace@::FlagRegisterer o_##name( \ |
| 448 #name, #type, MAYBE_STRIPPED_HELP(help), __FILE__, \ |
| 449 &FLAGS_##name, &FLAGS_no##name); \ |
| 450 } \ |
| 451 using fL##shorttype::FLAGS_##name |
| 452 |
| 453 #define DECLARE_VARIABLE(type, shorttype, name) \ |
| 454 namespace fL##shorttype { \ |
| 455 extern type FLAGS_##name; \ |
| 456 } \ |
| 457 using fL##shorttype::FLAGS_##name |
| 458 |
| 459 // For boolean flags, we want to do the extra check that the passed-in |
| 460 // value is actually a bool, and not a string or something that can be |
| 461 // coerced to a bool. These declarations (no definition needed!) will |
| 462 // help us do that, and never evaluate from, which is important. |
| 463 // We'll use 'sizeof(IsBool(val))' to distinguish. |
| 464 namespace fLB { |
| 465 template<typename From> double IsBoolFlag(const From& from); |
| 466 bool IsBoolFlag(bool from); |
| 467 } |
| 468 extern bool FlagsTypeWarn(const char *name); |
| 469 |
| 470 #define DECLARE_bool(name) DECLARE_VARIABLE(bool,B, name) |
| 471 // We have extra code here to make sure 'val' is actually a boolean. |
| 472 #define DEFINE_bool(name,val,txt) namespace fLB { \ |
| 473 const bool FLAGS_nonono##name = \ |
| 474 (sizeof(@ac_google_namespace@::fLB::IsBo
olFlag(val)) \ |
| 475 == sizeof(double)) \ |
| 476 ? @ac_google_namespace@::FlagsTypeWarn(#
name) : true; \ |
| 477 } \ |
| 478 DEFINE_VARIABLE(bool,B, name, val, txt) |
| 479 #define DECLARE_int32(name) DECLARE_VARIABLE(@ac_google_namespace@::int3
2,I, name) |
| 480 #define DEFINE_int32(name,val,txt) DEFINE_VARIABLE(@ac_google_namespace@::int32
,I, name, val, txt) |
| 481 |
| 482 #define DECLARE_int64(name) DECLARE_VARIABLE(@ac_google_namespace@::int6
4,I64, name) |
| 483 #define DEFINE_int64(name,val,txt) DEFINE_VARIABLE(@ac_google_namespace@::int64
,I64, name, val, txt) |
| 484 |
| 485 #define DECLARE_uint64(name) DECLARE_VARIABLE(@ac_google_namespace@::uint
64,U64, name) |
| 486 #define DEFINE_uint64(name,val,txt) DEFINE_VARIABLE(@ac_google_namespace@::uint6
4,U64, name, val, txt) |
| 487 |
| 488 #define DECLARE_double(name) DECLARE_VARIABLE(double,D, name) |
| 489 #define DEFINE_double(name,val,txt) DEFINE_VARIABLE(double,D, name, val, txt) |
| 490 |
| 491 // Strings are trickier, because they're not a POD, so we can't |
| 492 // construct them at static-initialization time (instead they get |
| 493 // constructed at global-constructor time, which is much later). To |
| 494 // try to avoid crashes in that case, we use a char buffer to store |
| 495 // the string, which we can static-initialize, and then placement-new |
| 496 // into it later. It's not perfect, but the best we can do. |
| 497 #define DECLARE_string(name) namespace fLS { extern std::string& FLAGS_##name;
} \ |
| 498 using fLS::FLAGS_##name |
| 499 |
| 500 // We need to define a var named FLAGS_no##name so people don't define |
| 501 // --string and --nostring. And we need a temporary place to put val |
| 502 // so we don't have to evaluate it twice. Two great needs that go |
| 503 // great together! |
| 504 #define DEFINE_string(name, val, txt) \ |
| 505 namespace fLS { \ |
| 506 static union { void* align; char s[sizeof(std::string)]; } s_##name[2]; \ |
| 507 const std::string* const FLAGS_no##name = new (s_##name[0].s) std::string(va
l); \ |
| 508 static @ac_google_namespace@::FlagRegisterer o_##name( \ |
| 509 #name, "string", MAYBE_STRIPPED_HELP(txt), __FILE__, \ |
| 510 s_##name[0].s, new (s_##name[1].s) std::string(*FLAGS_no##name)); \ |
| 511 std::string& FLAGS_##name = *(reinterpret_cast<std::string*>(s_##name[0].s))
; \ |
| 512 } \ |
| 513 using fLS::FLAGS_##name |
| 514 |
| 515 #endif // SWIG |
| 516 |
| 517 @ac_google_end_namespace@ |
| 518 |
| 519 #endif // GOOGLE_GFLAGS_H_ |
OLD | NEW |