Add object helper

This commit is contained in:
Milo Yip 2016-02-14 13:49:52 +08:00
parent 923db0e641
commit 1634395378
3 changed files with 217 additions and 22 deletions

View File

@ -492,10 +492,27 @@ struct TypeHelper<ValueType, typename ValueType::ConstArray> {
static ArratType Get(const ValueType& v) { return v.GetArray(); } static ArratType Get(const ValueType& v) { return v.GetArray(); }
}; };
template<typename ValueType>
struct TypeHelper<ValueType, typename ValueType::Object> {
typedef typename ValueType::Object ArratType;
static bool Is(const ValueType& v) { return v.IsObject(); }
static ArratType Get(ValueType& v) { return v.GetObject(); }
static ValueType& Set(ValueType& v, ArratType data) { return v.SetObject(data); }
static ValueType& Set(ValueType& v, ArratType data, typename ValueType::AllocatorType&) { return v.SetObject(data); }
};
template<typename ValueType>
struct TypeHelper<ValueType, typename ValueType::ConstObject> {
typedef typename ValueType::ConstObject ArratType;
static bool Is(const ValueType& v) { return v.IsObject(); }
static ArratType Get(const ValueType& v) { return v.GetObject(); }
};
} // namespace internal } // namespace internal
template <bool, typename> // Forward declarations
class GenericArray; template <bool, typename> class GenericArray;
template <bool, typename> class GenericObject;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// GenericValue // GenericValue
@ -526,6 +543,8 @@ public:
typedef GenericValue<Encoding, Allocator> ValueType; //!< Value type of itself. typedef GenericValue<Encoding, Allocator> ValueType; //!< Value type of itself.
typedef GenericArray<false, ValueType> Array; typedef GenericArray<false, ValueType> Array;
typedef GenericArray<true, ValueType> ConstArray; typedef GenericArray<true, ValueType> ConstArray;
typedef GenericObject<false, ValueType> Object;
typedef GenericObject<true, ValueType> ConstObject;
//!@name Constructors and destructor. //!@name Constructors and destructor.
//@{ //@{
@ -951,6 +970,9 @@ public:
/*! \post IsObject() == true */ /*! \post IsObject() == true */
GenericValue& SetObject() { this->~GenericValue(); new (this) GenericValue(kObjectType); return *this; } GenericValue& SetObject() { this->~GenericValue(); new (this) GenericValue(kObjectType); return *this; }
//! Set this value with an object.
GenericValue& SetObject(Object& o) { return *this = *o.ptr_; }
//! Get the number of members in the object. //! Get the number of members in the object.
SizeType MemberCount() const { RAPIDJSON_ASSERT(IsObject()); return data_.o.size; } SizeType MemberCount() const { RAPIDJSON_ASSERT(IsObject()); return data_.o.size; }
@ -1397,6 +1419,9 @@ public:
return false; return false;
} }
Object GetObject() { RAPIDJSON_ASSERT(IsObject()); return Object(*this); }
ConstObject GetObject() const { RAPIDJSON_ASSERT(IsObject()); return ConstObject(*this); }
//@} //@}
//!@name Array //!@name Array
@ -2321,7 +2346,7 @@ public:
typedef ValueT PlainType; typedef ValueT PlainType;
typedef typename internal::MaybeAddConst<Const,PlainType>::Type ValueType; typedef typename internal::MaybeAddConst<Const,PlainType>::Type ValueType;
typedef ValueType* ValueIterator; // This may be const or non-const iterator typedef ValueType* ValueIterator; // This may be const or non-const iterator
typedef const ValueType* ConstValueIterator; typedef const ValueT* ConstValueIterator;
typedef typename ValueType::AllocatorType AllocatorType; typedef typename ValueType::AllocatorType AllocatorType;
typedef typename ValueType::StringRefType StringRefType; typedef typename ValueType::StringRefType StringRefType;
@ -2346,9 +2371,7 @@ public:
const GenericArray& PushBack(ValueType&& value, AllocatorType& allocator) const { ptr_->PushBack(value, allocator); return *this; } const GenericArray& PushBack(ValueType&& value, AllocatorType& allocator) const { ptr_->PushBack(value, allocator); return *this; }
#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS #endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS
const GenericArray& PushBack(StringRefType value, AllocatorType& allocator) const { ptr_->PushBack(value, allocator); return *this; } const GenericArray& PushBack(StringRefType value, AllocatorType& allocator) const { ptr_->PushBack(value, allocator); return *this; }
template <typename T> template <typename T> RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>, internal::IsGenericValue<T> >), (const GenericArray&)) PushBack(T value, AllocatorType& allocator) const { ptr_->PushBack(value, allocator); return *this; }
RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>, internal::IsGenericValue<T> >), (const GenericArray&))
PushBack(T value, AllocatorType& allocator) const { ptr_->PushBack(value, allocator); return *this; }
const GenericArray& PopBack() const { ptr_->PopBack(); return *this; } const GenericArray& PopBack() const { ptr_->PopBack(); return *this; }
ValueIterator Erase(ConstValueIterator pos) const { return ptr_->Erase(pos); } ValueIterator Erase(ConstValueIterator pos) const { return ptr_->Erase(pos); }
ValueIterator Erase(ConstValueIterator first, ConstValueIterator last) const { return ptr_->Erase(first, last); } ValueIterator Erase(ConstValueIterator first, ConstValueIterator last) const { return ptr_->Erase(first, last); }
@ -2363,6 +2386,92 @@ private:
ValueType* ptr_; ValueType* ptr_;
}; };
//! Helper class for accessing Value of array type.
/*!
Instance of this helper class is obtained by \c GenericValue::GetArray().
In addition to all APIs for array type, it provides range-based for loop if \c RAPIDJSON_HAS_CXX11_RANGE_FOR=1.
*/
template <bool Const, typename ValueT>
class GenericObject {
public:
typedef GenericObject<true, ValueT> ConstObject;
typedef GenericObject<false, ValueT> Object;
typedef ValueT PlainType;
typedef typename internal::MaybeAddConst<Const,PlainType>::Type ValueType;
typedef GenericMemberIterator<Const, typename ValueT::EncodingType, typename ValueT::AllocatorType> MemberIterator; // This may be const or non-const iterator
typedef GenericMemberIterator<true, typename ValueT::EncodingType, typename ValueT::AllocatorType> ConstMemberIterator;
typedef typename ValueType::AllocatorType AllocatorType;
typedef typename ValueType::StringRefType StringRefType;
typedef typename ValueType::EncodingType EncodingType;
typedef typename ValueType::Ch Ch;
template <typename, typename>
friend class GenericValue;
GenericObject() : ptr_() {}
GenericObject(const GenericObject& rhs) : ptr_(rhs.ptr_) {}
GenericObject& operator=(const GenericObject& rhs) { ptr_ = rhs.ptr_; return *this; }
~GenericObject() {}
SizeType MemberCount() const { return ptr_->MemberCount(); }
bool ObjectEmpty() const { return ptr_->ObjectEmpty(); }
ValueType& operator[](Ch* name) const { return (*ptr_)[name]; }
template <typename SourceAllocator> ValueType& operator[](const GenericValue<EncodingType, SourceAllocator>& name) const { return (*ptr_)[name]; }
#if RAPIDJSON_HAS_STDSTRING
ValueType& operator[](const std::basic_string<Ch>& name) const { return (*ptr_)[name]; }
#endif
MemberIterator MemberBegin() const { return ptr_->MemberBegin(); }
MemberIterator MemberEnd() const { return ptr_->MemberEnd(); }
bool HasMember(const Ch* name) const { return ptr_->HasMember(name); }
#if RAPIDJSON_HAS_STDSTRING
bool HasMember(const std::basic_string<Ch>& name) const { return ptr_->HasMember(name); }
#endif
template <typename SourceAllocator> bool HasMember(const GenericValue<EncodingType, SourceAllocator>& name) const { return ptr_->HasMember(name); }
MemberIterator FindMember(const Ch* name) const { ptr_->FindMember(name); }
template <typename SourceAllocator> MemberIterator FindMember(const GenericValue<EncodingType, SourceAllocator>& name) const { ptr_->FindMember(name); }
#if RAPIDJSON_HAS_STDSTRING
MemberIterator FindMember(const std::basic_string<Ch>& name) const { return ptr_->FindMember(name); }
#endif
ValueType& AddMember(ValueType& name, ValueType& value, AllocatorType& allocator) const { return ptr_->AddMember(name, value, allocator); }
ValueType& AddMember(ValueType& name, StringRefType value, AllocatorType& allocator) const { return ptr_->AddMember(name, value, allocator); }
#if RAPIDJSON_HAS_STDSTRING
ValueType& AddMember(ValueType& name, std::basic_string<Ch>& value, AllocatorType& allocator) const { return ptr_->AddMember(name, value, allocator); }
#endif
template <typename T> RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>, internal::IsGenericValue<T> >), (ValueType&)) AddMember(ValueType& name, T value, AllocatorType& allocator) const { return ptr_->AddMember(name, value, allocator); }
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
ValueType& AddMember(ValueType&& name, ValueType&& value, AllocatorType& allocator) const { return ptr_->AddMember(name, value, allocator); }
ValueType& AddMember(ValueType&& name, ValueType& value, AllocatorType& allocator) const { return ptr_->AddMember(name, value, allocator); }
ValueType& AddMember(ValueType& name, ValueType&& value, AllocatorType& allocator) const { return ptr_->AddMember(name, value, allocator); }
ValueType& AddMember(StringRefType name, ValueType&& value, AllocatorType& allocator) const { return ptr_->AddMember(name, value, allocator); }
#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS
ValueType& AddMember(StringRefType name, ValueType& value, AllocatorType& allocator) const { return ptr_->AddMember(name, value, allocator); }
ValueType& AddMember(StringRefType name, StringRefType value, AllocatorType& allocator) const { return ptr_->AddMember(name, value, allocator); }
template <typename T> RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>, internal::IsGenericValue<T> >), (ValueType&)) AddMember(StringRefType name, T value, AllocatorType& allocator) const { return ptr_->AddMember(name, value, allocator); }
void RemoveAllMembers() { return ptr_->RemoveAllMembers(); }
bool RemoveMember(const Ch* name) const { return ptr_->RemoveMember(name); }
#if RAPIDJSON_HAS_STDSTRING
bool RemoveMember(const std::basic_string<Ch>& name) const { return ptr_->RemoveMember(name); }
#endif
template <typename SourceAllocator> bool RemoveMember(const GenericValue<EncodingType, SourceAllocator>& name) const { return ptr_->RemoveMember(name); }
MemberIterator RemoveMember(MemberIterator m) const { return ptr_->RemoveMember(m); }
MemberIterator EraseMember(ConstMemberIterator pos) const { return ptr_->EraseMember(pos); }
MemberIterator EraseMember(ConstMemberIterator first, ConstMemberIterator last) const { return ptr_->EraseMember(first, last); }
bool EraseMember(const Ch* name) const { ptr_->EraseMember(name); }
#if RAPIDJSON_HAS_STDSTRING
bool EraseMember(const std::basic_string<Ch>& name) const { return EraseMember(ValueType(StringRef(name))); }
#endif
template <typename SourceAllocator> bool EraseMember(const GenericValue<EncodingType, SourceAllocator>& name) const { ptr_->EraseMember(name); }
#if RAPIDJSON_HAS_CXX11_RANGE_FOR
MemberIterator begin() const { return ptr_->MemberBegin(); }
MemberIterator end() const { return ptr_->MemberEnd(); }
#endif
private:
GenericObject(ValueType& value) : ptr_(&value) {}
ValueType* ptr_;
};
RAPIDJSON_NAMESPACE_END RAPIDJSON_NAMESPACE_END
#ifdef _MSC_VER #ifdef _MSC_VER

