From 32be8ec384bfd78e189d3de6609e50cf4dd072a2 Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Fri, 16 Oct 2009 22:26:13 +0200 Subject: Fix Node lifecycle and change argument as reference to const reference. --- include/plist/Array.h | 6 ++++-- include/plist/Boolean.h | 4 ++++ include/plist/Data.h | 8 ++++++-- include/plist/Date.h | 4 ++++ include/plist/Dictionary.h | 12 +++++++----- include/plist/Integer.h | 4 ++++ include/plist/Node.h | 8 ++++---- include/plist/Real.h | 4 ++++ include/plist/String.h | 8 ++++++-- include/plist/Utils.h | 4 ++-- src/Array.cpp | 36 ++++++++++++++++++------------------ src/Boolean.cpp | 16 ++++++++++++++++ src/Data.cpp | 21 +++++++++++++++++++-- src/Date.cpp | 15 +++++++++++++++ src/Dictionary.cpp | 20 +++++++++++++------- src/Integer.cpp | 16 ++++++++++++++++ src/Node.cpp | 18 +----------------- src/Real.cpp | 16 ++++++++++++++++ src/String.cpp | 20 ++++++++++++++++++-- src/Utils.cpp | 4 ++-- 20 files changed, 179 insertions(+), 65 deletions(-) diff --git a/include/plist/Array.h b/include/plist/Array.h index f990928..106555d 100644 --- a/include/plist/Array.h +++ b/include/plist/Array.h @@ -34,9 +34,11 @@ class Array : public Structure Array(); Array(plist_t node); Array(Array& a); - Array& operator=(const Array& a); + Array& operator=(Array& a); virtual ~Array(); - + + Node* Clone(); + Node* operator[](unsigned int index); void Append(Node* node); void Insert(Node* node, unsigned int pos); diff --git a/include/plist/Boolean.h b/include/plist/Boolean.h index 01b5b85..917bc9d 100644 --- a/include/plist/Boolean.h +++ b/include/plist/Boolean.h @@ -32,9 +32,13 @@ class Boolean : public Node public : Boolean(); Boolean(plist_t node); + Boolean(Boolean& b); + Boolean& operator=(Boolean& b); Boolean(bool b); virtual ~Boolean(); + Node* Clone(); + void SetValue(bool b); bool GetValue(); }; diff --git a/include/plist/Data.h b/include/plist/Data.h index 92bda0a..86a26d9 100644 --- a/include/plist/Data.h +++ b/include/plist/Data.h @@ -33,10 +33,14 @@ class Data : public Node public : Data(); Data(plist_t node); - Data(std::vector& buff); + Data(Data& d); + Data& operator=(Data& d); + Data(const std::vector& buff); virtual ~Data(); - void SetValue(std::vector& buff); + Node* Clone(); + + void SetValue(const std::vector& buff); std::vector GetValue(); }; diff --git a/include/plist/Date.h b/include/plist/Date.h index 78d8601..e9645aa 100644 --- a/include/plist/Date.h +++ b/include/plist/Date.h @@ -32,9 +32,13 @@ class Date : public Node public : Date(); Date(plist_t node); + Date(Date& d); + Date& operator=(Date& d); Date(uint64_t i); virtual ~Date(); + Node* Clone(); + void SetValue(uint64_t i); uint64_t GetValue(); }; diff --git a/include/plist/Dictionary.h b/include/plist/Dictionary.h index 8468ab5..6169774 100644 --- a/include/plist/Dictionary.h +++ b/include/plist/Dictionary.h @@ -35,17 +35,19 @@ class Dictionary : public Structure Dictionary(); Dictionary(plist_t node); Dictionary(Dictionary& d); - Dictionary& operator=(const Dictionary& d); + Dictionary& operator=(Dictionary& d); virtual ~Dictionary(); - + + Node* Clone(); + typedef std::map::iterator iterator; - Node* operator[](std::string& key); + Node* operator[](const std::string& key); iterator Begin(); iterator End(); - void Insert(std::string& key, Node* node); + void Insert(const std::string& key, Node* node); void Remove(Node* node); - void Remove(std::string& key); + void Remove(const std::string& key); private : std::map _map; diff --git a/include/plist/Integer.h b/include/plist/Integer.h index 823e3a0..3a3a0e3 100644 --- a/include/plist/Integer.h +++ b/include/plist/Integer.h @@ -32,9 +32,13 @@ class Integer : public Node public : Integer(); Integer(plist_t node); + Integer(Integer& i); + Integer& operator=(Integer& i); Integer(uint64_t i); virtual ~Integer(); + Node* Clone(); + void SetValue(uint64_t i); uint64_t GetValue(); }; diff --git a/include/plist/Node.h b/include/plist/Node.h index 3be900a..a59d469 100644 --- a/include/plist/Node.h +++ b/include/plist/Node.h @@ -31,11 +31,11 @@ class Node { public : virtual ~Node(); - Node(Node& node); - Node& operator=(const Node& node); - + + virtual Node* Clone() = 0; + plist_type GetType(); - plist_t GetPlist() const; + plist_t GetPlist(); protected: Node(); diff --git a/include/plist/Real.h b/include/plist/Real.h index 8d898c7..c0095e4 100644 --- a/include/plist/Real.h +++ b/include/plist/Real.h @@ -32,9 +32,13 @@ class Real : public Node public : Real(); Real(plist_t node); + Real(Real& d); + Real& operator=(Real& d); Real(double d); virtual ~Real(); + Node* Clone(); + void SetValue(double d); double GetValue(); }; diff --git a/include/plist/String.h b/include/plist/String.h index 5314065..769c98b 100644 --- a/include/plist/String.h +++ b/include/plist/String.h @@ -33,10 +33,14 @@ class String : public Node public : String(); String(plist_t node); - String(std::string& s); + String(String& s); + String& operator=(String& s); + String(const std::string& s); virtual ~String(); - void SetValue(std::string& s); + Node* Clone(); + + void SetValue(const std::string& s); std::string GetValue(); }; diff --git a/include/plist/Utils.h b/include/plist/Utils.h index b499635..f3e2685 100644 --- a/include/plist/Utils.h +++ b/include/plist/Utils.h @@ -30,8 +30,8 @@ namespace PList class Utils { public: - static Structure* FromXml(std::string& in); - static Structure* FromBin(std::vector& in); + static Structure* FromXml(const std::string& in); + static Structure* FromBin(const std::vector& in); private: Utils(); diff --git a/src/Array.cpp b/src/Array.cpp index 0505a27..b85a114 100644 --- a/src/Array.cpp +++ b/src/Array.cpp @@ -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(); - _node = plist_copy(a.GetPlist()); uint32_t size = plist_array_get_size(_node); @@ -125,7 +119,7 @@ Array::Array(Array& a) } } -Array& Array::operator=(const Array& a) +Array& Array::operator=(PList::Array& a) { plist_free(_node); for (int it = 0; it < _array.size(); it++) @@ -175,12 +169,16 @@ Array& Array::operator=(const Array& a) Array::~Array() { - plist_free(_node); - for (int it = 0; it < _array.size(); it++) - { - delete _array.at(it); - } - _array.clear(); + for (int it = 0; it < _array.size(); it++) + { + delete (_array.at(it)); + } + _array.clear(); +} + +Node* Array::Clone() +{ + return new Array(*this); } Node* Array::operator[](unsigned int index) @@ -192,8 +190,9 @@ void Array::Append(Node* node) { if (node) { - plist_array_append_item(_node, node->GetPlist()); - _array.push_back(node); + Node* clone = node->Clone(); + plist_array_append_item(_node, clone->GetPlist()); + _array.push_back(clone); } } @@ -201,10 +200,11 @@ void Array::Insert(Node* node, unsigned int pos) { if (node) { - plist_array_insert_item(_node, node->GetPlist(), pos); + Node* clone = node->Clone(); + plist_array_insert_item(_node, clone->GetPlist(), pos); std::vector::iterator it = _array.begin(); it += pos; - _array.insert(it, node); + _array.insert(it, clone); } } diff --git a/src/Boolean.cpp b/src/Boolean.cpp index cc704c9..03d17c8 100644 --- a/src/Boolean.cpp +++ b/src/Boolean.cpp @@ -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) { plist_set_bool_val(_node, b); @@ -41,6 +52,11 @@ Boolean::~Boolean() { } +Node* Boolean::Clone() +{ + return new Boolean(*this); +} + void Boolean::SetValue(bool b) { plist_set_bool_val(_node, b); diff --git a/src/Data.cpp b/src/Data.cpp index 53adfa4..87a508a 100644 --- a/src/Data.cpp +++ b/src/Data.cpp @@ -32,7 +32,19 @@ Data::Data(plist_t node) : Node(node) { } -Data::Data(std::vector& buff) : Node(PLIST_DATA) +Data::Data(PList::Data& d) : Node(PLIST_DATA) +{ + std::vector 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& buff) : Node(PLIST_DATA) { plist_set_data_val(_node, &buff[0], buff.size()); } @@ -41,7 +53,12 @@ Data::~Data() { } -void Data::SetValue(std::vector& buff) +Node* Data::Clone() +{ + return new Data(*this); +} + +void Data::SetValue(const std::vector& buff) { plist_set_data_val(_node, &buff[0], buff.size()); } diff --git a/src/Date.cpp b/src/Date.cpp index f6a6f42..18e1d27 100644 --- a/src/Date.cpp +++ b/src/Date.cpp @@ -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) { plist_set_date_val(_node, i, 0); @@ -41,6 +51,11 @@ Date::~Date() { } +Node* Date::Clone() +{ + return new Date(*this); +} + void Date::SetValue(uint64_t i) { plist_set_date_val(_node, i, 0); diff --git a/src/Dictionary.cpp b/src/Dictionary.cpp index 6879e33..15df0b4 100644 --- a/src/Dictionary.cpp +++ b/src/Dictionary.cpp @@ -85,7 +85,7 @@ Dictionary::Dictionary(plist_t node) : Structure() free(it); } -Dictionary::Dictionary(Dictionary& d) +Dictionary::Dictionary(PList::Dictionary& d) : Structure() { for (Dictionary::iterator it = _map.begin(); it != _map.end(); it++) { @@ -142,7 +142,7 @@ Dictionary::Dictionary(Dictionary& d) free(it); } -Dictionary& Dictionary::operator=(const Dictionary& d) +Dictionary& Dictionary::operator=(PList::Dictionary& d) { for (Dictionary::iterator it = _map.begin(); it != _map.end(); it++) { @@ -209,7 +209,12 @@ Dictionary::~Dictionary() _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]; } @@ -224,13 +229,14 @@ Dictionary::iterator Dictionary::End() return _map.end(); } -void Dictionary::Insert(std::string& key, Node* node) +void Dictionary::Insert(const std::string& key, Node* 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]; - _map[key] = node; + _map[key] = clone; } } @@ -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()); delete _map[key]; diff --git a/src/Integer.cpp b/src/Integer.cpp index 2a7429a..cf1ad33 100644 --- a/src/Integer.cpp +++ b/src/Integer.cpp @@ -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) { plist_set_uint_val(_node, i); @@ -41,6 +52,11 @@ Integer::~Integer() { } +Node* Integer::Clone() +{ + return new Integer(*this); +} + void Integer::SetValue(uint64_t i) { plist_set_uint_val(_node, i); diff --git a/src/Node.cpp b/src/Node.cpp index dbcd6d6..ace1990 100644 --- a/src/Node.cpp +++ b/src/Node.cpp @@ -74,22 +74,6 @@ Node::~Node() _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() { if (_node) @@ -98,7 +82,7 @@ plist_type Node::GetType() } } -plist_t Node::GetPlist() const +plist_t Node::GetPlist() { return _node; } diff --git a/src/Real.cpp b/src/Real.cpp index 5416887..e527f6d 100644 --- a/src/Real.cpp +++ b/src/Real.cpp @@ -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) { plist_set_real_val(_node, d); @@ -41,6 +52,11 @@ Real::~Real() { } +Node* Real::Clone() +{ + return new Real(*this); +} + void Real::SetValue(double d) { plist_set_real_val(_node, d); diff --git a/src/String.cpp b/src/String.cpp index 7bf744c..dbc81ac 100644 --- a/src/String.cpp +++ b/src/String.cpp @@ -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()); } @@ -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()); } diff --git a/src/Utils.cpp b/src/Utils.cpp index a9d2459..4e47994 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -54,7 +54,7 @@ static Structure* FromPlist(plist_t root) return ret; } -Structure* Utils::FromXml(std::string& in) +Structure* Utils::FromXml(const std::string& in) { plist_t root = NULL; plist_from_xml(in.c_str(), in.size(), &root); @@ -62,7 +62,7 @@ Structure* Utils::FromXml(std::string& in) return FromPlist(root); } -Structure* Utils::FromBin(std::vector& in) +Structure* Utils::FromBin(const std::vector& in) { plist_t root = NULL; plist_from_bin(&in[0], in.size(), &root); -- cgit v1.1-32-gdbae