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 // element accesses can be chained together, allowing the following
// (when document structure is well-known): // (when document structure is well-known):
// String str = objInvoices[1]["Customer"]["Company"]; // String str = objInvoices[1]["Customer"]["Company"];
class UnknownElement class UnknownElement
{ {
public: public:
@ -132,46 +130,40 @@ private:
Imp* m_pImp; Imp* m_pImp;
}; };
class Array : private std::deque<UnknownElement>
/////////////////////////////////////////////////////////////////////////////////
// Array - mimics std::deque<UnknownElement>. The array contents are effectively
// heterogeneous thanks to the ElementUnknown class.
class Array
{ {
public: public:
typedef std::deque<UnknownElement> elements; using std::deque<UnknownElement>::back;
typedef elements::iterator iterator; using std::deque<UnknownElement>::begin;
typedef elements::const_iterator const_iterator; 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(); } UnknownElement& operator[](size_t idx);
iterator end() { return m_Elements.end(); } const UnknownElement& operator[](size_t idx) const;
const_iterator begin() const { return m_Elements.begin(); } bool operator==(Array const& rgt) const;
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;
}; };
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
// Object - mimics std::map<std::string, UnknownElement>. The member value // Object - mimics std::map<std::string, UnknownElement>. The member value
// contents are effectively heterogeneous thanks to the UnknownElement class // 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 // Array members
inline UnknownElement& Array::operator[](size_t idx) {
inline UnknownElement& Array::operator[] (size_t index) if (idx >= size())
{ resize(idx + 1);
size_t nMinsize = index + 1; // zero indexed return std::deque<UnknownElement>::operator[](idx);
if (m_Elements.size() < nMinsize)
m_Elements.resize(nMinsize);
return m_Elements[index];
} }
inline const UnknownElement& Array::operator[] (size_t index) const inline const UnknownElement& Array::operator[](size_t idx) const {
{ if (idx >= size())
if (index >= m_Elements.size()) throw Exception("Array out of bounds");
throw Exception("Array out of bounds"); return std::deque<UnknownElement>::operator[](idx);
return m_Elements[index]; }
inline bool Array::operator==(Array const& rgt) const {
return *static_cast<const std::deque<UnknownElement> *>(this) == rgt;
} }
} // end namespace } // end namespace