commandlineflags.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #ifndef BENCHMARK_COMMANDLINEFLAGS_H_
  2. #define BENCHMARK_COMMANDLINEFLAGS_H_
  3. #include <cstdint>
  4. #include <string>
  5. // Macro for referencing flags.
  6. #define FLAG(name) FLAGS_##name
  7. // Macros for declaring flags.
  8. #define DECLARE_bool(name) extern bool FLAG(name)
  9. #define DECLARE_int32(name) extern int32_t FLAG(name)
  10. #define DECLARE_int64(name) extern int64_t FLAG(name)
  11. #define DECLARE_double(name) extern double FLAG(name)
  12. #define DECLARE_string(name) extern std::string FLAG(name)
  13. // Macros for defining flags.
  14. #define DEFINE_bool(name, default_val, doc) bool FLAG(name) = (default_val)
  15. #define DEFINE_int32(name, default_val, doc) int32_t FLAG(name) = (default_val)
  16. #define DEFINE_int64(name, default_val, doc) int64_t FLAG(name) = (default_val)
  17. #define DEFINE_double(name, default_val, doc) double FLAG(name) = (default_val)
  18. #define DEFINE_string(name, default_val, doc) \
  19. std::string FLAG(name) = (default_val)
  20. namespace benchmark {
  21. // Parses 'str' for a 32-bit signed integer. If successful, writes the result
  22. // to *value and returns true; otherwise leaves *value unchanged and returns
  23. // false.
  24. bool ParseInt32(const std::string& src_text, const char* str, int32_t* value);
  25. // Parses a bool/Int32/string from the environment variable
  26. // corresponding to the given Google Test flag.
  27. bool BoolFromEnv(const char* flag, bool default_val);
  28. int32_t Int32FromEnv(const char* flag, int32_t default_val);
  29. double DoubleFromEnv(const char* flag, double default_val);
  30. const char* StringFromEnv(const char* flag, const char* default_val);
  31. // Parses a string for a bool flag, in the form of either
  32. // "--flag=value" or "--flag".
  33. //
  34. // In the former case, the value is taken as true if it passes IsTruthyValue().
  35. //
  36. // In the latter case, the value is taken as true.
  37. //
  38. // On success, stores the value of the flag in *value, and returns
  39. // true. On failure, returns false without changing *value.
  40. bool ParseBoolFlag(const char* str, const char* flag, bool* value);
  41. // Parses a string for an Int32 flag, in the form of
  42. // "--flag=value".
  43. //
  44. // On success, stores the value of the flag in *value, and returns
  45. // true. On failure, returns false without changing *value.
  46. bool ParseInt32Flag(const char* str, const char* flag, int32_t* value);
  47. // Parses a string for a Double flag, in the form of
  48. // "--flag=value".
  49. //
  50. // On success, stores the value of the flag in *value, and returns
  51. // true. On failure, returns false without changing *value.
  52. bool ParseDoubleFlag(const char* str, const char* flag, double* value);
  53. // Parses a string for a string flag, in the form of
  54. // "--flag=value".
  55. //
  56. // On success, stores the value of the flag in *value, and returns
  57. // true. On failure, returns false without changing *value.
  58. bool ParseStringFlag(const char* str, const char* flag, std::string* value);
  59. // Returns true if the string matches the flag.
  60. bool IsFlag(const char* str, const char* flag);
  61. // Returns true unless value starts with one of: '0', 'f', 'F', 'n' or 'N', or
  62. // some non-alphanumeric character. As a special case, also returns true if
  63. // value is the empty string.
  64. bool IsTruthyFlagValue(const std::string& value);
  65. } // end namespace benchmark
  66. #endif // BENCHMARK_COMMANDLINEFLAGS_H_