Commit 20a9cd28 authored by ecorm's avatar ecorm

Merge branch 'master' into issue123movesupport

parents fd12dcb3 c1a57c37
...@@ -121,9 +121,7 @@ In the following, details about querying individual types are discussed. ...@@ -121,9 +121,7 @@ In the following, details about querying individual types are discussed.
By default, `SizeType` is typedef of `unsigned`. In most systems, array is limited to store up to 2^32-1 elements. By default, `SizeType` is typedef of `unsigned`. In most systems, array is limited to store up to 2^32-1 elements.
You may access the elements in array by integer literal, for example, `a[1]`, `a[2]`. However, `a[0]` will generate a compiler error. It is because two overloaded operators `operator[](SizeType)` and `operator[](const char*)` is available, and C++ can treat `0` as a null pointer. Workarounds: You may access the elements in array by integer literal, for example, `a[0]`, `a[1]`, `a[2]`.
* `a[SizeType(0)]`
* `a[0u]`
Array is similar to `std::vector`, instead of using indices, you may also use iterator to access all the elements. Array is similar to `std::vector`, instead of using indices, you may also use iterator to access all the elements.
~~~~~~~~~~cpp ~~~~~~~~~~cpp
......
...@@ -799,41 +799,32 @@ public: ...@@ -799,41 +799,32 @@ public:
//! Check whether the object is empty. //! Check whether the object is empty.
bool ObjectEmpty() const { RAPIDJSON_ASSERT(IsObject()); return data_.o.size == 0; } bool ObjectEmpty() const { RAPIDJSON_ASSERT(IsObject()); return data_.o.size == 0; }
template <typename T> //! Get a value from an object associated with the name.
GenericValue& operator[](T t) { /*! \pre IsObject() == true
return DoIndex(t, internal::IsPointer<T>()); \tparam T Either \c Ch or \c const \c Ch (template used for disambiguation with \ref operator[](SizeType))
}
template <typename T>
const GenericValue& operator[](T t) const { return const_cast<GenericValue&>(*this)[t]; }
private:
//! Get the value associated with the name.
/*!
\note In version 0.1x, if the member is not found, this function returns a null value. This makes issue 7. \note In version 0.1x, if the member is not found, this function returns a null value. This makes issue 7.
Since 0.2, if the name is not correct, it will assert. Since 0.2, if the name is not correct, it will assert.
If user is unsure whether a member exists, user should use HasMember() first. If user is unsure whether a member exists, user should use HasMember() first.
A better approach is to use FindMember(). A better approach is to use FindMember().
\note Linear time complexity. \note Linear time complexity.
*/ */
template <typename T>
GenericValue& DoIndex(const Ch* name, internal::TrueType) { RAPIDJSON_DISABLEIF_RETURN((internal::NotExpr<internal::IsSame<typename internal::RemoveConst<T>::Type, Ch> >),(GenericValue&)) operator[](T* name) {
GenericValue n(StringRef(name)); GenericValue n(StringRef(name));
return (*this)[n]; return (*this)[n];
} }
template <typename T>
RAPIDJSON_DISABLEIF_RETURN((internal::NotExpr<internal::IsSame<typename internal::RemoveConst<T>::Type, Ch> >),(const GenericValue&)) operator[](T* name) const { return const_cast<GenericValue&>(*this)[name]; }
//! Get an element from array by index. //! Get a value from an object associated with the name.
/*! \param index Zero-based index of element. /*! \pre IsObject() == true
*/ \tparam SourceAllocator Allocator of the \c name value
GenericValue& DoIndex(SizeType index, internal::FalseType) {
RAPIDJSON_ASSERT(IsArray());
RAPIDJSON_ASSERT(index < data_.a.size);
return data_.a.elements[index];
}
public: \note Compared to \ref operator[](T*), this version is faster because it does not need a StrLen().
// This version is faster because it does not need a StrLen(). And it can also handle strings with embedded null characters.
// It can also handle string with null character.
\note Linear time complexity.
*/
template <typename SourceAllocator> template <typename SourceAllocator>
GenericValue& operator[](const GenericValue<Encoding, SourceAllocator>& name) { GenericValue& operator[](const GenericValue<Encoding, SourceAllocator>& name) {
MemberIterator member = FindMember(name); MemberIterator member = FindMember(name);
...@@ -1154,6 +1145,18 @@ public: ...@@ -1154,6 +1145,18 @@ public:
data_.a.size = 0; data_.a.size = 0;
} }
//! Get an element from array by index.
/*! \pre IsArray() == true
\param index Zero-based index of element.
\see operator[](T*)
*/
GenericValue& operator[](SizeType index) {
RAPIDJSON_ASSERT(IsArray());
RAPIDJSON_ASSERT(index < data_.a.size);
return data_.a.elements[index];
}
const GenericValue& operator[](SizeType index) const { return const_cast<GenericValue&>(*this)[index]; }
//! Element iterator //! Element iterator
/*! \pre IsArray() == true */ /*! \pre IsArray() == true */
ValueIterator Begin() { RAPIDJSON_ASSERT(IsArray()); return data_.a.elements; } ValueIterator Begin() { RAPIDJSON_ASSERT(IsArray()); return data_.a.elements; }
......
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