Commit a1e461d6 authored by dreamer.dead's avatar dreamer.dead

Change template FlagValue::OfType() to Type() getter.

parent 46ea10f0
...@@ -191,6 +191,17 @@ static void ReportError(DieWhenReporting should_die, const char* format, ...) { ...@@ -191,6 +191,17 @@ static void ReportError(DieWhenReporting should_die, const char* format, ...) {
class CommandLineFlag; class CommandLineFlag;
class FlagValue { class FlagValue {
public: public:
enum ValueType {
FV_BOOL = 0,
FV_INT32 = 1,
FV_UINT32 = 2,
FV_INT64 = 3,
FV_UINT64 = 4,
FV_DOUBLE = 5,
FV_STRING = 6,
FV_MAX_INDEX = 6,
};
template <typename FlagType> template <typename FlagType>
FlagValue(FlagType* valbuf, bool transfer_ownership_of_value); FlagValue(FlagType* valbuf, bool transfer_ownership_of_value);
~FlagValue(); ~FlagValue();
...@@ -198,8 +209,7 @@ class FlagValue { ...@@ -198,8 +209,7 @@ class FlagValue {
bool ParseFrom(const char* spec); bool ParseFrom(const char* spec);
string ToString() const; string ToString() const;
template <typename FlagType> ValueType Type() const { return static_cast<ValueType>(type_); }
bool OfType() const { return type_ == FlagValueTraits<FlagType>::kValueType; }
private: private:
friend class CommandLineFlag; // for many things, including Validate() friend class CommandLineFlag; // for many things, including Validate()
...@@ -209,17 +219,6 @@ class FlagValue { ...@@ -209,17 +219,6 @@ class FlagValue {
friend bool TryParseLocked(const CommandLineFlag*, FlagValue*, friend bool TryParseLocked(const CommandLineFlag*, FlagValue*,
const char*, string*); // for New(), CopyFrom() const char*, string*); // for New(), CopyFrom()
enum ValueType {
FV_BOOL = 0,
FV_INT32 = 1,
FV_UINT32 = 2,
FV_INT64 = 3,
FV_UINT64 = 4,
FV_DOUBLE = 5,
FV_STRING = 6,
FV_MAX_INDEX = 6,
};
template <typename FlagType> template <typename FlagType>
struct FlagValueTraits; struct FlagValueTraits;
...@@ -533,8 +532,7 @@ class CommandLineFlag { ...@@ -533,8 +532,7 @@ class CommandLineFlag {
ValidateFnProto validate_function() const { return validate_fn_proto_; } ValidateFnProto validate_function() const { return validate_fn_proto_; }
const void* flag_ptr() const { return current_->value_buffer_; } const void* flag_ptr() const { return current_->value_buffer_; }
template <typename FlagType> FlagValue::ValueType Type() const { return defvalue_->Type(); }
bool OfType() const { return defvalue_->OfType<FlagType>(); }
void FillCommandLineFlagInfo(struct CommandLineFlagInfo* result); void FillCommandLineFlagInfo(struct CommandLineFlagInfo* result);
...@@ -826,7 +824,7 @@ CommandLineFlag* FlagRegistry::SplitArgumentLocked(const char* arg, ...@@ -826,7 +824,7 @@ CommandLineFlag* FlagRegistry::SplitArgumentLocked(const char* arg,
kError, key->c_str()); kError, key->c_str());
return NULL; return NULL;
} }
if (!flag->OfType<bool>()) { if (flag->Type() != FlagValue::FV_BOOL) {
// 'x' exists but is not boolean, so we're not in the exception case. // 'x' exists but is not boolean, so we're not in the exception case.
*error_message = StringPrintf( *error_message = StringPrintf(
"%sboolean value (%s) specified for %s command line flag\n", "%sboolean value (%s) specified for %s command line flag\n",
...@@ -840,7 +838,7 @@ CommandLineFlag* FlagRegistry::SplitArgumentLocked(const char* arg, ...@@ -840,7 +838,7 @@ CommandLineFlag* FlagRegistry::SplitArgumentLocked(const char* arg,
} }
// Assign a value if this is a boolean flag // Assign a value if this is a boolean flag
if (*v == NULL && flag->OfType<bool>()) { if (*v == NULL && flag->Type() == FlagValue::FV_BOOL) {
*v = "1"; // the --nox case was already handled, so this is the --x case *v = "1"; // the --nox case was already handled, so this is the --x case
} }
...@@ -1124,7 +1122,7 @@ uint32 CommandLineFlagParser::ParseNewCommandLineFlags(int* argc, char*** argv, ...@@ -1124,7 +1122,7 @@ uint32 CommandLineFlagParser::ParseNewCommandLineFlags(int* argc, char*** argv,
// "-lat -30.5" would trigger the warning. The common cases we // "-lat -30.5" would trigger the warning. The common cases we
// want to solve talk about true and false as values. // want to solve talk about true and false as values.
if (value[0] == '-' if (value[0] == '-'
&& flag->OfType<string>() && flag->Type() != FlagValue::FV_STRING
&& (strstr(flag->help(), "true") && (strstr(flag->help(), "true")
|| strstr(flag->help(), "false"))) { || strstr(flag->help(), "false"))) {
LOG(WARNING) << "Did you really mean to set flag '" LOG(WARNING) << "Did you really mean to set flag '"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment