10a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Copyright (c) 2006, Google Inc. 20a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// All rights reserved. 30a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// 40a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Redistribution and use in source and binary forms, with or without 50a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// modification, are permitted provided that the following conditions are 60a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// met: 70a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// 80a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// * Redistributions of source code must retain the above copyright 90a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// notice, this list of conditions and the following disclaimer. 100a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// * Redistributions in binary form must reproduce the above 110a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// copyright notice, this list of conditions and the following disclaimer 120a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// in the documentation and/or other materials provided with the 130a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// distribution. 140a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// * Neither the name of Google Inc. nor the names of its 150a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// contributors may be used to endorse or promote products derived from 160a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// this software without specific prior written permission. 170a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// 180a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 190a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 200a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 210a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 220a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 230a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 240a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 250a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 260a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 270a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 280a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 290a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 300a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// --- 310a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Author: Ray Sidney 320a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Revamped and reorganized by Craig Silverstein 330a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// 340a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// This is the file that should be included by any file which declares 350a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// or defines a command line flag or wants to parse command line flags 360a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// or print a program usage message (which will include information about 370a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// flags). Executive summary, in the form of an example foo.cc file: 380a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// 390a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// #include "foo.h" // foo.h has a line "DECLARE_int32(start);" 400a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// 410a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// DEFINE_int32(end, 1000, "The last record to read"); 420a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// DECLARE_bool(verbose); // some other file has a DEFINE_bool(verbose, ...) 430a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// 440a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// void MyFunc() { 450a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// if (FLAGS_verbose) printf("Records %d-%d\n", FLAGS_start, FLAGS_end); 460a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// } 470a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// 480a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Then, at the command-line: 490a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// ./foo --noverbose --start=5 --end=100 500a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// 510a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// For more details, see 520a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// doc/gflags.html 530a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// 540a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// --- A note about thread-safety: 550a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// 560a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// We describe many functions in this routine as being thread-hostile, 570a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// thread-compatible, or thread-safe. Here are the meanings we use: 580a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// 590a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// thread-safe: it is safe for multiple threads to call this routine 600a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// (or, when referring to a class, methods of this class) 610a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// concurrently. 620a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// thread-hostile: it is not safe for multiple threads to call this 630a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// routine (or methods of this class) concurrently. In gflags, 640a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// most thread-hostile routines are intended to be called early in, 650a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// or even before, main() -- that is, before threads are spawned. 660a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// thread-compatible: it is safe for multiple threads to read from 670a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// this variable (when applied to variables), or to call const 680a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// methods of this class (when applied to classes), as long as no 690a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// other thread is writing to the variable or calling non-const 700a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// methods of this class. 710a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 720a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#ifndef GOOGLE_GFLAGS_H_ 730a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#define GOOGLE_GFLAGS_H_ 740a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 750a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#include <string> 760a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#include <vector> 770a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 780a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// We care a lot about number of bits things take up. Unfortunately, 790a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// systems define their bit-specific ints in a lot of different ways. 800a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// We use our own way, and have a typedef to get there. 810a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Note: these commands below may look like "#if 1" or "#if 0", but 820a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// that's because they were constructed that way at ./configure time. 830a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Look at gflags.h.in to see how they're calculated (based on your config). 840a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#if 0 850a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#include <stdint.h> // the normal place uint16_t is defined 860a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#endif 870a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#if 1 880a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#include <sys/types.h> // the normal place u_int16_t is defined 890a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#endif 900a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#if 0 910a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#include <inttypes.h> // a third place for uint16_t or u_int16_t 920a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#endif 930a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 940a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Annoying stuff for windows -- makes sure clients can import these functions 950a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#ifndef GFLAGS_DLL_DECL 960a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath# ifdef _WIN32 970a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath# define GFLAGS_DLL_DECL __declspec(dllimport) 980a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath# else 990a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath# define GFLAGS_DLL_DECL 1000a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath# endif 1010a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#endif 1020a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 1030a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathnamespace google { 1040a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 1050a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#if 0 // the C99 format 1060a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathtypedef int32_t int32; 1070a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathtypedef uint32_t uint32; 1080a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathtypedef int64_t int64; 1090a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathtypedef uint64_t uint64; 1100a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#elif 0 // the BSD format 1110a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathtypedef int32_t int32; 1120a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathtypedef u_int32_t uint32; 1130a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathtypedef int64_t int64; 1140a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathtypedef u_int64_t uint64; 1150a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#elif 1 // the windows (vc7) format 1160a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathtypedef __int32 int32; 1170a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathtypedef unsigned __int32 uint32; 1180a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathtypedef __int64 int64; 1190a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathtypedef unsigned __int64 uint64; 1200a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#else 1210a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#error Do not know how to define a 32-bit integer quantity on your system 1220a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#endif 1230a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 1240a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// -------------------------------------------------------------------- 1250a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// To actually define a flag in a file, use DEFINE_bool, 1260a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// DEFINE_string, etc. at the bottom of this file. You may also find 1270a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// it useful to register a validator with the flag. This ensures that 1280a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// when the flag is parsed from the commandline, or is later set via 1290a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// SetCommandLineOption, we call the validation function. 1300a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// 1310a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// The validation function should return true if the flag value is valid, and 1320a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// false otherwise. If the function returns false for the new setting of the 1330a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// flag, the flag will retain its current value. If it returns false for the 1340a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// default value, InitGoogle will die. 1350a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// 1360a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// This function is safe to call at global construct time (as in the 1370a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// example below). 1380a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// 1390a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Example use: 1400a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// static bool ValidatePort(const char* flagname, int32 value) { 1410a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// if (value > 0 && value < 32768) // value is ok 1420a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// return true; 1430a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// printf("Invalid value for --%s: %d\n", flagname, (int)value); 1440a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// return false; 1450a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// } 1460a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// DEFINE_int32(port, 0, "What port to listen on"); 1470a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// static bool dummy = RegisterFlagValidator(&FLAGS_port, &ValidatePort); 1480a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 1490a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Returns true if successfully registered, false if not (because the 1500a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// first argument doesn't point to a command-line flag, or because a 1510a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// validator is already registered for this flag). 1520a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan KamathGFLAGS_DLL_DECL bool RegisterFlagValidator(const bool* flag, 1530a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath bool (*validate_fn)(const char*, bool)); 1540a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan KamathGFLAGS_DLL_DECL bool RegisterFlagValidator(const int32* flag, 1550a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath bool (*validate_fn)(const char*, int32)); 1560a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan KamathGFLAGS_DLL_DECL bool RegisterFlagValidator(const int64* flag, 1570a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath bool (*validate_fn)(const char*, int64)); 1580a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan KamathGFLAGS_DLL_DECL bool RegisterFlagValidator(const uint64* flag, 1590a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath bool (*validate_fn)(const char*, uint64)); 1600a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan KamathGFLAGS_DLL_DECL bool RegisterFlagValidator(const double* flag, 1610a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath bool (*validate_fn)(const char*, double)); 1620a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan KamathGFLAGS_DLL_DECL bool RegisterFlagValidator(const std::string* flag, 1630a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath bool (*validate_fn)(const char*, const std::string&)); 1640a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 1650a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 1660a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// -------------------------------------------------------------------- 1670a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// These methods are the best way to get access to info about the 1680a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// list of commandline flags. Note that these routines are pretty slow. 1690a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// GetAllFlags: mostly-complete info about the list, sorted by file. 1700a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// ShowUsageWithFlags: pretty-prints the list to stdout (what --help does) 1710a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// ShowUsageWithFlagsRestrict: limit to filenames with restrict as a substr 1720a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// 1730a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// In addition to accessing flags, you can also access argv[0] (the program 1740a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// name) and argv (the entire commandline), which we sock away a copy of. 1750a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// These variables are static, so you should only set them once. 1760a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 1770a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathstruct GFLAGS_DLL_DECL CommandLineFlagInfo { 1780a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath std::string name; // the name of the flag 1790a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath std::string type; // the type of the flag: int32, etc 1800a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath std::string description; // the "help text" associated with the flag 1810a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath std::string current_value; // the current value, as a string 1820a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath std::string default_value; // the default value, as a string 1830a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath std::string filename; // 'cleaned' version of filename holding the flag 1840a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath bool has_validator_fn; // true if RegisterFlagValidator called on flag 1850a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath bool is_default; // true if the flag has default value 1860a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath}; 1870a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 1880a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Using this inside of a validator is a recipe for a deadlock. 1890a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// TODO(wojtekm) Fix locking when validators are running, to make it safe to 1900a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// call validators during ParseAllFlags. 1910a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Also make sure then to uncomment the corresponding unit test in 1920a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// commandlineflags_unittest.sh 1930a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL void GetAllFlags(std::vector<CommandLineFlagInfo>* OUTPUT); 1940a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// These two are actually defined in commandlineflags_reporting.cc. 1950a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL void ShowUsageWithFlags(const char *argv0); // what --help does 1960a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL void ShowUsageWithFlagsRestrict(const char *argv0, const char *restrict); 1970a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 1980a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Create a descriptive string for a flag. 1990a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Goes to some trouble to make pretty line breaks. 2000a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL std::string DescribeOneFlag(const CommandLineFlagInfo& flag); 2010a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 2020a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Thread-hostile; meant to be called before any threads are spawned. 2030a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL void SetArgv(int argc, const char** argv); 2040a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// The following functions are thread-safe as long as SetArgv() is 2050a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// only called before any threads start. 2060a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL const std::vector<std::string>& GetArgvs(); // all of argv as a vector 2070a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL const char* GetArgv(); // all of argv as a string 2080a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL const char* GetArgv0(); // only argv0 2090a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL uint32 GetArgvSum(); // simple checksum of argv 2100a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL const char* ProgramInvocationName(); // argv0, or "UNKNOWN" if not set 2110a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL const char* ProgramInvocationShortName(); // basename(argv0) 2120a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// ProgramUsage() is thread-safe as long as SetUsageMessage() is only 2130a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// called before any threads start. 2140a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL const char* ProgramUsage(); // string set by SetUsageMessage() 2150a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 2160a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 2170a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// -------------------------------------------------------------------- 2180a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Normally you access commandline flags by just saying "if (FLAGS_foo)" 2190a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// or whatever, and set them by calling "FLAGS_foo = bar" (or, more 2200a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// commonly, via the DEFINE_foo macro). But if you need a bit more 2210a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// control, we have programmatic ways to get/set the flags as well. 2220a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// These programmatic ways to access flags are thread-safe, but direct 2230a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// access is only thread-compatible. 2240a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 2250a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Return true iff the flagname was found. 2260a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// OUTPUT is set to the flag's value, or unchanged if we return false. 2270a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL bool GetCommandLineOption(const char* name, std::string* OUTPUT); 2280a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 2290a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Return true iff the flagname was found. OUTPUT is set to the flag's 2300a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// CommandLineFlagInfo or unchanged if we return false. 2310a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL bool GetCommandLineFlagInfo(const char* name, 2320a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath CommandLineFlagInfo* OUTPUT); 2330a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 2340a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Return the CommandLineFlagInfo of the flagname. exit() if name not found. 2350a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Example usage, to check if a flag's value is currently the default value: 2360a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// if (GetCommandLineFlagInfoOrDie("foo").is_default) ... 2370a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL CommandLineFlagInfo GetCommandLineFlagInfoOrDie(const char* name); 2380a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 2390a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathenum GFLAGS_DLL_DECL FlagSettingMode { 2400a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath // update the flag's value (can call this multiple times). 2410a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath SET_FLAGS_VALUE, 2420a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath // update the flag's value, but *only if* it has not yet been updated 2430a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath // with SET_FLAGS_VALUE, SET_FLAG_IF_DEFAULT, or "FLAGS_xxx = nondef". 2440a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath SET_FLAG_IF_DEFAULT, 2450a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath // set the flag's default value to this. If the flag has not yet updated 2460a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath // yet (via SET_FLAGS_VALUE, SET_FLAG_IF_DEFAULT, or "FLAGS_xxx = nondef") 2470a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath // change the flag's current value to the new default value as well. 2480a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath SET_FLAGS_DEFAULT 2490a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath}; 2500a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 2510a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Set a particular flag ("command line option"). Returns a string 2520a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// describing the new value that the option has been set to. The 2530a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// return value API is not well-specified, so basically just depend on 2540a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// it to be empty if the setting failed for some reason -- the name is 2550a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// not a valid flag name, or the value is not a valid value -- and 2560a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// non-empty else. 2570a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 2580a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// SetCommandLineOption uses set_mode == SET_FLAGS_VALUE (the common case) 2590a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL std::string SetCommandLineOption(const char* name, const char* value); 2600a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL std::string SetCommandLineOptionWithMode(const char* name, const char* value, 2610a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath FlagSettingMode set_mode); 2620a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 2630a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 2640a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// -------------------------------------------------------------------- 2650a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Saves the states (value, default value, whether the user has set 2660a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// the flag, registered validators, etc) of all flags, and restores 2670a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// them when the FlagSaver is destroyed. This is very useful in 2680a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// tests, say, when you want to let your tests change the flags, but 2690a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// make sure that they get reverted to the original states when your 2700a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// test is complete. 2710a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// 2720a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Example usage: 2730a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// void TestFoo() { 2740a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// FlagSaver s1; 2750a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// FLAG_foo = false; 2760a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// FLAG_bar = "some value"; 2770a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// 2780a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// // test happens here. You can return at any time 2790a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// // without worrying about restoring the FLAG values. 2800a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// } 2810a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// 2820a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Note: This class is marked with __attribute__((unused)) because all the 2830a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// work is done in the constructor and destructor, so in the standard 2840a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// usage example above, the compiler would complain that it's an 2850a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// unused variable. 2860a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// 2870a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// This class is thread-safe. 2880a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 2890a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathclass GFLAGS_DLL_DECL FlagSaver { 2900a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath public: 2910a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath FlagSaver(); 2920a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath ~FlagSaver(); 2930a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 2940a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath private: 2950a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath class FlagSaverImpl* impl_; // we use pimpl here to keep API steady 2960a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 2970a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath FlagSaver(const FlagSaver&); // no copying! 2980a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath void operator=(const FlagSaver&); 2990a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath} ; 3000a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 3010a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// -------------------------------------------------------------------- 3020a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Some deprecated or hopefully-soon-to-be-deprecated functions. 3030a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 3040a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// This is often used for logging. TODO(csilvers): figure out a better way 3050a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL std::string CommandlineFlagsIntoString(); 3060a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Usually where this is used, a FlagSaver should be used instead. 3070a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL bool ReadFlagsFromString(const std::string& flagfilecontents, 3080a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath const char* prog_name, 3090a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath bool errors_are_fatal); // uses SET_FLAGS_VALUE 3100a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 3110a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// These let you manually implement --flagfile functionality. 3120a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// DEPRECATED. 3130a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL bool AppendFlagsIntoFile(const std::string& filename, const char* prog_name); 3140a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL bool SaveCommandFlags(); // actually defined in google.cc ! 3150a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL bool ReadFromFlagsFile(const std::string& filename, const char* prog_name, 3160a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath bool errors_are_fatal); // uses SET_FLAGS_VALUE 3170a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 3180a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 3190a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// -------------------------------------------------------------------- 3200a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Useful routines for initializing flags from the environment. 3210a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// In each case, if 'varname' does not exist in the environment 3220a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// return defval. If 'varname' does exist but is not valid 3230a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// (e.g., not a number for an int32 flag), abort with an error. 3240a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Otherwise, return the value. NOTE: for booleans, for true use 3250a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// 't' or 'T' or 'true' or '1', for false 'f' or 'F' or 'false' or '0'. 3260a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 3270a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL bool BoolFromEnv(const char *varname, bool defval); 3280a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL int32 Int32FromEnv(const char *varname, int32 defval); 3290a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL int64 Int64FromEnv(const char *varname, int64 defval); 3300a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL uint64 Uint64FromEnv(const char *varname, uint64 defval); 3310a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL double DoubleFromEnv(const char *varname, double defval); 3320a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL const char *StringFromEnv(const char *varname, const char *defval); 3330a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 3340a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 3350a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// -------------------------------------------------------------------- 3360a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// The next two functions parse commandlineflags from main(): 3370a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 3380a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Set the "usage" message for this program. For example: 3390a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// string usage("This program does nothing. Sample usage:\n"); 3400a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// usage += argv[0] + " <uselessarg1> <uselessarg2>"; 3410a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// SetUsageMessage(usage); 3420a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Do not include commandline flags in the usage: we do that for you! 3430a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Thread-hostile; meant to be called before any threads are spawned. 3440a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL void SetUsageMessage(const std::string& usage); 3450a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 3460a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Looks for flags in argv and parses them. Rearranges argv to put 3470a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// flags first, or removes them entirely if remove_flags is true. 3480a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// If a flag is defined more than once in the command line or flag 3490a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// file, the last definition is used. 3500a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// See top-of-file for more details on this function. 3510a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#ifndef SWIG // In swig, use ParseCommandLineFlagsScript() instead. 3520a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL uint32 ParseCommandLineFlags(int *argc, char*** argv, 3530a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath bool remove_flags); 3540a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#endif 3550a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 3560a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 3570a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Calls to ParseCommandLineNonHelpFlags and then to 3580a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// HandleCommandLineHelpFlags can be used instead of a call to 3590a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// ParseCommandLineFlags during initialization, in order to allow for 3600a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// changing default values for some FLAGS (via 3610a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// e.g. SetCommandLineOptionWithMode calls) between the time of 3620a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// command line parsing and the time of dumping help information for 3630a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// the flags as a result of command line parsing. 3640a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// If a flag is defined more than once in the command line or flag 3650a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// file, the last definition is used. 3660a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL uint32 ParseCommandLineNonHelpFlags(int *argc, char*** argv, 3670a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath bool remove_flags); 3680a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// This is actually defined in commandlineflags_reporting.cc. 3690a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// This function is misnamed (it also handles --version, etc.), but 3700a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// it's too late to change that now. :-( 3710a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL void HandleCommandLineHelpFlags(); // in commandlineflags_reporting.cc 3720a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 3730a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Allow command line reparsing. Disables the error normally 3740a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// generated when an unknown flag is found, since it may be found in a 3750a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// later parse. Thread-hostile; meant to be called before any threads 3760a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// are spawned. 3770a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL void AllowCommandLineReparsing(); 3780a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 3790a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Reparse the flags that have not yet been recognized. 3800a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Only flags registered since the last parse will be recognized. 3810a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Any flag value must be provided as part of the argument using "=", 3820a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// not as a separate command line argument that follows the flag argument. 3830a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Intended for handling flags from dynamically loaded libraries, 3840a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// since their flags are not registered until they are loaded. 3850a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern GFLAGS_DLL_DECL uint32 ReparseCommandLineNonHelpFlags(); 3860a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 3870a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 3880a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// -------------------------------------------------------------------- 3890a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Now come the command line flag declaration/definition macros that 3900a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// will actually be used. They're kind of hairy. A major reason 3910a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// for this is initialization: we want people to be able to access 3920a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// variables in global constructors and have that not crash, even if 3930a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// their global constructor runs before the global constructor here. 3940a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// (Obviously, we can't guarantee the flags will have the correct 3950a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// default value in that case, but at least accessing them is safe.) 3960a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// The only way to do that is have flags point to a static buffer. 3970a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// So we make one, using a union to ensure proper alignment, and 3980a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// then use placement-new to actually set up the flag with the 3990a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// correct default value. In the same vein, we have to worry about 4000a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// flag access in global destructors, so FlagRegisterer has to be 4010a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// careful never to destroy the flag-values it constructs. 4020a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// 4030a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Note that when we define a flag variable FLAGS_<name>, we also 4040a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// preemptively define a junk variable, FLAGS_no<name>. This is to 4050a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// cause a link-time error if someone tries to define 2 flags with 4060a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// names like "logging" and "nologging". We do this because a bool 4070a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// flag FLAG can be set from the command line to true with a "-FLAG" 4080a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// argument, and to false with a "-noFLAG" argument, and so this can 4090a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// potentially avert confusion. 4100a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// 4110a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// We also put flags into their own namespace. It is purposefully 4120a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// named in an opaque way that people should have trouble typing 4130a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// directly. The idea is that DEFINE puts the flag in the weird 4140a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// namespace, and DECLARE imports the flag from there into the current 4150a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// namespace. The net result is to force people to use DECLARE to get 4160a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// access to a flag, rather than saying "extern bool FLAGS_whatever;" 4170a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// or some such instead. We want this so we can put extra 4180a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// functionality (like sanity-checking) in DECLARE if we want, and 4190a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// make sure it is picked up everywhere. 4200a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// 4210a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// We also put the type of the variable in the namespace, so that 4220a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// people can't DECLARE_int32 something that they DEFINE_bool'd 4230a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// elsewhere. 4240a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 4250a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathclass GFLAGS_DLL_DECL FlagRegisterer { 4260a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath public: 4270a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath FlagRegisterer(const char* name, const char* type, 4280a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath const char* help, const char* filename, 4290a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath void* current_storage, void* defvalue_storage); 4300a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath}; 4310a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 4320a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern bool FlagsTypeWarn(const char *name); 4330a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 4340a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// If your application #defines STRIP_FLAG_HELP to a non-zero value 4350a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// before #including this file, we remove the help message from the 4360a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// binary file. This can reduce the size of the resulting binary 4370a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// somewhat, and may also be useful for security reasons. 4380a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 4390a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern const char kStrippedFlagHelp[]; 4400a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 4410a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath} 4420a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 4430a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#ifndef SWIG // In swig, ignore the main flag declarations 4440a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 4450a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#if defined(STRIP_FLAG_HELP) && STRIP_FLAG_HELP > 0 4460a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Need this construct to avoid the 'defined but not used' warning. 4470a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#define MAYBE_STRIPPED_HELP(txt) (false ? (txt) : kStrippedFlagHelp) 4480a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#else 4490a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#define MAYBE_STRIPPED_HELP(txt) txt 4500a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#endif 4510a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 4520a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Each command-line flag has two variables associated with it: one 4530a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// with the current value, and one with the default value. However, 4540a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// we have a third variable, which is where value is assigned; it's a 4550a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// constant. This guarantees that FLAG_##value is initialized at 4560a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// static initialization time (e.g. before program-start) rather than 4570a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// than global construction time (which is after program-start but 4580a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// before main), at least when 'value' is a compile-time constant. We 4590a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// use a small trick for the "default value" variable, and call it 4600a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// FLAGS_no<name>. This serves the second purpose of assuring a 4610a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// compile error if someone tries to define a flag named no<name> 4620a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// which is illegal (--foo and --nofoo both affect the "foo" flag). 4630a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#define DEFINE_VARIABLE(type, shorttype, name, value, help) \ 4640a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath namespace fL##shorttype { \ 4650a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath static const type FLAGS_nono##name = value; \ 4660a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath /* We always want to export defined variables, dll or no */ \ 4670a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath __declspec(dllexport) type FLAGS_##name = FLAGS_nono##name; \ 4680a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath type FLAGS_no##name = FLAGS_nono##name; \ 4690a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath static ::google::FlagRegisterer o_##name( \ 4700a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath #name, #type, MAYBE_STRIPPED_HELP(help), __FILE__, \ 4710a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath &FLAGS_##name, &FLAGS_no##name); \ 4720a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath } \ 4730a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath using fL##shorttype::FLAGS_##name 4740a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 4750a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#define DECLARE_VARIABLE(type, shorttype, name) \ 4760a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath namespace fL##shorttype { \ 4770a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath /* We always want to import declared variables, dll or no */ \ 4780a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath extern __declspec(dllimport) type FLAGS_##name; \ 4790a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath } \ 4800a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath using fL##shorttype::FLAGS_##name 4810a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 4820a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// For DEFINE_bool, we want to do the extra check that the passed-in 4830a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// value is actually a bool, and not a string or something that can be 4840a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// coerced to a bool. These declarations (no definition needed!) will 4850a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// help us do that, and never evaluate From, which is important. 4860a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// We'll use 'sizeof(IsBool(val))' to distinguish. This code requires 4870a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// that the compiler have different sizes for bool & double. Since 4880a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// this is not guaranteed by the standard, we check it with a 4890a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// compile-time assert (msg[-1] will give a compile-time error). 4900a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathnamespace fLB { 4910a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathstruct CompileAssert {}; 4920a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathtypedef CompileAssert expected_sizeof_double_neq_sizeof_bool[ 4930a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath (sizeof(double) != sizeof(bool)) ? 1 : -1]; 4940a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathtemplate<typename From> GFLAGS_DLL_DECL double IsBoolFlag(const From& from); 4950a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan KamathGFLAGS_DLL_DECL bool IsBoolFlag(bool from); 4960a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath} // namespace fLB 4970a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 4980a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#define DECLARE_bool(name) DECLARE_VARIABLE(bool, B, name) 4990a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#define DEFINE_bool(name, val, txt) \ 5000a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath namespace fLB { \ 5010a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath typedef CompileAssert FLAG_##name##_value_is_not_a_bool[ \ 5020a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath (sizeof(::fLB::IsBoolFlag(val)) != sizeof(double)) ? 1 : -1]; \ 5030a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath } \ 5040a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath DEFINE_VARIABLE(bool, B, name, val, txt) 5050a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 5060a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#define DECLARE_int32(name) DECLARE_VARIABLE(::google::int32, I, name) 5070a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#define DEFINE_int32(name,val,txt) DEFINE_VARIABLE(::google::int32, I, name, val, txt) 5080a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 5090a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#define DECLARE_int64(name) DECLARE_VARIABLE(::google::int64, I64, name) 5100a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#define DEFINE_int64(name,val,txt) DEFINE_VARIABLE(::google::int64, I64, name, val, txt) 5110a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 5120a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#define DECLARE_uint64(name) DECLARE_VARIABLE(::google::uint64, U64, name) 5130a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#define DEFINE_uint64(name,val,txt) DEFINE_VARIABLE(::google::uint64, U64, name, val, txt) 5140a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 5150a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#define DECLARE_double(name) DECLARE_VARIABLE(double, D, name) 5160a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#define DEFINE_double(name, val, txt) DEFINE_VARIABLE(double, D, name, val, txt) 5170a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 5180a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Strings are trickier, because they're not a POD, so we can't 5190a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// construct them at static-initialization time (instead they get 5200a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// constructed at global-constructor time, which is much later). To 5210a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// try to avoid crashes in that case, we use a char buffer to store 5220a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// the string, which we can static-initialize, and then placement-new 5230a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// into it later. It's not perfect, but the best we can do. 5240a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#define DECLARE_string(name) namespace fLS { extern __declspec(dllimport) std::string& FLAGS_##name; } \ 5250a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath using fLS::FLAGS_##name 5260a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 5270a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// We need to define a var named FLAGS_no##name so people don't define 5280a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// --string and --nostring. And we need a temporary place to put val 5290a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// so we don't have to evaluate it twice. Two great needs that go 5300a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// great together! 5310a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// The weird 'using' + 'extern' inside the fLS namespace is to work around 5320a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// an unknown compiler bug/issue with the gcc 4.2.1 on SUSE 10. See 5330a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// http://code.google.com/p/google-gflags/issues/detail?id=20 5340a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#define DEFINE_string(name, val, txt) \ 5350a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath namespace fLS { \ 5360a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath static union { void* align; char s[sizeof(std::string)]; } s_##name[2]; \ 5370a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath const std::string* const FLAGS_no##name = new (s_##name[0].s) std::string(val); \ 5380a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath static ::google::FlagRegisterer o_##name( \ 5390a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath #name, "string", MAYBE_STRIPPED_HELP(txt), __FILE__, \ 5400a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath s_##name[0].s, new (s_##name[1].s) std::string(*FLAGS_no##name)); \ 5410a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath extern __declspec(dllexport) std::string& FLAGS_##name; \ 5420a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath using fLS::FLAGS_##name; \ 5430a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath std::string& FLAGS_##name = *(reinterpret_cast<std::string*>(s_##name[0].s)); \ 5440a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath } \ 5450a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath using fLS::FLAGS_##name 5460a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 5470a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#endif // SWIG 5480a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath 5490a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#endif // GOOGLE_GFLAGS_H_ 550