From f333c75218705ceb64d202285471c01797deae4a Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Wed, 18 Mar 2015 16:00:21 +1100 Subject: [PATCH] json/object: add find method --- json/tree.cpp | 46 ++++++++++++++++++++++++++++++++++------------ json/tree.hpp | 8 ++++++-- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/json/tree.cpp b/json/tree.cpp index e5296ccd..39568267 100644 --- a/json/tree.cpp +++ b/json/tree.cpp @@ -422,18 +422,11 @@ json::tree::object::has (const std::string &key) const } -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); +//----------------------------------------------------------------------------- +json::tree::object::const_iterator +json::tree::object::find (const std::string &key) const +{ + return m_values.find (key); } @@ -449,6 +442,35 @@ json::tree::object::end (void) const { 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& json::tree::object::write (std::ostream &os) const { diff --git a/json/tree.hpp b/json/tree.hpp index dc3e2343..19a85cca 100644 --- a/json/tree.hpp +++ b/json/tree.hpp @@ -134,14 +134,18 @@ namespace json { namespace tree { virtual node& operator[](const std::string &key); virtual bool has (const std::string&) const; - virtual void clear (void); - virtual void erase (const std::string &key); + virtual const_iterator find (const std::string&) const; virtual const_iterator begin (void) const; virtual const_iterator end (void) const; virtual const_iterator cbegin (void) const { return begin (); } 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; };