json/tree: add mutable indexing operators

This commit is contained in:
Danny Robson 2015-03-18 15:52:33 +11:00
parent a2535e2006
commit 2592cd0442
2 changed files with 35 additions and 4 deletions

View File

@ -320,6 +320,19 @@ bool json::tree::node::operator==(const char *rhs) const {
} }
//-----------------------------------------------------------------------------
json::tree::node&
json::tree::node::operator[] (const std::string &key)
{ return as_object ()[key]; }
//-----------------------------------------------------------------------------
json::tree::node&
json::tree::node::operator[] (unsigned int idx)
{ return as_array()[idx]; }
//-----------------------------------------------------------------------------
const json::tree::node& const json::tree::node&
json::tree::node::operator[] (const std::string &key) const json::tree::node::operator[] (const std::string &key) const
{ return as_object ()[key]; } { return as_object ()[key]; }
@ -486,6 +499,23 @@ json::tree::array::operator ==(const json::tree::array &rhs) const {
} }
//-----------------------------------------------------------------------------
json::tree::node&
json::tree::array::operator[] (unsigned int idx)
{
return *m_values[idx];
}
//-----------------------------------------------------------------------------
const json::tree::node&
json::tree::array::operator[] (unsigned int idx) const
{
return *m_values[idx];
}
//-----------------------------------------------------------------------------
std::ostream& std::ostream&
json::tree::array::write (std::ostream &os) const { json::tree::array::write (std::ostream &os) const {
os << "[\n"; os << "[\n";

View File

@ -96,6 +96,8 @@ namespace json { namespace tree {
virtual bool operator==(const char *rhs) const; virtual bool operator==(const char *rhs) const;
virtual bool operator!=(const char *rhs) const { return !(*this == rhs); } virtual bool operator!=(const char *rhs) const { return !(*this == rhs); }
virtual node& operator[] (const std::string&);
virtual node& operator[] (unsigned int);
virtual const node& operator[] (const std::string&) const; virtual const node& operator[] (const std::string&) const;
virtual const node& operator[] (unsigned int) const; virtual const node& operator[] (unsigned int) const;
@ -130,6 +132,7 @@ namespace json { namespace tree {
virtual void insert (const std::string &key, std::unique_ptr<node>&& value); virtual void insert (const std::string &key, std::unique_ptr<node>&& value);
virtual const node& operator[](const std::string &key) const; virtual const node& operator[](const std::string &key) const;
virtual bool has (const std::string&) const; virtual bool has (const std::string&) const;
virtual node& operator[](const std::string &key);
virtual void clear (void); virtual void clear (void);
virtual void erase (const std::string &key); virtual void erase (const std::string &key);
@ -169,10 +172,8 @@ namespace json { namespace tree {
virtual size_t size (void) const virtual size_t size (void) const
{ return m_values.size (); } { return m_values.size (); }
virtual node& operator [](unsigned int idx) virtual node& operator [](unsigned int idx);
{ return *m_values[idx]; } virtual const node& operator [](unsigned int idx) const;
virtual const node& operator [](unsigned int idx) const
{ return *m_values[idx]; }
virtual const_iterator begin (void) const { return const_iterator (m_values.begin ()); } virtual const_iterator begin (void) const { return const_iterator (m_values.begin ()); }
virtual const_iterator end (void) const { return const_iterator (m_values.end ()); } virtual const_iterator end (void) const { return const_iterator (m_values.end ()); }