diff options
author | Shaun Verch <shaun.verch@10gen.com> | 2013-08-18 21:52:37 -0700 |
---|---|---|
committer | Shaun Verch <shaun.verch@10gen.com> | 2013-09-05 13:49:33 -0400 |
commit | 4ffd53218fc9d8ad63e34c7cfec4247da2d1bef7 (patch) | |
tree | 1999d23314d09c2f88799d1153f84ad16c80bb11 | |
parent | 5ee03db5cdc40ecc0a28aaad8031df177188ffee (diff) | |
download | mongo-4ffd53218fc9d8ad63e34c7cfec4247da2d1bef7.tar.gz |
SERVER-8510 Added better error messages for Value class and added underscores to member variables
-rw-r--r-- | src/mongo/util/options_parser/value.cpp | 128 | ||||
-rw-r--r-- | src/mongo/util/options_parser/value.h | 37 |
2 files changed, 95 insertions, 70 deletions
diff --git a/src/mongo/util/options_parser/value.cpp b/src/mongo/util/options_parser/value.cpp index 89ad7388a41..a4c46954e6b 100644 --- a/src/mongo/util/options_parser/value.cpp +++ b/src/mongo/util/options_parser/value.cpp @@ -25,74 +25,98 @@ namespace optionenvironment { // Value access functions Status Value::get(std::vector<std::string>* val) const { - if (type != StringVector) { - return Status(ErrorCodes::TypeMismatch, "Value not of type: stringVector"); + if (_type != StringVector) { + StringBuilder sb; + sb << "Attempting to get Value as type: StringVector, but Value is of type: " + << typeToString(); + return Status(ErrorCodes::TypeMismatch, sb.str()); } - *val = stringVectorVal; + *val = _stringVectorVal; return Status::OK(); } Status Value::get(bool* val) const { - if (type != Bool) { - return Status(ErrorCodes::TypeMismatch, "Value not of type: bool"); + if (_type != Bool) { + StringBuilder sb; + sb << "Attempting to get Value as type: Bool, but Value is of type: " + << typeToString(); + return Status(ErrorCodes::TypeMismatch, sb.str()); } - *val = boolVal; + *val = _boolVal; return Status::OK(); } Status Value::get(double* val) const { - if (type != Double) { - return Status(ErrorCodes::TypeMismatch, "Value not of type: double"); + if (_type != Double) { + StringBuilder sb; + sb << "Attempting to get Value as type: Double, but Value is of type: " + << typeToString(); + return Status(ErrorCodes::TypeMismatch, sb.str()); } - *val = doubleVal; + *val = _doubleVal; return Status::OK(); } Status Value::get(int* val) const { - if (type != Int) { - return Status(ErrorCodes::TypeMismatch, "Value not of type: int"); + if (_type != Int) { + StringBuilder sb; + sb << "Attempting to get Value as type: Int, but Value is of type: " + << typeToString(); + return Status(ErrorCodes::TypeMismatch, sb.str()); } - *val = intVal; + *val = _intVal; return Status::OK(); } Status Value::get(long* val) const { - if (type == Long) { - *val = longVal; + if (_type == Long) { + *val = _longVal; return Status::OK(); } - else if (type == Int) { - *val = intVal; + else if (_type == Int) { + *val = _intVal; return Status::OK(); } - return Status(ErrorCodes::TypeMismatch, "Value not convertible to type: long"); + StringBuilder sb; + sb << "Value of type: " << typeToString() + << " is not convertible to type: Long"; + return Status(ErrorCodes::TypeMismatch, sb.str()); } Status Value::get(std::string* val) const { - if (type != String) { - return Status(ErrorCodes::TypeMismatch, "Value not of type: string"); + if (_type != String) { + StringBuilder sb; + sb << "Attempting to get Value as type: string, but Value is of type: " + << typeToString(); + return Status(ErrorCodes::TypeMismatch, sb.str()); } - *val = stringVal; + *val = _stringVal; return Status::OK(); } Status Value::get(unsigned long long* val) const { - if (type == UnsignedLongLong) { - *val = unsignedLongLongVal; + if (_type == UnsignedLongLong) { + *val = _unsignedLongLongVal; return Status::OK(); } - else if (type == Unsigned) { - *val = unsignedVal; + else if (_type == Unsigned) { + *val = _unsignedVal; return Status::OK(); } - return Status(ErrorCodes::TypeMismatch, "Value not convertible to type: unsignedlonglong"); + StringBuilder sb; + sb << "Value of type: " << typeToString() + << " is not convertible to type: UnsignedLongLong"; + return Status(ErrorCodes::TypeMismatch, sb.str()); } Status Value::get(unsigned* val) const { - if (type != Unsigned) { - return Status(ErrorCodes::TypeMismatch, "Value not of type: unsigned"); + if (_type != Unsigned) { + StringBuilder sb; + sb << "Attempting to get Value as type: Unsigned, but Value is of type: " + << typeToString(); + return Status(ErrorCodes::TypeMismatch, sb.str()); } - *val = unsignedVal; + *val = _unsignedVal; return Status::OK(); } // Value utility functions std::string Value::typeToString() const { - switch (type) { + switch (_type) { case StringVector: return "StringVector"; case Bool: return "Bool"; case Double: return "Double"; @@ -106,48 +130,48 @@ namespace optionenvironment { } } bool Value::isEmpty() const { - return type == None; + return _type == None; } bool Value::equal(Value& otherVal) const { - if (type != otherVal.type) { + if (_type != otherVal._type) { return false; } - switch (type) { - case StringVector: return stringVectorVal == otherVal.stringVectorVal; - case Bool: return boolVal == otherVal.boolVal; - case Double: return doubleVal == otherVal.doubleVal; - case Int: return intVal == otherVal.intVal; - case Long: return longVal == otherVal.longVal; - case String: return stringVal == otherVal.stringVal; - case UnsignedLongLong: return unsignedLongLongVal == otherVal.unsignedLongLongVal; - case Unsigned: return unsignedVal == otherVal.unsignedVal; + switch (_type) { + case StringVector: return _stringVectorVal == otherVal._stringVectorVal; + case Bool: return _boolVal == otherVal._boolVal; + case Double: return _doubleVal == otherVal._doubleVal; + case Int: return _intVal == otherVal._intVal; + case Long: return _longVal == otherVal._longVal; + case String: return _stringVal == otherVal._stringVal; + case UnsignedLongLong: return _unsignedLongLongVal == otherVal._unsignedLongLongVal; + case Unsigned: return _unsignedVal == otherVal._unsignedVal; case None: return true; default: return false; /* Undefined */ } } std::string Value::toString() const { StringBuilder sb; - switch (type) { + switch (_type) { case StringVector: - if (!stringVectorVal.empty()) + if (!_stringVectorVal.empty()) { // Convert all but the last element to avoid a trailing "," - for(std::vector<std::string>::const_iterator iterator = stringVectorVal.begin(); - iterator != stringVectorVal.end() - 1; iterator++) { + for(std::vector<std::string>::const_iterator iterator = _stringVectorVal.begin(); + iterator != _stringVectorVal.end() - 1; iterator++) { sb << *iterator; } // Now add the last element with no delimiter - sb << stringVectorVal.back(); + sb << _stringVectorVal.back(); } break; - case Bool: sb << boolVal; break; - case Double: sb << doubleVal; break; - case Int: sb << intVal; break; - case Long: sb << longVal; break; - case String: sb << stringVal; break; - case UnsignedLongLong: sb << unsignedLongLongVal; break; - case Unsigned: sb << unsignedVal; break; + case Bool: sb << _boolVal; break; + case Double: sb << _doubleVal; break; + case Int: sb << _intVal; break; + case Long: sb << _longVal; break; + case String: sb << _stringVal; break; + case UnsignedLongLong: sb << _unsignedLongLongVal; break; + case Unsigned: sb << _unsignedVal; break; case None: sb << "(not set)"; break; default: sb << "(undefined)"; break; } diff --git a/src/mongo/util/options_parser/value.h b/src/mongo/util/options_parser/value.h index 5b0ab751e9a..3d50cc7a445 100644 --- a/src/mongo/util/options_parser/value.h +++ b/src/mongo/util/options_parser/value.h @@ -48,15 +48,16 @@ namespace optionenvironment { // Constructors - explicit Value() : type(None) { } - explicit Value(std::vector<std::string> val) : stringVectorVal(val), type(StringVector) { } - explicit Value(bool val) : boolVal(val), type(Bool) { } - explicit Value(double val) : doubleVal(val), type(Double) { } - explicit Value(int val) : intVal(val), type(Int) { } - explicit Value(long val) : longVal(val), type(Long) { } - explicit Value(std::string val) : stringVal(val), type(String) { } - explicit Value(unsigned long long val) : unsignedLongLongVal(val), type(UnsignedLongLong) {} - explicit Value(unsigned val) : unsignedVal(val), type(Unsigned) { } + explicit Value() : _type(None) { } + explicit Value(std::vector<std::string> val) : _stringVectorVal(val), _type(StringVector) {} + explicit Value(bool val) : _boolVal(val), _type(Bool) { } + explicit Value(double val) : _doubleVal(val), _type(Double) { } + explicit Value(int val) : _intVal(val), _type(Int) { } + explicit Value(long val) : _longVal(val), _type(Long) { } + explicit Value(std::string val) : _stringVal(val), _type(String) { } + explicit Value(unsigned long long val) : _unsignedLongLongVal(val), + _type(UnsignedLongLong) { } + explicit Value(unsigned val) : _unsignedVal(val), _type(Unsigned) { } // Access interface @@ -94,15 +95,15 @@ namespace optionenvironment { std::string toString() const; private: - std::vector<std::string> stringVectorVal; - std::string stringVal; + std::vector<std::string> _stringVectorVal; + std::string _stringVal; union { - bool boolVal; - double doubleVal; - int intVal; - long longVal; - unsigned long long unsignedLongLongVal; - unsigned unsignedVal; + bool _boolVal; + double _doubleVal; + int _intVal; + long _longVal; + unsigned long long _unsignedLongLongVal; + unsigned _unsignedVal; }; // Types currently supported by Value @@ -118,7 +119,7 @@ namespace optionenvironment { None, // (not set) }; - Type type; + Type _type; }; } // namespace optionenvironment |