mirror of
https://github.com/open-source-parsers/jsoncpp.git
synced 2025-07-01 01:43:26 +02:00
refactor comments_ into a class
This commit is contained in:
parent
a732207060
commit
433107f1d9
@ -9,7 +9,9 @@
|
|||||||
#if !defined(JSON_IS_AMALGAMATION)
|
#if !defined(JSON_IS_AMALGAMATION)
|
||||||
#include "forwards.h"
|
#include "forwards.h"
|
||||||
#endif // if !defined(JSON_IS_AMALGAMATION)
|
#endif // if !defined(JSON_IS_AMALGAMATION)
|
||||||
|
#include <array>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@ -587,11 +589,15 @@ Json::Value obj_value(Json::objectValue); // {}
|
|||||||
|
|
||||||
/// \deprecated Always pass len.
|
/// \deprecated Always pass len.
|
||||||
JSONCPP_DEPRECATED("Use setComment(String const&) instead.")
|
JSONCPP_DEPRECATED("Use setComment(String const&) instead.")
|
||||||
void setComment(const char* comment, CommentPlacement placement);
|
void setComment(const char* comment, CommentPlacement placement) {
|
||||||
|
setComment(String(comment, strlen(comment)), placement);
|
||||||
|
}
|
||||||
/// Comments must be //... or /* ... */
|
/// Comments must be //... or /* ... */
|
||||||
void setComment(const char* comment, size_t len, CommentPlacement placement);
|
void setComment(const char* comment, size_t len, CommentPlacement placement) {
|
||||||
|
setComment(String(comment, len), placement);
|
||||||
|
}
|
||||||
/// Comments must be //... or /* ... */
|
/// Comments must be //... or /* ... */
|
||||||
void setComment(const String& comment, CommentPlacement placement);
|
void setComment(String comment, CommentPlacement placement);
|
||||||
bool hasComment(CommentPlacement placement) const;
|
bool hasComment(CommentPlacement placement) const;
|
||||||
/// Include delimiters and embedded newlines.
|
/// Include delimiters and embedded newlines.
|
||||||
String getComment(CommentPlacement placement) const;
|
String getComment(CommentPlacement placement) const;
|
||||||
@ -624,15 +630,6 @@ private:
|
|||||||
Value& resolveReference(const char* key);
|
Value& resolveReference(const char* key);
|
||||||
Value& resolveReference(const char* key, const char* end);
|
Value& resolveReference(const char* key, const char* end);
|
||||||
|
|
||||||
struct CommentInfo {
|
|
||||||
CommentInfo();
|
|
||||||
~CommentInfo();
|
|
||||||
|
|
||||||
void setComment(const char* text, size_t len);
|
|
||||||
|
|
||||||
char* comment_{nullptr};
|
|
||||||
};
|
|
||||||
|
|
||||||
// struct MemberNamesTransform
|
// struct MemberNamesTransform
|
||||||
//{
|
//{
|
||||||
// typedef const char *result_type;
|
// typedef const char *result_type;
|
||||||
@ -658,7 +655,22 @@ private:
|
|||||||
unsigned int allocated_ : 1;
|
unsigned int allocated_ : 1;
|
||||||
} bits_;
|
} bits_;
|
||||||
|
|
||||||
CommentInfo* comments_;
|
class Comments {
|
||||||
|
public:
|
||||||
|
Comments() = default;
|
||||||
|
Comments(const Comments& that);
|
||||||
|
Comments(Comments&&) = default;
|
||||||
|
Comments& operator=(const Comments& that);
|
||||||
|
Comments& operator=(Comments&&) = default;
|
||||||
|
bool has(CommentPlacement slot) const;
|
||||||
|
String get(CommentPlacement slot) const;
|
||||||
|
void set(CommentPlacement slot, String s);
|
||||||
|
|
||||||
|
private:
|
||||||
|
using Array = std::array<String, numberOfCommentPlacement>;
|
||||||
|
std::unique_ptr<Array> ptr_;
|
||||||
|
};
|
||||||
|
Comments comments_;
|
||||||
|
|
||||||
// [start, limit) byte offsets in the source JSON text from which this Value
|
// [start, limit) byte offsets in the source JSON text from which this Value
|
||||||
// was extracted.
|
// was extracted.
|
||||||
|
@ -55,6 +55,15 @@ int JSON_API msvc_pre1900_c99_snprintf(char* outBuf,
|
|||||||
|
|
||||||
namespace Json {
|
namespace Json {
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
static std::unique_ptr<T> cloneUnique(const std::unique_ptr<T>& p) {
|
||||||
|
std::unique_ptr<T> r;
|
||||||
|
if (p) {
|
||||||
|
r = std::unique_ptr<T>(new T(*p));
|
||||||
|
}
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
// This is a walkaround to avoid the static initialization of Value::null.
|
// This is a walkaround to avoid the static initialization of Value::null.
|
||||||
// kNull must be word-aligned to avoid crashing on ARM. We use an alignment of
|
// kNull must be word-aligned to avoid crashing on ARM. We use an alignment of
|
||||||
// 8 (instead of 4) as a bit of future-proofing.
|
// 8 (instead of 4) as a bit of future-proofing.
|
||||||
@ -229,34 +238,6 @@ JSONCPP_NORETURN void throwLogicError(String const& msg) {
|
|||||||
throw LogicError(msg);
|
throw LogicError(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
// //////////////////////////////////////////////////////////////////
|
|
||||||
// //////////////////////////////////////////////////////////////////
|
|
||||||
// //////////////////////////////////////////////////////////////////
|
|
||||||
// class Value::CommentInfo
|
|
||||||
// //////////////////////////////////////////////////////////////////
|
|
||||||
// //////////////////////////////////////////////////////////////////
|
|
||||||
// //////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
Value::CommentInfo::CommentInfo() = default;
|
|
||||||
|
|
||||||
Value::CommentInfo::~CommentInfo() {
|
|
||||||
if (comment_)
|
|
||||||
releaseStringValue(comment_, 0u);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Value::CommentInfo::setComment(const char* text, size_t len) {
|
|
||||||
if (comment_) {
|
|
||||||
releaseStringValue(comment_, 0u);
|
|
||||||
comment_ = nullptr;
|
|
||||||
}
|
|
||||||
JSON_ASSERT(text != nullptr);
|
|
||||||
JSON_ASSERT_MESSAGE(
|
|
||||||
text[0] == '\0' || text[0] == '/',
|
|
||||||
"in Json::Value::setComment(): Comments must start with /");
|
|
||||||
// It seems that /**/ style comments are acceptable as well.
|
|
||||||
comment_ = duplicateStringValue(text, len);
|
|
||||||
}
|
|
||||||
|
|
||||||
// //////////////////////////////////////////////////////////////////
|
// //////////////////////////////////////////////////////////////////
|
||||||
// //////////////////////////////////////////////////////////////////
|
// //////////////////////////////////////////////////////////////////
|
||||||
// //////////////////////////////////////////////////////////////////
|
// //////////////////////////////////////////////////////////////////
|
||||||
@ -488,7 +469,6 @@ Value::Value(Value&& other) {
|
|||||||
|
|
||||||
Value::~Value() {
|
Value::~Value() {
|
||||||
releasePayload();
|
releasePayload();
|
||||||
delete[] comments_;
|
|
||||||
value_.uint_ = 0;
|
value_.uint_ = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -521,7 +501,6 @@ void Value::swap(Value& other) {
|
|||||||
|
|
||||||
void Value::copy(const Value& other) {
|
void Value::copy(const Value& other) {
|
||||||
copyPayload(other);
|
copyPayload(other);
|
||||||
delete[] comments_;
|
|
||||||
dupMeta(other);
|
dupMeta(other);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1027,7 +1006,7 @@ const Value& Value::operator[](int index) const {
|
|||||||
void Value::initBasic(ValueType type, bool allocated) {
|
void Value::initBasic(ValueType type, bool allocated) {
|
||||||
setType(type);
|
setType(type);
|
||||||
setIsAllocated(allocated);
|
setIsAllocated(allocated);
|
||||||
comments_ = nullptr;
|
comments_ = Comments{};
|
||||||
start_ = 0;
|
start_ = 0;
|
||||||
limit_ = 0;
|
limit_ = 0;
|
||||||
}
|
}
|
||||||
@ -1086,17 +1065,7 @@ void Value::releasePayload() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Value::dupMeta(const Value& other) {
|
void Value::dupMeta(const Value& other) {
|
||||||
if (other.comments_) {
|
comments_ = other.comments_;
|
||||||
comments_ = new CommentInfo[numberOfCommentPlacement];
|
|
||||||
for (int comment = 0; comment < numberOfCommentPlacement; ++comment) {
|
|
||||||
const CommentInfo& otherComment = other.comments_[comment];
|
|
||||||
if (otherComment.comment_)
|
|
||||||
comments_[comment].setComment(otherComment.comment_,
|
|
||||||
strlen(otherComment.comment_));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
comments_ = nullptr;
|
|
||||||
}
|
|
||||||
start_ = other.start_;
|
start_ = other.start_;
|
||||||
limit_ = other.limit_;
|
limit_ = other.limit_;
|
||||||
}
|
}
|
||||||
@ -1468,34 +1437,49 @@ bool Value::isArray() const { return type() == arrayValue; }
|
|||||||
|
|
||||||
bool Value::isObject() const { return type() == objectValue; }
|
bool Value::isObject() const { return type() == objectValue; }
|
||||||
|
|
||||||
void Value::setComment(const char* comment,
|
Value::Comments::Comments(const Comments& that)
|
||||||
size_t len,
|
: ptr_{cloneUnique(that.ptr_)} {}
|
||||||
CommentPlacement placement) {
|
|
||||||
if (!comments_)
|
Value::Comments& Value::Comments::operator=(const Comments& that) {
|
||||||
comments_ = new CommentInfo[numberOfCommentPlacement];
|
ptr_ = cloneUnique(that.ptr_);
|
||||||
if ((len > 0) && (comment[len - 1] == '\n')) {
|
return *this;
|
||||||
// Always discard trailing newline, to aid indentation.
|
}
|
||||||
len -= 1;
|
|
||||||
|
bool Value::Comments::has(CommentPlacement slot) const {
|
||||||
|
return ptr_ && !(*ptr_)[slot].empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
String Value::Comments::get(CommentPlacement slot) const {
|
||||||
|
if (!ptr_)
|
||||||
|
return {};
|
||||||
|
return (*ptr_)[slot];
|
||||||
|
}
|
||||||
|
|
||||||
|
void Value::Comments::set(CommentPlacement slot, String comment) {
|
||||||
|
if (!ptr_) {
|
||||||
|
ptr_ = std::unique_ptr<Array>(new Array());
|
||||||
}
|
}
|
||||||
comments_[placement].setComment(comment, len);
|
(*ptr_)[slot] = std::move(comment);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Value::setComment(const char* comment, CommentPlacement placement) {
|
void Value::setComment(String comment, CommentPlacement placement) {
|
||||||
setComment(comment, strlen(comment), placement);
|
if (!comment.empty() && (comment.back() == '\n')) {
|
||||||
}
|
// Always discard trailing newline, to aid indentation.
|
||||||
|
comment.pop_back();
|
||||||
void Value::setComment(const String& comment, CommentPlacement placement) {
|
}
|
||||||
setComment(comment.c_str(), comment.length(), placement);
|
JSON_ASSERT(!comment.empty());
|
||||||
|
JSON_ASSERT_MESSAGE(
|
||||||
|
comment[0] == '\0' || comment[0] == '/',
|
||||||
|
"in Json::Value::setComment(): Comments must start with /");
|
||||||
|
comments_.set(placement, std::move(comment));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Value::hasComment(CommentPlacement placement) const {
|
bool Value::hasComment(CommentPlacement placement) const {
|
||||||
return comments_ != nullptr && comments_[placement].comment_ != nullptr;
|
return comments_.has(placement);
|
||||||
}
|
}
|
||||||
|
|
||||||
String Value::getComment(CommentPlacement placement) const {
|
String Value::getComment(CommentPlacement placement) const {
|
||||||
if (hasComment(placement))
|
return comments_.get(placement);
|
||||||
return comments_[placement].comment_;
|
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Value::setOffsetStart(ptrdiff_t start) { start_ = start; }
|
void Value::setOffsetStart(ptrdiff_t start) { start_ = start; }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user