Skip to content

Commit b4aa663

Browse files
lanzkronChristopher Dunn
authored and
Christopher Dunn
committed
move ctors
* Add move constructor to Value::CZString * Add unit test for Value move constructor * Allow includer to specify in advance the value for JSON_HAS_RVALUE_REFERENCES
1 parent f4ee48b commit b4aa663

File tree

4 files changed

+81
-23
lines changed

4 files changed

+81
-23
lines changed

include/json/config.h

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,46 @@
5656
// Storages, and 64 bits integer support is disabled.
5757
// #define JSON_NO_INT64 1
5858

59-
#if defined(_MSC_VER) && _MSC_VER <= 1200 // MSVC 6
60-
// Microsoft Visual Studio 6 only support conversion from __int64 to double
61-
// (no conversion from unsigned __int64).
62-
#define JSON_USE_INT64_DOUBLE_CONVERSION 1
63-
// Disable warning 4786 for VS6 caused by STL (identifier was truncated to '255'
64-
// characters in the debug information)
65-
// All projects I've ever seen with VS6 were using this globally (not bothering
66-
// with pragma push/pop).
67-
#pragma warning(disable : 4786)
68-
#endif // if defined(_MSC_VER) && _MSC_VER < 1200 // MSVC 6
69-
70-
#if defined(_MSC_VER) && _MSC_VER >= 1500 // MSVC 2008
71-
/// Indicates that the following function is deprecated.
72-
#define JSONCPP_DEPRECATED(message) __declspec(deprecated(message))
73-
#elif defined(__clang__) && defined(__has_feature)
74-
#if __has_feature(attribute_deprecated_with_message)
75-
#define JSONCPP_DEPRECATED(message) __attribute__ ((deprecated(message)))
59+
#if defined(_MSC_VER) // MSVC
60+
# if _MSC_VER <= 1200 // MSVC 6
61+
// Microsoft Visual Studio 6 only support conversion from __int64 to double
62+
// (no conversion from unsigned __int64).
63+
# define JSON_USE_INT64_DOUBLE_CONVERSION 1
64+
// Disable warning 4786 for VS6 caused by STL (identifier was truncated to '255'
65+
// characters in the debug information)
66+
// All projects I've ever seen with VS6 were using this globally (not bothering
67+
// with pragma push/pop).
68+
# pragma warning(disable : 4786)
69+
# endif // MSVC 6
70+
71+
# if _MSC_VER >= 1500 // MSVC 2008
72+
/// Indicates that the following function is deprecated.
73+
# define JSONCPP_DEPRECATED(message) __declspec(deprecated(message))
74+
# endif
75+
76+
#endif // defined(_MSC_VER)
77+
78+
79+
#ifndef JSON_HAS_RVALUE_REFERENCES
80+
81+
#if defined(_MSC_VER) && _MSC_VER >= 1600 // MSVC >= 2010
82+
#define JSON_HAS_RVALUE_REFERENCES 1
83+
#endif // MSVC >= 2010
84+
85+
#ifdef __clang__
86+
#if __has_feature(cxx_rvalue_references)
87+
#define JSON_HAS_RVALUE_REFERENCES 1
88+
#endif
89+
#elif defined __GNUC__ // not clang (gcc comes later since clang emulates gcc)
90+
#if defined(__GXX_EXPERIMENTAL_CXX0X__) || (__cplusplus >= 201103L)
91+
#define JSON_HAS_RVALUE_REFERENCES 1
7692
#endif
77-
#elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5))
78-
#define JSONCPP_DEPRECATED(message) __attribute__ ((deprecated(message)))
79-
#elif defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
80-
#define JSONCPP_DEPRECATED(message) __attribute__((__deprecated__))
93+
#endif // __clang__ || __GNUC__
94+
95+
#endif // not defined JSON_HAS_RVALUE_REFERENCES
96+
97+
#ifndef JSON_HAS_RVALUE_REFERENCES
98+
#define JSON_HAS_RVALUE_REFERENCES 0
8199
#endif
82100

