Use unique_ptr for all compound node children

This commit is contained in:
Danny Robson 2012-04-12 16:09:49 +10:00
parent 4a092f92cb
commit c0476c553e
2 changed files with 18 additions and 22 deletions

View File

@ -87,9 +87,9 @@ struct parse_context {
if (!nodestack.back ().key->is_string ()) if (!nodestack.back ().key->is_string ())
throw parse_error ("object keys must be strings"); throw parse_error ("object keys must be strings");
json::object *object = (json::object *)nodestack.back ().root; json::object *object = (json::object*)nodestack.back ().root;
object->insert (nodestack.back ().key->to_string (), object->insert (nodestack.back ().key->as_string (),
nodestack.back ().value); unique_ptr<json::node> (nodestack.back ().value));
nodestack.back ().key = NULL; nodestack.back ().key = NULL;
nodestack.back ().value = NULL; nodestack.back ().value = NULL;
} }
@ -99,7 +99,7 @@ struct parse_context {
check (nodestack.back ().value); check (nodestack.back ().value);
json::array *array = (json::array *)nodestack.back ().root; json::array *array = (json::array *)nodestack.back ().root;
array->insert (nodestack.back ().value); array->insert (unique_ptr<json::node> (nodestack.back ().value));
nodestack.back ().value = NULL; nodestack.back ().value = NULL;
} }
@ -327,10 +327,8 @@ json::node::operator[] (unsigned int idx) const
* Object * Object
*/ */
json::object::~object () { json::object::~object ()
for (auto i = m_values.begin (); i != m_values.end (); ++i) { ; }
delete i->second;
}
bool bool
@ -350,8 +348,8 @@ json::object::operator ==(const json::object &rhs) const {
void void
json::object::insert (const std::string &_key, json::node* value) json::object::insert (const std::string &_key, unique_ptr<json::node> &&value)
{ m_values[_key] = value; } { m_values[_key] = move(value); }
const json::node& const json::node&
@ -390,15 +388,13 @@ json::object::write (std::ostream &os) const {
* Array * Array
*/ */
json::array::~array() { json::array::~array()
for (auto i = m_values.begin(); i != m_values.end (); ++i) { ; }
delete *i;
}
void void
json::array::insert (json::node *_value) json::array::insert (unique_ptr<json::node> &&_value)
{ m_values.push_back (_value); } { m_values.push_back (move (_value)); }
bool bool

View File

@ -85,7 +85,7 @@ namespace json {
/// Represents a JSON object, and contains its children. /// Represents a JSON object, and contains its children.
class object : public node { class object : public node {
protected: protected:
std::map<std::string, node*> m_values; std::map<std::string, std::unique_ptr<node>> m_values;
public: public:
object () { ; } object () { ; }
@ -97,7 +97,7 @@ namespace json {
virtual bool operator==(const node &rhs) const virtual bool operator==(const node &rhs) const
{ return rhs == *this; } { return rhs == *this; }
virtual void insert (const std::string &_key, 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 std::ostream& write (std::ostream &os) const; virtual std::ostream& write (std::ostream &os) const;
@ -107,11 +107,11 @@ namespace json {
/// Represents a JSON array, and contains its children. /// Represents a JSON array, and contains its children.
class array : public node { class array : public node {
public: public:
typedef std::vector<node*>::iterator array_iterator; typedef std::vector<std::unique_ptr<node>>::iterator array_iterator;
typedef std::vector<node*>::const_iterator const_array_iterator; typedef std::vector<std::unique_ptr<node>>::const_iterator const_array_iterator;
protected: protected:
std::vector<node*> m_values; std::vector<std::unique_ptr<node>> m_values;
public: public:
virtual ~array(); virtual ~array();
@ -132,7 +132,7 @@ namespace json {
virtual const_array_iterator begin (void) const { return m_values.begin (); } virtual const_array_iterator begin (void) const { return m_values.begin (); }
virtual const_array_iterator end (void) const { return m_values.end (); } virtual const_array_iterator end (void) const { return m_values.end (); }
virtual void insert (json::node *_value); virtual void insert (std::unique_ptr<json::node> &&_value);
virtual std::ostream& write (std::ostream &os) const; virtual std::ostream& write (std::ostream &os) const;
}; };