json/tree: add clone method

This commit is contained in:
Danny Robson 2015-03-18 15:44:46 +11:00
parent 47cc2adfab
commit 4f0f040f7d
2 changed files with 68 additions and 0 deletions

View File

@ -299,6 +299,20 @@ json::tree::object::~object ()
{ ; }
//-----------------------------------------------------------------------------
std::unique_ptr<json::tree::node>
json::tree::object::clone (void) const
{
auto obj = std::make_unique<json::tree::object> ();
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::node>
json::tree::array::clone (void) const
{
auto ret = std::make_unique<array> ();
for (const auto &i: *this)
ret->insert (i.clone ());
return std::move (ret);
}
//-----------------------------------------------------------------------------
void
json::tree::array::insert (unique_ptr<json::tree::node> &&_value)
{
@ -441,6 +469,14 @@ json::tree::array::write (std::ostream &os) const {
//-----------------------------------------------------------------------------
// String
std::unique_ptr<json::tree::node>
json::tree::string::clone (void) const
{
return std::make_unique<json::tree::string> (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::node>
json::tree::number::clone (void) const
{
return std::make_unique<json::tree::number> (m_value);
}
//-----------------------------------------------------------------------------
std::ostream&
json::tree::number::write (std::ostream &os) const {
os << setprecision (numeric_limits<double>::digits10) << m_value;
@ -476,6 +520,14 @@ json::tree::number::operator ==(const json::tree::number &rhs) const
//-----------------------------------------------------------------------------
// Boolean
std::unique_ptr<json::tree::node>
json::tree::boolean::clone (void) const
{
return std::make_unique<json::tree::boolean> (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::node>
json::tree::null::clone (void) const
{
return std::make_unique<json::tree::null> ();
}
//-----------------------------------------------------------------------------
std::ostream&
json::tree::null::write (std::ostream &os) const {
os << "null";

View File

@ -52,6 +52,7 @@ namespace json { namespace tree {
class node {
public:
virtual ~node () { ; }
virtual std::unique_ptr<node> 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<node> 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<node> 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<node> 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<node> 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<node> 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<node> clone (void) const;
virtual bool operator==(const null&) const { return true; }
virtual bool operator==(const node &rhs) const
{ return rhs == *this; }