diff options
Diffstat (limited to 'src/Array.cpp')
| -rw-r--r-- | src/Array.cpp | 159 |
1 files changed, 119 insertions, 40 deletions
diff --git a/src/Array.cpp b/src/Array.cpp index 419249f..784df7c 100644 --- a/src/Array.cpp +++ b/src/Array.cpp @@ -18,10 +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 <climits> +#include "plist.h" +#include <plist/Array.h> namespace PList { @@ -31,71 +32,131 @@ Array::Array(Node* parent) : Structure(PLIST_ARRAY, parent) _array.clear(); } +static void array_fill(Array *_this, std::vector<Node*> &array, plist_t node) +{ + plist_array_iter iter = NULL; + plist_array_new_iter(node, &iter); + plist_t subnode; + do { + subnode = NULL; + plist_array_next_item(node, iter, &subnode); + if (subnode) { + array.push_back( Node::FromPlist(subnode, _this) ); + } + } while (subnode); + free(iter); +} + Array::Array(plist_t node, Node* parent) : Structure(parent) { _node = node; - uint32_t size = plist_array_get_size(_node); - - for (uint32_t i = 0; i < size; i++) - { - plist_t subnode = plist_array_get_item(_node, i); - _array.push_back( Node::FromPlist(subnode, this) ); - } + array_fill(this, _array, _node); } -Array::Array(PList::Array& a) : Structure() +Array::Array(const PList::Array& a) : Structure(a.GetParent()) { _array.clear(); _node = plist_copy(a.GetPlist()); - uint32_t size = plist_array_get_size(_node); - - for (uint32_t i = 0; i < size; i++) - { - plist_t subnode = plist_array_get_item(_node, i); - _array.push_back( Node::FromPlist(subnode, this) ); - } + 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(); - _node = plist_copy(a.GetPlist()); - uint32_t size = plist_array_get_size(_node); - - for (uint32_t i = 0; i < size; i++) - { - plist_t subnode = plist_array_get_item(_node, i); - _array.push_back( Node::FromPlist(subnode, this) ); - } + array_fill(this, _array, _node); return *this; } 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(); } -Node* Array::Clone() +Node* Array::Clone() const { return new Array(*this); } -Node* Array::operator[](unsigned int index) +Node* Array::operator[](unsigned int array_index) +{ + return _array.at(array_index); +} + +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.at(index); + return _array.begin(); } -void Array::Append(Node* node) +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) { @@ -106,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) { @@ -119,16 +185,24 @@ 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) { uint32_t pos = plist_array_get_item_index(node->GetPlist()); + if (pos == UINT_MAX) { + return; + } plist_array_remove_item(_node, pos); std::vector<Node*>::iterator it = _array.begin(); it += pos; _array.erase(it); - delete node; + free(node); } } @@ -141,10 +215,15 @@ void Array::Remove(unsigned int pos) _array.erase(it); } -unsigned int Array::GetNodeIndex(Node* node) +unsigned int Array::GetNodeIndex(const Node* node) const { - std::vector<Node*>::iterator it = std::find(_array.begin(), _array.end(), node); + 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 |
