benchmark 1.9.2
 
Loading...
Searching...
No Matches
commandlineflags.h
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// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
15#define BM_DECLARE_bool(name) BENCHMARK_EXPORT extern bool FLAG(name)
16#define BM_DECLARE_int32(name) BENCHMARK_EXPORT extern int32_t FLAG(name)
17#define BM_DECLARE_double(name) BENCHMARK_EXPORT extern double FLAG(name)
18#define BM_DECLARE_string(name) BENCHMARK_EXPORT extern std::string FLAG(name)
19#define BM_DECLARE_kvpairs(name) \
20 BENCHMARK_EXPORT extern std::map<std::string, std::string> FLAG(name)
21// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)
22
23// Macros for defining flags.
24// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
25#define BM_DEFINE_bool(name, default_val) \
26 BENCHMARK_EXPORT bool FLAG(name) = benchmark::BoolFromEnv(#name, default_val)
27#define BM_DEFINE_int32(name, default_val) \
28 BENCHMARK_EXPORT int32_t FLAG(name) = \
29 benchmark::Int32FromEnv(#name, default_val)
30#define BM_DEFINE_double(name, default_val) \
31 BENCHMARK_EXPORT double FLAG(name) = \
32 benchmark::DoubleFromEnv(#name, default_val)
33#define BM_DEFINE_string(name, default_val) \
34 BENCHMARK_EXPORT std::string FLAG(name) = \
35 benchmark::StringFromEnv(#name, default_val)
36#define BM_DEFINE_kvpairs(name, default_val) \
37 BENCHMARK_EXPORT std::map<std::string, std::string> FLAG(name) = \
38 benchmark::KvPairsFromEnv(#name, default_val)
39// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)
40
41namespace benchmark {
42
43// Parses a bool from the environment variable corresponding to the given flag.
44//
45// If the variable exists, returns IsTruthyFlagValue() value; if not,
46// returns the given default value.
47BENCHMARK_EXPORT
48bool BoolFromEnv(const char* flag, bool default_val);
49
50// Parses an Int32 from the environment variable corresponding to the given
51// flag.
52//
53// If the variable exists, returns ParseInt32() value; if not, returns
54// the given default value.
55BENCHMARK_EXPORT
56int32_t Int32FromEnv(const char* flag, int32_t default_val);
57
58// Parses an Double from the environment variable corresponding to the given
59// flag.
60//
61// If the variable exists, returns ParseDouble(); if not, returns
62// the given default value.
63BENCHMARK_EXPORT
64double DoubleFromEnv(const char* flag, double default_val);
65
66// Parses a string from the environment variable corresponding to the given
67// flag.
68//
69// If variable exists, returns its value; if not, returns
70// the given default value.
71BENCHMARK_EXPORT
72const char* StringFromEnv(const char* flag, const char* default_val);
73
74// Parses a set of kvpairs from the environment variable corresponding to the
75// given flag.
76//
77// If variable exists, returns its value; if not, returns
78// the given default value.
79BENCHMARK_EXPORT
80std::map<std::string, std::string> KvPairsFromEnv(
81 const char* flag, std::map<std::string, std::string> default_val);
82
83// Parses a string for a bool flag, in the form of either
84// "--flag=value" or "--flag".
85//
86// In the former case, the value is taken as true if it passes IsTruthyValue().
87//
88// In the latter case, the value is taken as true.
89//
90// On success, stores the value of the flag in *value, and returns
91// true. On failure, returns false without changing *value.
92BENCHMARK_EXPORT
93bool ParseBoolFlag(const char* str, const char* flag, bool* value);
94
95// Parses a string for an Int32 flag, in the form of "--flag=value".
96//
97// On success, stores the value of the flag in *value, and returns
98// true. On failure, returns false without changing *value.
99BENCHMARK_EXPORT
100bool ParseInt32Flag(const char* str, const char* flag, int32_t* value);
101
102// Parses a string for a Double flag, in the form of "--flag=value".
103//
104// On success, stores the value of the flag in *value, and returns
105// true. On failure, returns false without changing *value.
106BENCHMARK_EXPORT
107bool ParseDoubleFlag(const char* str, const char* flag, double* value);
108
109// Parses a string for a string flag, in the form of "--flag=value".
110//
111// On success, stores the value of the flag in *value, and returns
112// true. On failure, returns false without changing *value.
113BENCHMARK_EXPORT
114bool ParseStringFlag(const char* str, const char* flag, std::string* value);
115
116// Parses a string for a kvpairs flag in the form "--flag=key=value,key=value"
117//
118// On success, stores the value of the flag in *value and returns true. On
119// failure returns false, though *value may have been mutated.
120BENCHMARK_EXPORT
121bool ParseKeyValueFlag(const char* str, const char* flag,
122 std::map<std::string, std::string>* value);
123
124// Returns true if the string matches the flag.
125BENCHMARK_EXPORT
126bool IsFlag(const char* str, const char* flag);
127
128// Returns true unless value starts with one of: '0', 'f', 'F', 'n' or 'N', or
129// some non-alphanumeric character. Also returns false if the value matches
130// one of 'no', 'false', 'off' (case-insensitive). As a special case, also
131// returns true if value is the empty string.
132BENCHMARK_EXPORT
133bool IsTruthyFlagValue(const std::string& value);
134
135} // end namespace benchmark
136
137#endif // BENCHMARK_COMMANDLINEFLAGS_H_