commandlineflags.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. // This file is a compatibility layer that defines Google's version of
  31. // command line flags that are used for configuration.
  32. //
  33. // We put flags into their own namespace. It is purposefully
  34. // named in an opaque way that people should have trouble typing
  35. // directly. The idea is that DEFINE puts the flag in the weird
  36. // namespace, and DECLARE imports the flag from there into the
  37. // current namespace. The net result is to force people to use
  38. // DECLARE to get access to a flag, rather than saying
  39. // extern bool FLAGS_logtostderr;
  40. // or some such instead. We want this so we can put extra
  41. // functionality (like sanity-checking) in DECLARE if we want,
  42. // and make sure it is picked up everywhere.
  43. //
  44. // We also put the type of the variable in the namespace, so that
  45. // people can't DECLARE_int32 something that they DEFINE_bool'd
  46. // elsewhere.
  47. #ifndef BASE_COMMANDLINEFLAGS_H__
  48. #define BASE_COMMANDLINEFLAGS_H__
  49. #include <string>
  50. #include "basictypes.h"
  51. #define DECLARE_VARIABLE(type, name) \
  52. namespace FLAG__namespace_do_not_use_directly_use_DECLARE_##type##_instead { \
  53. extern type FLAGS_##name; \
  54. } \
  55. using FLAG__namespace_do_not_use_directly_use_DECLARE_##type##_instead::FLAGS_##name
  56. #define DEFINE_VARIABLE(type, name, value, meaning) \
  57. namespace FLAG__namespace_do_not_use_directly_use_DECLARE_##type##_instead { \
  58. type FLAGS_##name(value); \
  59. char FLAGS_no##name; \
  60. } \
  61. using FLAG__namespace_do_not_use_directly_use_DECLARE_##type##_instead::FLAGS_##name
  62. // bool specialization
  63. #define DECLARE_bool(name) \
  64. DECLARE_VARIABLE(bool, name)
  65. #define DEFINE_bool(name, value, meaning) \
  66. DEFINE_VARIABLE(bool, name, value, meaning)
  67. // int32 specialization
  68. #define DECLARE_int32(name) \
  69. DECLARE_VARIABLE(int32, name)
  70. #define DEFINE_int32(name, value, meaning) \
  71. DEFINE_VARIABLE(int32, name, value, meaning)
  72. // int64 specialization
  73. #define DECLARE_int64(name) \
  74. DECLARE_VARIABLE(int64, name)
  75. #define DEFINE_int64(name, value, meaning) \
  76. DEFINE_VARIABLE(int64, name, value, meaning)
  77. #define DECLARE_uint64(name) \
  78. DECLARE_VARIABLE(uint64, name)
  79. #define DEFINE_uint64(name, value, meaning) \
  80. DEFINE_VARIABLE(uint64, name, value, meaning)
  81. // double specialization
  82. #define DECLARE_double(name) \
  83. DECLARE_VARIABLE(double, name)
  84. #define DEFINE_double(name, value, meaning) \
  85. DEFINE_VARIABLE(double, name, value, meaning)
  86. // Special case for string, because we have to specify the namespace
  87. // std::string, which doesn't play nicely with our FLAG__namespace hackery.
  88. #define DECLARE_string(name) \
  89. namespace FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead { \
  90. extern std::string FLAGS_##name; \
  91. } \
  92. using FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead::FLAGS_##name
  93. #define DEFINE_string(name, value, meaning) \
  94. namespace FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead { \
  95. std::string FLAGS_##name(value); \
  96. char FLAGS_no##name; \
  97. } \
  98. using FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead::FLAGS_##name
  99. #endif // BASE_COMMANDLINEFLAGS_H__