commandlineflags.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. // Copyright (c) 2005, Google Inc.
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  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. #include <config.h>
  51. #include <string>
  52. #include <string.h> // for memchr
  53. #include <stdlib.h> // for getenv
  54. #include "base/basictypes.h"
  55. #define DECLARE_VARIABLE(type, name) \
  56. namespace FLAG__namespace_do_not_use_directly_use_DECLARE_##type##_instead { \
  57. extern PERFTOOLS_DLL_DECL type FLAGS_##name; \
  58. } \
  59. using FLAG__namespace_do_not_use_directly_use_DECLARE_##type##_instead::FLAGS_##name
  60. #define DEFINE_VARIABLE(type, name, value, meaning) \
  61. namespace FLAG__namespace_do_not_use_directly_use_DECLARE_##type##_instead { \
  62. PERFTOOLS_DLL_DECL type FLAGS_##name(value); \
  63. char FLAGS_no##name; \
  64. } \
  65. using FLAG__namespace_do_not_use_directly_use_DECLARE_##type##_instead::FLAGS_##name
  66. // bool specialization
  67. #define DECLARE_bool(name) \
  68. DECLARE_VARIABLE(bool, name)
  69. #define DEFINE_bool(name, value, meaning) \
  70. DEFINE_VARIABLE(bool, name, value, meaning)
  71. // int32 specialization
  72. #define DECLARE_int32(name) \
  73. DECLARE_VARIABLE(int32, name)
  74. #define DEFINE_int32(name, value, meaning) \
  75. DEFINE_VARIABLE(int32, name, value, meaning)
  76. // int64 specialization
  77. #define DECLARE_int64(name) \
  78. DECLARE_VARIABLE(int64, name)
  79. #define DEFINE_int64(name, value, meaning) \
  80. DEFINE_VARIABLE(int64, name, value, meaning)
  81. #define DECLARE_uint64(name) \
  82. DECLARE_VARIABLE(uint64, name)
  83. #define DEFINE_uint64(name, value, meaning) \
  84. DEFINE_VARIABLE(uint64, name, value, meaning)
  85. // double specialization
  86. #define DECLARE_double(name) \
  87. DECLARE_VARIABLE(double, name)
  88. #define DEFINE_double(name, value, meaning) \
  89. DEFINE_VARIABLE(double, name, value, meaning)
  90. // Special case for string, because we have to specify the namespace
  91. // std::string, which doesn't play nicely with our FLAG__namespace hackery.
  92. #define DECLARE_string(name) \
  93. namespace FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead { \
  94. extern std::string FLAGS_##name; \
  95. } \
  96. using FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead::FLAGS_##name
  97. #define DEFINE_string(name, value, meaning) \
  98. namespace FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead { \
  99. std::string FLAGS_##name(value); \
  100. char FLAGS_no##name; \
  101. } \
  102. using FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead::FLAGS_##name
  103. // implemented in sysinfo.cc
  104. namespace tcmalloc {
  105. namespace commandlineflags {
  106. inline bool StringToBool(const char *value, bool def) {
  107. if (!value) {
  108. return def;
  109. }
  110. return memchr("tTyY1\0", value[0], 6) != NULL;
  111. }
  112. inline int StringToInt(const char *value, int def) {
  113. if (!value) {
  114. return def;
  115. }
  116. return strtol(value, NULL, 10);
  117. }
  118. inline long long StringToLongLong(const char *value, long long def) {
  119. if (!value) {
  120. return def;
  121. }
  122. return strtoll(value, NULL, 10);
  123. }
  124. inline double StringToDouble(const char *value, double def) {
  125. if (!value) {
  126. return def;
  127. }
  128. return strtod(value, NULL);
  129. }
  130. }
  131. }
  132. // These macros (could be functions, but I don't want to bother with a .cc
  133. // file), make it easier to initialize flags from the environment.
  134. #define EnvToString(envname, dflt) \
  135. (!getenv(envname) ? (dflt) : getenv(envname))
  136. #define EnvToBool(envname, dflt) \
  137. tcmalloc::commandlineflags::StringToBool(getenv(envname), dflt)
  138. #define EnvToInt(envname, dflt) \
  139. tcmalloc::commandlineflags::StringToInt(getenv(envname), dflt)
  140. #define EnvToInt64(envname, dflt) \
  141. tcmalloc::commandlineflags::StringToLongLong(getenv(envname), dflt)
  142. #define EnvToDouble(envname, dflt) \
  143. tcmalloc::commandlineflags::StringToDouble(getenv(envname), dflt)
  144. #endif // BASE_COMMANDLINEFLAGS_H_