1 #ifndef BENCHMARK_COMMANDLINEFLAGS_H_
2 #define BENCHMARK_COMMANDLINEFLAGS_H_
8 #include "benchmark/export.h"
10 // Macro for referencing flags.
11 #define FLAG(name) FLAGS_##name
13 // Macros for declaring flags.
14 #define BM_DECLARE_bool(name) BENCHMARK_EXPORT extern bool FLAG(name)
15 #define BM_DECLARE_int32(name) BENCHMARK_EXPORT extern int32_t FLAG(name)
16 #define BM_DECLARE_double(name) BENCHMARK_EXPORT extern double FLAG(name)
17 #define BM_DECLARE_string(name) BENCHMARK_EXPORT extern std::string FLAG(name)
18 #define BM_DECLARE_kvpairs(name) \
19 BENCHMARK_EXPORT extern std::map<std::string, std::string> FLAG(name)
21 // Macros for defining flags.
22 #define BM_DEFINE_bool(name, default_val) \
23 BENCHMARK_EXPORT bool FLAG(name) = benchmark::BoolFromEnv(#name, default_val)
24 #define BM_DEFINE_int32(name, default_val) \
25 BENCHMARK_EXPORT int32_t FLAG(name) = \
26 benchmark::Int32FromEnv(#name, default_val)
27 #define BM_DEFINE_double(name, default_val) \
28 BENCHMARK_EXPORT double FLAG(name) = \
29 benchmark::DoubleFromEnv(#name, default_val)
30 #define BM_DEFINE_string(name, default_val) \
31 BENCHMARK_EXPORT std::string FLAG(name) = \
32 benchmark::StringFromEnv(#name, default_val)
33 #define BM_DEFINE_kvpairs(name, default_val) \
34 BENCHMARK_EXPORT std::map<std::string, std::string> FLAG(name) = \
35 benchmark::KvPairsFromEnv(#name, default_val)
39 // Parses a bool from the environment variable corresponding to the given flag.
41 // If the variable exists, returns IsTruthyFlagValue() value; if not,
42 // returns the given default value.
44 bool BoolFromEnv(const char* flag
, bool default_val
);
46 // Parses an Int32 from the environment variable corresponding to the given
49 // If the variable exists, returns ParseInt32() value; if not, returns
50 // the given default value.
52 int32_t Int32FromEnv(const char* flag
, int32_t default_val
);
54 // Parses an Double from the environment variable corresponding to the given
57 // If the variable exists, returns ParseDouble(); if not, returns
58 // the given default value.
60 double DoubleFromEnv(const char* flag
, double default_val
);
62 // Parses a string from the environment variable corresponding to the given
65 // If variable exists, returns its value; if not, returns
66 // the given default value.
68 const char* StringFromEnv(const char* flag
, const char* default_val
);
70 // Parses a set of kvpairs from the environment variable corresponding to the
73 // If variable exists, returns its value; if not, returns
74 // the given default value.
76 std::map
<std::string
, std::string
> KvPairsFromEnv(
77 const char* flag
, std::map
<std::string
, std::string
> default_val
);
79 // Parses a string for a bool flag, in the form of either
80 // "--flag=value" or "--flag".
82 // In the former case, the value is taken as true if it passes IsTruthyValue().
84 // In the latter case, the value is taken as true.
86 // On success, stores the value of the flag in *value, and returns
87 // true. On failure, returns false without changing *value.
89 bool ParseBoolFlag(const char* str
, const char* flag
, bool* value
);
91 // Parses a string for an Int32 flag, in the form of "--flag=value".
93 // On success, stores the value of the flag in *value, and returns
94 // true. On failure, returns false without changing *value.
96 bool ParseInt32Flag(const char* str
, const char* flag
, int32_t* value
);
98 // Parses a string for a Double flag, in the form of "--flag=value".
100 // On success, stores the value of the flag in *value, and returns
101 // true. On failure, returns false without changing *value.
103 bool ParseDoubleFlag(const char* str
, const char* flag
, double* value
);
105 // Parses a string for a string flag, in the form of "--flag=value".
107 // On success, stores the value of the flag in *value, and returns
108 // true. On failure, returns false without changing *value.
110 bool ParseStringFlag(const char* str
, const char* flag
, std::string
* value
);
112 // Parses a string for a kvpairs flag in the form "--flag=key=value,key=value"
114 // On success, stores the value of the flag in *value and returns true. On
115 // failure returns false, though *value may have been mutated.
117 bool ParseKeyValueFlag(const char* str
, const char* flag
,
118 std::map
<std::string
, std::string
>* value
);
120 // Returns true if the string matches the flag.
122 bool IsFlag(const char* str
, const char* flag
);
124 // Returns true unless value starts with one of: '0', 'f', 'F', 'n' or 'N', or
125 // some non-alphanumeric character. Also returns false if the value matches
126 // one of 'no', 'false', 'off' (case-insensitive). As a special case, also
127 // returns true if value is the empty string.
129 bool IsTruthyFlagValue(const std::string
& value
);
131 } // end namespace benchmark
133 #endif // BENCHMARK_COMMANDLINEFLAGS_H_