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

View File

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