diff --git a/json/tree.cpp b/json/tree.cpp index 359728de..bb5ccbd1 100644 --- a/json/tree.cpp +++ b/json/tree.cpp @@ -299,6 +299,20 @@ json::tree::object::~object () { ; } +//----------------------------------------------------------------------------- +std::unique_ptr +json::tree::object::clone (void) const +{ + auto obj = std::make_unique (); + + for (auto &i: *this) + obj->insert (i.first, i.second->clone ()); + + return std::move (obj); +} + + +//----------------------------------------------------------------------------- bool json::tree::object::operator ==(const json::tree::object &rhs) const { for (auto i = rhs.m_values.begin (), j = m_values.begin (); @@ -402,6 +416,20 @@ json::tree::array::~array() } +//----------------------------------------------------------------------------- +std::unique_ptr +json::tree::array::clone (void) const +{ + auto ret = std::make_unique (); + + for (const auto &i: *this) + ret->insert (i.clone ()); + + return std::move (ret); +} + + +//----------------------------------------------------------------------------- void json::tree::array::insert (unique_ptr &&_value) { @@ -441,6 +469,14 @@ json::tree::array::write (std::ostream &os) const { //----------------------------------------------------------------------------- // String +std::unique_ptr +json::tree::string::clone (void) const +{ + return std::make_unique (m_value); +} + + +//----------------------------------------------------------------------------- std::ostream& json::tree::string::write (std::ostream &os) const { os << '"' << m_value << '"'; @@ -461,6 +497,14 @@ json::tree::string::operator ==(const char *rhs) const //----------------------------------------------------------------------------- // Number +std::unique_ptr +json::tree::number::clone (void) const +{ + return std::make_unique (m_value); +} + + +//----------------------------------------------------------------------------- std::ostream& json::tree::number::write (std::ostream &os) const { os << setprecision (numeric_limits::digits10) << m_value; @@ -476,6 +520,14 @@ json::tree::number::operator ==(const json::tree::number &rhs) const //----------------------------------------------------------------------------- // Boolean +std::unique_ptr +json::tree::boolean::clone (void) const +{ + return std::make_unique (m_value); +} + + +//----------------------------------------------------------------------------- std::ostream& json::tree::boolean::write (std::ostream &os) const { os << (m_value ? "true" : "false"); @@ -490,6 +542,14 @@ json::tree::boolean::operator ==(const json::tree::boolean &rhs) const //----------------------------------------------------------------------------- // Null +std::unique_ptr +json::tree::null::clone (void) const +{ + return std::make_unique (); +} + + +//----------------------------------------------------------------------------- std::ostream& json::tree::null::write (std::ostream &os) const { os << "null"; diff --git a/json/tree.hpp b/json/tree.hpp index 8f5d8d81..4cabc495 100644 --- a/json/tree.hpp +++ b/json/tree.hpp @@ -52,6 +52,7 @@ namespace json { namespace tree { class node { public: virtual ~node () { ; } + virtual std::unique_ptr clone (void) const = 0; virtual const object& as_object (void) const; virtual const array& as_array (void) const; @@ -103,6 +104,7 @@ namespace json { namespace tree { public: virtual ~object (); + virtual std::unique_ptr clone (void) const; virtual const object& as_object (void) const { return *this; } virtual bool is_object (void) const { return true; } @@ -139,6 +141,7 @@ namespace json { namespace tree { public: virtual ~array(); + virtual std::unique_ptr clone (void) const; virtual const array& as_array (void) const { return *this; } virtual bool is_array (void) const { return true; } @@ -171,6 +174,7 @@ namespace json { namespace tree { explicit string (const std::string &_value): m_value (_value) { ; } explicit string (const char *_value): m_value (_value) { ; } string (const char *_first, const char *_last): m_value (_first, _last) { ; } + virtual std::unique_ptr clone (void) const; virtual const string& as_string (void) const { return *this; } virtual bool is_string (void) const { return true; } @@ -197,6 +201,7 @@ namespace json { namespace tree { explicit number (double _value): m_value (_value) { ; } explicit number (int _value): m_value (_value) { ; } explicit number (size_t _value): m_value (_value) { ; } + virtual std::unique_ptr clone (void) const; virtual const number& as_number (void) const { return *this; } virtual bool is_number (void) const { return true; } @@ -218,6 +223,7 @@ namespace json { namespace tree { public: explicit boolean (bool _value): m_value (_value) { ; } + virtual std::unique_ptr clone (void) const; virtual const boolean& as_boolean (void) const { return *this; } virtual bool is_boolean (void) const { return true; } @@ -235,6 +241,8 @@ namespace json { namespace tree { /// Represents a JSON null value. class null : public node { public: + virtual std::unique_ptr clone (void) const; + virtual bool operator==(const null&) const { return true; } virtual bool operator==(const node &rhs) const { return rhs == *this; }