Skip to content

STYLE: Use modern C++11 style #865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
8 changes: 4 additions & 4 deletions include/json/features.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ class JSON_API Features {
Features();

/// \c true if comments are allowed. Default: \c true.
bool allowComments_;
bool allowComments_{true};

/// \c true if root must be either an array or an object value. Default: \c
/// false.
bool strictRoot_;
bool strictRoot_{false};

/// \c true if dropped null placeholders are allowed. Default: \c false.
bool allowDroppedNullPlaceholders_;
bool allowDroppedNullPlaceholders_{false};

/// \c true if numeric object key are allowed. Default: \c false.
bool allowNumericKeys_;
bool allowNumericKeys_{false};
};

} // namespace Json
Expand Down
16 changes: 8 additions & 8 deletions include/json/reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,21 +242,21 @@ class JSON_API Reader {
Nodes nodes_;
Errors errors_;
JSONCPP_STRING document_;
Location begin_;
Location end_;
Location current_;
Location lastValueEnd_;
Value* lastValue_;
Location begin_{};
Location end_{};
Location current_{};
Location lastValueEnd_{};
Value* lastValue_{};
JSONCPP_STRING commentsBefore_;
Features features_;
bool collectComments_;
bool collectComments_{};
}; // Reader

/** Interface for reading JSON from a char array.
*/
class JSON_API CharReader {
public:
virtual ~CharReader() {}
virtual ~CharReader() = default;
/** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a>
document.
* The document must be a UTF-8 encoded string containing the document to
Expand All @@ -282,7 +282,7 @@ class JSON_API CharReader {

class JSON_API Factory {
public:
virtual ~Factory() {}
virtual ~Factory() = default;
/** \brief Allocate a CharReader via operator new().
* \throw std::exception if something goes wrong (e.g. invalid settings)
*/
Expand Down
10 changes: 5 additions & 5 deletions include/json/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace Json {
*/
class JSON_API Exception : public std::exception {
public:
Exception(JSONCPP_STRING const& msg);
Exception(JSONCPP_STRING msg);
~Exception() JSONCPP_NOEXCEPT override;
char const* what() const JSONCPP_NOEXCEPT override;

Expand Down Expand Up @@ -630,7 +630,7 @@ Json::Value obj_value(Json::objectValue); // {}

void setComment(const char* text, size_t len);

char* comment_;
char* comment_{nullptr};
};

// struct MemberNamesTransform
Expand Down Expand Up @@ -678,8 +678,8 @@ class JSON_API PathArgument {
private:
enum Kind { kindNone = 0, kindIndex, kindKey };
JSONCPP_STRING key_;
ArrayIndex index_;
Kind kind_;
ArrayIndex index_{};
Kind kind_{kindNone};
};

/** \brief Experimental and untested: represents a "path" to access a node.
Expand Down Expand Up @@ -780,7 +780,7 @@ class JSON_API ValueIteratorBase {
private:
Value::ObjectValues::iterator current_;
// Indicates that iterator is for a null value.
bool isNull_;
bool isNull_{true};

public:
// For some reason, BORLAND needs these at the end, rather
Expand Down
22 changes: 11 additions & 11 deletions include/json/writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API FastWriter
: public Writer {
public:
FastWriter();
~FastWriter() override {}
~FastWriter() override = default;

void enableYAMLCompatibility();

Expand All @@ -189,9 +189,9 @@ class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API FastWriter
void writeValue(const Value& value);

JSONCPP_STRING document_;
bool yamlCompatibilityEnabled_;
bool dropNullPlaceholders_;
bool omitEndingLineFeed_;
bool yamlCompatibilityEnabled_{false};
bool dropNullPlaceholders_{false};
bool omitEndingLineFeed_{false};
};
#if defined(_MSC_VER)
#pragma warning(pop)
Expand Down Expand Up @@ -229,7 +229,7 @@ class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API
StyledWriter : public Writer {
public:
StyledWriter();
~StyledWriter() override {}
~StyledWriter() override = default;

public: // overridden from Writer
/** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
Expand Down Expand Up @@ -257,9 +257,9 @@ class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API
ChildValues childValues_;
JSONCPP_STRING document_;
JSONCPP_STRING indentString_;
unsigned int rightMargin_;
unsigned int indentSize_;
bool addChildValues_;
unsigned int rightMargin_{74};
unsigned int indentSize_{3};
bool addChildValues_{false};
};
#if defined(_MSC_VER)
#pragma warning(pop)
Expand Down Expand Up @@ -300,8 +300,8 @@ class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API
/**
* \param indentation Each level will be indented by this amount extra.
*/
StyledStreamWriter(const JSONCPP_STRING& indentation = "\t");
~StyledStreamWriter() {}
StyledStreamWriter(JSONCPP_STRING indentation = "\t");
~StyledStreamWriter() = default;

public:
/** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
Expand Down Expand Up @@ -331,7 +331,7 @@ class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API
ChildValues childValues_;
JSONCPP_OSTREAM* document_;
JSONCPP_STRING indentString_;
unsigned int rightMargin_;
unsigned int rightMargin_{74};
JSONCPP_STRING indentation_;
bool addChildValues_ : 1;
bool indented_ : 1;
Expand Down
6 changes: 2 additions & 4 deletions src/jsontestrunner/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static JSONCPP_STRING readInputTestFile(const char* path) {
return JSONCPP_STRING("");
fseek(file, 0, SEEK_END);
long const size = ftell(file);
unsigned long const usize = static_cast<unsigned long>(size);
size_t const usize = static_cast<unsigned long>(size);
fseek(file, 0, SEEK_SET);
JSONCPP_STRING text;
char* buffer = new char[size + 1];
Expand Down Expand Up @@ -110,9 +110,7 @@ static void printValueTree(FILE* fout,
Json::Value::Members members(value.getMemberNames());
std::sort(members.begin(), members.end());
JSONCPP_STRING suffix = *(path.end() - 1) == '.' ? "" : ".";
for (Json::Value::Members::iterator it = members.begin();
it != members.end(); ++it) {
const JSONCPP_STRING name = *it;
for (auto name : members) {
printValueTree(fout, value[name], path + suffix + name);
}
} break;
Expand Down
48 changes: 21 additions & 27 deletions src/lib_json/json_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ typedef std::auto_ptr<CharReader> CharReaderPtr;
// ////////////////////////////////

Features::Features()
: allowComments_(true), strictRoot_(false),
allowDroppedNullPlaceholders_(false), allowNumericKeys_(false) {}
= default;

Features Features::all() { return {}; }

Expand All @@ -88,9 +87,8 @@ bool Reader::containsNewLine(Reader::Location begin, Reader::Location end) {
// //////////////////////////////////////////////////////////////////

Reader::Reader()
: errors_(), document_(), begin_(), end_(), current_(), lastValueEnd_(),
lastValue_(), commentsBefore_(), features_(Features::all()),
collectComments_() {}
: errors_(), document_(), commentsBefore_(), features_(Features::all())
{}

Reader::Reader(const Features& features)
: errors_(), document_(), begin_(), end_(), current_(), lastValueEnd_(),
Expand Down Expand Up @@ -577,7 +575,7 @@ bool Reader::decodeNumber(Token& token, Value& decoded) {
Char c = *current++;
if (c < '0' || c > '9')
return decodeDouble(token, decoded);
Value::UInt digit(static_cast<Value::UInt>(c - '0'));
auto digit(static_cast<Value::UInt>(c - '0'));
if (value >= threshold) {
// We've hit or exceeded the max value divided by 10 (rounded down). If
// a) we've only just touched the limit, b) this is the last digit, and
Expand Down Expand Up @@ -818,9 +816,7 @@ JSONCPP_STRING Reader::getFormatedErrorMessages() const {

JSONCPP_STRING Reader::getFormattedErrorMessages() const {
JSONCPP_STRING formattedMessage;
for (Errors::const_iterator itError = errors_.begin();
itError != errors_.end(); ++itError) {
const ErrorInfo& error = *itError;
for (const auto & error : errors_) {
formattedMessage +=
"* " + getLocationLineAndColumn(error.token_.start_) + "\n";
formattedMessage += " " + error.message_ + "\n";
Expand All @@ -833,9 +829,7 @@ JSONCPP_STRING Reader::getFormattedErrorMessages() const {

std::vector<Reader::StructuredError> Reader::getStructuredErrors() const {
std::vector<Reader::StructuredError> allErrors;
for (Errors::const_iterator itError = errors_.begin();
itError != errors_.end(); ++itError) {
const ErrorInfo& error = *itError;
for (const auto & error : errors_) {
Reader::StructuredError structured;
structured.offset_start = error.token_.start_ - begin_;
structured.offset_limit = error.token_.end_ - begin_;
Expand Down Expand Up @@ -880,7 +874,7 @@ bool Reader::pushError(const Value& value,
return true;
}

bool Reader::good() const { return !errors_.size(); }
bool Reader::good() const { return errors_.empty(); }

// exact copy of Features
class OurFeatures {
Expand All @@ -894,7 +888,7 @@ class OurFeatures {
bool failIfExtra_;
bool rejectDupKeys_;
bool allowSpecialFloats_;
int stackLimit_;
size_t stackLimit_;
}; // OurFeatures

// exact copy of Implementation of class Features
Expand Down Expand Up @@ -1093,7 +1087,7 @@ bool OurReader::parse(const char* beginDoc,

bool OurReader::readValue() {
// To preserve the old behaviour we cast size_t to int.
if (static_cast<int>(nodes_.size()) > features_.stackLimit_)
if (nodes_.size() > features_.stackLimit_)
throwRuntimeError("Exceeded stackLimit in readValue().");
Token token;
skipCommentTokens(token);
Expand Down Expand Up @@ -1573,7 +1567,7 @@ bool OurReader::decodeNumber(Token& token, Value& decoded) {
Char c = *current++;
if (c < '0' || c > '9')
return decodeDouble(token, decoded);
Value::UInt digit(static_cast<Value::UInt>(c - '0'));
auto digit(static_cast<Value::UInt>(c - '0'));
if (value >= threshold) {
// We've hit or exceeded the max value divided by 10 (rounded down). If
// a) we've only just touched the limit, b) this is the last digit, and
Expand Down Expand Up @@ -1615,7 +1609,7 @@ bool OurReader::decodeDouble(Token& token, Value& decoded) {
if (length < 0) {
return addError("Unable to parse token length", token);
}
size_t const ulength = static_cast<size_t>(length);
auto const ulength = static_cast<size_t>(length);

// Avoid using a string constant for the format control string given to
// sscanf, as this can cause hard to debug crashes on OS X. See here for more
Expand Down Expand Up @@ -1833,9 +1827,7 @@ JSONCPP_STRING OurReader::getLocationLineAndColumn(Location location) const {

JSONCPP_STRING OurReader::getFormattedErrorMessages() const {
JSONCPP_STRING formattedMessage;
for (Errors::const_iterator itError = errors_.begin();
itError != errors_.end(); ++itError) {
const ErrorInfo& error = *itError;
for (const auto & error : errors_) {
formattedMessage +=
"* " + getLocationLineAndColumn(error.token_.start_) + "\n";
formattedMessage += " " + error.message_ + "\n";
Expand All @@ -1848,9 +1840,7 @@ JSONCPP_STRING OurReader::getFormattedErrorMessages() const {

std::vector<OurReader::StructuredError> OurReader::getStructuredErrors() const {
std::vector<OurReader::StructuredError> allErrors;
for (Errors::const_iterator itError = errors_.begin();
itError != errors_.end(); ++itError) {
const ErrorInfo& error = *itError;
for (const auto & error : errors_) {
OurReader::StructuredError structured;
structured.offset_start = error.token_.start_ - begin_;
structured.offset_limit = error.token_.end_ - begin_;
Expand Down Expand Up @@ -1895,7 +1885,7 @@ bool OurReader::pushError(const Value& value,
return true;
}

bool OurReader::good() const { return !errors_.size(); }
bool OurReader::good() const { return errors_.empty(); }

class OurCharReader : public CharReader {
bool const collectComments_;
Expand All @@ -1917,7 +1907,7 @@ class OurCharReader : public CharReader {
};

CharReaderBuilder::CharReaderBuilder() { setDefaults(&settings_); }
CharReaderBuilder::~CharReaderBuilder() {}
CharReaderBuilder::~CharReaderBuilder() = default;
CharReader* CharReaderBuilder::newCharReader() const {
bool collectComments = settings_["collectComments"].asBool();
OurFeatures features = OurFeatures::all();
Expand All @@ -1927,7 +1917,11 @@ CharReader* CharReaderBuilder::newCharReader() const {
settings_["allowDroppedNullPlaceholders"].asBool();
features.allowNumericKeys_ = settings_["allowNumericKeys"].asBool();
features.allowSingleQuotes_ = settings_["allowSingleQuotes"].asBool();
features.stackLimit_ = settings_["stackLimit"].asInt();
#if defined(JSON_HAS_INT64)
features.stackLimit_ = settings_["stackLimit"].asUInt64();
#else
features.stackLimit_ = settings_["stackLimit"].asUInt();
#endif
features.failIfExtra_ = settings_["failIfExtra"].asBool();
features.rejectDupKeys_ = settings_["rejectDupKeys"].asBool();
features.allowSpecialFloats_ = settings_["allowSpecialFloats"].asBool();
Expand Down Expand Up @@ -1961,7 +1955,7 @@ bool CharReaderBuilder::validate(Json::Value* invalid) const {
inv[key] = settings_[key];
}
}
return 0u == inv.size();
return inv.empty();
}
Value& CharReaderBuilder::operator[](const JSONCPP_STRING& key) {
return settings_[key];
Expand Down
Loading