Home | History | Annotate | Download | only in src
      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_int64(name) extern int64_t FLAG(name)
     14 #define DECLARE_double(name) extern double FLAG(name)
     15 #define DECLARE_string(name) extern std::string FLAG(name)
     16 
     17 // Macros for defining flags.
     18 #define DEFINE_bool(name, default_val, doc) bool FLAG(name) = (default_val)
     19 #define DEFINE_int32(name, default_val, doc) int32_t FLAG(name) = (default_val)
     20 #define DEFINE_int64(name, default_val, doc) int64_t FLAG(name) = (default_val)
     21 #define DEFINE_double(name, default_val, doc) double FLAG(name) = (default_val)
     22 #define DEFINE_string(name, default_val, doc) \
     23   std::string FLAG(name) = (default_val)
     24 
     25 namespace benchmark {
     26 // Parses 'str' for a 32-bit signed integer.  If successful, writes the result
     27 // to *value and returns true; otherwise leaves *value unchanged and returns
     28 // false.
     29 bool ParseInt32(const std::string& src_text, const char* str, int32_t* value);
     30 
     31 // Parses a bool/Int32/string from the environment variable
     32 // corresponding to the given Google Test flag.
     33 bool BoolFromEnv(const char* flag, bool default_val);
     34 int32_t Int32FromEnv(const char* flag, int32_t default_val);
     35 double DoubleFromEnv(const char* flag, double default_val);
     36 const char* StringFromEnv(const char* flag, const char* default_val);
     37 
     38 // Parses a string for a bool flag, in the form of either
     39 // "--flag=value" or "--flag".
     40 //
     41 // In the former case, the value is taken as true if it passes IsTruthyValue().
     42 //
     43 // In the latter case, the value is taken as true.
     44 //
     45 // On success, stores the value of the flag in *value, and returns
     46 // true.  On failure, returns false without changing *value.
     47 bool ParseBoolFlag(const char* str, const char* flag, bool* value);
     48 
     49 // Parses a string for an Int32 flag, in the form of
     50 // "--flag=value".
     51 //
     52 // On success, stores the value of the flag in *value, and returns
     53 // true.  On failure, returns false without changing *value.
     54 bool ParseInt32Flag(const char* str, const char* flag, int32_t* value);
     55 
     56 // Parses a string for a Double flag, in the form of
     57 // "--flag=value".
     58 //
     59 // On success, stores the value of the flag in *value, and returns
     60 // true.  On failure, returns false without changing *value.
     61 bool ParseDoubleFlag(const char* str, const char* flag, double* value);
     62 
     63 // Parses a string for a string flag, in the form of
     64 // "--flag=value".
     65 //
     66 // On success, stores the value of the flag in *value, and returns
     67 // true.  On failure, returns false without changing *value.
     68 bool ParseStringFlag(const char* str, const char* flag, std::string* value);
     69 
     70 // Returns true if the string matches the flag.
     71 bool IsFlag(const char* str, const char* flag);
     72 
     73 // Returns true unless value starts with one of: '0', 'f', 'F', 'n' or 'N', or
     74 // some non-alphanumeric character. As a special case, also returns true if
     75 // value is the empty string.
     76 bool IsTruthyFlagValue(const std::string& value);
     77 }  // end namespace benchmark
     78 
     79 #endif  // BENCHMARK_COMMANDLINEFLAGS_H_
     80