Commit 32be8ec3 authored by Jonathan Beck's avatar Jonathan Beck

Fix Node lifecycle and change argument as reference to const reference.

parent 8aeef4dd
...@@ -34,9 +34,11 @@ class Array : public Structure ...@@ -34,9 +34,11 @@ class Array : public Structure
Array(); Array();
Array(plist_t node); Array(plist_t node);
Array(Array& a); Array(Array& a);
Array& operator=(const Array& a); Array& operator=(Array& a);
virtual ~Array(); virtual ~Array();
Node* Clone();
Node* operator[](unsigned int index); Node* operator[](unsigned int index);
void Append(Node* node); void Append(Node* node);
void Insert(Node* node, unsigned int pos); void Insert(Node* node, unsigned int pos);
......
...@@ -32,9 +32,13 @@ class Boolean : public Node ...@@ -32,9 +32,13 @@ class Boolean : public Node
public : public :
Boolean(); Boolean();
Boolean(plist_t node); Boolean(plist_t node);
Boolean(Boolean& b);
Boolean& operator=(Boolean& b);
Boolean(bool b); Boolean(bool b);
virtual ~Boolean(); virtual ~Boolean();
Node* Clone();
void SetValue(bool b); void SetValue(bool b);
bool GetValue(); bool GetValue();
}; };
......
...@@ -33,10 +33,14 @@ class Data : public Node ...@@ -33,10 +33,14 @@ class Data : public Node
public : public :
Data(); Data();
Data(plist_t node); Data(plist_t node);
Data(std::vector<char>& buff); Data(Data& d);
Data& operator=(Data& d);
Data(const std::vector<char>& buff);
virtual ~Data(); virtual ~Data();
void SetValue(std::vector<char>& buff); Node* Clone();
void SetValue(const std::vector<char>& buff);
std::vector<char> GetValue(); std::vector<char> GetValue();
}; };
......
...@@ -32,9 +32,13 @@ class Date : public Node ...@@ -32,9 +32,13 @@ class Date : public Node
public : public :
Date(); Date();
Date(plist_t node); Date(plist_t node);
Date(Date& d);
Date& operator=(Date& d);
Date(uint64_t i); Date(uint64_t i);
virtual ~Date(); virtual ~Date();
Node* Clone();
void SetValue(uint64_t i); void SetValue(uint64_t i);
uint64_t GetValue(); uint64_t GetValue();
}; };
......
...@@ -35,17 +35,19 @@ class Dictionary : public Structure ...@@ -35,17 +35,19 @@ class Dictionary : public Structure
Dictionary(); Dictionary();
Dictionary(plist_t node); Dictionary(plist_t node);
Dictionary(Dictionary& d); Dictionary(Dictionary& d);
Dictionary& operator=(const Dictionary& d); Dictionary& operator=(Dictionary& d);
virtual ~Dictionary(); virtual ~Dictionary();
Node* Clone();
typedef std::map<std::string,Node*>::iterator iterator; typedef std::map<std::string,Node*>::iterator iterator;
Node* operator[](std::string& key); Node* operator[](const std::string& key);
iterator Begin(); iterator Begin();
iterator End(); iterator End();
void Insert(std::string& key, Node* node); void Insert(const std::string& key, Node* node);
void Remove(Node* node); void Remove(Node* node);
void Remove(std::string& key); void Remove(const std::string& key);
private : private :
std::map<std::string,Node*> _map; std::map<std::string,Node*> _map;
......
...@@ -32,9 +32,13 @@ class Integer : public Node ...@@ -32,9 +32,13 @@ class Integer : public Node
public : public :
Integer(); Integer();
Integer(plist_t node); Integer(plist_t node);
Integer(Integer& i);
Integer& operator=(Integer& i);
Integer(uint64_t i); Integer(uint64_t i);
virtual ~Integer(); virtual ~Integer();
Node* Clone();
void SetValue(uint64_t i); void SetValue(uint64_t i);
uint64_t GetValue(); uint64_t GetValue();
}; };
......
...@@ -31,11 +31,11 @@ class Node ...@@ -31,11 +31,11 @@ class Node
{ {
public : public :
virtual ~Node(); virtual ~Node();
Node(Node& node);
Node& operator=(const Node& node); virtual Node* Clone() = 0;
plist_type GetType(); plist_type GetType();
plist_t GetPlist() const; plist_t GetPlist();
protected: protected:
Node(); Node();
......
...@@ -32,9 +32,13 @@ class Real : public Node ...@@ -32,9 +32,13 @@ class Real : public Node
public : public :
Real(); Real();
Real(plist_t node); Real(plist_t node);
Real(Real& d);
Real& operator=(Real& d);
Real(double d); Real(double d);
virtual ~Real(); virtual ~Real();
Node* Clone();
void SetValue(double d); void SetValue(double d);
double GetValue(); double GetValue();
}; };
......
...@@ -33,10 +33,14 @@ class String : public Node ...@@ -33,10 +33,14 @@ class String : public Node
public : public :
String(); String();
String(plist_t node); String(plist_t node);
String(std::string& s); String(String& s);
String& operator=(String& s);
String(const std::string& s);
virtual ~String(); virtual ~String();
void SetValue(std::string& s); Node* Clone();
void SetValue(const std::string& s);
std::string GetValue(); std::string GetValue();
}; };
......
...@@ -30,8 +30,8 @@ namespace PList ...@@ -30,8 +30,8 @@ namespace PList
class Utils class Utils
{ {
public: public:
static Structure* FromXml(std::string& in); static Structure* FromXml(const std::string& in);
static Structure* FromBin(std::vector<char>& in); static Structure* FromBin(const std::vector<char>& in);
private: private:
Utils(); Utils();
......
...@@ -77,15 +77,9 @@ Array::Array(plist_t node) : Structure() ...@@ -77,15 +77,9 @@ Array::Array(plist_t node) : Structure()
} }
} }
Array::Array(Array& a) Array::Array(PList::Array& a) : Structure()
{ {
plist_free(_node);
for (int it = 0; it < _array.size(); it++)
{
delete _array.at(it);
}
_array.clear(); _array.clear();
_node = plist_copy(a.GetPlist()); _node = plist_copy(a.GetPlist());
uint32_t size = plist_array_get_size(_node); uint32_t size = plist_array_get_size(_node);
...@@ -125,7 +119,7 @@ Array::Array(Array& a) ...@@ -125,7 +119,7 @@ Array::Array(Array& a)
} }
} }
Array& Array::operator=(const Array& a) Array& Array::operator=(PList::Array& a)
{ {
plist_free(_node); plist_free(_node);
for (int it = 0; it < _array.size(); it++) for (int it = 0; it < _array.size(); it++)
...@@ -175,12 +169,16 @@ Array& Array::operator=(const Array& a) ...@@ -175,12 +169,16 @@ Array& Array::operator=(const Array& a)
Array::~Array() Array::~Array()
{ {
plist_free(_node); for (int it = 0; it < _array.size(); it++)
for (int it = 0; it < _array.size(); it++) {
{ delete (_array.at(it));
delete _array.at(it); }
} _array.clear();
_array.clear(); }
Node* Array::Clone()
{
return new Array(*this);
} }
Node* Array::operator[](unsigned int index) Node* Array::operator[](unsigned int index)
...@@ -192,8 +190,9 @@ void Array::Append(Node* node) ...@@ -192,8 +190,9 @@ void Array::Append(Node* node)
{ {
if (node) if (node)
{ {
plist_array_append_item(_node, node->GetPlist()); Node* clone = node->Clone();
_array.push_back(node); plist_array_append_item(_node, clone->GetPlist());
_array.push_back(clone);
} }
} }
...@@ -201,10 +200,11 @@ void Array::Insert(Node* node, unsigned int pos) ...@@ -201,10 +200,11 @@ void Array::Insert(Node* node, unsigned int pos)
{ {
if (node) if (node)
{ {
plist_array_insert_item(_node, node->GetPlist(), pos); Node* clone = node->Clone();
plist_array_insert_item(_node, clone->GetPlist(), pos);
std::vector<Node*>::iterator it = _array.begin(); std::vector<Node*>::iterator it = _array.begin();
it += pos; it += pos;
_array.insert(it, node); _array.insert(it, clone);
} }
} }
......
...@@ -32,6 +32,17 @@ Boolean::Boolean(plist_t node) : Node(node) ...@@ -32,6 +32,17 @@ Boolean::Boolean(plist_t node) : Node(node)
{ {
} }
Boolean::Boolean(PList::Boolean& b) : Node(PLIST_BOOLEAN)
{
plist_set_bool_val(_node, b.GetValue());
}
Boolean& Boolean::operator=(PList::Boolean& b)
{
plist_free(_node);
_node = plist_copy(b.GetPlist());
}
Boolean::Boolean(bool b) : Node(PLIST_BOOLEAN) Boolean::Boolean(bool b) : Node(PLIST_BOOLEAN)
{ {
plist_set_bool_val(_node, b); plist_set_bool_val(_node, b);
...@@ -41,6 +52,11 @@ Boolean::~Boolean() ...@@ -41,6 +52,11 @@ Boolean::~Boolean()
{ {
} }
Node* Boolean::Clone()
{
return new Boolean(*this);
}
void Boolean::SetValue(bool b) void Boolean::SetValue(bool b)
{ {
plist_set_bool_val(_node, b); plist_set_bool_val(_node, b);
......
...@@ -32,7 +32,19 @@ Data::Data(plist_t node) : Node(node) ...@@ -32,7 +32,19 @@ Data::Data(plist_t node) : Node(node)
{ {
} }
Data::Data(std::vector<char>& buff) : Node(PLIST_DATA) Data::Data(PList::Data& d) : Node(PLIST_DATA)
{
std::vector<char> b = d.GetValue();
plist_set_data_val(_node, &b[0], b.size());
}
Data& Data::operator=(PList::Data& b)
{
plist_free(_node);
_node = plist_copy(b.GetPlist());
}
Data::Data(const std::vector<char>& buff) : Node(PLIST_DATA)
{ {
plist_set_data_val(_node, &buff[0], buff.size()); plist_set_data_val(_node, &buff[0], buff.size());
} }
...@@ -41,7 +53,12 @@ Data::~Data() ...@@ -41,7 +53,12 @@ Data::~Data()
{ {
} }
void Data::SetValue(std::vector<char>& buff) Node* Data::Clone()
{
return new Data(*this);
}
void Data::SetValue(const std::vector<char>& buff)
{ {
plist_set_data_val(_node, &buff[0], buff.size()); plist_set_data_val(_node, &buff[0], buff.size());
} }
......
...@@ -32,6 +32,16 @@ Date::Date(plist_t node) : Node(node) ...@@ -32,6 +32,16 @@ Date::Date(plist_t node) : Node(node)
{ {
} }
Date::Date(Date& d) : Node(PLIST_DATE)
{
//TODO
}
Date& Date::operator=(PList::Date& b)
{
//TODO
}
Date::Date(uint64_t i) : Node(PLIST_DATE) Date::Date(uint64_t i) : Node(PLIST_DATE)
{ {
plist_set_date_val(_node, i, 0); plist_set_date_val(_node, i, 0);
...@@ -41,6 +51,11 @@ Date::~Date() ...@@ -41,6 +51,11 @@ Date::~Date()
{ {
} }
Node* Date::Clone()
{
return new Date(*this);
}
void Date::SetValue(uint64_t i) void Date::SetValue(uint64_t i)
{ {
plist_set_date_val(_node, i, 0); plist_set_date_val(_node, i, 0);
......
...@@ -85,7 +85,7 @@ Dictionary::Dictionary(plist_t node) : Structure() ...@@ -85,7 +85,7 @@ Dictionary::Dictionary(plist_t node) : Structure()
free(it); free(it);
} }
Dictionary::Dictionary(Dictionary& d) Dictionary::Dictionary(PList::Dictionary& d) : Structure()
{ {
for (Dictionary::iterator it = _map.begin(); it != _map.end(); it++) for (Dictionary::iterator it = _map.begin(); it != _map.end(); it++)
{ {
...@@ -142,7 +142,7 @@ Dictionary::Dictionary(Dictionary& d) ...@@ -142,7 +142,7 @@ Dictionary::Dictionary(Dictionary& d)
free(it); free(it);
} }
Dictionary& Dictionary::operator=(const Dictionary& d) Dictionary& Dictionary::operator=(PList::Dictionary& d)
{ {
for (Dictionary::iterator it = _map.begin(); it != _map.end(); it++) for (Dictionary::iterator it = _map.begin(); it != _map.end(); it++)
{ {
...@@ -209,7 +209,12 @@ Dictionary::~Dictionary() ...@@ -209,7 +209,12 @@ Dictionary::~Dictionary()
_map.clear(); _map.clear();
} }
Node* Dictionary::operator[](std::string& key) Node* Dictionary::Clone()
{
return new Dictionary(*this);
}
Node* Dictionary::operator[](const std::string& key)
{ {
return _map[key]; return _map[key];
} }
...@@ -224,13 +229,14 @@ Dictionary::iterator Dictionary::End() ...@@ -224,13 +229,14 @@ Dictionary::iterator Dictionary::End()
return _map.end(); return _map.end();
} }
void Dictionary::Insert(std::string& key, Node* node) void Dictionary::Insert(const std::string& key, Node* node)
{ {
if (node) if (node)
{ {
plist_dict_insert_item(_node, key.c_str(), node->GetPlist()); Node* clone = node->Clone();
plist_dict_insert_item(_node, key.c_str(), clone->GetPlist());
delete _map[key]; delete _map[key];
_map[key] = node; _map[key] = clone;
} }
} }
...@@ -247,7 +253,7 @@ void Dictionary::Remove(Node* node) ...@@ -247,7 +253,7 @@ void Dictionary::Remove(Node* node)
} }
} }
void Dictionary::Remove(std::string& key) void Dictionary::Remove(const std::string& key)
{ {
plist_dict_remove_item(_node, key.c_str()); plist_dict_remove_item(_node, key.c_str());
delete _map[key]; delete _map[key];
......
...@@ -32,6 +32,17 @@ Integer::Integer(plist_t node) : Node(node) ...@@ -32,6 +32,17 @@ Integer::Integer(plist_t node) : Node(node)
{ {
} }
Integer::Integer(PList::Integer& i) : Node(PLIST_UINT)
{
plist_set_uint_val(_node, i.GetValue());
}
Integer& Integer::operator=(PList::Integer& i)
{
plist_free(_node);
_node = plist_copy(i.GetPlist());
}
Integer::Integer(uint64_t i) : Node(PLIST_UINT) Integer::Integer(uint64_t i) : Node(PLIST_UINT)
{ {
plist_set_uint_val(_node, i); plist_set_uint_val(_node, i);
...@@ -41,6 +52,11 @@ Integer::~Integer() ...@@ -41,6 +52,11 @@ Integer::~Integer()
{ {
} }
Node* Integer::Clone()
{
return new Integer(*this);
}
void Integer::SetValue(uint64_t i) void Integer::SetValue(uint64_t i)
{ {
plist_set_uint_val(_node, i); plist_set_uint_val(_node, i);
......
...@@ -74,22 +74,6 @@ Node::~Node() ...@@ -74,22 +74,6 @@ Node::~Node()
_node = NULL; _node = NULL;
} }
Node::Node(Node& node)
{
plist_free(_node);
_node = NULL;
_node = plist_copy(_node);
}
Node& Node::operator=(const Node& node)
{
plist_free(_node);
_node = NULL;
_node = plist_copy(_node);
}
plist_type Node::GetType() plist_type Node::GetType()
{ {
if (_node) if (_node)
...@@ -98,7 +82,7 @@ plist_type Node::GetType() ...@@ -98,7 +82,7 @@ plist_type Node::GetType()
} }
} }
plist_t Node::GetPlist() const plist_t Node::GetPlist()
{ {
return _node; return _node;
} }
......
...@@ -32,6 +32,17 @@ Real::Real(plist_t node) : Node(node) ...@@ -32,6 +32,17 @@ Real::Real(plist_t node) : Node(node)
{ {
} }
Real::Real(PList::Real& d) : Node(PLIST_UINT)
{
plist_set_real_val(_node, d.GetValue());
}
Real& Real::operator=(PList::Real& d)
{
plist_free(_node);
_node = plist_copy(d.GetPlist());
}
Real::Real(double d) : Node(PLIST_REAL) Real::Real(double d) : Node(PLIST_REAL)
{ {
plist_set_real_val(_node, d); plist_set_real_val(_node, d);
...@@ -41,6 +52,11 @@ Real::~Real() ...@@ -41,6 +52,11 @@ Real::~Real()
{ {
} }
Node* Real::Clone()
{
return new Real(*this);
}
void Real::SetValue(double d) void Real::SetValue(double d)
{ {
plist_set_real_val(_node, d); plist_set_real_val(_node, d);
......
...@@ -32,7 +32,18 @@ String::String(plist_t node) : Node(node) ...@@ -32,7 +32,18 @@ String::String(plist_t node) : Node(node)
{ {
} }
String::String(std::string& s) : Node(PLIST_STRING) String::String(PList::String& s) : Node(PLIST_UINT)
{
plist_set_string_val(_node, s.GetValue().c_str());
}
String& String::operator=(PList::String& s)
{
plist_free(_node);
_node = plist_copy(s.GetPlist());
}
String::String(const std::string& s) : Node(PLIST_STRING)
{ {
plist_set_string_val(_node, s.c_str()); plist_set_string_val(_node, s.c_str());
} }
...@@ -41,7 +52,12 @@ String::~String() ...@@ -41,7 +52,12 @@ String::~String()
{ {
} }
void String::SetValue(std::string& s) Node* String::Clone()
{
return new String(*this);
}
void String::SetValue(const std::string& s)
{ {
plist_set_string_val(_node, s.c_str()); plist_set_string_val(_node, s.c_str());
} }
......
...@@ -54,7 +54,7 @@ static Structure* FromPlist(plist_t root) ...@@ -54,7 +54,7 @@ static Structure* FromPlist(plist_t root)
return ret; return ret;
} }
Structure* Utils::FromXml(std::string& in) Structure* Utils::FromXml(const std::string& in)
{ {
plist_t root = NULL; plist_t root = NULL;
plist_from_xml(in.c_str(), in.size(), &root); plist_from_xml(in.c_str(), in.size(), &root);
...@@ -62,7 +62,7 @@ Structure* Utils::FromXml(std::string& in) ...@@ -62,7 +62,7 @@ Structure* Utils::FromXml(std::string& in)
return FromPlist(root); return FromPlist(root);
} }
Structure* Utils::FromBin(std::vector<char>& in) Structure* Utils::FromBin(const std::vector<char>& in)
{ {
plist_t root = NULL; plist_t root = NULL;
plist_from_bin(&in[0], in.size(), &root); plist_from_bin(&in[0], in.size(), &root);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment