You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

284 lines
8.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2006, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. // Originally comes from shared/commandlineflags/flags.h
  28. // Flags are defined and declared using DEFINE_xxx and DECLARE_xxx macros,
  29. // where xxx is the flag type. Flags are referred to via FLAG_yyy,
  30. // where yyy is the flag name. For intialization and iteration of flags,
  31. // see the FlagList class. For full programmatic access to any
  32. // flag, see the Flag class.
  33. //
  34. // The implementation only relies and basic C++ functionality
  35. // and needs no special library or STL support.
  36. #ifndef TALK_BASE_FLAGS_H__
  37. #define TALK_BASE_FLAGS_H__
  38. #include <assert.h>
  39. #include "talk/base/checks.h"
  40. #include "talk/base/common.h"
  41. // Internal use only.
  42. union FlagValue {
  43. // Note: Because in C++ non-bool values are silently converted into
  44. // bool values ('bool b = "false";' results in b == true!), we pass
  45. // and int argument to New_BOOL as this appears to be safer - sigh.
  46. // In particular, it prevents the (not uncommon!) bug where a bool
  47. // flag is defined via: DEFINE_bool(flag, "false", "some comment");.
  48. static FlagValue New_BOOL(int b) {
  49. FlagValue v;
  50. v.b = (b != 0);
  51. return v;
  52. }
  53. static FlagValue New_INT(int i) {
  54. FlagValue v;
  55. v.i = i;
  56. return v;
  57. }
  58. static FlagValue New_FLOAT(float f) {
  59. FlagValue v;
  60. v.f = f;
  61. return v;
  62. }
  63. static FlagValue New_STRING(const char* s) {
  64. FlagValue v;
  65. v.s = s;
  66. return v;
  67. }
  68. bool b;
  69. int i;
  70. double f;
  71. const char* s;
  72. };
  73. // Each flag can be accessed programmatically via a Flag object.
  74. class Flag {
  75. public:
  76. enum Type { BOOL, INT, FLOAT, STRING };
  77. // Internal use only.
  78. Flag(const char* file, const char* name, const char* comment,
  79. Type type, void* variable, FlagValue default_);
  80. // General flag information
  81. const char* file() const { return file_; }
  82. const char* name() const { return name_; }
  83. const char* comment() const { return comment_; }
  84. // Flag type
  85. Type type() const { return type_; }
  86. // Flag variables
  87. bool* bool_variable() const {
  88. assert(type_ == BOOL);
  89. return &variable_->b;
  90. }
  91. int* int_variable() const {
  92. assert(type_ == INT);
  93. return &variable_->i;
  94. }
  95. double* float_variable() const {
  96. assert(type_ == FLOAT);
  97. return &variable_->f;
  98. }
  99. const char** string_variable() const {
  100. assert(type_ == STRING);
  101. return &variable_->s;
  102. }
  103. // Default values
  104. bool bool_default() const {
  105. assert(type_ == BOOL);
  106. return default_.b;
  107. }
  108. int int_default() const {
  109. assert(type_ == INT);
  110. return default_.i;
  111. }
  112. double float_default() const {
  113. assert(type_ == FLOAT);
  114. return default_.f;
  115. }
  116. const char* string_default() const {
  117. assert(type_ == STRING);
  118. return default_.s;
  119. }
  120. // Resets a flag to its default value
  121. void SetToDefault();
  122. // Iteration support
  123. Flag* next() const { return next_; }
  124. // Prints flag information. The current flag value is only printed
  125. // if print_current_value is set.
  126. void Print(bool print_current_value);
  127. private:
  128. const char* file_;
  129. const char* name_;
  130. const char* comment_;
  131. Type type_;
  132. FlagValue* variable_;
  133. FlagValue default_;
  134. Flag* next_;
  135. friend class FlagList; // accesses next_
  136. };
  137. // Internal use only.
  138. #define DEFINE_FLAG(type, c_type, name, default, comment) \
  139. /* define and initialize the flag */ \
  140. c_type FLAG_##name = (default); \
  141. /* register the flag */ \
  142. static Flag Flag_##name(__FILE__, #name, (comment), \
  143. Flag::type, &FLAG_##name, \
  144. FlagValue::New_##type(default))
  145. // Internal use only.
  146. #define DECLARE_FLAG(c_type, name) \
  147. /* declare the external flag */ \
  148. extern c_type FLAG_##name
  149. // Use the following macros to define a new flag:
  150. #define DEFINE_bool(name, default, comment) \
  151. DEFINE_FLAG(BOOL, bool, name, default, comment)
  152. #define DEFINE_int(name, default, comment) \
  153. DEFINE_FLAG(INT, int, name, default, comment)
  154. #define DEFINE_float(name, default, comment) \
  155. DEFINE_FLAG(FLOAT, double, name, default, comment)
  156. #define DEFINE_string(name, default, comment) \
  157. DEFINE_FLAG(STRING, const char*, name, default, comment)
  158. // Use the following macros to declare a flag defined elsewhere:
  159. #define DECLARE_bool(name) DECLARE_FLAG(bool, name)
  160. #define DECLARE_int(name) DECLARE_FLAG(int, name)
  161. #define DECLARE_float(name) DECLARE_FLAG(double, name)
  162. #define DECLARE_string(name) DECLARE_FLAG(const char*, name)
  163. // The global list of all flags.
  164. class FlagList {
  165. public:
  166. FlagList();
  167. // The NULL-terminated list of all flags. Traverse with Flag::next().
  168. static Flag* list() { return list_; }
  169. // If file != NULL, prints information for all flags defined in file;
  170. // otherwise prints information for all flags in all files. The current
  171. // flag value is only printed if print_current_value is set.
  172. static void Print(const char* file, bool print_current_value);
  173. // Lookup a flag by name. Returns the matching flag or NULL.
  174. static Flag* Lookup(const char* name);
  175. // Helper function to parse flags: Takes an argument arg and splits it into
  176. // a flag name and flag value (or NULL if they are missing). is_bool is set
  177. // if the arg started with "-no" or "--no". The buffer may be used to NUL-
  178. // terminate the name, it must be large enough to hold any possible name.
  179. static void SplitArgument(const char* arg,
  180. char* buffer, int buffer_size,
  181. const char** name, const char** value,
  182. bool* is_bool);
  183. // Set the flag values by parsing the command line. If remove_flags
  184. // is set, the flags and associated values are removed from (argc,
  185. // argv). Returns 0 if no error occurred. Otherwise, returns the
  186. // argv index > 0 for the argument where an error occurred. In that
  187. // case, (argc, argv) will remain unchanged indepdendent of the
  188. // remove_flags value, and no assumptions about flag settings should
  189. // be made.
  190. //
  191. // The following syntax for flags is accepted (both '-' and '--' are ok):
  192. //
  193. // --flag (bool flags only)
  194. // --noflag (bool flags only)
  195. // --flag=value (non-bool flags only, no spaces around '=')
  196. // --flag value (non-bool flags only)
  197. static int SetFlagsFromCommandLine(int* argc,
  198. const char** argv,
  199. bool remove_flags);
  200. static inline int SetFlagsFromCommandLine(int* argc,
  201. char** argv,
  202. bool remove_flags) {
  203. return SetFlagsFromCommandLine(argc, const_cast<const char**>(argv),
  204. remove_flags);
  205. }
  206. // Registers a new flag. Called during program initialization. Not
  207. // thread-safe.
  208. static void Register(Flag* flag);
  209. private:
  210. static Flag* list_;
  211. };
  212. #ifdef WIN32
  213. // A helper class to translate Windows command line arguments into UTF8,
  214. // which then allows us to just pass them to the flags system.
  215. // This encapsulates all the work of getting the command line and translating
  216. // it to an array of 8-bit strings; all you have to do is create one of these,
  217. // and then call argc() and argv().
  218. class WindowsCommandLineArguments {
  219. public:
  220. WindowsCommandLineArguments();
  221. ~WindowsCommandLineArguments();
  222. int argc() { return argc_; }
  223. char **argv() { return argv_; }
  224. private:
  225. int argc_;
  226. char **argv_;
  227. private:
  228. DISALLOW_EVIL_CONSTRUCTORS(WindowsCommandLineArguments);
  229. };
  230. #endif // WIN32
  231. #endif // SHARED_COMMANDLINEFLAGS_FLAGS_H__