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_double(name) extern double FLAG(name) |
14 | #define DECLARE_string(name) extern std::string FLAG(name) |
15 | |
16 | // Macros for defining flags. |
17 | #define DEFINE_bool(name, default_val) \ |
18 | bool FLAG(name) = \ |
19 | benchmark::BoolFromEnv(#name, default_val) |
20 | #define DEFINE_int32(name, default_val) \ |
21 | int32_t FLAG(name) = \ |
22 | benchmark::Int32FromEnv(#name, default_val) |
23 | #define DEFINE_double(name, default_val) \ |
24 | double FLAG(name) = \ |
25 | benchmark::DoubleFromEnv(#name, default_val) |
26 | #define DEFINE_string(name, default_val) \ |
27 | std::string FLAG(name) = \ |
28 | benchmark::StringFromEnv(#name, default_val) |
29 | |
30 | namespace benchmark { |
31 | |
32 | // Parses a bool from the environment variable |
33 | // corresponding to the given flag. |
34 | // |
35 | // If the variable exists, returns IsTruthyFlagValue() value; if not, |
36 | // returns the given default value. |
37 | bool BoolFromEnv(const char* flag, bool default_val); |
38 | |
39 | // Parses an Int32 from the environment variable |
40 | // corresponding to the given flag. |
41 | // |
42 | // If the variable exists, returns ParseInt32() value; if not, returns |
43 | // the given default value. |
44 | int32_t Int32FromEnv(const char* flag, int32_t default_val); |
45 | |
46 | // Parses an Double from the environment variable |
47 | // corresponding to the given flag. |
48 | // |
49 | // If the variable exists, returns ParseDouble(); if not, returns |
50 | // the given default value. |
51 | double DoubleFromEnv(const char* flag, double default_val); |
52 | |
53 | // Parses a string from the environment variable |
54 | // corresponding to the given flag. |
55 | // |
56 | // If variable exists, returns its value; if not, returns |
57 | // the given default value. |
58 | const char* StringFromEnv(const char* flag, const char* default_val); |
59 | |
60 | // Parses a string for a bool flag, in the form of either |
61 | // "--flag=value" or "--flag". |
62 | // |
63 | // In the former case, the value is taken as true if it passes IsTruthyValue(). |
64 | // |
65 | // In the latter case, the value is taken as true. |
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 ParseBoolFlag(const char* str, const char* flag, bool* value); |
70 | |
71 | // Parses a string for an Int32 flag, in the form of |
72 | // "--flag=value". |
73 | // |
74 | // On success, stores the value of the flag in *value, and returns |
75 | // true. On failure, returns false without changing *value. |
76 | bool ParseInt32Flag(const char* str, const char* flag, int32_t* value); |
77 | |
78 | // Parses a string for a Double flag, in the form of |
79 | // "--flag=value". |
80 | // |
81 | // On success, stores the value of the flag in *value, and returns |
82 | // true. On failure, returns false without changing *value. |
83 | bool ParseDoubleFlag(const char* str, const char* flag, double* value); |
84 | |
85 | // Parses a string for a string flag, in the form of |
86 | // "--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 ParseStringFlag(const char* str, const char* flag, std::string* value); |
91 | |
92 | // Returns true if the string matches the flag. |
93 | bool IsFlag(const char* str, const char* flag); |
94 | |
95 | // Returns true unless value starts with one of: '0', 'f', 'F', 'n' or 'N', or |
96 | // some non-alphanumeric character. Also returns false if the value matches |
97 | // one of 'no', 'false', 'off' (case-insensitive). As a special case, also |
98 | // returns true if value is the empty string. |
99 | bool IsTruthyFlagValue(const std::string& value); |
100 | |
101 | } // end namespace benchmark |
102 | |
103 | #endif // BENCHMARK_COMMANDLINEFLAGS_H_ |
104 | |