diff options
| author | 2009-10-16 22:26:13 +0200 | |
|---|---|---|
| committer | 2009-10-16 22:26:13 +0200 | |
| commit | 32be8ec384bfd78e189d3de6609e50cf4dd072a2 (patch) | |
| tree | ac7edad0f19be2a31efaaaaa3acd477dd2f3c233 /src | |
| parent | 8aeef4dd2331445fea8a7a40466b19973e9d09c4 (diff) | |
| download | libplist-32be8ec384bfd78e189d3de6609e50cf4dd072a2.tar.gz libplist-32be8ec384bfd78e189d3de6609e50cf4dd072a2.tar.bz2 | |
Fix Node lifecycle and change argument as reference to const reference.
Diffstat (limited to 'src')
| -rw-r--r-- | src/Array.cpp | 36 | ||||
| -rw-r--r-- | src/Boolean.cpp | 16 | ||||
| -rw-r--r-- | src/Data.cpp | 21 | ||||
| -rw-r--r-- | src/Date.cpp | 15 | ||||
| -rw-r--r-- | src/Dictionary.cpp | 20 | ||||
| -rw-r--r-- | src/Integer.cpp | 16 | ||||
| -rw-r--r-- | src/Node.cpp | 18 | ||||
| -rw-r--r-- | src/Real.cpp | 16 | ||||
| -rw-r--r-- | src/String.cpp | 20 | ||||
| -rw-r--r-- | src/Utils.cpp | 4 |
10 files changed, 134 insertions, 48 deletions
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() | |||
| 77 | } | 77 | } |
| 78 | } | 78 | } |
| 79 | 79 | ||
| 80 | Array::Array(Array& a) | 80 | Array::Array(PList::Array& a) : Structure() |
| 81 | { | 81 | { |
| 82 | plist_free(_node); | ||
| 83 | for (int it = 0; it < _array.size(); it++) | ||
| 84 | { | ||
| 85 | delete _array.at(it); | ||
| 86 | } | ||
| 87 | _array.clear(); | 82 | _array.clear(); |
| 88 | |||
| 89 | _node = plist_copy(a.GetPlist()); | 83 | _node = plist_copy(a.GetPlist()); |
| 90 | uint32_t size = plist_array_get_size(_node); | 84 | uint32_t size = plist_array_get_size(_node); |
| 91 | 85 | ||
| @@ -125,7 +119,7 @@ Array::Array(Array& a) | |||
| 125 | } | 119 | } |
| 126 | } | 120 | } |
| 127 | 121 | ||
| 128 | Array& Array::operator=(const Array& a) | 122 | Array& Array::operator=(PList::Array& a) |
| 129 | { | 123 | { |
| 130 | plist_free(_node); | 124 | plist_free(_node); |
| 131 | for (int it = 0; it < _array.size(); it++) | 125 | for (int it = 0; it < _array.size(); it++) |
| @@ -175,12 +169,16 @@ Array& Array::operator=(const Array& a) | |||
| 175 | 169 | ||
| 176 | Array::~Array() | 170 | Array::~Array() |
| 177 | { | 171 | { |
| 178 | plist_free(_node); | 172 | for (int it = 0; it < _array.size(); it++) |
| 179 | for (int it = 0; it < _array.size(); it++) | 173 | { |
| 180 | { | 174 | delete (_array.at(it)); |
| 181 | delete _array.at(it); | 175 | } |
| 182 | } | 176 | _array.clear(); |
| 183 | _array.clear(); | 177 | } |
| 178 | |||
| 179 | Node* Array::Clone() | ||
| 180 | { | ||
| 181 | return new Array(*this); | ||
| 184 | } | 182 | } |
| 185 | 183 | ||
| 186 | Node* Array::operator[](unsigned int index) | 184 | Node* Array::operator[](unsigned int index) |
| @@ -192,8 +190,9 @@ void Array::Append(Node* node) | |||
| 192 | { | 190 | { |
| 193 | if (node) | 191 | if (node) |
| 194 | { | 192 | { |
| 195 | plist_array_append_item(_node, node->GetPlist()); | 193 | Node* clone = node->Clone(); |
| 196 | _array.push_back(node); | 194 | plist_array_append_item(_node, clone->GetPlist()); |
| 195 | _array.push_back(clone); | ||
| 197 | } | 196 | } |
| 198 | } | 197 | } |
| 199 | 198 | ||
| @@ -201,10 +200,11 @@ void Array::Insert(Node* node, unsigned int pos) | |||
| 201 | { | 200 | { |
| 202 | if (node) | 201 | if (node) |
| 203 | { | 202 | { |
| 204 | plist_array_insert_item(_node, node->GetPlist(), pos); | 203 | Node* clone = node->Clone(); |
| 204 | plist_array_insert_item(_node, clone->GetPlist(), pos); | ||
| 205 | std::vector<Node*>::iterator it = _array.begin(); | 205 | std::vector<Node*>::iterator it = _array.begin(); |
| 206 | it += pos; | 206 | it += pos; |
| 207 | _array.insert(it, node); | 207 | _array.insert(it, clone); |
| 208 | } | 208 | } |
| 209 | } | 209 | } |
| 210 | 210 | ||
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) | |||
| 32 | { | 32 | { |
| 33 | } | 33 | } |
| 34 | 34 | ||
| 35 | Boolean::Boolean(PList::Boolean& b) : Node(PLIST_BOOLEAN) | ||
| 36 | { | ||
| 37 | plist_set_bool_val(_node, b.GetValue()); | ||
| 38 | } | ||
| 39 | |||
| 40 | Boolean& Boolean::operator=(PList::Boolean& b) | ||
| 41 | { | ||
| 42 | plist_free(_node); | ||
| 43 | _node = plist_copy(b.GetPlist()); | ||
| 44 | } | ||
| 45 | |||
| 35 | Boolean::Boolean(bool b) : Node(PLIST_BOOLEAN) | 46 | Boolean::Boolean(bool b) : Node(PLIST_BOOLEAN) |
| 36 | { | 47 | { |
| 37 | plist_set_bool_val(_node, b); | 48 | plist_set_bool_val(_node, b); |
| @@ -41,6 +52,11 @@ Boolean::~Boolean() | |||
| 41 | { | 52 | { |
| 42 | } | 53 | } |
| 43 | 54 | ||
| 55 | Node* Boolean::Clone() | ||
| 56 | { | ||
| 57 | return new Boolean(*this); | ||
| 58 | } | ||
| 59 | |||
| 44 | void Boolean::SetValue(bool b) | 60 | void Boolean::SetValue(bool b) |
| 45 | { | 61 | { |
| 46 | plist_set_bool_val(_node, b); | 62 | 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) | |||
| 32 | { | 32 | { |
| 33 | } | 33 | } |
| 34 | 34 | ||
| 35 | Data::Data(std::vector<char>& buff) : Node(PLIST_DATA) | 35 | Data::Data(PList::Data& d) : Node(PLIST_DATA) |
| 36 | { | ||
| 37 | std::vector<char> b = d.GetValue(); | ||
| 38 | plist_set_data_val(_node, &b[0], b.size()); | ||
| 39 | } | ||
| 40 | |||
| 41 | Data& Data::operator=(PList::Data& b) | ||
| 42 | { | ||
| 43 | plist_free(_node); | ||
| 44 | _node = plist_copy(b.GetPlist()); | ||
| 45 | } | ||
| 46 | |||
| 47 | Data::Data(const std::vector<char>& buff) : Node(PLIST_DATA) | ||
| 36 | { | 48 | { |
| 37 | plist_set_data_val(_node, &buff[0], buff.size()); | 49 | plist_set_data_val(_node, &buff[0], buff.size()); |
| 38 | } | 50 | } |
| @@ -41,7 +53,12 @@ Data::~Data() | |||
| 41 | { | 53 | { |
| 42 | } | 54 | } |
| 43 | 55 | ||
| 44 | void Data::SetValue(std::vector<char>& buff) | 56 | Node* Data::Clone() |
| 57 | { | ||
| 58 | return new Data(*this); | ||
| 59 | } | ||
| 60 | |||
| 61 | void Data::SetValue(const std::vector<char>& buff) | ||
| 45 | { | 62 | { |
| 46 | plist_set_data_val(_node, &buff[0], buff.size()); | 63 | plist_set_data_val(_node, &buff[0], buff.size()); |
| 47 | } | 64 | } |
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) | |||
| 32 | { | 32 | { |
| 33 | } | 33 | } |
| 34 | 34 | ||
| 35 | Date::Date(Date& d) : Node(PLIST_DATE) | ||
| 36 | { | ||
| 37 | //TODO | ||
| 38 | } | ||
| 39 | |||
| 40 | Date& Date::operator=(PList::Date& b) | ||
| 41 | { | ||
| 42 | //TODO | ||
| 43 | } | ||
| 44 | |||
| 35 | Date::Date(uint64_t i) : Node(PLIST_DATE) | 45 | Date::Date(uint64_t i) : Node(PLIST_DATE) |
| 36 | { | 46 | { |
| 37 | plist_set_date_val(_node, i, 0); | 47 | plist_set_date_val(_node, i, 0); |
| @@ -41,6 +51,11 @@ Date::~Date() | |||
| 41 | { | 51 | { |
| 42 | } | 52 | } |
| 43 | 53 | ||
| 54 | Node* Date::Clone() | ||
| 55 | { | ||
| 56 | return new Date(*this); | ||
| 57 | } | ||
| 58 | |||
| 44 | void Date::SetValue(uint64_t i) | 59 | void Date::SetValue(uint64_t i) |
| 45 | { | 60 | { |
| 46 | plist_set_date_val(_node, i, 0); | 61 | 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() | |||
| 85 | free(it); | 85 | free(it); |
| 86 | } | 86 | } |
| 87 | 87 | ||
| 88 | Dictionary::Dictionary(Dictionary& d) | 88 | Dictionary::Dictionary(PList::Dictionary& d) : Structure() |
| 89 | { | 89 | { |
| 90 | for (Dictionary::iterator it = _map.begin(); it != _map.end(); it++) | 90 | for (Dictionary::iterator it = _map.begin(); it != _map.end(); it++) |
| 91 | { | 91 | { |
| @@ -142,7 +142,7 @@ Dictionary::Dictionary(Dictionary& d) | |||
| 142 | free(it); | 142 | free(it); |
| 143 | } | 143 | } |
| 144 | 144 | ||
| 145 | Dictionary& Dictionary::operator=(const Dictionary& d) | 145 | Dictionary& Dictionary::operator=(PList::Dictionary& d) |
| 146 | { | 146 | { |
| 147 | for (Dictionary::iterator it = _map.begin(); it != _map.end(); it++) | 147 | for (Dictionary::iterator it = _map.begin(); it != _map.end(); it++) |
| 148 | { | 148 | { |
| @@ -209,7 +209,12 @@ Dictionary::~Dictionary() | |||
| 209 | _map.clear(); | 209 | _map.clear(); |
| 210 | } | 210 | } |
| 211 | 211 | ||
| 212 | Node* Dictionary::operator[](std::string& key) | 212 | Node* Dictionary::Clone() |
| 213 | { | ||
| 214 | return new Dictionary(*this); | ||
| 215 | } | ||
| 216 | |||
| 217 | Node* Dictionary::operator[](const std::string& key) | ||
| 213 | { | 218 | { |
| 214 | return _map[key]; | 219 | return _map[key]; |
| 215 | } | 220 | } |
| @@ -224,13 +229,14 @@ Dictionary::iterator Dictionary::End() | |||
| 224 | return _map.end(); | 229 | return _map.end(); |
| 225 | } | 230 | } |
| 226 | 231 | ||
| 227 | void Dictionary::Insert(std::string& key, Node* node) | 232 | void Dictionary::Insert(const std::string& key, Node* node) |
| 228 | { | 233 | { |
| 229 | if (node) | 234 | if (node) |
| 230 | { | 235 | { |
| 231 | plist_dict_insert_item(_node, key.c_str(), node->GetPlist()); | 236 | Node* clone = node->Clone(); |
| 237 | plist_dict_insert_item(_node, key.c_str(), clone->GetPlist()); | ||
| 232 | delete _map[key]; | 238 | delete _map[key]; |
| 233 | _map[key] = node; | 239 | _map[key] = clone; |
| 234 | } | 240 | } |
| 235 | } | 241 | } |
| 236 | 242 | ||
| @@ -247,7 +253,7 @@ void Dictionary::Remove(Node* node) | |||
| 247 | } | 253 | } |
| 248 | } | 254 | } |
| 249 | 255 | ||
| 250 | void Dictionary::Remove(std::string& key) | 256 | void Dictionary::Remove(const std::string& key) |
| 251 | { | 257 | { |
| 252 | plist_dict_remove_item(_node, key.c_str()); | 258 | plist_dict_remove_item(_node, key.c_str()); |
| 253 | delete _map[key]; | 259 | 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) | |||
| 32 | { | 32 | { |
| 33 | } | 33 | } |
| 34 | 34 | ||
| 35 | Integer::Integer(PList::Integer& i) : Node(PLIST_UINT) | ||
| 36 | { | ||
| 37 | plist_set_uint_val(_node, i.GetValue()); | ||
| 38 | } | ||
| 39 | |||
| 40 | Integer& Integer::operator=(PList::Integer& i) | ||
| 41 | { | ||
| 42 | plist_free(_node); | ||
| 43 | _node = plist_copy(i.GetPlist()); | ||
| 44 | } | ||
| 45 | |||
| 35 | Integer::Integer(uint64_t i) : Node(PLIST_UINT) | 46 | Integer::Integer(uint64_t i) : Node(PLIST_UINT) |
| 36 | { | 47 | { |
| 37 | plist_set_uint_val(_node, i); | 48 | plist_set_uint_val(_node, i); |
| @@ -41,6 +52,11 @@ Integer::~Integer() | |||
| 41 | { | 52 | { |
| 42 | } | 53 | } |
| 43 | 54 | ||
| 55 | Node* Integer::Clone() | ||
| 56 | { | ||
| 57 | return new Integer(*this); | ||
| 58 | } | ||
| 59 | |||
| 44 | void Integer::SetValue(uint64_t i) | 60 | void Integer::SetValue(uint64_t i) |
| 45 | { | 61 | { |
| 46 | plist_set_uint_val(_node, i); | 62 | 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() | |||
| 74 | _node = NULL; | 74 | _node = NULL; |
| 75 | } | 75 | } |
| 76 | 76 | ||
| 77 | Node::Node(Node& node) | ||
| 78 | { | ||
| 79 | plist_free(_node); | ||
| 80 | _node = NULL; | ||
| 81 | |||
| 82 | _node = plist_copy(_node); | ||
| 83 | } | ||
| 84 | |||
| 85 | Node& Node::operator=(const Node& node) | ||
| 86 | { | ||
| 87 | plist_free(_node); | ||
| 88 | _node = NULL; | ||
| 89 | |||
| 90 | _node = plist_copy(_node); | ||
| 91 | } | ||
| 92 | |||
| 93 | plist_type Node::GetType() | 77 | plist_type Node::GetType() |
| 94 | { | 78 | { |
| 95 | if (_node) | 79 | if (_node) |
| @@ -98,7 +82,7 @@ plist_type Node::GetType() | |||
| 98 | } | 82 | } |
| 99 | } | 83 | } |
| 100 | 84 | ||
| 101 | plist_t Node::GetPlist() const | 85 | plist_t Node::GetPlist() |
| 102 | { | 86 | { |
| 103 | return _node; | 87 | return _node; |
| 104 | } | 88 | } |
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) | |||
| 32 | { | 32 | { |
| 33 | } | 33 | } |
| 34 | 34 | ||
| 35 | Real::Real(PList::Real& d) : Node(PLIST_UINT) | ||
| 36 | { | ||
| 37 | plist_set_real_val(_node, d.GetValue()); | ||
| 38 | } | ||
| 39 | |||
| 40 | Real& Real::operator=(PList::Real& d) | ||
| 41 | { | ||
| 42 | plist_free(_node); | ||
| 43 | _node = plist_copy(d.GetPlist()); | ||
| 44 | } | ||
| 45 | |||
| 35 | Real::Real(double d) : Node(PLIST_REAL) | 46 | Real::Real(double d) : Node(PLIST_REAL) |
| 36 | { | 47 | { |
| 37 | plist_set_real_val(_node, d); | 48 | plist_set_real_val(_node, d); |
| @@ -41,6 +52,11 @@ Real::~Real() | |||
| 41 | { | 52 | { |
| 42 | } | 53 | } |
| 43 | 54 | ||
| 55 | Node* Real::Clone() | ||
| 56 | { | ||
| 57 | return new Real(*this); | ||
| 58 | } | ||
| 59 | |||
| 44 | void Real::SetValue(double d) | 60 | void Real::SetValue(double d) |
| 45 | { | 61 | { |
| 46 | plist_set_real_val(_node, d); | 62 | 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) | |||
| 32 | { | 32 | { |
| 33 | } | 33 | } |
| 34 | 34 | ||
| 35 | String::String(std::string& s) : Node(PLIST_STRING) | 35 | String::String(PList::String& s) : Node(PLIST_UINT) |
| 36 | { | ||
| 37 | plist_set_string_val(_node, s.GetValue().c_str()); | ||
| 38 | } | ||
| 39 | |||
| 40 | String& String::operator=(PList::String& s) | ||
| 41 | { | ||
| 42 | plist_free(_node); | ||
| 43 | _node = plist_copy(s.GetPlist()); | ||
| 44 | } | ||
| 45 | |||
| 46 | String::String(const std::string& s) : Node(PLIST_STRING) | ||
| 36 | { | 47 | { |
| 37 | plist_set_string_val(_node, s.c_str()); | 48 | plist_set_string_val(_node, s.c_str()); |
| 38 | } | 49 | } |
| @@ -41,7 +52,12 @@ String::~String() | |||
| 41 | { | 52 | { |
| 42 | } | 53 | } |
| 43 | 54 | ||
| 44 | void String::SetValue(std::string& s) | 55 | Node* String::Clone() |
| 56 | { | ||
| 57 | return new String(*this); | ||
| 58 | } | ||
| 59 | |||
| 60 | void String::SetValue(const std::string& s) | ||
| 45 | { | 61 | { |
| 46 | plist_set_string_val(_node, s.c_str()); | 62 | plist_set_string_val(_node, s.c_str()); |
| 47 | } | 63 | } |
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) | |||
| 54 | return ret; | 54 | return ret; |
| 55 | } | 55 | } |
| 56 | 56 | ||
| 57 | Structure* Utils::FromXml(std::string& in) | 57 | Structure* Utils::FromXml(const std::string& in) |
| 58 | { | 58 | { |
| 59 | plist_t root = NULL; | 59 | plist_t root = NULL; |
| 60 | plist_from_xml(in.c_str(), in.size(), &root); | 60 | plist_from_xml(in.c_str(), in.size(), &root); |
| @@ -62,7 +62,7 @@ Structure* Utils::FromXml(std::string& in) | |||
| 62 | return FromPlist(root); | 62 | return FromPlist(root); |
| 63 | } | 63 | } |
| 64 | 64 | ||
| 65 | Structure* Utils::FromBin(std::vector<char>& in) | 65 | Structure* Utils::FromBin(const std::vector<char>& in) |
| 66 | { | 66 | { |
| 67 | plist_t root = NULL; | 67 | plist_t root = NULL; |
| 68 | plist_from_bin(&in[0], in.size(), &root); | 68 | plist_from_bin(&in[0], in.size(), &root); |
