1 #ifndef BENCHMARK_COMMANDLINEFLAGS_H_
2 #define BENCHMARK_COMMANDLINEFLAGS_H_
3 
4 #include <cstdint>
5 #include <map>
6 #include <string>
7 
8 // Macro for referencing flags.
9 #define FLAG(name) FLAGS_##name
10 
11 // Macros for declaring flags.
12 #define DECLARE_bool(name) extern bool FLAG(name)
13 #define DECLARE_int32(name) extern int32_t FLAG(name)
14 #define DECLARE_double(name) extern double FLAG(name)
15 #define DECLARE_string(name) extern std::string FLAG(name)
16 #define DECLARE_kvpairs(name) \
17   extern std::map<std::string, std::string> FLAG(name)
18 
19 // Macros for defining flags.
20 #define DEFINE_bool(name, default_val) \
21   bool FLAG(name) = benchmark::BoolFromEnv(#name, default_val)
22 #define DEFINE_int32(name, default_val) \
23   int32_t FLAG(name) = benchmark::Int32FromEnv(#name, default_val)
24 #define DEFINE_double(name, default_val) \
25   double FLAG(name) = benchmark::DoubleFromEnv(#name, default_val)
26 #define DEFINE_string(name, default_val) \
27   std::string FLAG(name) = benchmark::StringFromEnv(#name, default_val)
28 #define DEFINE_kvpairs(name, default_val)         \
29   std::map<std::string, std::string> FLAG(name) = \
30       benchmark::KvPairsFromEnv(#name, default_val)
31 
32 namespace benchmark {
33 
34 // Parses a bool from the environment variable corresponding to the given flag.
35 //
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);
39 
40 // Parses an Int32 from the environment variable corresponding to the given
41 // flag.
42 //
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);
46 
47 // Parses an Double from the environment variable corresponding to the given
48 // flag.
49 //
50 // If the variable exists, returns ParseDouble();  if not, returns
51 // the given default value.
52 double DoubleFromEnv(const char* flag, double default_val);
53 
54 // Parses a string from the environment variable corresponding to the given
55 // flag.
56 //
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);
60 
61 // Parses a set of kvpairs from the environment variable corresponding to the
62 // given flag.
63 //
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);
68 
69 // Parses a string for a bool flag, in the form of either
70 // "--flag=value" or "--flag".
71 //
72 // In the former case, the value is taken as true if it passes IsTruthyValue().
73 //
74 // In the latter case, the value is taken as true.
75 //
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);
79 
80 // Parses a string for an Int32 flag, in the form of "--flag=value".
81 //
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);
85 
86 // Parses a string for a Double flag, in the form of "--flag=value".
87 //
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);
91 
92 // Parses a string for a string flag, in the form of "--flag=value".
93 //
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);
97 
98 // Parses a string for a kvpairs flag in the form "--flag=key=value,key=value"
99 //
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);
104 
105 // Returns true if the string matches the flag.
106 bool IsFlag(const char* str, const char* flag);
107 
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);
113 
114 }  // end namespace benchmark
115 
116 #endif  // BENCHMARK_COMMANDLINEFLAGS_H_
117