83101
#if !defined(JSONCPP_DEPRECATED)

include/json/value.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,11 @@ class JSON_API Value {
195195
duplicateOnCopy
196196
};
197197
CZString(ArrayIndex index);
198-
CZString(char const* str, unsigned length, DuplicationPolicy allocate);
199-
CZString(CZString const& other);
198+
CZString(const char* cstr, unsigned length, DuplicationPolicy allocate);
199+
CZString(const CZString& other);
200+
#if JSON_HAS_RVALUE_REFERENCES
201+
CZString(CZString&& other);
202+
#endif
200203
~CZString();
201204
CZString& operator=(CZString other);
202205
bool operator<(CZString const& other) const;
@@ -279,6 +282,10 @@ Json::Value obj_value(Json::objectValue); // {}
279282
Value(bool value);
280283
/// Deep copy.
281284
Value(const Value& other);
285+
#if JSON_HAS_RVALUE_REFERENCES
286+
/// Move constructor
287+
Value(Value&& other);
288+
#endif
282289
~Value();
283290

284291
/// Deep copy, then swap(other).

src/lib_json/json_value.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,15 @@ Value::CZString::CZString(const CZString& other)
254254
storage_.length_ = other.storage_.length_;
255255
}
256256

257+
#if JSON_HAS_RVALUE_REFERENCES
258+
Value::CZString::CZString(CZString&& other)
259+
: cstr_(other.cstr_),
260+
index_(other.index_)
261+
{
262+
other.cstr_ = 0;
263+
}
264+
#endif
265+
257266
Value::CZString::~CZString() {
258267
if (cstr_ && storage_.policy_ == duplicate)
259268
releaseStringValue(const_cast<char*>(cstr_));
@@ -442,6 +451,15 @@ Value::Value(Value const& other)
442451
}
443452
}
444453

454+
#if JSON_HAS_RVALUE_REFERENCES
455+
// Move constructor
456+
Value::Value(Value&& other)
457+
{
458+
initBasic(nullValue);
459+
swap(other);
460+
}
461+
#endif
462+
445463
Value::~Value() {
446464
switch (type_) {
447465
case nullValue:

src/test_lib_json/main.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2320,6 +2320,19 @@ JSONTEST_FIXTURE(IteratorTest, indexes) {
23202320
JSONTEST_ASSERT(it == json.end());
23212321
}
23222322

2323+
struct RValueTest : JsonTest::TestCase {};
2324+
2325+
JSONTEST_FIXTURE(RValueTest, moveConstruction) {
2326+
//#if JSON_HAS_RVALUE_REFERENCES
2327+
Json::Value json;
2328+
json["key"] = "value";
2329+
Json::Value moved = std::move(json);
2330+
JSONTEST_ASSERT(moved != json); // Possibly not nullValue; definitely not equal.
2331+
JSONTEST_ASSERT_EQUAL(Json::objectValue, moved.type());
2332+
JSONTEST_ASSERT_EQUAL(Json::stringValue, moved["key"].type());
2333+
//#endif
2334+
}
2335+
23232336
int main(int argc, const char* argv[]) {
23242337
JsonTest::Runner runner;
23252338
JSONTEST_REGISTER_FIXTURE(runner, ValueTest, checkNormalizeFloatingPointStr);
@@ -2387,5 +2400,7 @@ int main(int argc, const char* argv[]) {
23872400
JSONTEST_REGISTER_FIXTURE(runner, IteratorTest, names);
23882401
JSONTEST_REGISTER_FIXTURE(runner, IteratorTest, indexes);
23892402

2403+
JSONTEST_REGISTER_FIXTURE(runner, RValueTest, moveConstruction);
2404+
23902405
return runner.runCommandLine(argc, argv);
23912406
}

0 commit comments

Comments
 (0)