Expose all std::deque members in json::Array

Originally committed to SVN as r5750.
This commit is contained in:
Thomas Goyne 2011-10-17 22:00:18 +00:00
parent 0dee6ea223
commit b4fa4e6f0c
2 changed files with 41 additions and 49 deletions

View file

@ -55,8 +55,6 @@ public:
// element accesses can be chained together, allowing the following
// (when document structure is well-known):
// String str = objInvoices[1]["Customer"]["Company"];
class UnknownElement
{
public:
@ -132,46 +130,40 @@ private:
Imp* m_pImp;
};
/////////////////////////////////////////////////////////////////////////////////
// Array - mimics std::deque<UnknownElement>. The array contents are effectively
// heterogeneous thanks to the ElementUnknown class.
class Array
class Array : private std::deque<UnknownElement>
{
public:
typedef std::deque<UnknownElement> elements;
typedef elements::iterator iterator;
typedef elements::const_iterator const_iterator;
using std::deque<UnknownElement>::back;
using std::deque<UnknownElement>::begin;
using std::deque<UnknownElement>::clear;
using std::deque<UnknownElement>::const_iterator;
using std::deque<UnknownElement>::const_reference;
using std::deque<UnknownElement>::const_reverse_iterator;
using std::deque<UnknownElement>::difference_type;
using std::deque<UnknownElement>::empty;
using std::deque<UnknownElement>::end;
using std::deque<UnknownElement>::front;
using std::deque<UnknownElement>::iterator;
using std::deque<UnknownElement>::max_size;
using std::deque<UnknownElement>::pointer;
using std::deque<UnknownElement>::pop_back;
using std::deque<UnknownElement>::pop_front;
using std::deque<UnknownElement>::push_back;
using std::deque<UnknownElement>::push_front;
using std::deque<UnknownElement>::rbegin;
using std::deque<UnknownElement>::reference;
using std::deque<UnknownElement>::rend;
using std::deque<UnknownElement>::reverse_iterator;
using std::deque<UnknownElement>::size;
using std::deque<UnknownElement>::size_type;
using std::deque<UnknownElement>::swap;
using std::deque<UnknownElement>::value_type;
iterator begin() { return m_Elements.begin(); }
iterator end() { return m_Elements.end(); }
const_iterator begin() const { return m_Elements.begin(); }
const_iterator end() const { return m_Elements.end(); }
iterator insert(iterator it, const UnknownElement& element) { m_Elements.insert(it, element); }
void push_back(const UnknownElement& element) { m_Elements.push_back(element); }
iterator erase(iterator it) { return m_Elements.erase(it); }
void resize(size_t newsize) { m_Elements.resize(newsize); }
void clear() { m_Elements.clear(); }
size_t size() const { return m_Elements.size(); }
bool empty() const { return m_Elements.empty(); }
UnknownElement& front() { return m_Elements.front(); }
const UnknownElement& front() const { return m_Elements.front(); }
UnknownElement& back() { return m_Elements.back(); }
const UnknownElement& back() const { return m_Elements.back(); }
UnknownElement& operator[] (size_t index);
const UnknownElement& operator[] (size_t index) const;
bool operator == (const Array& array) const { return m_Elements == array.m_Elements; }
private:
elements m_Elements;
UnknownElement& operator[](size_t idx);
const UnknownElement& operator[](size_t idx) const;
bool operator==(Array const& rgt) const;
};
/////////////////////////////////////////////////////////////////////////////////
// Object - mimics std::map<std::string, UnknownElement>. The member value
// contents are effectively heterogeneous thanks to the UnknownElement class

View file

@ -222,20 +222,20 @@ inline const UnknownElement& Object::operator [](const std::string& name) const
/////////////////
// Array members
inline UnknownElement& Array::operator[] (size_t index)
{
size_t nMinsize = index + 1; // zero indexed
if (m_Elements.size() < nMinsize)
m_Elements.resize(nMinsize);
return m_Elements[index];
inline UnknownElement& Array::operator[](size_t idx) {
if (idx >= size())
resize(idx + 1);
return std::deque<UnknownElement>::operator[](idx);
}
inline const UnknownElement& Array::operator[] (size_t index) const
{
if (index >= m_Elements.size())
throw Exception("Array out of bounds");
return m_Elements[index];
inline const UnknownElement& Array::operator[](size_t idx) const {
if (idx >= size())
throw Exception("Array out of bounds");
return std::deque<UnknownElement>::operator[](idx);
}
inline bool Array::operator==(Array const& rgt) const {
return *static_cast<const std::deque<UnknownElement> *>(this) == rgt;
}
} // end namespace