15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This is the file that should be included by any file which declares
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// or defines a command line flag or wants to parse command line flags
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// or print a program usage message (which will include information about
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// flags).  Executive summary, in the form of an example foo.cc file:
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    #include "foo.h"         // foo.h has a line "DECLARE_int32(start);"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    DEFINE_int32(end, 1000, "The last record to read");
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    DECLARE_bool(verbose);   // some other file has a DEFINE_bool(verbose, ...)
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    void MyFunc() {
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//      if (FLAGS_verbose) printf("Records %d-%d\n", FLAGS_start, FLAGS_end);
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    }
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    Then, at the command-line:
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//       ./foo --noverbose --start=5 --end=100
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef BASE_COMMANDLINEFLAGS_H_
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define BASE_COMMANDLINEFLAGS_H_
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <assert.h>
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/port.h"
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/stl_decl_msvc.h"
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/global_strip_options.h"
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// --------------------------------------------------------------------
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// To actually define a flag in a file, use DEFINE_bool,
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DEFINE_string, etc. at the bottom of this file.  You may also find
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// it useful to register a validator with the flag.  This ensures that
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// when the flag is parsed from the commandline, or is later set via
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// SetCommandLineOption, we call the validation function.
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The validation function should return true if the flag value is valid, and
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// false otherwise. If the function returns false for the new setting of the
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// flag, the flag will retain its current value. If it returns false for the
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// default value, InitGoogle will die.
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This function is safe to call at global construct time (as in the
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// example below).
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Example use:
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    static bool ValidatePort(const char* flagname, int32 value) {
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//       if (value > 0 && value < 32768)   // value is ok
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//         return true;
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//       printf("Invalid value for --%s: %d\n", flagname, (int)value);
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//       return false;
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    }
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    DEFINE_int32(port, 0, "What port to listen on");
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    static bool dummy = RegisterFlagValidator(&FLAGS_port, &ValidatePort);
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns true if successfully registered, false if not (because the
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// first argument doesn't point to a command-line flag, or because a
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// validator is already registered for this flag).
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool RegisterFlagValidator(const bool* flag,
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           bool (*validate_fn)(const char*, bool));
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool RegisterFlagValidator(const int32* flag,
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           bool (*validate_fn)(const char*, int32));
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool RegisterFlagValidator(const int64* flag,
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           bool (*validate_fn)(const char*, int64));
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool RegisterFlagValidator(const uint64* flag,
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           bool (*validate_fn)(const char*, uint64));
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool RegisterFlagValidator(const double* flag,
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           bool (*validate_fn)(const char*, double));
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool RegisterFlagValidator(const string* flag,
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           bool (*validate_fn)(const char*, const string&));
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// --------------------------------------------------------------------
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// These methods are the best way to get access to info about the
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// list of commandline flags.  Note that these routines are pretty slow.
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   GetAllFlags: mostly-complete info about the list, sorted by file.
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   ShowUsageWithFlags: pretty-prints the list to stdout (what --help does)
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   ShowUsageWithFlagsRestrict: limit to filenames with restrict as a substr
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// In addition to accessing flags, you can also access argv[0] (the program
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// name) and argv (the entire commandline), which we sock away a copy of.
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// These variables are static, so you should only set them once.
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct CommandLineFlagInfo {
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  string name;            // the name of the flag
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  string type;            // the type of the flag: int32, etc
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  string description;     // the "help text" associated with the flag
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  string current_value;   // the current value, as a string
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  string default_value;   // the default value, as a string
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  string filename;        // 'cleaned' version of filename holding the flag
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool is_default;        // true if the flag has default value
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool has_validator_fn;  // true if RegisterFlagValidator called on this flag
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern void GetAllFlags(vector<CommandLineFlagInfo>* OUTPUT);
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// These two are actually defined in commandlineflags_reporting.cc.
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern void ShowUsageWithFlags(const char *argv0);  // what --help does
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern void ShowUsageWithFlagsRestrict(const char *argv0, const char *restrict);
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Create a descriptive string for a flag.
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Goes to some trouble to make pretty line breaks.
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern string DescribeOneFlag(const CommandLineFlagInfo& flag);
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Thread-hostile; meant to be called before any threads are spawned.
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern void SetArgv(int argc, const char** argv);
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The following functions are thread-safe as long as SetArgv() is
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// only called before any threads start.
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern const vector<string>& GetArgvs();    // all of argv = vector of strings
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern const char* GetArgv();               // all of argv as a string
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern const char* GetArgv0();              // only argv0
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern uint32 GetArgvSum();                 // simple checksum of argv
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern const char* ProgramInvocationName(); // argv0, or "UNKNOWN" if not set
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern const char* ProgramInvocationShortName();   // basename(argv0)
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ProgramUsage() is thread-safe as long as SetUsageMessage() is only
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// called before any threads start.
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern const char* ProgramUsage();          // string set by SetUsageMessage()
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// --------------------------------------------------------------------
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Normally you access commandline flags by just saying "if (FLAGS_foo)"
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// or whatever, and set them by calling "FLAGS_foo = bar" (or, more
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// commonly, via the DEFINE_foo macro).  But if you need a bit more
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// control, we have programmatic ways to get/set the flags as well.
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// These programmatic ways to access flags are thread-safe, but direct
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// access is only thread-compatible.
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Return true iff the flagname was found.
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// OUTPUT is set to the flag's value, or unchanged if we return false.
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern bool GetCommandLineOption(const char* name, string* OUTPUT);
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Return true iff the flagname was found. OUTPUT is set to the flag's
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// CommandLineFlagInfo or unchanged if we return false.
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern bool GetCommandLineFlagInfo(const char* name,
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   CommandLineFlagInfo* OUTPUT);
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Return the CommandLineFlagInfo of the flagname.  exit() if name not found.
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Example usage, to check if a flag's value is currently the default value:
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   if (GetCommandLineFlagInfoOrDie("foo").is_default) ...
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern CommandLineFlagInfo GetCommandLineFlagInfoOrDie(const char* name);
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)enum FlagSettingMode {
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // update the flag's value (can call this multiple times).
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SET_FLAGS_VALUE,
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // update the flag's value, but *only if* it has not yet been updated
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // with SET_FLAGS_VALUE, SET_FLAG_IF_DEFAULT, or "FLAGS_xxx = nondef".
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SET_FLAG_IF_DEFAULT,
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // set the flag's default value to this.  If the flag has not yet updated
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // yet (via SET_FLAGS_VALUE, SET_FLAG_IF_DEFAULT, or "FLAGS_xxx = nondef")
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // change the flag's current value to the new default value as well.
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SET_FLAGS_DEFAULT
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Set a particular flag ("command line option").  Returns a string
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// describing the new value that the option has been set to.  The
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// return value API is not well-specified, so basically just depend on
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// it to be empty if the setting failed for some reason -- the name is
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// not a valid flag name, or the value is not a valid value -- and
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// non-empty else.
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// SetCommandLineOption uses set_mode == SET_FLAGS_VALUE (the common case)
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern string SetCommandLineOption(const char* name, const char* value);
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern string SetCommandLineOptionWithMode(const char* name, const char* value,
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                           FlagSettingMode set_mode);
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// --------------------------------------------------------------------
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Saves the states (value, default value, whether the user has set
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the flag, registered validators, etc) of all flags, and restores
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// them when the FlagSaver is destroyed.  This is very useful in
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// tests, say, when you want to let your tests change the flags, but
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// make sure that they get reverted to the original states when your
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// test is complete.
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Example usage:
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   void TestFoo() {
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     FlagSaver s1;
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     FLAG_foo = false;
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     FLAG_bar = "some value";
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     // test happens here.  You can return at any time
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     // without worrying about restoring the FLAG values.
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   }
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Note: This class is marked with ATTRIBUTE_UNUSED because all the
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// work is done in the constructor and destructor, so in the standard
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// usage example above, the compiler would complain that it's an
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// unused variable.
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This class is thread-safe.
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class FlagSaver {
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FlagSaver();
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~FlagSaver();
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class FlagSaverImpl* impl_;   // we use pimpl here to keep API steady
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FlagSaver(const FlagSaver&);  // no copying!
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void operator=(const FlagSaver&);
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef SWIG   // swig seems to have trouble with this for some reason
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ATTRIBUTE_UNUSED
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles);
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)*/
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// --------------------------------------------------------------------
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Some deprecated or hopefully-soon-to-be-deprecated functions.
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This is often used for logging.  TODO(csilvers): figure out a better way
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern string CommandlineFlagsIntoString();
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Usually where this is used, a FlagSaver should be used instead.
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern bool ReadFlagsFromString(const string& flagfilecontents,
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                const char* prog_name,
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                bool errors_are_fatal); // uses SET_FLAGS_VALUE
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// These let you manually implement --flagfile functionality.
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DEPRECATED.
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern bool AppendFlagsIntoFile(const string& filename, const char* prog_name);
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern bool SaveCommandFlags();  // actually defined in google.cc !
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern bool ReadFromFlagsFile(const string& filename, const char* prog_name,
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              bool errors_are_fatal);   // uses SET_FLAGS_VALUE
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// --------------------------------------------------------------------
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Useful routines for initializing flags from the environment.
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// In each case, if 'varname' does not exist in the environment
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// return defval.  If 'varname' does exist but is not valid
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (e.g., not a number for an int32 flag), abort with an error.
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Otherwise, return the value.  NOTE: for booleans, for true use
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 't' or 'T' or 'true' or '1', for false 'f' or 'F' or 'false' or '0'.
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern bool BoolFromEnv(const char *varname, bool defval);
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern int32 Int32FromEnv(const char *varname, int32 defval);
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern int64 Int64FromEnv(const char *varname, int64 defval);
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern uint64 Uint64FromEnv(const char *varname, uint64 defval);
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern double DoubleFromEnv(const char *varname, double defval);
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern const char *StringFromEnv(const char *varname, const char *defval);
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// --------------------------------------------------------------------
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The next two functions parse commandlineflags from main():
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Set the "usage" message for this program.  For example:
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   string usage("This program does nothing.  Sample usage:\n");
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   usage += argv[0] + " <uselessarg1> <uselessarg2>";
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   SetUsageMessage(usage);
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Do not include commandline flags in the usage: we do that for you!
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Thread-hostile; meant to be called before any threads are spawned.
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern void SetUsageMessage(const string& usage);
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Looks for flags in argv and parses them.  Rearranges argv to put
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// flags first, or removes them entirely if remove_flags is true.
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If a flag is defined more than once in the command line or flag
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// file, the last definition is used.
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// See top-of-file for more details on this function.
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef SWIG   // In swig, use ParseCommandLineFlagsScript() instead.
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern uint32 ParseCommandLineFlags(int *argc, char*** argv,
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                    bool remove_flags);
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Calls to ParseCommandLineNonHelpFlags and then to
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// HandleCommandLineHelpFlags can be used instead of a call to
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ParseCommandLineFlags during initialization, in order to allow for
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// changing default values for some FLAGS (via
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// e.g. SetCommandLineOptionWithMode calls) between the time of
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// command line parsing and the time of dumping help information for
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the flags as a result of command line parsing.
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If a flag is defined more than once in the command line or flag
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// file, the last definition is used.
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern uint32 ParseCommandLineNonHelpFlags(int *argc, char*** argv,
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                           bool remove_flags);
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This is actually defined in commandlineflags_reporting.cc.
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This function is misnamed (it also handles --version, etc.), but
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// it's too late to change that now. :-(
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern void HandleCommandLineHelpFlags();   // in commandlineflags_reporting.cc
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Allow command line reparsing.  Disables the error normally
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// generated when an unknown flag is found, since it may be found in a
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// later parse.  Thread-hostile; meant to be called before any threads
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// are spawned.
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern void AllowCommandLineReparsing();
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Reparse the flags that have not yet been recognized.
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Only flags registered since the last parse will be recognized.
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Any flag value must be provided as part of the argument using "=",
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// not as a separate command line argument that follows the flag argument.
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Intended for handling flags from dynamically loaded libraries,
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// since their flags are not registered until they are loaded.
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern uint32 ReparseCommandLineNonHelpFlags();
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// --------------------------------------------------------------------
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Now come the command line flag declaration/definition macros that
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// will actually be used.  They're kind of hairy.  A major reason
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// for this is initialization: we want people to be able to access
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// variables in global constructors and have that not crash, even if
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// their global constructor runs before the global constructor here.
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (Obviously, we can't guarantee the flags will have the correct
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// default value in that case, but at least accessing them is safe.)
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The only way to do that is have flags point to a static buffer.
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// So we make one, using a union to ensure proper alignment, and
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// then use placement-new to actually set up the flag with the
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// correct default value.  In the same vein, we have to worry about
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// flag access in global destructors, so FlagRegisterer has to be
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// careful never to destroy the flag-values it constructs.
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Note that when we define a flag variable FLAGS_<name>, we also
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// preemptively define a junk variable, FLAGS_no<name>.  This is to
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// cause a link-time error if someone tries to define 2 flags with
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// names like "logging" and "nologging".  We do this because a bool
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// flag FLAG can be set from the command line to true with a "-FLAG"
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// argument, and to false with a "-noFLAG" argument, and so this can
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// potentially avert confusion.
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// We also put flags into their own namespace.  It is purposefully
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// named in an opaque way that people should have trouble typing
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// directly.  The idea is that DEFINE puts the flag in the weird
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// namespace, and DECLARE imports the flag from there into the current
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// namespace.  The net result is to force people to use DECLARE to get
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// access to a flag, rather than saying "extern bool FLAGS_whatever;"
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// or some such instead.  We want this so we can put extra
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// functionality (like sanity-checking) in DECLARE if we want, and
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// make sure it is picked up everywhere.
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// We also put the type of the variable in the namespace, so that
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// people can't DECLARE_int32 something that they DEFINE_bool'd
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// elsewhere.
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class FlagRegisterer {
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FlagRegisterer(const char* name, const char* type,
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 const char* help, const char* filename,
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 void* current_storage, void* defvalue_storage);
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef SWIG  // In swig, ignore the main flag declarations
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If STRIP_FLAG_HELP is defined and is non-zero, we remove the help
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// message from the binary file. This is useful for security reasons
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// when shipping a binary outside of Google (if the user cannot see
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the usage message by executing the program, they shouldn't be able
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// to see it by running "strings binary_file").
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern const char kStrippedFlagHelp[];
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if STRIP_FLAG_HELP > 0
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Need this construct to avoid the 'defined but not used' warning.
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define MAYBE_STRIPPED_HELP(txt) (false ? (txt) : kStrippedFlagHelp)
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define MAYBE_STRIPPED_HELP(txt) txt
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Each command-line flag has two variables associated with it: one
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// with the current value, and one with the default value.  However,
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// we have a third variable, which is where value is assigned; it's a
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// constant.  This guarantees that FLAG_##value is initialized at
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static initialization time (e.g. before program-start) rather than
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// than global construction time (which is after program-start but
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// before main), at least when 'value' is a compile-time constant.  We
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// use a small trick for the "default value" variable, and call it
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// FLAGS_no<name>.  This serves the second purpose of assuring a
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// compile error if someone tries to define a flag named no<name>
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// which is illegal (--foo and --nofoo both affect the "foo" flag).
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DEFINE_VARIABLE(type, shorttype, name, value, help) \
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  namespace fL##shorttype {                                     \
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    static const type FLAGS_nono##name = value;                 \
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    type FLAGS_##name = FLAGS_nono##name;                       \
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    type FLAGS_no##name = FLAGS_nono##name;                     \
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    static FlagRegisterer o_##name(                             \
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      #name, #type, MAYBE_STRIPPED_HELP(help), __FILE__,        \
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      &FLAGS_##name, &FLAGS_no##name);                          \
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }                                                             \
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  using fL##shorttype::FLAGS_##name
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DECLARE_VARIABLE(type, shorttype, name) \
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  namespace fL##shorttype {                     \
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    extern type FLAGS_##name;                   \
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }                                             \
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  using fL##shorttype::FLAGS_##name
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// For boolean flags, we want to do the extra check that the passed-in
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// value is actually a bool, and not a string or something that can be
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// coerced to a bool.  These declarations (no definition needed!) will
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// help us do that, and never evaluate from, which is important.
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// We'll use 'sizeof(IsBool(val))' to distinguish.
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace fLB {
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template<typename From> double IsBoolFlag(const From& from);
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool IsBoolFlag(bool from);
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern bool FlagsTypeWarn(const char *name);
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DECLARE_bool(name)          DECLARE_VARIABLE(bool,B, name)
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// We have extra code here to make sure 'val' is actually a boolean.
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DEFINE_bool(name,val,txt)   namespace fLB { \
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                      const bool FLAGS_nonono##name = \
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        (sizeof(::fLB::IsBoolFlag(val)) \
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        == sizeof(double)) \
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        ? FlagsTypeWarn(#name) : true; \
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                    } \
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                    DEFINE_VARIABLE(bool,B, name, val, txt)
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DECLARE_int32(name)         DECLARE_VARIABLE(int32,I, name)
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DEFINE_int32(name,val,txt)  DEFINE_VARIABLE(int32,I, name, val, txt)
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DECLARE_int64(name)         DECLARE_VARIABLE(int64,I64, name)
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DEFINE_int64(name,val,txt)  DEFINE_VARIABLE(int64,I64, name, val, txt)
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DECLARE_uint64(name)        DECLARE_VARIABLE(uint64,U64, name)
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DEFINE_uint64(name,val,txt) DEFINE_VARIABLE(uint64,U64, name, val, txt)
4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DECLARE_double(name)        DECLARE_VARIABLE(double,D, name)
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DEFINE_double(name,val,txt) DEFINE_VARIABLE(double,D, name, val, txt)
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Strings are trickier, because they're not a POD, so we can't
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// construct them at static-initialization time (instead they get
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// constructed at global-constructor time, which is much later).  To
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// try to avoid crashes in that case, we use a char buffer to store
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the string, which we can static-initialize, and then placement-new
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// into it later.  It's not perfect, but the best we can do.
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DECLARE_string(name)  namespace fLS { extern string& FLAGS_##name; } \
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              using fLS::FLAGS_##name
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// We need to define a var named FLAGS_no##name so people don't define
4275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// --string and --nostring.  And we need a temporary place to put val
4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// so we don't have to evaluate it twice.  Two great needs that go
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// great together!
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DEFINE_string(name, val, txt)                                     \
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  namespace fLS {                                                         \
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    static union { void* align; char s[sizeof(string)]; } s_##name[2];    \
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const string* const FLAGS_no##name = new (s_##name[0].s) string(val); \
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    static FlagRegisterer o_##name(                                       \
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      #name, "string", MAYBE_STRIPPED_HELP(txt), __FILE__,                \
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      s_##name[0].s, new (s_##name[1].s) string(*FLAGS_no##name));        \
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    string& FLAGS_##name = *(reinterpret_cast<string*>(s_##name[0].s));   \
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }                                                                       \
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  using fLS::FLAGS_##name
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // SWIG
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // BASE_COMMANDLINEFLAGS_H_
444