View File

@ -531,7 +531,7 @@ RAPIDJSON_NAMESPACE_END
#endif #endif
#ifndef RAPIDJSON_HAS_CXX11_RANGE_FOR #ifndef RAPIDJSON_HAS_CXX11_RANGE_FOR
#if defined(__clang) #if defined(__clang__)
#define RAPIDJSON_HAS_CXX11_RANGE_FOR __has_feature(cxx_range_for) #define RAPIDJSON_HAS_CXX11_RANGE_FOR __has_feature(cxx_range_for)
#elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,3,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__)) || \ #elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,3,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__)) || \
(defined(_MSC_VER) && _MSC_VER >= 1600) (defined(_MSC_VER) && _MSC_VER >= 1600)

View File

@ -827,7 +827,7 @@ TEST(Value, SetStringNullException) {
} }
template <typename T, typename Allocator> template <typename T, typename Allocator>
void TestArray(T& x, Allocator& allocator) { static void TestArray(T& x, Allocator& allocator) {
const T& y = x; const T& y = x;
// PushBack() // PushBack()
@ -1092,19 +1092,9 @@ TEST(Value, ArrayHelperRangeFor) {
} }
#endif #endif
TEST(Value, Object) { template <typename T, typename Allocator>
Value x(kObjectType); static void TestObject(T& x, Allocator& allocator) {
const Value& y = x; // const version const T& y = x; // const version
Value::AllocatorType allocator;
EXPECT_EQ(kObjectType, x.GetType());
EXPECT_TRUE(x.IsObject());
EXPECT_TRUE(x.ObjectEmpty());
EXPECT_EQ(0u, x.MemberCount());
EXPECT_EQ(kObjectType, y.GetType());
EXPECT_TRUE(y.IsObject());
EXPECT_TRUE(y.ObjectEmpty());
EXPECT_EQ(0u, y.MemberCount());
// AddMember() // AddMember()
x.AddMember("A", "Apple", allocator); x.AddMember("A", "Apple", allocator);
@ -1345,7 +1335,7 @@ TEST(Value, Object) {
const unsigned n = 10; const unsigned n = 10;
for (unsigned first = 0; first < n; first++) { for (unsigned first = 0; first < n; first++) {
for (unsigned last = first; last <= n; last++) { for (unsigned last = first; last <= n; last++) {
Value(kObjectType).Swap(x); x.RemoveAllMembers();
for (unsigned i = 0; i < n; i++) for (unsigned i = 0; i < n; i++)
x.AddMember(keys[i], Value(kArrayType).PushBack(i, allocator), allocator); x.AddMember(keys[i], Value(kArrayType).PushBack(i, allocator), allocator);
@ -1368,6 +1358,23 @@ TEST(Value, Object) {
x.RemoveAllMembers(); x.RemoveAllMembers();
EXPECT_TRUE(x.ObjectEmpty()); EXPECT_TRUE(x.ObjectEmpty());
EXPECT_EQ(0u, x.MemberCount()); EXPECT_EQ(0u, x.MemberCount());
}
TEST(Value, Object) {
Value x(kObjectType);
const Value& y = x; // const version
Value::AllocatorType allocator;
EXPECT_EQ(kObjectType, x.GetType());
EXPECT_TRUE(x.IsObject());
EXPECT_TRUE(x.ObjectEmpty());
EXPECT_EQ(0u, x.MemberCount());
EXPECT_EQ(kObjectType, y.GetType());
EXPECT_TRUE(y.IsObject());
EXPECT_TRUE(y.ObjectEmpty());
EXPECT_EQ(0u, y.MemberCount());
TestObject(x, allocator);
// SetObject() // SetObject()
Value z; Value z;
@ -1375,6 +1382,85 @@ TEST(Value, Object) {
EXPECT_TRUE(z.IsObject()); EXPECT_TRUE(z.IsObject());
} }
TEST(Value, ObjectHelper) {
Value::AllocatorType allocator;
{
Value x(kObjectType);
Value::Object o = x.GetObject();
TestObject(o, allocator);
}
{
Value x(kObjectType);
Value::Object o = x.GetObject();
o.AddMember("1", 1, allocator);
Value::Object o2(o); // copy constructor
EXPECT_EQ(1, o2.MemberCount());
Value::Object o3; // default constructor
o3 = o; // assignment operator
EXPECT_EQ(1, o3.MemberCount());
Value::ConstObject y = static_cast<const Value&>(x).GetObject();
(void)y;
// y.AddMember("1", 1, allocator); // should not compile
// Templated functions
x.RemoveAllMembers();
EXPECT_TRUE(x.Is<Value::Object>());
EXPECT_TRUE(x.Is<Value::ConstObject>());
o.AddMember("1", 1, allocator);
o = x.Get<Value::Object>();
EXPECT_EQ(1, o["1"].GetInt());
EXPECT_EQ(1, x.Get<Value::ConstObject>()["1"].GetInt());
Value x2;
x2.Set<Value::Object>(o);
EXPECT_TRUE(x.IsNull());
EXPECT_EQ(1, x2.Get<Value::Object>()["1"].GetInt());
}
}
#if RAPIDJSON_HAS_CXX11_RANGE_FOR
TEST(Value, ObjectHelperRangeFor) {
Value::AllocatorType allocator;
Value x(kObjectType);
for (int i = 0; i < 10; i++) {
char name[10];
Value n(name, static_cast<SizeType>(sprintf(name, "%d", i)), allocator);
x.AddMember(n, i, allocator);
}
{
int i = 0;
for (auto& m : x.GetObject()) {
char name[10];
sprintf(name, "%d", i);
EXPECT_STREQ(name, m.name.GetString());
EXPECT_EQ(i, m.value.GetInt());
i++;
}
EXPECT_EQ(i, 10);
}
{
int i = 0;
for (const auto& m : const_cast<const Value&>(x).GetObject()) {
char name[10];
sprintf(name, "%d", i);
EXPECT_STREQ(name, m.name.GetString());
EXPECT_EQ(i, m.value.GetInt());
i++;
}
EXPECT_EQ(i, 10);
}
// Object a = x.GetObject();
// Object ca = const_cast<const Value&>(x).GetObject();
}
#endif
TEST(Value, EraseMember_String) { TEST(Value, EraseMember_String) {
Value::AllocatorType allocator; Value::AllocatorType allocator;
Value x(kObjectType); Value x(kObjectType);