Commit 15d70d6a authored by Milo Yip's avatar Milo Yip

Merge pull request #127 from pah/feature/value-different-allocators

GenericValue: accept values with different allocators for read-only access
parents c6e6bca2 a2a0d161
This diff is collapsed.
...@@ -38,6 +38,8 @@ RAPIDJSON_DIAG_OFF(6334) ...@@ -38,6 +38,8 @@ RAPIDJSON_DIAG_OFF(6334)
namespace rapidjson { namespace rapidjson {
namespace internal { namespace internal {
// Helper to wrap/convert arbitrary types to void, useful for arbitrary type matching
template <typename T> struct Void { typedef void Type; };
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// BoolType, TrueType, FalseType // BoolType, TrueType, FalseType
......
...@@ -89,15 +89,9 @@ inline FILE* TempFile(char *filename) { ...@@ -89,15 +89,9 @@ inline FILE* TempFile(char *filename) {
#pragma warning(disable : 4127) #pragma warning(disable : 4127)
#endif #endif
class AssertException : public std::exception { class AssertException : public std::logic_error {
public: public:
AssertException(const char* w) : what_(w) {} AssertException(const char* w) : std::logic_error(w) {}
AssertException(const AssertException& other) : what_(other.what_) {}
AssertException& operator=(const AssertException& rhs) { what_ = rhs.what_; return *this; }
virtual const char* what() const throw() { return what_; }
private:
const char* what_;
}; };
#define RAPIDJSON_ASSERT(x) if (!(x)) throw AssertException(RAPIDJSON_STRINGIFY(x)) #define RAPIDJSON_ASSERT(x) if (!(x)) throw AssertException(RAPIDJSON_STRINGIFY(x))
......
...@@ -105,20 +105,12 @@ TEST(Value, equalto_operator) { ...@@ -105,20 +105,12 @@ TEST(Value, equalto_operator) {
TestEqual(x["i"], 123); TestEqual(x["i"], 123);
TestEqual(x["pi"], 3.14); TestEqual(x["pi"], 3.14);
// Test operator==() // Test operator==() (including different allocators)
#ifdef RAPIDJSON_COMPARE_DIFFERENT_ALLOCATORS
CrtAllocator crtAllocator; CrtAllocator crtAllocator;
GenericValue<UTF8<>, CrtAllocator> y; GenericValue<UTF8<>, CrtAllocator> y;
GenericDocument<UTF8<>, CrtAllocator> z(&crtAllocator); GenericDocument<UTF8<>, CrtAllocator> z(&crtAllocator);
CrtAllocator& yAllocator = crtAllocator; y.CopyFrom(x, crtAllocator);
#else
Value::AllocatorType& yAllocator = allocator;
Value y;
Document z;
#endif // RAPIDJSON_COMPARE_DIFFERENT_ALLOCATORS
y.CopyFrom(x, yAllocator);
z.CopyFrom(y, z.GetAllocator()); z.CopyFrom(y, z.GetAllocator());
TestEqual(x, y); TestEqual(x, y);
TestEqual(y, z); TestEqual(y, z);
TestEqual(z, x); TestEqual(z, x);
...@@ -130,7 +122,7 @@ TEST(Value, equalto_operator) { ...@@ -130,7 +122,7 @@ TEST(Value, equalto_operator) {
EXPECT_TRUE(z.RemoveMember("t")); EXPECT_TRUE(z.RemoveMember("t"));
TestUnequal(x, z); TestUnequal(x, z);
TestEqual(y, z); TestEqual(y, z);
y.AddMember("t", true, yAllocator); y.AddMember("t", true, crtAllocator);
z.AddMember("t", true, z.GetAllocator()); z.AddMember("t", true, z.GetAllocator());
TestEqual(x, y); TestEqual(x, y);
TestEqual(y, z); TestEqual(y, z);
...@@ -843,10 +835,18 @@ TEST(Value, Object) { ...@@ -843,10 +835,18 @@ TEST(Value, Object) {
EXPECT_TRUE(x.HasMember(name)); EXPECT_TRUE(x.HasMember(name));
EXPECT_TRUE(y.HasMember(name)); EXPECT_TRUE(y.HasMember(name));
GenericValue<UTF8<>, CrtAllocator> othername("A");
EXPECT_TRUE(x.HasMember(othername));
EXPECT_TRUE(y.HasMember(othername));
othername.SetString("C\0D");
EXPECT_TRUE(x.HasMember(othername));
EXPECT_TRUE(y.HasMember(othername));
// operator[] // operator[]
EXPECT_STREQ("Apple", x["A"].GetString()); EXPECT_STREQ("Apple", x["A"].GetString());
EXPECT_STREQ("Banana", x["B"].GetString()); EXPECT_STREQ("Banana", x["B"].GetString());
EXPECT_STREQ("CherryD", x[C0D].GetString()); EXPECT_STREQ("CherryD", x[C0D].GetString());
EXPECT_STREQ("CherryD", x[othername].GetString());
// const operator[] // const operator[]
EXPECT_STREQ("Apple", y["A"].GetString()); EXPECT_STREQ("Apple", y["A"].GetString());
...@@ -917,7 +917,7 @@ TEST(Value, Object) { ...@@ -917,7 +917,7 @@ TEST(Value, Object) {
x.RemoveMember("B"); x.RemoveMember("B");
EXPECT_FALSE(x.HasMember("B")); EXPECT_FALSE(x.HasMember("B"));
x.RemoveMember(name); x.RemoveMember(othername);
EXPECT_FALSE(x.HasMember(name)); EXPECT_FALSE(x.HasMember(name));
EXPECT_TRUE(x.MemberBegin() == x.MemberEnd()); EXPECT_TRUE(x.MemberBegin() == x.MemberEnd());
...@@ -930,11 +930,14 @@ TEST(Value, Object) { ...@@ -930,11 +930,14 @@ TEST(Value, Object) {
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
x.AddMember(keys[i], Value(kArrayType).PushBack(i, allocator), allocator); x.AddMember(keys[i], Value(kArrayType).PushBack(i, allocator), allocator);
// MemberCount, iterator difference
EXPECT_EQ(x.MemberCount(), SizeType(x.MemberEnd() - x.MemberBegin()));
// Erase the first // Erase the first
itr = x.EraseMember(x.MemberBegin()); itr = x.EraseMember(x.MemberBegin());
EXPECT_FALSE(x.HasMember(keys[0])); EXPECT_FALSE(x.HasMember(keys[0]));
EXPECT_EQ(x.MemberBegin(), itr); EXPECT_EQ(x.MemberBegin(), itr);
EXPECT_EQ(9, x.MemberEnd() - x.MemberBegin()); EXPECT_EQ(9u, x.MemberCount());
for (; itr != x.MemberEnd(); ++itr) { for (; itr != x.MemberEnd(); ++itr) {
int i = (itr - x.MemberBegin()) + 1; int i = (itr - x.MemberBegin()) + 1;
EXPECT_STREQ(itr->name.GetString(), keys[i]); EXPECT_STREQ(itr->name.GetString(), keys[i]);
...@@ -945,7 +948,7 @@ TEST(Value, Object) { ...@@ -945,7 +948,7 @@ TEST(Value, Object) {
itr = x.EraseMember(x.MemberEnd() - 1); itr = x.EraseMember(x.MemberEnd() - 1);
EXPECT_FALSE(x.HasMember(keys[9])); EXPECT_FALSE(x.HasMember(keys[9]));
EXPECT_EQ(x.MemberEnd(), itr); EXPECT_EQ(x.MemberEnd(), itr);
EXPECT_EQ(8, x.MemberEnd() - x.MemberBegin()); EXPECT_EQ(8u, x.MemberCount());
for (; itr != x.MemberEnd(); ++itr) { for (; itr != x.MemberEnd(); ++itr) {
int i = (itr - x.MemberBegin()) + 1; int i = (itr - x.MemberBegin()) + 1;
EXPECT_STREQ(itr->name.GetString(), keys[i]); EXPECT_STREQ(itr->name.GetString(), keys[i]);
...@@ -956,7 +959,7 @@ TEST(Value, Object) { ...@@ -956,7 +959,7 @@ TEST(Value, Object) {
itr = x.EraseMember(x.MemberBegin() + 4); itr = x.EraseMember(x.MemberBegin() + 4);
EXPECT_FALSE(x.HasMember(keys[5])); EXPECT_FALSE(x.HasMember(keys[5]));
EXPECT_EQ(x.MemberBegin() + 4, itr); EXPECT_EQ(x.MemberBegin() + 4, itr);
EXPECT_EQ(7, x.MemberEnd() - x.MemberBegin()); EXPECT_EQ(7u, x.MemberCount());
for (; itr != x.MemberEnd(); ++itr) { for (; itr != x.MemberEnd(); ++itr) {
int i = (itr - x.MemberBegin()); int i = (itr - x.MemberBegin());
i += (i<4) ? 1 : 2; i += (i<4) ? 1 : 2;
...@@ -980,7 +983,7 @@ TEST(Value, Object) { ...@@ -980,7 +983,7 @@ TEST(Value, Object) {
EXPECT_EQ(x.MemberBegin() + first, itr); EXPECT_EQ(x.MemberBegin() + first, itr);
size_t removeCount = last - first; size_t removeCount = last - first;
EXPECT_EQ(n - removeCount, size_t(x.MemberEnd() - x.MemberBegin())); EXPECT_EQ(n - removeCount, x.MemberCount());
for (unsigned i = 0; i < first; i++) for (unsigned i = 0; i < first; i++)
EXPECT_EQ(i, x[keys[i]][0u].GetUint()); EXPECT_EQ(i, x[keys[i]][0u].GetUint());
for (unsigned i = first; i < n - removeCount; i++) for (unsigned i = first; i < n - removeCount; i++)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment