Changing 'enum' to 'enum class'

Fix for Visual Studio warning - Warning C26812: Enum type is unscoped. Prefer enum class over enum

Signed-off-by: ganeshkamath89 <ganeshkamath89@gmail.com>
This commit is contained in:
gakamath 2022-12-28 11:37:16 +05:30
parent 8190e061bc
commit 6a304af819
5 changed files with 442 additions and 416 deletions

View File

@ -156,7 +156,7 @@ public:
bool good() const;
private:
enum TokenType {
enum class TokenType {
tokenEndOfStream = 0,
tokenObjectBegin,
tokenObjectEnd,

View File

@ -105,7 +105,7 @@ JSONCPP_NORETURN void throwLogicError(String const& msg);
/** \brief Type of the value held by a Value object.
*/
enum ValueType {
enum class ValueType {
nullValue = 0, ///< 'null' value
intValue, ///< signed integer value
uintValue, ///< unsigned integer value
@ -116,7 +116,7 @@ enum ValueType {
objectValue ///< object value (collection of name/value pairs).
};
enum CommentPlacement {
enum class CommentPlacement {
commentBefore = 0, ///< a comment placed on the line before a value
commentAfterOnSameLine, ///< a comment just after a value on the same line
commentAfter, ///< a comment on the line after a value (only make sense for
@ -126,7 +126,7 @@ enum CommentPlacement {
/** \brief Type of precision for formatting of real values.
*/
enum PrecisionType {
enum class PrecisionType {
significantDigits = 0, ///< we set max number of significant digits in string
decimalPlaces ///< we set max number of digits after "." in string
};
@ -260,7 +260,11 @@ private:
#ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
class CZString {
public:
enum DuplicationPolicy { noDuplication = 0, duplicate, duplicateOnCopy };
enum class DuplicationPolicy {
noDuplication = 0,
duplicate,
duplicateOnCopy
};
CZString(ArrayIndex index);
CZString(char const* str, unsigned length, DuplicationPolicy allocate);
CZString(CZString const& other);
@ -313,7 +317,7 @@ public:
* Json::Value obj_value(Json::objectValue); // {}
* \endcode
*/
Value(ValueType type = nullValue);
Value(ValueType type = ValueType::nullValue);
Value(Int value);
Value(UInt value);
#if defined(JSON_HAS_INT64)
@ -644,7 +648,7 @@ private:
void set(CommentPlacement slot, String comment);
private:
using Array = std::array<String, numberOfCommentPlacement>;
using Array = std::array<String, (size_t)(CommentPlacement::numberOfCommentPlacement)>;
std::unique_ptr<Array> ptr_;
};
Comments comments_;
@ -698,10 +702,14 @@ public:
PathArgument(String key);
private:
enum Kind { kindNone = 0, kindIndex, kindKey };
enum class Kind {
kindNone = 0,
kindIndex,
kindKey
};
String key_;
ArrayIndex index_{};
Kind kind_{kindNone};
Kind kind_{Kind::kindNone};
};
/** \brief Experimental and untested: represents a "path" to access a node.

View File

@ -131,12 +131,12 @@ bool Reader::parse(const char* beginDoc, const char* endDoc, Value& root,
Token token;
skipCommentTokens(token);
if (collectComments_ && !commentsBefore_.empty())
root.setComment(commentsBefore_, commentAfter);
root.setComment(commentsBefore_, CommentPlacement::commentAfter);
if (features_.strictRoot_) {
if (!root.isArray() && !root.isObject()) {
// Set error location to start of doc, ideally should be first token found
// in doc
token.type_ = tokenError;
token.type_ = TokenType::tokenError;
token.start_ = beginDoc;
token.end_ = endDoc;
addError(
@ -161,46 +161,46 @@ bool Reader::readValue() {
bool successful = true;
if (collectComments_ && !commentsBefore_.empty()) {
currentValue().setComment(commentsBefore_, commentBefore);
currentValue().setComment(commentsBefore_, CommentPlacement::commentBefore);
commentsBefore_.clear();
}
switch (token.type_) {
case tokenObjectBegin:
case TokenType::tokenObjectBegin:
successful = readObject(token);
currentValue().setOffsetLimit(current_ - begin_);
break;
case tokenArrayBegin:
case TokenType::tokenArrayBegin:
successful = readArray(token);
currentValue().setOffsetLimit(current_ - begin_);
break;
case tokenNumber:
case TokenType::tokenNumber:
successful = decodeNumber(token);
break;
case tokenString:
case TokenType::tokenString:
successful = decodeString(token);
break;
case tokenTrue: {
case TokenType::tokenTrue: {
Value v(true);
currentValue().swapPayload(v);
currentValue().setOffsetStart(token.start_ - begin_);
currentValue().setOffsetLimit(token.end_ - begin_);
} break;
case tokenFalse: {
case TokenType::tokenFalse: {
Value v(false);
currentValue().swapPayload(v);
currentValue().setOffsetStart(token.start_ - begin_);
currentValue().setOffsetLimit(token.end_ - begin_);
} break;
case tokenNull: {
case TokenType::tokenNull: {
Value v;
currentValue().swapPayload(v);
currentValue().setOffsetStart(token.start_ - begin_);
currentValue().setOffsetLimit(token.end_ - begin_);
} break;
case tokenArraySeparator:
case tokenObjectEnd:
case tokenArrayEnd:
case TokenType::tokenArraySeparator:
case TokenType::tokenObjectEnd:
case TokenType::tokenArrayEnd:
if (features_.allowDroppedNullPlaceholders_) {
// "Un-read" the current token and mark the current value as a null
// token.
@ -210,7 +210,8 @@ bool Reader::readValue() {
currentValue().setOffsetStart(current_ - begin_ - 1);
currentValue().setOffsetLimit(current_ - begin_);
break;
} // Else, fall through...
}
[[fallthrough]]; // Else, fall through...
default:
currentValue().setOffsetStart(token.start_ - begin_);
currentValue().setOffsetLimit(token.end_ - begin_);
@ -229,7 +230,7 @@ void Reader::skipCommentTokens(Token& token) {
if (features_.allowComments_) {
do {
readToken(token);
} while (token.type_ == tokenComment);
} while (token.type_ == TokenType::tokenComment);
} else {
readToken(token);
}
@ -242,23 +243,23 @@ bool Reader::readToken(Token& token) {
bool ok = true;
switch (c) {
case '{':
token.type_ = tokenObjectBegin;
token.type_ = TokenType::tokenObjectBegin;
break;
case '}':
token.type_ = tokenObjectEnd;
token.type_ = TokenType::tokenObjectEnd;
break;
case '[':
token.type_ = tokenArrayBegin;
token.type_ = TokenType::tokenArrayBegin;
break;
case ']':
token.type_ = tokenArrayEnd;
token.type_ = TokenType::tokenArrayEnd;
break;
case '"':
token.type_ = tokenString;
token.type_ = TokenType::tokenString;
ok = readString();
break;
case '/':
token.type_ = tokenComment;
token.type_ = TokenType::tokenComment;
ok = readComment();
break;
case '0':
@ -272,36 +273,36 @@ bool Reader::readToken(Token& token) {
case '8':
case '9':
case '-':
token.type_ = tokenNumber;
token.type_ = TokenType::tokenNumber;
readNumber();
break;
case 't':
token.type_ = tokenTrue;
token.type_ = TokenType::tokenTrue;
ok = match("rue", 3);
break;
case 'f':
token.type_ = tokenFalse;
token.type_ = TokenType::tokenFalse;
ok = match("alse", 4);
break;
case 'n':
token.type_ = tokenNull;
token.type_ = TokenType::tokenNull;
ok = match("ull", 3);
break;
case ',':
token.type_ = tokenArraySeparator;
token.type_ = TokenType::tokenArraySeparator;
break;
case ':':
token.type_ = tokenMemberSeparator;
token.type_ = TokenType::tokenMemberSeparator;
break;
case 0:
token.type_ = tokenEndOfStream;
token.type_ = TokenType::tokenEndOfStream;
break;
default:
ok = false;
break;
}
if (!ok)
token.type_ = tokenError;
token.type_ = TokenType::tokenError;
token.end_ = current_;
return ok;
}
@ -339,10 +340,10 @@ bool Reader::readComment() {
return false;
if (collectComments_) {
CommentPlacement placement = commentBefore;
CommentPlacement placement = CommentPlacement::commentBefore;
if (lastValueEnd_ && !containsNewLine(lastValueEnd_, commentBegin)) {
if (c != '*' || !containsNewLine(commentBegin, current_))
placement = commentAfterOnSameLine;
placement = CommentPlacement::commentAfterOnSameLine;
}
addComment(commentBegin, current_, placement);
@ -373,7 +374,7 @@ void Reader::addComment(Location begin, Location end,
CommentPlacement placement) {
assert(collectComments_);
const String& normalized = normalizeEOL(begin, end);
if (placement == commentAfterOnSameLine) {
if (placement == CommentPlacement::commentAfterOnSameLine) {
assert(lastValue_ != nullptr);
lastValue_->setComment(normalized, placement);
} else {
@ -443,61 +444,61 @@ bool Reader::readString() {
bool Reader::readObject(Token& token) {
Token tokenName;
String name;
Value init(objectValue);
Value init(ValueType::objectValue);
currentValue().swapPayload(init);
currentValue().setOffsetStart(token.start_ - begin_);
while (readToken(tokenName)) {
bool initialTokenOk = true;
while (tokenName.type_ == tokenComment && initialTokenOk)
while (tokenName.type_ == TokenType::tokenComment && initialTokenOk)
initialTokenOk = readToken(tokenName);
if (!initialTokenOk)
break;
if (tokenName.type_ == tokenObjectEnd && name.empty()) // empty object
if (tokenName.type_ == TokenType::tokenObjectEnd && name.empty()) // empty object
return true;
name.clear();
if (tokenName.type_ == tokenString) {
if (tokenName.type_ == TokenType::tokenString) {
if (!decodeString(tokenName, name))
return recoverFromError(tokenObjectEnd);
} else if (tokenName.type_ == tokenNumber && features_.allowNumericKeys_) {
return recoverFromError(TokenType::tokenObjectEnd);
} else if (tokenName.type_ == TokenType::tokenNumber && features_.allowNumericKeys_) {
Value numberName;
if (!decodeNumber(tokenName, numberName))
return recoverFromError(tokenObjectEnd);
return recoverFromError(TokenType::tokenObjectEnd);
name = numberName.asString();
} else {
break;
}
Token colon;
if (!readToken(colon) || colon.type_ != tokenMemberSeparator) {
if (!readToken(colon) || colon.type_ != TokenType::tokenMemberSeparator) {
return addErrorAndRecover("Missing ':' after object member name", colon,
tokenObjectEnd);
TokenType::tokenObjectEnd);
}
Value& value = currentValue()[name];
nodes_.push(&value);
bool ok = readValue();
nodes_.pop();
if (!ok) // error already set
return recoverFromError(tokenObjectEnd);
return recoverFromError(TokenType::tokenObjectEnd);
Token comma;
if (!readToken(comma) ||
(comma.type_ != tokenObjectEnd && comma.type_ != tokenArraySeparator &&
comma.type_ != tokenComment)) {
(comma.type_ != TokenType::tokenObjectEnd && comma.type_ != TokenType::tokenArraySeparator &&
comma.type_ != TokenType::tokenComment)) {
return addErrorAndRecover("Missing ',' or '}' in object declaration",
comma, tokenObjectEnd);
comma, TokenType::tokenObjectEnd);
}
bool finalizeTokenOk = true;
while (comma.type_ == tokenComment && finalizeTokenOk)
while (comma.type_ == TokenType::tokenComment && finalizeTokenOk)
finalizeTokenOk = readToken(comma);
if (comma.type_ == tokenObjectEnd)
if (comma.type_ == TokenType::tokenObjectEnd)
return true;
}
return addErrorAndRecover("Missing '}' or object member name", tokenName,
tokenObjectEnd);
TokenType::tokenObjectEnd);
}
bool Reader::readArray(Token& token) {
Value init(arrayValue);
Value init(ValueType::arrayValue);
currentValue().swapPayload(init);
currentValue().setOffsetStart(token.start_ - begin_);
skipSpaces();
@ -514,21 +515,21 @@ bool Reader::readArray(Token& token) {
bool ok = readValue();
nodes_.pop();
if (!ok) // error already set
return recoverFromError(tokenArrayEnd);
return recoverFromError(TokenType::tokenArrayEnd);
Token currentToken;
// Accept Comment after last item in the array.
ok = readToken(currentToken);
while (currentToken.type_ == tokenComment && ok) {
while (currentToken.type_ == TokenType::tokenComment && ok) {
ok = readToken(currentToken);
}
bool badTokenType = (currentToken.type_ != tokenArraySeparator &&
currentToken.type_ != tokenArrayEnd);
bool badTokenType = (currentToken.type_ != TokenType::tokenArraySeparator &&
currentToken.type_ != TokenType::tokenArrayEnd);
if (!ok || badTokenType) {
return addErrorAndRecover("Missing ',' or ']' in array declaration",
currentToken, tokenArrayEnd);
currentToken, TokenType::tokenArrayEnd);
}
if (currentToken.type_ == tokenArrayEnd)
if (currentToken.type_ == TokenType::tokenArrayEnd)
break;
}
return true;
@ -744,7 +745,7 @@ bool Reader::recoverFromError(TokenType skipUntilToken) {
for (;;) {
if (!readToken(skip))
errors_.resize(errorCount); // discard errors caused by recovery
if (skip.type_ == skipUntilToken || skip.type_ == tokenEndOfStream)
if (skip.type_ == skipUntilToken || skip.type_ == TokenType::tokenEndOfStream)
break;
}
errors_.resize(errorCount);
@ -830,7 +831,7 @@ bool Reader::pushError(const Value& value, const String& message) {
if (value.getOffsetStart() > length || value.getOffsetLimit() > length)
return false;
Token token;
token.type_ = tokenError;
token.type_ = TokenType::tokenError;
token.start_ = begin_ + value.getOffsetStart();
token.end_ = begin_ + value.getOffsetLimit();
ErrorInfo info;
@ -848,7 +849,7 @@ bool Reader::pushError(const Value& value, const String& message,
extra.getOffsetLimit() > length)
return false;
Token token;
token.type_ = tokenError;
token.type_ = TokenType::tokenError;
token.start_ = begin_ + value.getOffsetStart();
token.end_ = begin_ + value.getOffsetLimit();
ErrorInfo info;
@ -906,7 +907,7 @@ private:
OurReader(OurReader const&); // no impl
void operator=(OurReader const&); // no impl
enum TokenType {
enum class TokenType {
tokenEndOfStream = 0,
tokenObjectBegin,
tokenObjectEnd,
@ -1031,17 +1032,17 @@ bool OurReader::parse(const char* beginDoc, const char* endDoc, Value& root,
nodes_.pop();
Token token;
skipCommentTokens(token);
if (features_.failIfExtra_ && (token.type_ != tokenEndOfStream)) {
if (features_.failIfExtra_ && (token.type_ != TokenType::tokenEndOfStream)) {
addError("Extra non-whitespace after JSON value.", token);
return false;
}
if (collectComments_ && !commentsBefore_.empty())
root.setComment(commentsBefore_, commentAfter);
root.setComment(commentsBefore_, CommentPlacement::commentAfter);
if (features_.strictRoot_) {
if (!root.isArray() && !root.isObject()) {
// Set error location to start of doc, ideally should be first token found
// in doc
token.type_ = tokenError;
token.type_ = TokenType::tokenError;
token.start_ = beginDoc;
token.end_ = endDoc;
addError(
@ -1062,64 +1063,64 @@ bool OurReader::readValue() {
bool successful = true;
if (collectComments_ && !commentsBefore_.empty()) {
currentValue().setComment(commentsBefore_, commentBefore);
currentValue().setComment(commentsBefore_, CommentPlacement::commentBefore);
commentsBefore_.clear();
}
switch (token.type_) {
case tokenObjectBegin:
case TokenType::tokenObjectBegin:
successful = readObject(token);
currentValue().setOffsetLimit(current_ - begin_);
break;
case tokenArrayBegin:
case TokenType::tokenArrayBegin:
successful = readArray(token);
currentValue().setOffsetLimit(current_ - begin_);
break;
case tokenNumber:
case TokenType::tokenNumber:
successful = decodeNumber(token);
break;
case tokenString:
case TokenType::tokenString:
successful = decodeString(token);
break;
case tokenTrue: {
case TokenType::tokenTrue: {
Value v(true);
currentValue().swapPayload(v);
currentValue().setOffsetStart(token.start_ - begin_);
currentValue().setOffsetLimit(token.end_ - begin_);
} break;
case tokenFalse: {
case TokenType::tokenFalse: {
Value v(false);
currentValue().swapPayload(v);
currentValue().setOffsetStart(token.start_ - begin_);
currentValue().setOffsetLimit(token.end_ - begin_);
} break;
case tokenNull: {
case TokenType::tokenNull: {
Value v;
currentValue().swapPayload(v);
currentValue().setOffsetStart(token.start_ - begin_);
currentValue().setOffsetLimit(token.end_ - begin_);
} break;
case tokenNaN: {
case TokenType::tokenNaN: {
Value v(std::numeric_limits<double>::quiet_NaN());
currentValue().swapPayload(v);
currentValue().setOffsetStart(token.start_ - begin_);
currentValue().setOffsetLimit(token.end_ - begin_);
} break;
case tokenPosInf: {
case TokenType::tokenPosInf: {
Value v(std::numeric_limits<double>::infinity());
currentValue().swapPayload(v);
currentValue().setOffsetStart(token.start_ - begin_);
currentValue().setOffsetLimit(token.end_ - begin_);
} break;
case tokenNegInf: {
case TokenType::tokenNegInf: {
Value v(-std::numeric_limits<double>::infinity());
currentValue().swapPayload(v);
currentValue().setOffsetStart(token.start_ - begin_);
currentValue().setOffsetLimit(token.end_ - begin_);
} break;
case tokenArraySeparator:
case tokenObjectEnd:
case tokenArrayEnd:
case TokenType::tokenArraySeparator:
case TokenType::tokenObjectEnd:
case TokenType::tokenArrayEnd:
if (features_.allowDroppedNullPlaceholders_) {
// "Un-read" the current token and mark the current value as a null
// token.
@ -1129,7 +1130,8 @@ bool OurReader::readValue() {
currentValue().setOffsetStart(current_ - begin_ - 1);
currentValue().setOffsetLimit(current_ - begin_);
break;
} // else, fall through ...
}
[[fallthrough]]; // Else, fall through...
default:
currentValue().setOffsetStart(token.start_ - begin_);
currentValue().setOffsetLimit(token.end_ - begin_);
@ -1149,7 +1151,7 @@ void OurReader::skipCommentTokens(Token& token) {
if (features_.allowComments_) {
do {
readToken(token);
} while (token.type_ == tokenComment);
} while (token.type_ == TokenType::tokenComment);
} else {
readToken(token);
}
@ -1162,24 +1164,24 @@ bool OurReader::readToken(Token& token) {
bool ok = true;
switch (c) {
case '{':
token.type_ = tokenObjectBegin;
token.type_ = TokenType::tokenObjectBegin;
break;
case '}':
token.type_ = tokenObjectEnd;
token.type_ = TokenType::tokenObjectEnd;
break;
case '[':
token.type_ = tokenArrayBegin;
token.type_ = TokenType::tokenArrayBegin;
break;
case ']':
token.type_ = tokenArrayEnd;
token.type_ = TokenType::tokenArrayEnd;
break;
case '"':
token.type_ = tokenString;
token.type_ = TokenType::tokenString;
ok = readString();
break;
case '\'':
if (features_.allowSingleQuotes_) {
token.type_ = tokenString;
token.type_ = TokenType::tokenString;
ok = readStringSingleQuote();
} else {
// If we don't allow single quotes, this is a failure case.
@ -1187,7 +1189,7 @@ bool OurReader::readToken(Token& token) {
}
break;
case '/':
token.type_ = tokenComment;
token.type_ = TokenType::tokenComment;
ok = readComment();
break;
case '0':
@ -1200,40 +1202,40 @@ bool OurReader::readToken(Token& token) {
case '7':
case '8':
case '9':
token.type_ = tokenNumber;
token.type_ = TokenType::tokenNumber;
readNumber(false);
break;
case '-':
if (readNumber(true)) {
token.type_ = tokenNumber;
token.type_ = TokenType::tokenNumber;
} else {
token.type_ = tokenNegInf;
token.type_ = TokenType::tokenNegInf;
ok = features_.allowSpecialFloats_ && match("nfinity", 7);
}
break;
case '+':
if (readNumber(true)) {
token.type_ = tokenNumber;
token.type_ = TokenType::tokenNumber;
} else {
token.type_ = tokenPosInf;
token.type_ = TokenType::tokenPosInf;
ok = features_.allowSpecialFloats_ && match("nfinity", 7);
}
break;
case 't':
token.type_ = tokenTrue;
token.type_ = TokenType::tokenTrue;
ok = match("rue", 3);
break;
case 'f':
token.type_ = tokenFalse;
token.type_ = TokenType::tokenFalse;
ok = match("alse", 4);
break;
case 'n':
token.type_ = tokenNull;
token.type_ = TokenType::tokenNull;
ok = match("ull", 3);
break;
case 'N':
if (features_.allowSpecialFloats_) {
token.type_ = tokenNaN;
token.type_ = TokenType::tokenNaN;
ok = match("aN", 2);
} else {
ok = false;
@ -1241,27 +1243,27 @@ bool OurReader::readToken(Token& token) {
break;
case 'I':
if (features_.allowSpecialFloats_) {
token.type_ = tokenPosInf;
token.type_ = TokenType::tokenPosInf;
ok = match("nfinity", 7);
} else {
ok = false;
}
break;
case ',':
token.type_ = tokenArraySeparator;
token.type_ = TokenType::tokenArraySeparator;
break;
case ':':
token.type_ = tokenMemberSeparator;
token.type_ = TokenType::tokenMemberSeparator;
break;
case 0:
token.type_ = tokenEndOfStream;
token.type_ = TokenType::tokenEndOfStream;
break;
default:
ok = false;
break;
}
if (!ok)
token.type_ = tokenError;
token.type_ = TokenType::tokenError;
token.end_ = current_;
return ok;
}
@ -1315,12 +1317,12 @@ bool OurReader::readComment() {
return false;
if (collectComments_) {
CommentPlacement placement = commentBefore;
CommentPlacement placement = CommentPlacement::commentBefore;
if (!lastValueHasAComment_) {
if (lastValueEnd_ && !containsNewLine(lastValueEnd_, commentBegin)) {
if (isCppStyleComment || !cStyleWithEmbeddedNewline) {
placement = commentAfterOnSameLine;
placement = CommentPlacement::commentAfterOnSameLine;
lastValueHasAComment_ = true;
}
}
@ -1355,7 +1357,7 @@ void OurReader::addComment(Location begin, Location end,
CommentPlacement placement) {
assert(collectComments_);
const String& normalized = normalizeEOL(begin, end);
if (placement == commentAfterOnSameLine) {
if (placement == CommentPlacement::commentAfterOnSameLine) {
assert(lastValue_ != nullptr);
lastValue_->setComment(normalized, placement);
} else {
@ -1446,27 +1448,27 @@ bool OurReader::readStringSingleQuote() {
bool OurReader::readObject(Token& token) {
Token tokenName;
String name;
Value init(objectValue);
Value init(ValueType::objectValue);
currentValue().swapPayload(init);
currentValue().setOffsetStart(token.start_ - begin_);
while (readToken(tokenName)) {
bool initialTokenOk = true;
while (tokenName.type_ == tokenComment && initialTokenOk)
while (tokenName.type_ == TokenType::tokenComment && initialTokenOk)
initialTokenOk = readToken(tokenName);
if (!initialTokenOk)
break;
if (tokenName.type_ == tokenObjectEnd &&
if (tokenName.type_ == TokenType::tokenObjectEnd &&
(name.empty() ||
features_.allowTrailingCommas_)) // empty object or trailing comma
return true;
name.clear();
if (tokenName.type_ == tokenString) {
if (tokenName.type_ == TokenType::tokenString) {
if (!decodeString(tokenName, name))
return recoverFromError(tokenObjectEnd);
} else if (tokenName.type_ == tokenNumber && features_.allowNumericKeys_) {
return recoverFromError(TokenType::tokenObjectEnd);
} else if (tokenName.type_ == TokenType::tokenNumber && features_.allowNumericKeys_) {
Value numberName;
if (!decodeNumber(tokenName, numberName))
return recoverFromError(tokenObjectEnd);
return recoverFromError(TokenType::tokenObjectEnd);
name = numberName.asString();
} else {
break;
@ -1475,40 +1477,40 @@ bool OurReader::readObject(Token& token) {
throwRuntimeError("keylength >= 2^30");
if (features_.rejectDupKeys_ && currentValue().isMember(name)) {
String msg = "Duplicate key: '" + name + "'";
return addErrorAndRecover(msg, tokenName, tokenObjectEnd);
return addErrorAndRecover(msg, tokenName, TokenType::tokenObjectEnd);
}
Token colon;
if (!readToken(colon) || colon.type_ != tokenMemberSeparator) {
if (!readToken(colon) || colon.type_ != TokenType::tokenMemberSeparator) {
return addErrorAndRecover("Missing ':' after object member name", colon,
tokenObjectEnd);
TokenType::tokenObjectEnd);
}
Value& value = currentValue()[name];
nodes_.push(&value);
bool ok = readValue();
nodes_.pop();
if (!ok) // error already set
return recoverFromError(tokenObjectEnd);
return recoverFromError(TokenType::tokenObjectEnd);
Token comma;
if (!readToken(comma) ||
(comma.type_ != tokenObjectEnd && comma.type_ != tokenArraySeparator &&
comma.type_ != tokenComment)) {
(comma.type_ != TokenType::tokenObjectEnd && comma.type_ != TokenType::tokenArraySeparator &&
comma.type_ != TokenType::tokenComment)) {
return addErrorAndRecover("Missing ',' or '}' in object declaration",
comma, tokenObjectEnd);
comma, TokenType::tokenObjectEnd);
}
bool finalizeTokenOk = true;
while (comma.type_ == tokenComment && finalizeTokenOk)
while (comma.type_ == TokenType::tokenComment && finalizeTokenOk)
finalizeTokenOk = readToken(comma);
if (comma.type_ == tokenObjectEnd)
if (comma.type_ == TokenType::tokenObjectEnd)
return true;
}
return addErrorAndRecover("Missing '}' or object member name", tokenName,
tokenObjectEnd);
TokenType::tokenObjectEnd);
}
bool OurReader::readArray(Token& token) {
Value init(arrayValue);
Value init(ValueType::arrayValue);
currentValue().swapPayload(init);
currentValue().setOffsetStart(token.start_ - begin_);
int index = 0;
@ -1529,21 +1531,21 @@ bool OurReader::readArray(Token& token) {
bool ok = readValue();
nodes_.pop();
if (!ok) // error already set
return recoverFromError(tokenArrayEnd);
return recoverFromError(TokenType::tokenArrayEnd);
Token currentToken;
// Accept Comment after last item in the array.
ok = readToken(currentToken);
while (currentToken.type_ == tokenComment && ok) {
while (currentToken.type_ == TokenType::tokenComment && ok) {
ok = readToken(currentToken);
}
bool badTokenType = (currentToken.type_ != tokenArraySeparator &&
currentToken.type_ != tokenArrayEnd);
bool badTokenType = (currentToken.type_ != TokenType::tokenArraySeparator &&
currentToken.type_ != TokenType::tokenArrayEnd);
if (!ok || badTokenType) {
return addErrorAndRecover("Missing ',' or ']' in array declaration",
currentToken, tokenArrayEnd);
currentToken, TokenType::tokenArrayEnd);
}
if (currentToken.type_ == tokenArrayEnd)
if (currentToken.type_ == TokenType::tokenArrayEnd)
break;
}
return true;
@ -1796,7 +1798,7 @@ bool OurReader::recoverFromError(TokenType skipUntilToken) {
for (;;) {
if (!readToken(skip))
errors_.resize(errorCount); // discard errors caused by recovery
if (skip.type_ == skipUntilToken || skip.type_ == tokenEndOfStream)
if (skip.type_ == skipUntilToken || skip.type_ == TokenType::tokenEndOfStream)
break;
}
errors_.resize(errorCount);

File diff suppressed because it is too large Load Diff

View File

@ -380,20 +380,20 @@ String FastWriter::write(const Value& root) {
void FastWriter::writeValue(const Value& value) {
switch (value.type()) {
case nullValue:
case ValueType::nullValue:
if (!dropNullPlaceholders_)
document_ += "null";
break;
case intValue:
case ValueType::intValue:
document_ += valueToString(value.asLargestInt());
break;
case uintValue:
case ValueType::uintValue:
document_ += valueToString(value.asLargestUInt());
break;
case realValue:
case ValueType::realValue:
document_ += valueToString(value.asDouble());
break;
case stringValue: {
case ValueType::stringValue: {
// Is NULL possible for value.string_? No.
char const* str;
char const* end;
@ -402,10 +402,10 @@ void FastWriter::writeValue(const Value& value) {
document_ += valueToQuotedStringN(str, static_cast<size_t>(end - str));
break;
}
case booleanValue:
case ValueType::booleanValue:
document_ += valueToString(value.asBool());
break;
case arrayValue: {
case ValueType::arrayValue: {
document_ += '[';
ArrayIndex size = value.size();
for (ArrayIndex index = 0; index < size; ++index) {
@ -415,7 +415,7 @@ void FastWriter::writeValue(const Value& value) {
}
document_ += ']';
} break;
case objectValue: {
case ValueType::objectValue: {
Value::Members members(value.getMemberNames());
document_ += '{';
for (auto it = members.begin(); it != members.end(); ++it) {
@ -449,19 +449,19 @@ String StyledWriter::write(const Value& root) {
void StyledWriter::writeValue(const Value& value) {
switch (value.type()) {
case nullValue:
case ValueType::nullValue:
pushValue("null");
break;
case intValue:
case ValueType::intValue:
pushValue(valueToString(value.asLargestInt()));
break;
case uintValue:
case ValueType::uintValue:
pushValue(valueToString(value.asLargestUInt()));
break;
case realValue:
case ValueType::realValue:
pushValue(valueToString(value.asDouble()));
break;
case stringValue: {
case ValueType::stringValue: {
// Is NULL possible for value.string_? No.
char const* str;
char const* end;
@ -472,13 +472,13 @@ void StyledWriter::writeValue(const Value& value) {
pushValue("");
break;
}
case booleanValue:
case ValueType::booleanValue:
pushValue(valueToString(value.asBool()));
break;
case arrayValue:
case ValueType::arrayValue:
writeArrayValue(value);
break;
case objectValue: {
case ValueType::objectValue: {
Value::Members members(value.getMemberNames());
if (members.empty())
pushValue("{}");
@ -608,12 +608,12 @@ void StyledWriter::unindent() {
}
void StyledWriter::writeCommentBeforeValue(const Value& root) {
if (!root.hasComment(commentBefore))
if (!root.hasComment(CommentPlacement::commentBefore))
return;
document_ += '\n';
writeIndent();
const String& comment = root.getComment(commentBefore);
const String& comment = root.getComment(CommentPlacement::commentBefore);
String::const_iterator iter = comment.begin();
while (iter != comment.end()) {
document_ += *iter;
@ -627,20 +627,21 @@ void StyledWriter::writeCommentBeforeValue(const Value& root) {
}
void StyledWriter::writeCommentAfterValueOnSameLine(const Value& root) {
if (root.hasComment(commentAfterOnSameLine))
document_ += " " + root.getComment(commentAfterOnSameLine);
if (root.hasComment(CommentPlacement::commentAfterOnSameLine))
document_ +=
" " + root.getComment(CommentPlacement::commentAfterOnSameLine);
if (root.hasComment(commentAfter)) {
if (root.hasComment(CommentPlacement::commentAfter)) {
document_ += '\n';
document_ += root.getComment(commentAfter);
document_ += root.getComment(CommentPlacement::commentAfter);
document_ += '\n';
}
}
bool StyledWriter::hasCommentForValue(const Value& value) {
return value.hasComment(commentBefore) ||
value.hasComment(commentAfterOnSameLine) ||
value.hasComment(commentAfter);
return value.hasComment(CommentPlacement::commentBefore) ||
value.hasComment(CommentPlacement::commentAfterOnSameLine) ||
value.hasComment(CommentPlacement::commentAfter);
}
// Class StyledStreamWriter
@ -667,19 +668,19 @@ void StyledStreamWriter::write(OStream& out, const Value& root) {
void StyledStreamWriter::writeValue(const Value& value) {
switch (value.type()) {
case nullValue:
case ValueType::nullValue:
pushValue("null");
break;
case intValue:
case ValueType::intValue:
pushValue(valueToString(value.asLargestInt()));
break;
case uintValue:
case ValueType::uintValue:
pushValue(valueToString(value.asLargestUInt()));
break;
case realValue:
case ValueType::realValue:
pushValue(valueToString(value.asDouble()));
break;
case stringValue: {
case ValueType::stringValue: {
// Is NULL possible for value.string_? No.
char const* str;
char const* end;
@ -690,13 +691,13 @@ void StyledStreamWriter::writeValue(const Value& value) {
pushValue("");
break;
}
case booleanValue:
case ValueType::booleanValue:
pushValue(valueToString(value.asBool()));
break;
case arrayValue:
case ValueType::arrayValue:
writeArrayValue(value);
break;
case objectValue: {
case ValueType::objectValue: {
Value::Members members(value.getMemberNames());
if (members.empty())
pushValue("{}");
@ -828,12 +829,12 @@ void StyledStreamWriter::unindent() {
}
void StyledStreamWriter::writeCommentBeforeValue(const Value& root) {
if (!root.hasComment(commentBefore))
if (!root.hasComment(CommentPlacement::commentBefore))
return;
if (!indented_)
writeIndent();
const String& comment = root.getComment(commentBefore);
const String& comment = root.getComment(CommentPlacement::commentBefore);
String::const_iterator iter = comment.begin();
while (iter != comment.end()) {
*document_ << *iter;
@ -846,20 +847,21 @@ void StyledStreamWriter::writeCommentBeforeValue(const Value& root) {
}
void StyledStreamWriter::writeCommentAfterValueOnSameLine(const Value& root) {
if (root.hasComment(commentAfterOnSameLine))
*document_ << ' ' << root.getComment(commentAfterOnSameLine);
if (root.hasComment(CommentPlacement::commentAfterOnSameLine))
*document_ << ' '
<< root.getComment(CommentPlacement::commentAfterOnSameLine);
if (root.hasComment(commentAfter)) {
if (root.hasComment(CommentPlacement::commentAfter)) {
writeIndent();
*document_ << root.getComment(commentAfter);
*document_ << root.getComment(CommentPlacement::commentAfter);
}
indented_ = false;
}
bool StyledStreamWriter::hasCommentForValue(const Value& value) {
return value.hasComment(commentBefore) ||
value.hasComment(commentAfterOnSameLine) ||
value.hasComment(commentAfter);
return value.hasComment(CommentPlacement::commentBefore) ||
value.hasComment(CommentPlacement::commentAfterOnSameLine) ||
value.hasComment(CommentPlacement::commentAfter);
}
//////////////////////////
@ -868,7 +870,7 @@ bool StyledStreamWriter::hasCommentForValue(const Value& value) {
/// Scoped enums are not available until C++11.
struct CommentStyle {
/// Decide whether to write comments.
enum Enum {
enum class Enum {
None, ///< Drop all comments.
Most, ///< Recover odd behavior of previous versions (not implemented yet).
All ///< Keep all comments.
@ -940,20 +942,20 @@ int BuiltStyledStreamWriter::write(Value const& root, OStream* sout) {
}
void BuiltStyledStreamWriter::writeValue(Value const& value) {
switch (value.type()) {
case nullValue:
case ValueType::nullValue:
pushValue(nullSymbol_);
break;
case intValue:
case ValueType::intValue:
pushValue(valueToString(value.asLargestInt()));
break;
case uintValue:
case ValueType::uintValue:
pushValue(valueToString(value.asLargestUInt()));
break;
case realValue:
case ValueType::realValue:
pushValue(valueToString(value.asDouble(), useSpecialFloats_, precision_,
precisionType_));
break;
case stringValue: {
case ValueType::stringValue: {
// Is NULL is possible for value.string_? No.
char const* str;
char const* end;
@ -965,13 +967,13 @@ void BuiltStyledStreamWriter::writeValue(Value const& value) {
pushValue("");
break;
}
case booleanValue:
case ValueType::booleanValue:
pushValue(valueToString(value.asBool()));
break;
case arrayValue:
case ValueType::arrayValue:
writeArrayValue(value);
break;
case objectValue: {
case ValueType::objectValue: {
Value::Members members(value.getMemberNames());
if (members.empty())
pushValue("{}");
@ -1006,7 +1008,7 @@ void BuiltStyledStreamWriter::writeArrayValue(Value const& value) {
if (size == 0)
pushValue("[]");
else {
bool isMultiLine = (cs_ == CommentStyle::All) || isMultilineArray(value);
bool isMultiLine = (cs_ == CommentStyle::Enum::All) || isMultilineArray(value);
if (isMultiLine) {
writeWithIndent("[");
indent();
@ -1112,14 +1114,14 @@ void BuiltStyledStreamWriter::unindent() {
}
void BuiltStyledStreamWriter::writeCommentBeforeValue(Value const& root) {
if (cs_ == CommentStyle::None)
if (cs_ == CommentStyle::Enum::None)
return;
if (!root.hasComment(commentBefore))
if (!root.hasComment(CommentPlacement::commentBefore))
return;
if (!indented_)
writeIndent();
const String& comment = root.getComment(commentBefore);
const String& comment = root.getComment(CommentPlacement::commentBefore);
String::const_iterator iter = comment.begin();
while (iter != comment.end()) {
*sout_ << *iter;
@ -1133,22 +1135,22 @@ void BuiltStyledStreamWriter::writeCommentBeforeValue(Value const& root) {
void BuiltStyledStreamWriter::writeCommentAfterValueOnSameLine(
Value const& root) {
if (cs_ == CommentStyle::None)
if (cs_ == CommentStyle::Enum::None)
return;
if (root.hasComment(commentAfterOnSameLine))
*sout_ << " " + root.getComment(commentAfterOnSameLine);
if (root.hasComment(CommentPlacement::commentAfterOnSameLine))
*sout_ << " " + root.getComment(CommentPlacement::commentAfterOnSameLine);
if (root.hasComment(commentAfter)) {
if (root.hasComment(CommentPlacement::commentAfter)) {
writeIndent();
*sout_ << root.getComment(commentAfter);
*sout_ << root.getComment(CommentPlacement::commentAfter);
}
}
// static
bool BuiltStyledStreamWriter::hasCommentForValue(const Value& value) {
return value.hasComment(commentBefore) ||
value.hasComment(commentAfterOnSameLine) ||
value.hasComment(commentAfter);
return value.hasComment(CommentPlacement::commentBefore) ||
value.hasComment(CommentPlacement::commentAfterOnSameLine) ||
value.hasComment(CommentPlacement::commentAfter);
}
///////////////
@ -1168,15 +1170,15 @@ StreamWriter* StreamWriterBuilder::newStreamWriter() const {
const bool usf = settings_["useSpecialFloats"].asBool();
const bool emitUTF8 = settings_["emitUTF8"].asBool();
unsigned int pre = settings_["precision"].asUInt();
CommentStyle::Enum cs = CommentStyle::All;
CommentStyle::Enum cs = CommentStyle::Enum::All;
if (cs_str == "All") {
cs = CommentStyle::All;
cs = CommentStyle::Enum::All;
} else if (cs_str == "None") {
cs = CommentStyle::None;
cs = CommentStyle::Enum::None;
} else {
throwRuntimeError("commentStyle must be 'All' or 'None'");
}
PrecisionType precisionType(significantDigits);
PrecisionType precisionType(PrecisionType::significantDigits);
if (pt_str == "significant") {
precisionType = PrecisionType::significantDigits;
} else if (pt_str == "decimal") {