134 lines · c
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_##name12 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 37namespace 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.43BENCHMARK_EXPORT44bool BoolFromEnv(const char* flag, bool default_val);45 46// Parses an Int32 from the environment variable corresponding to the given47// flag.48//49// If the variable exists, returns ParseInt32() value; if not, returns50// the given default value.51BENCHMARK_EXPORT52int32_t Int32FromEnv(const char* flag, int32_t default_val);53 54// Parses an Double from the environment variable corresponding to the given55// flag.56//57// If the variable exists, returns ParseDouble(); if not, returns58// the given default value.59BENCHMARK_EXPORT60double DoubleFromEnv(const char* flag, double default_val);61 62// Parses a string from the environment variable corresponding to the given63// flag.64//65// If variable exists, returns its value; if not, returns66// the given default value.67BENCHMARK_EXPORT68const char* StringFromEnv(const char* flag, const char* default_val);69 70// Parses a set of kvpairs from the environment variable corresponding to the71// given flag.72//73// If variable exists, returns its value; if not, returns74// the given default value.75BENCHMARK_EXPORT76std::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 either80// "--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 returns87// true. On failure, returns false without changing *value.88BENCHMARK_EXPORT89bool 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 returns94// true. On failure, returns false without changing *value.95BENCHMARK_EXPORT96bool 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 returns101// true. On failure, returns false without changing *value.102BENCHMARK_EXPORT103bool 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 returns108// true. On failure, returns false without changing *value.109BENCHMARK_EXPORT110bool 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. On115// failure returns false, though *value may have been mutated.116BENCHMARK_EXPORT117bool 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.121BENCHMARK_EXPORT122bool IsFlag(const char* str, const char* flag);123 124// Returns true unless value starts with one of: '0', 'f', 'F', 'n' or 'N', or125// some non-alphanumeric character. Also returns false if the value matches126// one of 'no', 'false', 'off' (case-insensitive). As a special case, also127// returns true if value is the empty string.128BENCHMARK_EXPORT129bool IsTruthyFlagValue(const std::string& value);130 131} // end namespace benchmark132 133#endif // BENCHMARK_COMMANDLINEFLAGS_H_134