json/object: add find method

This commit is contained in:
Danny Robson 2015-03-18 16:00:21 +11:00
parent 9230de8c98
commit f333c75218
2 changed files with 40 additions and 14 deletions

View File

@ -422,18 +422,11 @@ json::tree::object::has (const std::string &key) const
} }
void //-----------------------------------------------------------------------------
json::tree::object::clear (void) json::tree::object::const_iterator
{ m_values.clear (); } json::tree::object::find (const std::string &key) const
{
return m_values.find (key);
void
json::tree::object::erase (const std::string &key) {
auto pos = m_values.find (key);
if (pos == m_values.end ())
throw json::error ("erasing invalid key");
m_values.erase (key);
} }
@ -449,6 +442,35 @@ json::tree::object::end (void) const
{ return m_values.end (); } { return m_values.end (); }
//-----------------------------------------------------------------------------
size_t
json::tree::object::size (void) const
{
return m_values.size ();
}
//-----------------------------------------------------------------------------
void
json::tree::object::clear (void)
{
m_values.clear ();
}
//-----------------------------------------------------------------------------
void
json::tree::object::erase (const std::string &key)
{
auto pos = m_values.find (key);
if (pos == m_values.end ())
throw json::error ("erasing invalid key");
m_values.erase (key);
}
//-----------------------------------------------------------------------------
std::ostream& std::ostream&
json::tree::object::write (std::ostream &os) const json::tree::object::write (std::ostream &os) const
{ {

View File

@ -134,14 +134,18 @@ namespace json { namespace tree {
virtual node& operator[](const std::string &key); virtual node& operator[](const std::string &key);
virtual bool has (const std::string&) const; virtual bool has (const std::string&) const;
virtual void clear (void); virtual const_iterator find (const std::string&) const;
virtual void erase (const std::string &key);
virtual const_iterator begin (void) const; virtual const_iterator begin (void) const;
virtual const_iterator end (void) const; virtual const_iterator end (void) const;
virtual const_iterator cbegin (void) const { return begin (); } virtual const_iterator cbegin (void) const { return begin (); }
virtual const_iterator cend (void) const { return end (); } virtual const_iterator cend (void) const { return end (); }
virtual size_t size (void) const;
virtual void clear (void);
virtual void erase (const std::string &key);
virtual std::ostream& write (std::ostream &os) const; virtual std::ostream& write (std::ostream &os) const;
}; };