diff options
Diffstat (limited to 'src/Array.cpp')
| -rw-r--r-- | src/Array.cpp | 115 |
1 files changed, 98 insertions, 17 deletions
diff --git a/src/Array.cpp b/src/Array.cpp index d5d9d7c..784df7c 100644 --- a/src/Array.cpp +++ b/src/Array.cpp @@ -18,11 +18,11 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include <stdlib.h> -#include <plist/Array.h> - +#include <cstdlib> #include <algorithm> -#include <limits.h> +#include <climits> +#include "plist.h" +#include <plist/Array.h> namespace PList { @@ -32,7 +32,7 @@ Array::Array(Node* parent) : Structure(PLIST_ARRAY, parent) _array.clear(); } -static void array_fill(Array *_this, std::vector<Node*> array, plist_t node) +static void array_fill(Array *_this, std::vector<Node*> &array, plist_t node) { plist_array_iter iter = NULL; plist_array_new_iter(node, &iter); @@ -40,7 +40,9 @@ static void array_fill(Array *_this, std::vector<Node*> array, plist_t node) do { subnode = NULL; plist_array_next_item(node, iter, &subnode); - array.push_back( Node::FromPlist(subnode, _this) ); + if (subnode) { + array.push_back( Node::FromPlist(subnode, _this) ); + } } while (subnode); free(iter); } @@ -51,18 +53,19 @@ Array::Array(plist_t node, Node* parent) : Structure(parent) array_fill(this, _array, _node); } -Array::Array(const PList::Array& a) : Structure() +Array::Array(const PList::Array& a) : Structure(a.GetParent()) { _array.clear(); _node = plist_copy(a.GetPlist()); array_fill(this, _array, _node); } -Array& Array::operator=(PList::Array& a) +Array& Array::operator=(const PList::Array& a) { + if (this == &a) return *this; + plist_free(_node); - for (unsigned int it = 0; it < _array.size(); it++) - { + for (size_t it = 0; it < _array.size(); it++) { delete _array.at(it); } _array.clear(); @@ -73,8 +76,7 @@ Array& Array::operator=(PList::Array& a) Array::~Array() { - for (unsigned int it = 0; it < _array.size(); it++) - { + for (size_t it = 0; it < _array.size(); it++) { delete (_array.at(it)); } _array.clear(); @@ -90,7 +92,71 @@ Node* Array::operator[](unsigned int array_index) return _array.at(array_index); } -void Array::Append(Node* node) +Node* Array::Back() +{ + return _array.back(); +} + +Node* Array::back() +{ + return _array.back(); +} + +Node* Array::Front() +{ + return _array.front(); +} + +Node* Array::front() +{ + return _array.front(); +} + +Array::iterator Array::Begin() +{ + return _array.begin(); +} + +Array::iterator Array::begin() +{ + return _array.begin(); +} + +Array::iterator Array::End() +{ + return _array.end(); +} + +Array::iterator Array::end() +{ + return _array.end(); +} + +Array::const_iterator Array::Begin() const +{ + return _array.begin(); +} + +Array::const_iterator Array::begin() const +{ + return _array.begin(); +} + +Array::const_iterator Array::End() const +{ + return _array.end(); +} + +Array::const_iterator Array::end() const +{ + return _array.end(); +} + +size_t Array::size() const { + return _array.size(); +} + +void Array::Append(const Node* node) { if (node) { @@ -101,7 +167,12 @@ void Array::Append(Node* node) } } -void Array::Insert(Node* node, unsigned int pos) +void Array::Append(const Node& node) +{ + Append(&node); +} + +void Array::Insert(const Node* node, unsigned int pos) { if (node) { @@ -114,6 +185,11 @@ void Array::Insert(Node* node, unsigned int pos) } } +void Array::Insert(const Node &node, unsigned int pos) +{ + Insert(&node, pos); +} + void Array::Remove(Node* node) { if (node) @@ -126,7 +202,7 @@ void Array::Remove(Node* node) std::vector<Node*>::iterator it = _array.begin(); it += pos; _array.erase(it); - delete node; + free(node); } } @@ -139,10 +215,15 @@ void Array::Remove(unsigned int pos) _array.erase(it); } -unsigned int Array::GetNodeIndex(Node* node) const +unsigned int Array::GetNodeIndex(const Node* node) const { std::vector<Node*>::const_iterator it = std::find(_array.begin(), _array.end(), node); return std::distance (_array.begin(), it); } -}; +unsigned int Array::GetNodeIndex(const Node& node) const +{ + return GetNodeIndex(&node); +} + +} // namespace PList |
