Skip to content

Commit 0c1cc6e

Browse files
authored
pack the {type,allocated} bitfield (#876)
* pack the {type,allocated} bitfield (Issue#873) This allows special functions to be implemented more easily.
1 parent d85d750 commit 0c1cc6e

File tree

2 files changed

+117
-119
lines changed

2 files changed

+117
-119
lines changed

Diff for: include/json/value.h

+16-13
Original file line numberDiff line numberDiff line change
@@ -338,18 +338,14 @@ Json::Value obj_value(Json::objectValue); // {}
338338
Value(const CppTL::ConstString& value);
339339
#endif
340340
Value(bool value);
341-
/// Deep copy.
342341
Value(const Value& other);
343-
#if JSON_HAS_RVALUE_REFERENCES
344-
/// Move constructor
345342
Value(Value&& other);
346-
#endif
347343
~Value();
348344

349-
/// Deep copy, then swap(other).
350-
/// \note Over-write existing comments. To preserve comments, use
345+
/// \note Overwrite existing comments. To preserve comments, use
351346
/// #swapPayload().
352-
Value& operator=(Value other);
347+
Value& operator=(const Value& other);
348+
Value& operator=(Value&& other);
353349

354350
/// Swap everything.
355351
void swap(Value& other);
@@ -616,6 +612,10 @@ Json::Value obj_value(Json::objectValue); // {}
616612
ptrdiff_t getOffsetLimit() const;
617613

618614
private:
615+
void setType(ValueType v) { bits_.value_type_ = v; }
616+
bool isAllocated() const { return bits_.allocated_; }
617+
void setIsAllocated(bool v) { bits_.allocated_ = v; }
618+
619619
void initBasic(ValueType type, bool allocated = false);
620620
void dupPayload(const Value& other);
621621
void releasePayload();
@@ -647,14 +647,17 @@ Json::Value obj_value(Json::objectValue); // {}
647647
LargestUInt uint_;
648648
double real_;
649649
bool bool_;
650-
char* string_; // actually ptr to unsigned, followed by str, unless
651-
// !allocated_
650+
char* string_; // if allocated_, ptr to { unsigned, char[] }.
652651
ObjectValues* map_;
653652
} value_;
654-
ValueType type_ : 8;
655-
unsigned int allocated_ : 1; // Notes: if declared as bool, bitfield is
656-
// useless. If not allocated_, string_ must be
657-
// null-terminated.
653+
654+
struct {
655+
// Really a ValueType, but types should agree for bitfield packing.
656+
unsigned int value_type_ : 8;
657+
// Unless allocated_, string_ must be null-terminated.
658+
unsigned int allocated_ : 1;
659+
} bits_;
660+
658661
CommentInfo* comments_;
659662

660663
// [start, limit) byte offsets in the source JSON text from which this Value

0 commit comments

Comments
 (0)