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