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. --- 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 ++-- 10 files changed, 134 insertions(+), 48 deletions(-) (limited to 'src') 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