//################################################################################################## // // Custom Visualization Core library // Copyright (C) 2011-2013 Ceetron AS // // This library may be used under the terms of either the GNU General Public License or // the GNU Lesser General Public License as follows: // // GNU General Public License Usage // This library is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This library is distributed in the hope that it will be useful, but WITHOUT ANY // WARRANTY; without even the implied warranty of MERCHANTABILITY or // FITNESS FOR A PARTICULAR PURPOSE. // // See the GNU General Public License at <> // for more details. // // GNU Lesser General Public License Usage // This library is free software; you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation; either version 2.1 of the License, or // (at your option) any later version. // // This library is distributed in the hope that it will be useful, but WITHOUT ANY // WARRANTY; without even the implied warranty of MERCHANTABILITY or // FITNESS FOR A PARTICULAR PURPOSE. // // See the GNU Lesser General Public License at <> // for more details. // //################################################################################################## namespace external { namespace cvf { //================================================================================================== /// /// \class cvf::Collection /// \ingroup Core /// /// A collection class for reference counted objects (that derive from Object). /// /// The class add a reference to all objects when added to the array, and releases them when removed /// from the array. /// /// The class exposes the same public interface as a std::vector, so see the STL documentation /// for documentation on most of the methods. /// //================================================================================================== //-------------------------------------------------------------------------------------------------- /// Default constructor. Create an empty collection //-------------------------------------------------------------------------------------------------- template Collection::Collection() { } //-------------------------------------------------------------------------------------------------- /// Copy constructor //-------------------------------------------------------------------------------------------------- template Collection::Collection(const Collection& other) : m_vector(other.m_vector) { } //-------------------------------------------------------------------------------------------------- /// Create a collection and copy the contents from the passed collection //-------------------------------------------------------------------------------------------------- template Collection::Collection(const std::vector< ref >& vector) : m_vector(vector) { } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- template Collection& Collection::operator=(Collection rhs) { rhs.swap(*this); return *this; } //-------------------------------------------------------------------------------------------------- /// Set the collection to contain the same elements as the passed collection //-------------------------------------------------------------------------------------------------- template Collection& Collection::operator=(const std::vector< ref >& vector) { m_vector = vector; return *this; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- template const ref& Collection::operator[](size_t index) const { return m_vector[index]; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- template ref& Collection::operator[](size_t index) { return m_vector[index]; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- template void Collection::push_back(T* data) { m_vector.push_back(data); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- template const T* Collection::at(size_t index) const { return m_vector[index].p(); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- template T* Collection::at(size_t index) { return m_vector[index].p(); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- template void Collection::resize(size_t size) { m_vector.resize(size); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- template size_t Collection::size() const { return m_vector.size(); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- template void Collection::reserve(size_t capacity) { m_vector.reserve(capacity); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- template size_t Collection::capacity() const { return m_vector.capacity(); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- template void Collection::clear() { m_vector.clear(); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- template bool Collection::empty() const { return m_vector.empty(); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- template bool Collection::contains(const T* data) const { if (std::find(m_vector.begin(), m_vector.end(), data) != m_vector.end()) { return true; } else { return false; } } //-------------------------------------------------------------------------------------------------- /// Find index of the first occurence of the specified element /// /// Returns UNDEFINED_SIZE_T if element could not be found //-------------------------------------------------------------------------------------------------- template size_t Collection::indexOf(const T* data) const { typename std::vector >::const_iterator it = std::find(m_vector.begin(), m_vector.end(), data); if (it != m_vector.end()) { return static_cast(it - m_vector.begin()); } else { return UNDEFINED_SIZE_T; } } //-------------------------------------------------------------------------------------------------- /// Erase the specified element //-------------------------------------------------------------------------------------------------- template void Collection::erase(const T* data) { typename std::vector >::iterator it = std::find(m_vector.begin(), m_vector.end(), data); if (it != m_vector.end()) { m_vector.erase(it); } } //-------------------------------------------------------------------------------------------------- /// Erase the element at the specified index //-------------------------------------------------------------------------------------------------- template void Collection::eraseAt(size_t index) { CVF_ASSERT(index < m_vector.size()); // Cast may have to be to std::vector::difference_type m_vector.erase(m_vector.begin() + static_cast(index)); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- template void Collection::swap(Collection& other) { m_vector.swap(other.m_vector); } //-------------------------------------------------------------------------------------------------- /// Return iterator to beginning //-------------------------------------------------------------------------------------------------- template typename Collection::iterator Collection::begin() { return m_vector.begin(); } //-------------------------------------------------------------------------------------------------- /// Return iterator to end //-------------------------------------------------------------------------------------------------- template typename Collection::iterator Collection::end() { return m_vector.end(); } //-------------------------------------------------------------------------------------------------- /// Return const iterator to beginning //-------------------------------------------------------------------------------------------------- template typename Collection::const_iterator Collection::begin() const { return m_vector.begin(); } //-------------------------------------------------------------------------------------------------- /// Return const iterator to end //-------------------------------------------------------------------------------------------------- template typename Collection::const_iterator Collection::end() const { return m_vector.end(); } } // namespace cvf } //namespace external