summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorGravatar Jonathan Beck2009-10-16 22:26:13 +0200
committerGravatar Jonathan Beck2009-10-16 22:26:13 +0200
commit32be8ec384bfd78e189d3de6609e50cf4dd072a2 (patch)
treeac7edad0f19be2a31efaaaaa3acd477dd2f3c233 /src
parent8aeef4dd2331445fea8a7a40466b19973e9d09c4 (diff)
downloadlibplist-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.cpp36
-rw-r--r--src/Boolean.cpp16
-rw-r--r--src/Data.cpp21
-rw-r--r--src/Date.cpp15
-rw-r--r--src/Dictionary.cpp20
-rw-r--r--src/Integer.cpp16
-rw-r--r--src/Node.cpp18
-rw-r--r--src/Real.cpp16
-rw-r--r--src/String.cpp20
-rw-r--r--src/Utils.cpp4
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
80Array::Array(Array& a) 80Array::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
128Array& Array::operator=(const Array& a) 122Array& 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
176Array::~Array() 170Array::~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
179Node* Array::Clone()
180{
181 return new Array(*this);
184} 182}
185 183
186Node* Array::operator[](unsigned int index) 184Node* 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
35Boolean::Boolean(PList::Boolean& b) : Node(PLIST_BOOLEAN)
36{
37 plist_set_bool_val(_node, b.GetValue());
38}
39
40Boolean& Boolean::operator=(PList::Boolean& b)
41{
42 plist_free(_node);
43 _node = plist_copy(b.GetPlist());
44}
45
35Boolean::Boolean(bool b) : Node(PLIST_BOOLEAN) 46Boolean::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
55Node* Boolean::Clone()
56{
57 return new Boolean(*this);
58}
59
44void Boolean::SetValue(bool b) 60void 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
35Data::Data(std::vector<char>& buff) : Node(PLIST_DATA) 35Data::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
41Data& Data::operator=(PList::Data& b)
42{
43 plist_free(_node);
44 _node = plist_copy(b.GetPlist());
45}
46
47Data::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
44void Data::SetValue(std::vector<char>& buff) 56Node* Data::Clone()
57{
58 return new Data(*this);
59}
60
61void 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
35Date::Date(Date& d) : Node(PLIST_DATE)
36{
37 //TODO
38}
39
40Date& Date::operator=(PList::Date& b)
41{
42 //TODO
43}
44
35Date::Date(uint64_t i) : Node(PLIST_DATE) 45Date::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
54Node* Date::Clone()
55{
56 return new Date(*this);
57}
58
44void Date::SetValue(uint64_t i) 59void 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
88Dictionary::Dictionary(Dictionary& d) 88Dictionary::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
145Dictionary& Dictionary::operator=(const Dictionary& d) 145Dictionary& 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
212Node* Dictionary::operator[](std::string& key) 212Node* Dictionary::Clone()
213{
214 return new Dictionary(*this);
215}
216
217Node* 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
227void Dictionary::Insert(std::string& key, Node* node) 232void 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
250void Dictionary::Remove(std::string& key) 256void 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
35Integer::Integer(PList::Integer& i) : Node(PLIST_UINT)
36{
37 plist_set_uint_val(_node, i.GetValue());
38}
39
40Integer& Integer::operator=(PList::Integer& i)
41{
42 plist_free(_node);
43 _node = plist_copy(i.GetPlist());
44}
45
35Integer::Integer(uint64_t i) : Node(PLIST_UINT) 46Integer::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
55Node* Integer::Clone()
56{
57 return new Integer(*this);
58}
59
44void Integer::SetValue(uint64_t i) 60void 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
77Node::Node(Node& node)
78{
79 plist_free(_node);
80 _node = NULL;
81
82 _node = plist_copy(_node);
83}
84
85Node& Node::operator=(const Node& node)
86{
87 plist_free(_node);
88 _node = NULL;
89
90 _node = plist_copy(_node);
91}
92
93plist_type Node::GetType() 77plist_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
101plist_t Node::GetPlist() const 85plist_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
35Real::Real(PList::Real& d) : Node(PLIST_UINT)
36{
37 plist_set_real_val(_node, d.GetValue());
38}
39
40Real& Real::operator=(PList::Real& d)
41{
42 plist_free(_node);
43 _node = plist_copy(d.GetPlist());
44}
45
35Real::Real(double d) : Node(PLIST_REAL) 46Real::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
55Node* Real::Clone()
56{
57 return new Real(*this);
58}
59
44void Real::SetValue(double d) 60void 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
35String::String(std::string& s) : Node(PLIST_STRING) 35String::String(PList::String& s) : Node(PLIST_UINT)
36{
37 plist_set_string_val(_node, s.GetValue().c_str());
38}
39
40String& String::operator=(PList::String& s)
41{
42 plist_free(_node);
43 _node = plist_copy(s.GetPlist());
44}
45
46String::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
44void String::SetValue(std::string& s) 55Node* String::Clone()
56{
57 return new String(*this);
58}
59
60void 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
57Structure* Utils::FromXml(std::string& in) 57Structure* 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
65Structure* Utils::FromBin(std::vector<char>& in) 65Structure* 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);