1 | #ifndef BENCHMARK_COMMANDLINEFLAGS_H_ |
2 | #define BENCHMARK_COMMANDLINEFLAGS_H_ |
3 | |
4 | #include <cstdint> |
5 | #include <map> |
6 | #include <string> |
7 | |
8 | #include "benchmark/export.h" |
9 | |
10 | // Macro for referencing flags. |
11 | #define FLAG(name) FLAGS_##name |
12 | |
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) |
20 | |
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) |
36 | |
37 | namespace benchmark { |
38 | |
39 | // Parses a bool from the environment variable corresponding to the given flag. |
40 | // |
41 | // If the variable exists, returns IsTruthyFlagValue() value; if not, |
42 | // returns the given default value. |
43 | BENCHMARK_EXPORT |
44 | bool BoolFromEnv(const char* flag, bool default_val); |
45 | |
46 | // Parses an Int32 from the environment variable corresponding to the given |
47 | // flag. |
48 | // |
49 | // If the variable exists, returns ParseInt32() value; if not, returns |
50 | // the given default value. |
51 | BENCHMARK_EXPORT |
52 | int32_t Int32FromEnv(const char* flag, int32_t default_val); |
53 | |
54 | // Parses an Double from the environment variable corresponding to the given |
55 | // flag. |
56 | // |
57 | // If the variable exists, returns ParseDouble(); if not, returns |
58 | // the given default value. |
59 | BENCHMARK_EXPORT |
60 | double DoubleFromEnv(const char* flag, double default_val); |
61 | |
62 | // Parses a string from the environment variable corresponding to the given |
63 | // flag. |
64 | // |
65 | // If variable exists, returns its value; if not, returns |
66 | // the given default value. |
67 | BENCHMARK_EXPORT |
68 | const char* StringFromEnv(const char* flag, const char* default_val); |
69 | |
70 | // Parses a set of kvpairs from the environment variable corresponding to the |
71 | // given flag. |
72 | // |
73 | // If variable exists, returns its value; if not, returns |
74 | // the given default value. |
75 | BENCHMARK_EXPORT |
76 | std::map<std::string, std::string> KvPairsFromEnv( |
77 | const char* flag, std::map<std::string, std::string> default_val); |
78 | |
79 | // Parses a string for a bool flag, in the form of either |
80 | // "--flag=value" or "--flag". |
81 | // |
82 | // In the former case, the value is taken as true if it passes IsTruthyValue(). |
83 | // |
84 | // In the latter case, the value is taken as true. |
85 | // |
86 | // On success, stores the value of the flag in *value, and returns |
87 | // true. On failure, returns false without changing *value. |
88 | BENCHMARK_EXPORT |
89 | bool ParseBoolFlag(const char* str, const char* flag, bool* value); |
90 | |
91 | // Parses a string for an Int32 flag, in the form of "--flag=value". |
92 | // |
93 | // On success, stores the value of the flag in *value, and returns |
94 | // true. On failure, returns false without changing *value. |
95 | BENCHMARK_EXPORT |
96 | bool ParseInt32Flag(const char* str, const char* flag, int32_t* value); |
97 | |
98 | // Parses a string for a Double flag, in the form of "--flag=value". |
99 | // |
100 | // On success, stores the value of the flag in *value, and returns |
101 | // true. On failure, returns false without changing *value. |
102 | BENCHMARK_EXPORT |
103 | bool ParseDoubleFlag(const char* str, const char* flag, double* value); |
104 | |
105 | // Parses a string for a string flag, in the form of "--flag=value". |
106 | // |
107 | // On success, stores the value of the flag in *value, and returns |
108 | // true. On failure, returns false without changing *value. |
109 | BENCHMARK_EXPORT |
110 | bool ParseStringFlag(const char* str, const char* flag, std::string* value); |
111 | |
112 | // Parses a string for a kvpairs flag in the form "--flag=key=value,key=value" |
113 | // |
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. |
116 | BENCHMARK_EXPORT |
117 | bool ParseKeyValueFlag(const char* str, const char* flag, |
118 | std::map<std::string, std::string>* value); |
119 | |
120 | // Returns true if the string matches the flag. |
121 | BENCHMARK_EXPORT |
122 | bool IsFlag(const char* str, const char* flag); |
123 | |
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. |
128 | BENCHMARK_EXPORT |
129 | bool IsTruthyFlagValue(const std::string& value); |
130 | |
131 | } // end namespace benchmark |
132 | |
133 | #endif // BENCHMARK_COMMANDLINEFLAGS_H_ |
134 | |