Visualization Library 2.1.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
Collection.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #ifndef Collection_INCLUDE_ONCE
33 #define Collection_INCLUDE_ONCE
34 
35 #include <vlCore/Object.hpp>
36 #include <vector>
37 #include <algorithm>
38 
39 namespace vl
40 {
41  //------------------------------------------------------------------------------
42  // Collection
43  //------------------------------------------------------------------------------
48  template <typename T>
49  class Collection: public Object
50  {
52 
53  public:
54  const static size_t not_found = size_t( -1 );
55 
56  public:
57  Collection(const std::vector< ref<T> >& vector)
58  {
59  VL_DEBUG_SET_OBJECT_NAME()
60  mVector = vector;
61  }
62 
64  {
65  VL_DEBUG_SET_OBJECT_NAME()
66  }
67 
68  Collection& operator=(const std::vector< ref<T> >& vector)
69  {
70  mVector = vector;
71  return *this;
72  }
73 
74  operator std::vector< ref<T> >() const
75  {
76  return mVector;
77  }
78 
79  void push_back( T* data ) { mVector.push_back(data); }
80 
81  void pop_back() { mVector.pop_back(); }
82 
83  void resize(size_t size) { mVector.resize(size); }
84 
85  size_t size() const { return mVector.size(); }
86 
87  bool empty() const { return mVector.empty(); }
88 
89  void clear() { mVector.clear(); }
90 
91  const T* back() const { return mVector.back().get(); }
92 
93  T* back() { return mVector.back().get(); }
94 
95  void reserve(size_t capacity) { mVector.reserve(capacity); }
96 
97  size_t capacity() const { return mVector.capacity(); }
98 
99  const ref<T>& operator[](size_t i) const { return mVector[i]; }
100 
101  ref<T>& operator[](size_t i) { return mVector[i]; }
102 
103  const T* at(size_t i) const { return mVector[i].get(); }
104 
105  T* at(size_t i) { return mVector[i].get(); }
106 
107  void swap(Collection& other) { mVector.swap(other.mVector); }
108 
109  // added functionalities
110 
111  void sort()
112  {
113  std::sort(mVector.begin(), mVector.end(), less);
114  }
115 
116  size_t find(T* obj) const
117  {
118  typename std::vector< ref<T> >::const_iterator pos = std::find(mVector.begin(), mVector.end(), obj);
119  if (pos == mVector.end())
120  return size_t(-1);
121  else
122  return (size_t)(pos - mVector.begin());
123  }
124 
125  void shrink()
126  {
128  }
129 
130  void push_back(const Collection<T>& objs )
131  {
132  mVector.insert(mVector.end(), objs.mVector.begin(), objs.mVector.end());
133  }
134 
135  void insert(size_t start, const Collection<T>& objs )
136  {
137  mVector.insert(mVector.begin()+start, objs.mVector.begin(), objs.mVector.end());
138  }
139 
140  void set(const Collection<T>& objs)
141  {
142  mVector = objs.mVector;
143  }
144 
145  void erase(size_t start, size_t count)
146  {
147  mVector.erase(mVector.begin()+start, mVector.begin()+start+count);
148  }
149 
150  void set(size_t index, T* obj) { mVector[index] = obj; }
151 
152  void insert(size_t index, T* obj) { mVector.insert(mVector.begin() + index, obj); }
153 
154  void erase(const T* data)
155  {
156  typename std::vector< ref<T> >::iterator it = std::find(mVector.begin(), mVector.end(), data);
157  if (it != mVector.end())
158  mVector.erase(it);
159 
160  }
161 
162  void eraseAt(size_t index) { mVector.erase(mVector.begin()+index); }
163 
164  const std::vector< ref<T> >& vector() const { return mVector; }
165 
166  std::vector< ref<T> >& vector() { return mVector; }
167 
168  protected:
169  bool static less(const ref<T>& a, const ref<T>& b) { return *a < *b; }
170 
171  protected:
172  std::vector< ref<T> > mVector;
173  };
174  //-----------------------------------------------------------------------------
175 }
176 
177 #endif
ref< T > & operator[](size_t i)
Definition: Collection.hpp:101
std::vector< ref< T > > mVector
Definition: Collection.hpp:172
size_t find(T *obj) const
Definition: Collection.hpp:116
void reserve(size_t capacity)
Definition: Collection.hpp:95
void eraseAt(size_t index)
Definition: Collection.hpp:162
std::vector< ref< T > > & vector()
Definition: Collection.hpp:166
void push_back(const Collection< T > &objs)
Definition: Collection.hpp:130
void insert(size_t index, T *obj)
Definition: Collection.hpp:152
T * at(size_t i)
Definition: Collection.hpp:105
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:122
static const size_t not_found
Definition: Collection.hpp:54
void insert(size_t start, const Collection< T > &objs)
Definition: Collection.hpp:135
static bool less(const ref< T > &a, const ref< T > &b)
Definition: Collection.hpp:169
const T * at(size_t i) const
Definition: Collection.hpp:103
Visualization Library main namespace.
Collection(const std::vector< ref< T > > &vector)
Definition: Collection.hpp:57
Collection & operator=(const std::vector< ref< T > > &vector)
Definition: Collection.hpp:68
const std::vector< ref< T > > & vector() const
Definition: Collection.hpp:164
void erase(size_t start, size_t count)
Definition: Collection.hpp:145
The base class for all the reference counted objects.
Definition: Object.hpp:158
const ref< T > & operator[](size_t i) const
Definition: Collection.hpp:99
void push_back(T *data)
Definition: Collection.hpp:79
void erase(const T *data)
Definition: Collection.hpp:154
void swap(Collection &other)
Definition: Collection.hpp:107
It&#39;s basically an std::vector for Objects that is itself an Object so it can be reference counted and...
Definition: Collection.hpp:49
bool empty() const
Definition: Collection.hpp:87
void resize(size_t size)
Definition: Collection.hpp:83
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
const T * back() const
Definition: Collection.hpp:91
size_t size() const
Definition: Collection.hpp:85
size_t capacity() const
Definition: Collection.hpp:97