1// Copyright (c) 2005, 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// This file is a compatibility layer that defines Google's version of 32// command line flags that are used for configuration. 33// 34// We put flags into their own namespace. It is purposefully 35// named in an opaque way that people should have trouble typing 36// directly. The idea is that DEFINE puts the flag in the weird 37// namespace, and DECLARE imports the flag from there into the 38// current namespace. The net result is to force people to use 39// DECLARE to get access to a flag, rather than saying 40// extern bool FLAGS_logtostderr; 41// or some such instead. We want this so we can put extra 42// functionality (like sanity-checking) in DECLARE if we want, 43// and make sure it is picked up everywhere. 44// 45// We also put the type of the variable in the namespace, so that 46// people can't DECLARE_int32 something that they DEFINE_bool'd 47// elsewhere. 48#ifndef BASE_COMMANDLINEFLAGS_H_ 49#define BASE_COMMANDLINEFLAGS_H_ 50 51#include <config.h> 52#include <string> 53#include <string.h> // for memchr 54#include <stdlib.h> // for getenv 55#include "base/basictypes.h" 56 57#define DECLARE_VARIABLE(type, name) \ 58 namespace FLAG__namespace_do_not_use_directly_use_DECLARE_##type##_instead { \ 59 extern PERFTOOLS_DLL_DECL type FLAGS_##name; \ 60 } \ 61 using FLAG__namespace_do_not_use_directly_use_DECLARE_##type##_instead::FLAGS_##name 62 63#define DEFINE_VARIABLE(type, name, value, meaning) \ 64 namespace FLAG__namespace_do_not_use_directly_use_DECLARE_##type##_instead { \ 65 PERFTOOLS_DLL_DECL type FLAGS_##name(value); \ 66 char FLAGS_no##name; \ 67 } \ 68 using FLAG__namespace_do_not_use_directly_use_DECLARE_##type##_instead::FLAGS_##name 69 70// bool specialization 71#define DECLARE_bool(name) \ 72 DECLARE_VARIABLE(bool, name) 73#define DEFINE_bool(name, value, meaning) \ 74 DEFINE_VARIABLE(bool, name, value, meaning) 75 76// int32 specialization 77#define DECLARE_int32(name) \ 78 DECLARE_VARIABLE(int32, name) 79#define DEFINE_int32(name, value, meaning) \ 80 DEFINE_VARIABLE(int32, name, value, meaning) 81 82// int64 specialization 83#define DECLARE_int64(name) \ 84 DECLARE_VARIABLE(int64, name) 85#define DEFINE_int64(name, value, meaning) \ 86 DEFINE_VARIABLE(int64, name, value, meaning) 87 88#define DECLARE_uint64(name) \ 89 DECLARE_VARIABLE(uint64, name) 90#define DEFINE_uint64(name, value, meaning) \ 91 DEFINE_VARIABLE(uint64, name, value, meaning) 92 93// double specialization 94#define DECLARE_double(name) \ 95 DECLARE_VARIABLE(double, name) 96#define DEFINE_double(name, value, meaning) \ 97 DEFINE_VARIABLE(double, name, value, meaning) 98 99// Special case for string, because we have to specify the namespace 100// std::string, which doesn't play nicely with our FLAG__namespace hackery. 101#define DECLARE_string(name) \ 102 namespace FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead { \ 103 extern std::string FLAGS_##name; \ 104 } \ 105 using FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead::FLAGS_##name 106#define DEFINE_string(name, value, meaning) \ 107 namespace FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead { \ 108 std::string FLAGS_##name(value); \ 109 char FLAGS_no##name; \ 110 } \ 111 using FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead::FLAGS_##name 112 113// These macros (could be functions, but I don't want to bother with a .cc 114// file), make it easier to initialize flags from the environment. 115 116#define EnvToString(envname, dflt) \ 117 (!getenv(envname) ? (dflt) : getenv(envname)) 118 119#define EnvToBool(envname, dflt) \ 120 (!getenv(envname) ? (dflt) : memchr("tTyY1\0", getenv(envname)[0], 6) != NULL) 121 122#define EnvToInt(envname, dflt) \ 123 (!getenv(envname) ? (dflt) : strtol(getenv(envname), NULL, 10)) 124 125#define EnvToInt64(envname, dflt) \ 126 (!getenv(envname) ? (dflt) : strtoll(getenv(envname), NULL, 10)) 127 128#define EnvToDouble(envname, dflt) \ 129 (!getenv(envname) ? (dflt) : strtod(getenv(envname), NULL)) 130 131#endif // BASE_COMMANDLINEFLAGS_H_ 132