MixinVector 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSG_MIXIN_VECTOR
  14. #define OSG_MIXIN_VECTOR 1
  15. #include <vector>
  16. namespace osg {
  17. /** MixinVector is a base class that allows inheritance to be used to easily
  18. * emulate derivation from std::vector but without introducing undefined
  19. * behaviour through violation of virtual destructor rules.
  20. *
  21. * @author Neil Groves
  22. */
  23. template<class ValueT>
  24. class MixinVector
  25. {
  26. typedef typename std::vector<ValueT> vector_type;
  27. public:
  28. typedef typename vector_type::allocator_type allocator_type;
  29. typedef typename vector_type::value_type value_type;
  30. typedef typename vector_type::const_pointer const_pointer;
  31. typedef typename vector_type::pointer pointer;
  32. typedef typename vector_type::const_reference const_reference;
  33. typedef typename vector_type::reference reference;
  34. typedef typename vector_type::const_iterator const_iterator;
  35. typedef typename vector_type::iterator iterator;
  36. typedef typename vector_type::const_reverse_iterator const_reverse_iterator;
  37. typedef typename vector_type::reverse_iterator reverse_iterator;
  38. typedef typename vector_type::size_type size_type;
  39. typedef typename vector_type::difference_type difference_type;
  40. explicit MixinVector() : _impl()
  41. {
  42. }
  43. explicit MixinVector(size_type initial_size, const value_type& fill_value = value_type())
  44. : _impl(initial_size, fill_value)
  45. {
  46. }
  47. template<class InputIterator>
  48. MixinVector(InputIterator first, InputIterator last)
  49. : _impl(first, last)
  50. {
  51. }
  52. MixinVector(const vector_type& other)
  53. : _impl(other)
  54. {
  55. }
  56. MixinVector(const MixinVector& other)
  57. : _impl(other._impl)
  58. {
  59. }
  60. MixinVector& operator=(const vector_type& other)
  61. {
  62. _impl = other;
  63. return *this;
  64. }
  65. MixinVector& operator=(const MixinVector& other)
  66. {
  67. _impl = other._impl;
  68. return *this;
  69. }
  70. virtual ~MixinVector() {}
  71. void clear() { _impl.clear(); }
  72. void resize(size_type new_size, const value_type& fill_value = value_type()) { _impl.resize(new_size, fill_value); }
  73. void reserve(size_type new_capacity) { _impl.reserve(new_capacity); }
  74. void swap(vector_type& other) { _impl.swap(other); }
  75. void swap(MixinVector& other) { _impl.swap(other._impl); }
  76. bool empty() const { return _impl.empty(); }
  77. size_type size() const { return _impl.size(); }
  78. size_type capacity() const { return _impl.capacity(); }
  79. size_type max_size() const { return _impl.max_size(); }
  80. allocator_type get_allocator() const { return _impl.get_allocator(); }
  81. const_iterator begin() const { return _impl.begin(); }
  82. iterator begin() { return _impl.begin(); }
  83. const_iterator end() const { return _impl.end(); }
  84. iterator end() { return _impl.end(); }
  85. const_reverse_iterator rbegin() const { return _impl.rbegin(); }
  86. reverse_iterator rbegin() { return _impl.rbegin(); }
  87. const_reverse_iterator rend() const { return _impl.rend(); }
  88. reverse_iterator rend() { return _impl.rend(); }
  89. const_reference operator[](size_type index) const { return _impl[index]; }
  90. reference operator[](size_type index) { return _impl[index]; }
  91. const_reference at(size_type index) const { return _impl.at(index); }
  92. reference at(size_type index) { return _impl.at(index); }
  93. void assign(size_type count, const value_type& value) { _impl.assign(count, value); }
  94. template<class Iter>
  95. void assign(Iter first, Iter last) { _impl.assign(first, last); }
  96. void push_back(const value_type& value) { _impl.push_back(value); }
  97. void pop_back() { _impl.pop_back(); }
  98. iterator erase(iterator where) { return _impl.erase(where); }
  99. iterator erase(iterator first, iterator last) { return _impl.erase(first, last); }
  100. iterator insert(iterator where, const value_type& value) { return _impl.insert(where, value); }
  101. template<class InputIterator>
  102. void insert(iterator where, InputIterator first, InputIterator last)
  103. {
  104. _impl.insert(where, first, last);
  105. }
  106. void insert(iterator where, size_type count, const value_type& value)
  107. {
  108. _impl.insert(where, count, value);
  109. }
  110. const_reference back() const { return _impl.back(); }
  111. reference back() { return _impl.back(); }
  112. const_reference front() const { return _impl.front(); }
  113. reference front() { return _impl.front(); }
  114. vector_type& asVector() { return _impl; }
  115. const vector_type& asVector() const { return _impl; }
  116. friend inline bool operator==(const MixinVector<ValueT>& left, const MixinVector<ValueT>& right) { return left._impl == right._impl; }
  117. friend inline bool operator==(const MixinVector<ValueT>& left, const std::vector<ValueT>& right) { return left._impl == right; }
  118. friend inline bool operator==(const std::vector<ValueT>& left, const MixinVector<ValueT>& right) { return left == right._impl; }
  119. friend inline bool operator!=(const MixinVector<ValueT>& left, const MixinVector<ValueT>& right) { return left._impl != right._impl; }
  120. friend inline bool operator!=(const MixinVector<ValueT>& left, const std::vector<ValueT>& right) { return left._impl != right; }
  121. friend inline bool operator!=(const std::vector<ValueT>& left, const MixinVector<ValueT>& right) { return left != right._impl; }
  122. friend inline bool operator<(const MixinVector<ValueT>& left, const MixinVector<ValueT>& right) { return left._impl < right._impl; }
  123. friend inline bool operator<(const MixinVector<ValueT>& left, const std::vector<ValueT>& right) { return left._impl < right; }
  124. friend inline bool operator<(const std::vector<ValueT>& left, const MixinVector<ValueT>& right) { return left < right._impl; }
  125. friend inline bool operator>(const MixinVector<ValueT>& left, const MixinVector<ValueT>& right) { return left._impl > right._impl; }
  126. friend inline bool operator>(const MixinVector<ValueT>& left, const std::vector<ValueT>& right) { return left._impl > right; }
  127. friend inline bool operator>(const std::vector<ValueT>& left, const MixinVector<ValueT>& right) { return left > right._impl; }
  128. friend inline bool operator<=(const MixinVector<ValueT>& left, const MixinVector<ValueT>& right) { return left._impl <= right._impl; }
  129. friend inline bool operator<=(const MixinVector<ValueT>& left, const std::vector<ValueT>& right) { return left._impl <= right; }
  130. friend inline bool operator<=(const std::vector<ValueT>& left, const MixinVector<ValueT>& right) { return left <= right._impl; }
  131. friend inline bool operator>=(const MixinVector<ValueT>& left, const MixinVector<ValueT>& right) { return left._impl >= right._impl; }
  132. friend inline bool operator>=(const MixinVector<ValueT>& left, const std::vector<ValueT>& right) { return left._impl >= right; }
  133. friend inline bool operator>=(const std::vector<ValueT>& left, const MixinVector<ValueT>& right) { return left >= right._impl; }
  134. private:
  135. vector_type _impl;
  136. };
  137. template<class ValueT> inline
  138. void
  139. swap(MixinVector<ValueT>& left,
  140. MixinVector<ValueT>& right)
  141. {
  142. std::swap(left.asVector(), right.asVector());
  143. }
  144. template<class ValueT> inline
  145. void
  146. swap(MixinVector<ValueT>& left,
  147. std::vector<ValueT>& right)
  148. {
  149. std::swap(left.asVector(), right);
  150. }
  151. template<class ValueT> inline
  152. void
  153. swap(std::vector<ValueT>& left,
  154. MixinVector<ValueT>& right)
  155. {
  156. std::swap(left, right.asVector());
  157. }
  158. } // namespace osg
  159. #endif