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#if defined(__ANDROID__) || defined(ANDROID)
58#include <sys/system_properties.h>
59#endif
60
61#define DECLARE_VARIABLE(type, name)                                          \
62  namespace FLAG__namespace_do_not_use_directly_use_DECLARE_##type##_instead {\
63  extern PERFTOOLS_DLL_DECL type FLAGS_##name;                                \
64  }                                                                           \
65  using FLAG__namespace_do_not_use_directly_use_DECLARE_##type##_instead::FLAGS_##name
66
67#define DEFINE_VARIABLE(type, name, value, meaning) \
68  namespace FLAG__namespace_do_not_use_directly_use_DECLARE_##type##_instead {\
69  PERFTOOLS_DLL_DECL type FLAGS_##name(value);                                \
70  char FLAGS_no##name;                                                        \
71  }                                                                           \
72  using FLAG__namespace_do_not_use_directly_use_DECLARE_##type##_instead::FLAGS_##name
73
74// bool specialization
75#define DECLARE_bool(name) \
76  DECLARE_VARIABLE(bool, name)
77#define DEFINE_bool(name, value, meaning) \
78  DEFINE_VARIABLE(bool, name, value, meaning)
79
80// int32 specialization
81#define DECLARE_int32(name) \
82  DECLARE_VARIABLE(int32, name)
83#define DEFINE_int32(name, value, meaning) \
84  DEFINE_VARIABLE(int32, name, value, meaning)
85
86// int64 specialization
87#define DECLARE_int64(name) \
88  DECLARE_VARIABLE(int64, name)
89#define DEFINE_int64(name, value, meaning) \
90  DEFINE_VARIABLE(int64, name, value, meaning)
91
92#define DECLARE_uint64(name) \
93  DECLARE_VARIABLE(uint64, name)
94#define DEFINE_uint64(name, value, meaning) \
95  DEFINE_VARIABLE(uint64, name, value, meaning)
96
97// double specialization
98#define DECLARE_double(name) \
99  DECLARE_VARIABLE(double, name)
100#define DEFINE_double(name, value, meaning) \
101  DEFINE_VARIABLE(double, name, value, meaning)
102
103// Special case for string, because of the pointer type.
104#define DECLARE_string(name)                                          \
105  namespace FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead {  \
106  extern const char* FLAGS_##name;                                            \
107  }                                                                           \
108  using FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead::FLAGS_##name
109#define DEFINE_string(name, value, meaning) \
110  namespace FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead {  \
111  const char* FLAGS_##name = value;                                           \
112  char FLAGS_no##name;                                                        \
113  }                                                                           \
114  using FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead::FLAGS_##name
115
116// These macros (could be functions, but I don't want to bother with a .cc
117// file), make it easier to initialize flags from the environment.
118// They are functions in Android because __system_property_get() doesn't
119// return a string.
120
121#if defined(ENABLE_PROFILING)
122
123#if defined(__ANDROID__) || defined(ANDROID)
124
125// Returns a pointer to a static variable.  The string pointed by the returned
126// pointer must not be modified.
127inline const char* const EnvToString(const char* envname, const char* dflt) {
128  static char system_property_value[PROP_VALUE_MAX];
129  if (__system_property_get(envname, system_property_value) > 0)
130    return system_property_value;
131  return dflt;
132}
133
134inline bool EnvToBool(const char* envname, bool dflt) {
135  static const char kTrueValues[] = "tTyY1";
136  char system_property_value[PROP_VALUE_MAX];
137  if (__system_property_get(envname, system_property_value) > 0)
138    return memchr(kTrueValues, system_property_value[0], sizeof(kTrueValues));
139  return dflt;
140}
141
142inline int EnvToInt(const char* envname, int dflt) {
143  char system_property_value[PROP_VALUE_MAX];
144  if (__system_property_get(envname, system_property_value) > 0)
145    return strtol(system_property_value, NULL, 10);
146  return dflt;
147}
148
149inline int64 EnvToInt64(const char* envname, int64 dflt) {
150  char system_property_value[PROP_VALUE_MAX];
151  if (__system_property_get(envname, system_property_value) > 0)
152    return strtoll(system_property_value, NULL, 10);
153  return dflt;
154}
155
156inline double EnvToDouble(const char* envname, double dflt) {
157  char system_property_value[PROP_VALUE_MAX];
158  if (__system_property_get(envname, system_property_value) > 0)
159    return strtod(system_property_value, NULL);
160  return dflt;
161}
162
163#else  // defined(__ANDROID__) || defined(ANDROID)
164
165#define EnvToString(envname, dflt)   \
166  (!getenv(envname) ? (dflt) : getenv(envname))
167
168#define EnvToBool(envname, dflt)   \
169  (!getenv(envname) ? (dflt) : memchr("tTyY1\0", getenv(envname)[0], 6) != NULL)
170
171#define EnvToInt(envname, dflt)  \
172  (!getenv(envname) ? (dflt) : strtol(getenv(envname), NULL, 10))
173
174#define EnvToInt64(envname, dflt)  \
175  (!getenv(envname) ? (dflt) : strtoll(getenv(envname), NULL, 10))
176
177#define EnvToDouble(envname, dflt)  \
178  (!getenv(envname) ? (dflt) : strtod(getenv(envname), NULL))
179
180#endif  // defined(__ANDROID__) || defined(ANDROID)
181
182#else  // defined(ENABLE_PROFILING)
183
184#define EnvToString(envname, dflt) (dflt)
185#define EnvToBool(envname, dflt) (dflt)
186#define EnvToInt(envname, dflt) (dflt)
187#define EnvToInt64(envname, dflt) (dflt)
188#define EnvToDouble(envname, dflt) (dflt)
189
190#endif  // defined(ENABLE_PROFILING)
191
192#endif  // BASE_COMMANDLINEFLAGS_H_
193