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