Skip to content

Commit 20672ed

Browse files
committed
Merge pull request open-source-parsers#68 from BillyDonahue/refactor_ctor_boilerplate
Json::Value: Refactor common code in all constructors to an initBasic() function.
2 parents 9c80798 + 8eb5d89 commit 20672ed

File tree

2 files changed

+39
-100
lines changed

2 files changed

+39
-100
lines changed

include/json/value.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,8 @@ Json::Value obj_value(Json::objectValue); // {}
440440
size_t getOffsetLimit() const;
441441

442442
private:
443+
void initBasic(ValueType type, bool allocated = false);
444+
443445
Value& resolveReference(const char* key, bool isStatic);
444446

445447
#ifdef JSON_VALUE_USE_INTERNAL_MAP

src/lib_json/json_value.cpp

Lines changed: 37 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,8 @@ bool Value::CZString::isStaticString() const { return index_ == noDuplication; }
225225
* memset( this, 0, sizeof(Value) )
226226
* This optimization is used in ValueInternalMap fast allocator.
227227
*/
228-
Value::Value(ValueType type)
229-
: type_(type), allocated_(false)
230-
#ifdef JSON_VALUE_USE_INTERNAL_MAP
231-
,
232-
itemIsUsed_(0)
233-
#endif
234-
,
235-
comments_(0), start_(0), limit_(0) {
228+
Value::Value(ValueType type) {
229+
initBasic(type);
236230
switch (type) {
237231
case nullValue:
238232
break;
@@ -267,130 +261,62 @@ Value::Value(ValueType type)
267261
}
268262
}
269263

270-
Value::Value(UInt value)
271-
: type_(uintValue), allocated_(false)
272-
#ifdef JSON_VALUE_USE_INTERNAL_MAP
273-
,
274-
itemIsUsed_(0)
275-
#endif
276-
,
277-
comments_(0), start_(0), limit_(0) {
278-
value_.uint_ = value;
279-
}
280-
281-
Value::Value(Int value)
282-
: type_(intValue), allocated_(false)
283-
#ifdef JSON_VALUE_USE_INTERNAL_MAP
284-
,
285-
itemIsUsed_(0)
286-
#endif
287-
,
288-
comments_(0), start_(0), limit_(0) {
264+
Value::Value(Int value) {
265+
initBasic(intValue);
289266
value_.int_ = value;
290267
}
291268

269+
Value::Value(UInt value) {
270+
initBasic(uintValue);
271+
value_.uint_ = value;
272+
}
292273
#if defined(JSON_HAS_INT64)
293-
Value::Value(Int64 value)
294-
: type_(intValue), allocated_(false)
295-
#ifdef JSON_VALUE_USE_INTERNAL_MAP
296-
,
297-
itemIsUsed_(0)
298-
#endif
299-
,
300-
comments_(0), start_(0), limit_(0) {
274+
Value::Value(Int64 value) {
275+
initBasic(intValue);
301276
value_.int_ = value;
302277
}
303-
304-
Value::Value(UInt64 value)
305-
: type_(uintValue), allocated_(false)
306-
#ifdef JSON_VALUE_USE_INTERNAL_MAP
307-
,
308-
itemIsUsed_(0)
309-
#endif
310-
,
311-
comments_(0), start_(0), limit_(0) {
278+
Value::Value(UInt64 value) {
279+
initBasic(uintValue);
312280
value_.uint_ = value;
313281
}
314282
#endif // defined(JSON_HAS_INT64)
315283

316-
Value::Value(double value)
317-
: type_(realValue), allocated_(false)
318-
#ifdef JSON_VALUE_USE_INTERNAL_MAP
319-
,
320-
itemIsUsed_(0)
321-
#endif
322-
,
323-
comments_(0), start_(0), limit_(0) {
284+
Value::Value(double value) {
285+
initBasic(realValue);
324286
value_.real_ = value;
325287
}
326288

327-
Value::Value(const char* value)
328-
: type_(stringValue), allocated_(true)
329-
#ifdef JSON_VALUE_USE_INTERNAL_MAP
330-
,
331-
itemIsUsed_(0)
332-
#endif
333-
,
334-
comments_(0), start_(0), limit_(0) {
289+
Value::Value(const char* value) {
290+
initBasic(stringValue, true);
335291
value_.string_ = duplicateStringValue(value);
336292
}
337293

338-
Value::Value(const char* beginValue, const char* endValue)
339-
: type_(stringValue), allocated_(true)
340-
#ifdef JSON_VALUE_USE_INTERNAL_MAP
341-
,
342-
itemIsUsed_(0)
343-
#endif
344-
,
345-
comments_(0), start_(0), limit_(0) {
294+
Value::Value(const char* beginValue, const char* endValue) {
295+
initBasic(stringValue, true);
346296
value_.string_ =
347297
duplicateStringValue(beginValue, (unsigned int)(endValue - beginValue));
348298
}
349299

350-
Value::Value(const std::string& value)
351-
: type_(stringValue), allocated_(true)
352-
#ifdef JSON_VALUE_USE_INTERNAL_MAP
353-
,
354-
itemIsUsed_(0)
355-
#endif
356-
,
357-
comments_(0), start_(0), limit_(0) {
300+
Value::Value(const std::string& value) {
301+
initBasic(stringValue, true);
358302
value_.string_ =
359303
duplicateStringValue(value.c_str(), (unsigned int)value.length());
360304
}
361305

362-
Value::Value(const StaticString& value)
363-
: type_(stringValue), allocated_(false)
364-
#ifdef JSON_VALUE_USE_INTERNAL_MAP
365-
,
366-
itemIsUsed_(0)
367-
#endif
368-
,
369-
comments_(0), start_(0), limit_(0) {
306+
Value::Value(const StaticString& value) {
307+
initBasic(stringValue);
370308
value_.string_ = const_cast<char*>(value.c_str());
371309
}
372310

373311
#ifdef JSON_USE_CPPTL
374-
Value::Value(const CppTL::ConstString& value)
375-
: type_(stringValue), allocated_(true)
376-
#ifdef JSON_VALUE_USE_INTERNAL_MAP
377-
,
378-
itemIsUsed_(0)
379-
#endif
380-
,
381-
comments_(0), start_(0), limit_(0) {
312+
Value::Value(const CppTL::ConstString& value) {
313+
initBasic(stringValue, true);
382314
value_.string_ = duplicateStringValue(value, value.length());
383315
}
384316
#endif
385317

386-
Value::Value(bool value)
387-
: type_(booleanValue), allocated_(false)
388-
#ifdef JSON_VALUE_USE_INTERNAL_MAP
389-
,
390-
itemIsUsed_(0)
391-
#endif
392-
,
393-
comments_(0), start_(0), limit_(0) {
318+
Value::Value(bool value) {
319+
initBasic(booleanValue);
394320
value_.bool_ = value;
395321
}
396322

@@ -966,6 +892,17 @@ Value& Value::operator[](const char* key) {
966892
return resolveReference(key, false);
967893
}
968894

895+
void Value::initBasic(ValueType type, bool allocated) {
896+
type_ = type;
897+
allocated_ = allocated;
898+
#ifdef JSON_VALUE_USE_INTERNAL_MAP
899+
itemIsUsed_ = 0;
900+
#endif
901+
comments_ = 0;
902+
start_ = 0;
903+
limit_ = 0;
904+
}
905+
969906
Value& Value::resolveReference(const char* key, bool isStatic) {
970907
JSON_ASSERT_MESSAGE(
971908
type_ == nullValue || type_ == objectValue,

0 commit comments

Comments
 (0)