1 #ifndef BENCHMARK_COMMANDLINEFLAGS_H_
2 #define BENCHMARK_COMMANDLINEFLAGS_H_
8 // Macro for referencing flags.
9 #define FLAG(name) FLAGS_##name
11 // Macros for declaring flags.
12 #define BM_DECLARE_bool(name) extern bool FLAG(name)
13 #define BM_DECLARE_int32(name) extern int32_t FLAG(name)
14 #define BM_DECLARE_double(name) extern double FLAG(name)
15 #define BM_DECLARE_string(name) extern std::string FLAG(name)
16 #define BM_DECLARE_kvpairs(name) \
17 extern std::map<std::string, std::string> FLAG(name)
19 // Macros for defining flags.
20 #define BM_DEFINE_bool(name, default_val) \
21 bool FLAG(name) = benchmark::BoolFromEnv(#name, default_val)
22 #define BM_DEFINE_int32(name, default_val) \
23 int32_t FLAG(name) = benchmark::Int32FromEnv(#name, default_val)
24 #define BM_DEFINE_double(name, default_val) \
25 double FLAG(name) = benchmark::DoubleFromEnv(#name, default_val)
26 #define BM_DEFINE_string(name, default_val) \
27 std::string FLAG(name) = benchmark::StringFromEnv(#name, default_val)
28 #define BM_DEFINE_kvpairs(name, default_val) \
29 std::map<std::string, std::string> FLAG(name) = \
30 benchmark::KvPairsFromEnv(#name, default_val)
34 // Parses a bool from the environment variable corresponding to the given flag.
36 // If the variable exists, returns IsTruthyFlagValue() value; if not,
37 // returns the given default value.
38 bool BoolFromEnv(const char* flag
, bool default_val
);
40 // Parses an Int32 from the environment variable corresponding to the given
43 // If the variable exists, returns ParseInt32() value; if not, returns
44 // the given default value.
45 int32_t Int32FromEnv(const char* flag
, int32_t default_val
);
47 // Parses an Double from the environment variable corresponding to the given
50 // If the variable exists, returns ParseDouble(); if not, returns
51 // the given default value.
52 double DoubleFromEnv(const char* flag
, double default_val
);
54 // Parses a string from the environment variable corresponding to the given
57 // If variable exists, returns its value; if not, returns
58 // the given default value.
59 const char* StringFromEnv(const char* flag
, const char* default_val
);
61 // Parses a set of kvpairs from the environment variable corresponding to the
64 // If variable exists, returns its value; if not, returns
65 // the given default value.
66 std::map
<std::string
, std::string
> KvPairsFromEnv(
67 const char* flag
, std::map
<std::string
, std::string
> default_val
);
69 // Parses a string for a bool flag, in the form of either
70 // "--flag=value" or "--flag".
72 // In the former case, the value is taken as true if it passes IsTruthyValue().
74 // In the latter case, the value is taken as true.
76 // On success, stores the value of the flag in *value, and returns
77 // true. On failure, returns false without changing *value.
78 bool ParseBoolFlag(const char* str
, const char* flag
, bool* value
);
80 // Parses a string for an Int32 flag, in the form of "--flag=value".
82 // On success, stores the value of the flag in *value, and returns
83 // true. On failure, returns false without changing *value.
84 bool ParseInt32Flag(const char* str
, const char* flag
, int32_t* value
);
86 // Parses a string for a Double flag, in the form of "--flag=value".
88 // On success, stores the value of the flag in *value, and returns
89 // true. On failure, returns false without changing *value.
90 bool ParseDoubleFlag(const char* str
, const char* flag
, double* value
);
92 // Parses a string for a string flag, in the form of "--flag=value".
94 // On success, stores the value of the flag in *value, and returns
95 // true. On failure, returns false without changing *value.
96 bool ParseStringFlag(const char* str
, const char* flag
, std::string
* value
);
98 // Parses a string for a kvpairs flag in the form "--flag=key=value,key=value"
100 // On success, stores the value of the flag in *value and returns true. On
101 // failure returns false, though *value may have been mutated.
102 bool ParseKeyValueFlag(const char* str
, const char* flag
,
103 std::map
<std::string
, std::string
>* value
);
105 // Returns true if the string matches the flag.
106 bool IsFlag(const char* str
, const char* flag
);
108 // Returns true unless value starts with one of: '0', 'f', 'F', 'n' or 'N', or
109 // some non-alphanumeric character. Also returns false if the value matches
110 // one of 'no', 'false', 'off' (case-insensitive). As a special case, also
111 // returns true if value is the empty string.
112 bool IsTruthyFlagValue(const std::string
& value
);
114 } // end namespace benchmark
116 #endif // BENCHMARK_COMMANDLINEFLAGS_H_