2013-12-19 08:55:45 +08:00
|
|
|
#ifndef BENCHMARK_COMMANDLINEFLAGS_H_
|
|
|
|
#define BENCHMARK_COMMANDLINEFLAGS_H_
|
|
|
|
|
2015-03-07 06:01:05 +08:00
|
|
|
#include <cstdint>
|
2021-05-04 21:36:11 +08:00
|
|
|
#include <map>
|
2013-12-19 08:55:45 +08:00
|
|
|
#include <string>
|
|
|
|
|
2022-02-14 18:48:53 +08:00
|
|
|
#include "benchmark/export.h"
|
|
|
|
|
2013-12-19 08:55:45 +08:00
|
|
|
// Macro for referencing flags.
|
|
|
|
#define FLAG(name) FLAGS_##name
|
|
|
|
|
|
|
|
// Macros for declaring flags.
|
2022-02-14 18:48:53 +08:00
|
|
|
#define BM_DECLARE_bool(name) BENCHMARK_EXPORT extern bool FLAG(name)
|
|
|
|
#define BM_DECLARE_int32(name) BENCHMARK_EXPORT extern int32_t FLAG(name)
|
|
|
|
#define BM_DECLARE_double(name) BENCHMARK_EXPORT extern double FLAG(name)
|
|
|
|
#define BM_DECLARE_string(name) BENCHMARK_EXPORT extern std::string FLAG(name)
|
2021-06-25 01:21:59 +08:00
|
|
|
#define BM_DECLARE_kvpairs(name) \
|
2022-02-14 18:48:53 +08:00
|
|
|
BENCHMARK_EXPORT extern std::map<std::string, std::string> FLAG(name)
|
2013-12-19 08:55:45 +08:00
|
|
|
|
|
|
|
// Macros for defining flags.
|
2021-06-25 01:21:59 +08:00
|
|
|
#define BM_DEFINE_bool(name, default_val) \
|
2022-02-14 18:48:53 +08:00
|
|
|
BENCHMARK_EXPORT bool FLAG(name) = benchmark::BoolFromEnv(#name, default_val)
|
2021-06-25 01:21:59 +08:00
|
|
|
#define BM_DEFINE_int32(name, default_val) \
|
2022-02-14 18:48:53 +08:00
|
|
|
BENCHMARK_EXPORT int32_t FLAG(name) = \
|
|
|
|
benchmark::Int32FromEnv(#name, default_val)
|
2021-06-25 01:21:59 +08:00
|
|
|
#define BM_DEFINE_double(name, default_val) \
|
2022-02-14 18:48:53 +08:00
|
|
|
BENCHMARK_EXPORT double FLAG(name) = \
|
|
|
|
benchmark::DoubleFromEnv(#name, default_val)
|
2021-06-25 01:21:59 +08:00
|
|
|
#define BM_DEFINE_string(name, default_val) \
|
2022-02-14 18:48:53 +08:00
|
|
|
BENCHMARK_EXPORT std::string FLAG(name) = \
|
|
|
|
benchmark::StringFromEnv(#name, default_val)
|
|
|
|
#define BM_DEFINE_kvpairs(name, default_val) \
|
|
|
|
BENCHMARK_EXPORT std::map<std::string, std::string> FLAG(name) = \
|
2021-05-04 21:36:11 +08:00
|
|
|
benchmark::KvPairsFromEnv(#name, default_val)
|
2013-12-19 08:55:45 +08:00
|
|
|
|
|
|
|
namespace benchmark {
|
2019-10-23 16:07:08 +08:00
|
|
|
|
2021-05-04 21:36:11 +08:00
|
|
|
// Parses a bool from the environment variable corresponding to the given flag.
|
2019-10-23 16:07:08 +08:00
|
|
|
//
|
|
|
|
// If the variable exists, returns IsTruthyFlagValue() value; if not,
|
|
|
|
// returns the given default value.
|
2022-02-14 18:48:53 +08:00
|
|
|
BENCHMARK_EXPORT
|
2013-12-19 08:55:45 +08:00
|
|
|
bool BoolFromEnv(const char* flag, bool default_val);
|
2019-10-23 16:07:08 +08:00
|
|
|
|
2021-05-04 21:36:11 +08:00
|
|
|
// Parses an Int32 from the environment variable corresponding to the given
|
|
|
|
// flag.
|
2019-10-23 16:07:08 +08:00
|
|
|
//
|
|
|
|
// If the variable exists, returns ParseInt32() value; if not, returns
|
|
|
|
// the given default value.
|
2022-02-14 18:48:53 +08:00
|
|
|
BENCHMARK_EXPORT
|
2013-12-19 08:55:45 +08:00
|
|
|
int32_t Int32FromEnv(const char* flag, int32_t default_val);
|
2019-10-23 16:07:08 +08:00
|
|
|
|
2021-05-04 21:36:11 +08:00
|
|
|
// Parses an Double from the environment variable corresponding to the given
|
|
|
|
// flag.
|
2019-10-23 16:07:08 +08:00
|
|
|
//
|
|
|
|
// If the variable exists, returns ParseDouble(); if not, returns
|
|
|
|
// the given default value.
|
2022-02-14 18:48:53 +08:00
|
|
|
BENCHMARK_EXPORT
|
2019-10-23 16:07:08 +08:00
|
|
|
double DoubleFromEnv(const char* flag, double default_val);
|
|
|
|
|
2021-05-04 21:36:11 +08:00
|
|
|
// Parses a string from the environment variable corresponding to the given
|
|
|
|
// flag.
|
2019-10-23 16:07:08 +08:00
|
|
|
//
|
|
|
|
// If variable exists, returns its value; if not, returns
|
|
|
|
// the given default value.
|
2022-02-14 18:48:53 +08:00
|
|
|
BENCHMARK_EXPORT
|
2013-12-19 08:55:45 +08:00
|
|
|
const char* StringFromEnv(const char* flag, const char* default_val);
|
|
|
|
|
2021-05-04 21:36:11 +08:00
|
|
|
// Parses a set of kvpairs from the environment variable corresponding to the
|
|
|
|
// given flag.
|
|
|
|
//
|
|
|
|
// If variable exists, returns its value; if not, returns
|
|
|
|
// the given default value.
|
2022-02-14 18:48:53 +08:00
|
|
|
BENCHMARK_EXPORT
|
2021-05-04 21:36:11 +08:00
|
|
|
std::map<std::string, std::string> KvPairsFromEnv(
|
|
|
|
const char* flag, std::map<std::string, std::string> default_val);
|
|
|
|
|
2013-12-19 08:55:45 +08:00
|
|
|
// Parses a string for a bool flag, in the form of either
|
|
|
|
// "--flag=value" or "--flag".
|
|
|
|
//
|
2016-09-16 05:10:35 +08:00
|
|
|
// In the former case, the value is taken as true if it passes IsTruthyValue().
|
2013-12-19 08:55:45 +08:00
|
|
|
//
|
|
|
|
// In the latter case, the value is taken as true.
|
|
|
|
//
|
|
|
|
// On success, stores the value of the flag in *value, and returns
|
|
|
|
// true. On failure, returns false without changing *value.
|
2022-02-14 18:48:53 +08:00
|
|
|
BENCHMARK_EXPORT
|
2013-12-19 08:55:45 +08:00
|
|
|
bool ParseBoolFlag(const char* str, const char* flag, bool* value);
|
|
|
|
|
2021-05-04 21:36:11 +08:00
|
|
|
// Parses a string for an Int32 flag, in the form of "--flag=value".
|
2013-12-19 08:55:45 +08:00
|
|
|
//
|
|
|
|
// On success, stores the value of the flag in *value, and returns
|
|
|
|
// true. On failure, returns false without changing *value.
|
2022-02-14 18:48:53 +08:00
|
|
|
BENCHMARK_EXPORT
|
2013-12-19 08:55:45 +08:00
|
|
|
bool ParseInt32Flag(const char* str, const char* flag, int32_t* value);
|
|
|
|
|
2021-05-04 21:36:11 +08:00
|
|
|
// Parses a string for a Double flag, in the form of "--flag=value".
|
2013-12-19 08:55:45 +08:00
|
|
|
//
|
|
|
|
// On success, stores the value of the flag in *value, and returns
|
|
|
|
// true. On failure, returns false without changing *value.
|
2022-02-14 18:48:53 +08:00
|
|
|
BENCHMARK_EXPORT
|
2013-12-19 08:55:45 +08:00
|
|
|
bool ParseDoubleFlag(const char* str, const char* flag, double* value);
|
|
|
|
|
2021-05-04 21:36:11 +08:00
|
|
|
// Parses a string for a string flag, in the form of "--flag=value".
|
2013-12-19 08:55:45 +08:00
|
|
|
//
|
|
|
|
// On success, stores the value of the flag in *value, and returns
|
|
|
|
// true. On failure, returns false without changing *value.
|
2022-02-14 18:48:53 +08:00
|
|
|
BENCHMARK_EXPORT
|
2013-12-19 08:55:45 +08:00
|
|
|
bool ParseStringFlag(const char* str, const char* flag, std::string* value);
|
|
|
|
|
2021-05-04 21:36:11 +08:00
|
|
|
// Parses a string for a kvpairs flag in the form "--flag=key=value,key=value"
|
|
|
|
//
|
|
|
|
// On success, stores the value of the flag in *value and returns true. On
|
|
|
|
// failure returns false, though *value may have been mutated.
|
2022-02-14 18:48:53 +08:00
|
|
|
BENCHMARK_EXPORT
|
2021-05-04 21:36:11 +08:00
|
|
|
bool ParseKeyValueFlag(const char* str, const char* flag,
|
|
|
|
std::map<std::string, std::string>* value);
|
|
|
|
|
2013-12-19 08:55:45 +08:00
|
|
|
// Returns true if the string matches the flag.
|
2022-02-14 18:48:53 +08:00
|
|
|
BENCHMARK_EXPORT
|
2013-12-19 08:55:45 +08:00
|
|
|
bool IsFlag(const char* str, const char* flag);
|
|
|
|
|
2016-09-16 05:10:35 +08:00
|
|
|
// Returns true unless value starts with one of: '0', 'f', 'F', 'n' or 'N', or
|
2019-10-23 16:07:08 +08:00
|
|
|
// some non-alphanumeric character. Also returns false if the value matches
|
|
|
|
// one of 'no', 'false', 'off' (case-insensitive). As a special case, also
|
|
|
|
// returns true if value is the empty string.
|
2022-02-14 18:48:53 +08:00
|
|
|
BENCHMARK_EXPORT
|
2016-09-16 05:10:35 +08:00
|
|
|
bool IsTruthyFlagValue(const std::string& value);
|
2019-10-23 16:07:08 +08:00
|
|
|
|
2015-03-07 06:01:05 +08:00
|
|
|
} // end namespace benchmark
|
2013-12-19 08:55:45 +08:00
|
|
|
|
|
|
|
#endif // BENCHMARK_COMMANDLINEFLAGS_H_
|