Home | History | Annotate | Download | only in base
      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 we have to specify the namespace
    104 // std::string, which doesn't play nicely with our FLAG__namespace hackery.
    105 #define DECLARE_string(name)                                          \
    106   namespace FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead {  \
    107   extern std::string FLAGS_##name;                                                   \
    108   }                                                                           \
    109   using FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead::FLAGS_##name
    110 #define DEFINE_string(name, value, meaning) \
    111   namespace FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead {  \
    112   std::string FLAGS_##name(value);                                                   \
    113   char FLAGS_no##name;                                                        \
    114   }                                                                           \
    115   using FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead::FLAGS_##name
    116 
    117 // These macros (could be functions, but I don't want to bother with a .cc
    118 // file), make it easier to initialize flags from the environment.
    119 // They are functions in Android because __system_property_get() doesn't
    120 // return a string.
    121 
    122 #if defined(__ANDROID__) || defined(ANDROID)
    123 
    124 // Returns a pointer to a static variable.  The string pointed by the returned
    125 // pointer must not be modified.
    126 inline const char* const EnvToString(const char* envname, const char* dflt) {
    127   static char system_property_value[PROP_VALUE_MAX];
    128   if (__system_property_get(envname, system_property_value) > 0)
    129     return system_property_value;
    130   return dflt;
    131 }
    132 
    133 inline bool EnvToBool(const char* envname, bool dflt) {
    134   static const char kTrueValues[] = "tTyY1";
    135   char system_property_value[PROP_VALUE_MAX];
    136   if (__system_property_get(envname, system_property_value) > 0)
    137     return memchr(kTrueValues, system_property_value[0], sizeof(kTrueValues));
    138   return dflt;
    139 }
    140 
    141 inline int EnvToInt(const char* envname, int dflt) {
    142   char system_property_value[PROP_VALUE_MAX];
    143   if (__system_property_get(envname, system_property_value) > 0)
    144     return strtol(system_property_value, NULL, 10);
    145   return dflt;
    146 }
    147 
    148 inline int64 EnvToInt64(const char* envname, int64 dflt) {
    149   char system_property_value[PROP_VALUE_MAX];
    150   if (__system_property_get(envname, system_property_value) > 0)
    151     return strtoll(system_property_value, NULL, 10);
    152   return dflt;
    153 }
    154 
    155 inline double EnvToDouble(const char* envname, double dflt) {
    156   char system_property_value[PROP_VALUE_MAX];
    157   if (__system_property_get(envname, system_property_value) > 0)
    158     return strtod(system_property_value, NULL);
    159   return dflt;
    160 }
    161 
    162 #else  // defined(__ANDROID__) || defined(ANDROID)
    163 
    164 #define EnvToString(envname, dflt)   \
    165   (!getenv(envname) ? (dflt) : getenv(envname))
    166 
    167 #define EnvToBool(envname, dflt)   \
    168   (!getenv(envname) ? (dflt) : memchr("tTyY1\0", getenv(envname)[0], 6) != NULL)
    169 
    170 #define EnvToInt(envname, dflt)  \
    171   (!getenv(envname) ? (dflt) : strtol(getenv(envname), NULL, 10))
    172 
    173 #define EnvToInt64(envname, dflt)  \
    174   (!getenv(envname) ? (dflt) : strtoll(getenv(envname), NULL, 10))
    175 
    176 #define EnvToDouble(envname, dflt)  \
    177   (!getenv(envname) ? (dflt) : strtod(getenv(envname), NULL))
    178 
    179 #endif  // defined(__ANDROID__) || defined(ANDROID)
    180 
    181 #endif  // BASE_COMMANDLINEFLAGS_H_
    182