@@ -441,48 +441,9 @@ Value::Value(bool value) {
441
441
value_.bool_ = value;
442
442
}
443
443
444
- Value::Value (Value const & other)
445
- : type_(other.type_), allocated_(false )
446
- ,
447
- comments_ (0 ), start_(other.start_), limit_(other.limit_)
448
- {
449
- switch (type_) {
450
- case nullValue:
451
- case intValue:
452
- case uintValue:
453
- case realValue:
454
- case booleanValue:
455
- value_ = other.value_ ;
456
- break ;
457
- case stringValue:
458
- if (other.value_ .string_ && other.allocated_ ) {
459
- unsigned len;
460
- char const * str;
461
- decodePrefixedString (other.allocated_ , other.value_ .string_ ,
462
- &len, &str);
463
- value_.string_ = duplicateAndPrefixStringValue (str, len);
464
- allocated_ = true ;
465
- } else {
466
- value_.string_ = other.value_ .string_ ;
467
- allocated_ = false ;
468
- }
469
- break ;
470
- case arrayValue:
471
- case objectValue:
472
- value_.map_ = new ObjectValues (*other.value_ .map_ );
473
- break ;
474
- default :
475
- JSON_ASSERT_UNREACHABLE;
476
- }
477
- if (other.comments_ ) {
478
- comments_ = new CommentInfo[numberOfCommentPlacement];
479
- for (int comment = 0 ; comment < numberOfCommentPlacement; ++comment) {
480
- const CommentInfo& otherComment = other.comments_ [comment];
481
- if (otherComment.comment_ )
482
- comments_[comment].setComment (
483
- otherComment.comment_ , strlen (otherComment.comment_ ));
484
- }
485
- }
444
+ Value::Value (const Value& other) {
445
+ dupPayload (other);
446
+ dupMeta (other);
486
447
}
487
448
488
449
#if JSON_HAS_RVALUE_REFERENCES
@@ -494,24 +455,7 @@ Value::Value(Value&& other) {
494
455
#endif
495
456
496
457
Value::~Value () {
497
- switch (type_) {
498
- case nullValue:
499
- case intValue:
500
- case uintValue:
501
- case realValue:
502
- case booleanValue:
503
- break ;
504
- case stringValue:
505
- if (allocated_)
506
- releasePrefixedStringValue (value_.string_ );
507
- break ;
508
- case arrayValue:
509
- case objectValue:
510
- delete value_.map_ ;
511
- break ;
512
- default :
513
- JSON_ASSERT_UNREACHABLE;
514
- }
458
+ releasePayload ();
515
459
516
460
delete[] comments_;
517
461
@@ -534,9 +478,8 @@ void Value::swapPayload(Value& other) {
534
478
}
535
479
536
480
void Value::copyPayload (const Value& other) {
537
- type_ = other.type_ ;
538
- value_ = other.value_ ;
539
- allocated_ = other.allocated_ ;
481
+ releasePayload ();
482
+ dupPayload (other);
540
483
}
541
484
542
485
void Value::swap (Value& other) {
@@ -548,9 +491,8 @@ void Value::swap(Value& other) {
548
491
549
492
void Value::copy (const Value& other) {
550
493
copyPayload (other);
551
- comments_ = other.comments_ ;
552
- start_ = other.start_ ;
553
- limit_ = other.limit_ ;
494
+ delete[] comments_;
495
+ dupMeta (other);
554
496
}
555
497
556
498
ValueType Value::type () const { return type_; }
@@ -1049,6 +991,75 @@ void Value::initBasic(ValueType vtype, bool allocated) {
1049
991
limit_ = 0 ;
1050
992
}
1051
993
994
+ void Value::dupPayload (const Value& other) {
995
+ type_ = other.type_ ;
996
+ allocated_ = false ;
997
+ switch (type_) {
998
+ case nullValue:
999
+ case intValue:
1000
+ case uintValue:
1001
+ case realValue:
1002
+ case booleanValue:
1003
+ value_ = other.value_ ;
1004
+ break ;
1005
+ case stringValue:
1006
+ if (other.value_ .string_ && other.allocated_ ) {
1007
+ unsigned len;
1008
+ char const * str;
1009
+ decodePrefixedString (other.allocated_ , other.value_ .string_ ,
1010
+ &len, &str);
1011
+ value_.string_ = duplicateAndPrefixStringValue (str, len);
1012
+ allocated_ = true ;
1013
+ } else {
1014
+ value_.string_ = other.value_ .string_ ;
1015
+ }
1016
+ break ;
1017
+ case arrayValue:
1018
+ case objectValue:
1019
+ value_.map_ = new ObjectValues (*other.value_ .map_ );
1020
+ break ;
1021
+ default :
1022
+ JSON_ASSERT_UNREACHABLE;
1023
+ }
1024
+ }
1025
+
1026
+ void Value::releasePayload () {
1027
+ switch (type_) {
1028
+ case nullValue:
1029
+ case intValue:
1030
+ case uintValue:
1031
+ case realValue:
1032
+ case booleanValue:
1033
+ break ;
1034
+ case stringValue:
1035
+ if (allocated_)
1036
+ releasePrefixedStringValue (value_.string_ );
1037
+ break ;
1038
+ case arrayValue:
1039
+ case objectValue:
1040
+ delete value_.map_ ;
1041
+ break ;
1042
+ default :
1043
+ JSON_ASSERT_UNREACHABLE;
1044
+ }
1045
+ }
1046
+
1047
+ void Value::dupMeta (const Value& other) {
1048
+ if (other.comments_ ) {
1049
+ comments_ = new CommentInfo[numberOfCommentPlacement];
1050
+ for (int comment = 0 ; comment < numberOfCommentPlacement; ++comment) {
1051
+ const CommentInfo& otherComment = other.comments_ [comment];
1052
+ if (otherComment.comment_ )
1053
+ comments_[comment].setComment (
1054
+ otherComment.comment_ , strlen (otherComment.comment_ ));
1055
+ }
1056
+ } else {
1057
+ comments_ = 0 ;
1058
+ }
1059
+ start_ = other.start_ ;
1060
+ limit_ = other.limit_ ;
1061
+ }
1062
+
1052
1063
// Access an object value by name, create a null member if it does not exist.
1053
1064
// @pre Type of '*this' is object or null.
1054
1065
// @param key is null-terminated.
0 commit comments