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