~alinuxninja/nginx-edge/trunk

« back to all changes in this revision

Viewing changes to debian/modules/ngx_pagespeed/psol/include/third_party/gflags/src/windows/gflags/gflags.h

  • Committer: Vivian
  • Date: 2015-12-04 18:20:11 UTC
  • Revision ID: git-v1:a36f2bc32e884f7473b3a47040e5411306144d7d
* Do not extract psol.tar.gz

Show diffs side-by-side

added added

removed removed

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