Visualization Library 2.0.0-b3

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-2017, 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  Collection(const std::vector< ref<T> >& vector)
55  {
56  VL_DEBUG_SET_OBJECT_NAME()
57  mVector = vector;
58  }
59 
61  {
62  VL_DEBUG_SET_OBJECT_NAME()
63  }
64 
65  Collection& operator=(const std::vector< ref<T> >& vector)
66  {
67  mVector = vector;
68  return *this;
69  }
70 
71  operator std::vector< ref<T> >() const
72  {
73  return mVector;
74  }
75 
76  void push_back( T* data ) { mVector.push_back(data); }
77 
78  void pop_back() { mVector.pop_back(); }
79 
80  void resize(int size) { mVector.resize(size); }
81 
82  int size() const { return (int)mVector.size(); }
83 
84  bool empty() const { return mVector.empty(); }
85 
86  void clear() { mVector.clear(); }
87 
88  const T* back() const { return mVector.back().get(); }
89 
90  T* back() { return mVector.back().get(); }
91 
92  void reserve(int capacity) { mVector.reserve(capacity); }
93 
94  int capacity() const { return (int)mVector.capacity(); }
95 
96  const ref<T>& operator[](int i) const { return mVector[i]; }
97 
98  ref<T>& operator[](int i) { return mVector[i]; }
99 
100  const T* at(int i) const { return mVector[i].get(); }
101 
102  T* at(int i) { return mVector[i].get(); }
103 
104  void swap(Collection& other) { mVector.swap(other.mVector); }
105 
106  // added functionalities
107 
108  void sort()
109  {
110  std::sort(mVector.begin(), mVector.end(), less);
111  }
112 
113  int find(T* obj) const
114  {
115  typename std::vector< ref<T> >::const_iterator pos = std::find(mVector.begin(), mVector.end(), obj);
116  if (pos == mVector.end())
117  return -1;
118  else
119  return (int)(pos - mVector.begin());
120  }
121 
122  void shrink()
123  {
125  }
126 
127  void push_back(const Collection<T>& objs )
128  {
129  mVector.insert(mVector.end(), objs.mVector.begin(), objs.mVector.end());
130  }
131 
132  void insert(int start, const Collection<T>& objs )
133  {
134  mVector.insert(mVector.begin()+start, objs.mVector.begin(), objs.mVector.end());
135  }
136 
137  void set(const Collection<T>& objs)
138  {
139  mVector = objs.mVector;
140  }
141 
142  void erase(int start, int count)
143  {
144  mVector.erase(mVector.begin()+start, mVector.begin()+start+count);
145  }
146 
147  void set(int index, T* obj) { mVector[index] = obj; }
148 
149  void insert(int index, T* obj) { mVector.insert(mVector.begin() + index, obj); }
150 
151  void erase(const T* data)
152  {
153  typename std::vector< ref<T> >::iterator it = std::find(mVector.begin(), mVector.end(), data);
154  if (it != mVector.end())
155  mVector.erase(it);
156 
157  }
158 
159  void eraseAt(int index) { mVector.erase(mVector.begin()+index); }
160 
161  const std::vector< ref<T> >& vector() const { return mVector; }
162 
163  std::vector< ref<T> >& vector() { return mVector; }
164 
165  protected:
166  bool static less(const ref<T>& a, const ref<T>& b) { return *a < *b; }
167 
168  protected:
169  std::vector< ref<T> > mVector;
170  };
171  //-----------------------------------------------------------------------------
172 }
173 
174 #endif
const T * at(int i) const
Definition: Collection.hpp:100
std::vector< ref< T > > mVector
Definition: Collection.hpp:169
void resize(int size)
Definition: Collection.hpp:80
void insert(int index, T *obj)
Definition: Collection.hpp:149
int capacity() const
Definition: Collection.hpp:94
void eraseAt(int index)
Definition: Collection.hpp:159
std::vector< ref< T > > & vector()
Definition: Collection.hpp:163
int size() const
Definition: Collection.hpp:82
const ref< T > & operator[](int i) const
Definition: Collection.hpp:96
int find(T *obj) const
Definition: Collection.hpp:113
void push_back(const Collection< T > &objs)
Definition: Collection.hpp:127
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:122
static bool less(const ref< T > &a, const ref< T > &b)
Definition: Collection.hpp:166
Visualization Library main namespace.
Collection(const std::vector< ref< T > > &vector)
Definition: Collection.hpp:54
Collection & operator=(const std::vector< ref< T > > &vector)
Definition: Collection.hpp:65
const std::vector< ref< T > > & vector() const
Definition: Collection.hpp:161
The base class for all the reference counted objects.
Definition: Object.hpp:158
void reserve(int capacity)
Definition: Collection.hpp:92
void push_back(T *data)
Definition: Collection.hpp:76
void erase(int start, int count)
Definition: Collection.hpp:142
void erase(const T *data)
Definition: Collection.hpp:151
void swap(Collection &other)
Definition: Collection.hpp:104
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
ref< T > & operator[](int i)
Definition: Collection.hpp:98
T * at(int i)
Definition: Collection.hpp:102
bool empty() const
Definition: Collection.hpp:84
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
void insert(int start, const Collection< T > &objs)
Definition: Collection.hpp:132
const T * back() const
Definition: Collection.hpp:88