C++ main module for gpm Package  1.0
CORE_SharedPointersList.h
Go to the documentation of this file.
1 #ifndef CORE_SharedPointersList_H
2 #define CORE_SharedPointersList_H
3 
4 #include "boost/shared_ptr.hpp"
5 #include "boost/weak_ptr.hpp"
6 #include "CORE_Object.h"
7 
10 template <class T>
12  //ATTRIBUTES
13  vector<boost::shared_ptr<T> > mVector;
14  int mIterator;
15 
16  //ASSOCIATION
17 
18 public:
19 
20  //CONSTRUCTORS
27  template<class Q>
29  // DESTRUCTORS
30 
31 public:
34  virtual ~CORE_SharedPointersList();
35  // OPERATORS
36 
41  inline const boost::shared_ptr<T>& operator[](const int & i) const {
42  ASSERT_IN(i>-1);
43  ASSERT_IN(i<((int)mVector.size()));
44  return mVector[(int)i];
45  };
50  inline boost::shared_ptr<T>& operator[](const int & i) {
51  ASSERT_IN(i>-1);
52  ASSERT_IN(i<((int)mVector.size()));
53  return mVector[(int)i];
54  };
59  inline const T& operator()(const int & i) const {
60  ASSERT_IN(i>-1);
61  ASSERT_IN(i<((int)mVector.size()));
62  ASSERT_IN(mVector[(int)i].get()!=null);
63  return *mVector[(int)i].get();
64  };
69  inline T& operator()(const int & i) {
70  ASSERT_IN(i>-1);
71  ASSERT_IN(i<((int)mVector.size()));
72  ASSERT_IN(mVector[(int)i].get()!=null);
73  return *mVector[(int)i].get();
74  };
75 
76  // New
79  static inline boost::shared_ptr<CORE_SharedPointersList<T> > New() {
80  boost::shared_ptr<CORE_SharedPointersList<T> > p(new CORE_SharedPointersList<T>(),
82  return p;
83  };
84  //COPY
88  template<class Q>
89  void copy(const CORE_SharedPointersList<Q>& cpy);
90 
91  // SET
94  inline void setSize(const int& n) {
95  mVector.resize(n);
96  };
97 
100  tBoolean set(const int& i,const boost::shared_ptr<T>& obj);
103  inline tBoolean set(const int& i,T& obj) {
104  boost::shared_ptr<T> p;
105  obj.getSharedPointer(p);
106  return set(i,p);
107  };
110  inline tBoolean set(const int & i,T* obj) {
111  boost::shared_ptr<T> p;
112  if (obj!=null) obj->getSharedPointer(p);
113  return set(i,p);
114  };
115 
119  tBoolean insert(const int& i,boost::shared_ptr<T> obj);
120 
124  inline tBoolean insert(const int & i,T* obj) {
125  boost::shared_ptr<T> p;
126  if (obj!=null) obj->getSharedPointer(p);
127  return insert(i,p);
128  };
132  inline tBoolean insert(const int& i,T& obj) {
133  boost::shared_ptr<T> p;
134  obj.getSharedPointer(p);
135  return insert(i,p);
136  };
137 
138 
141  void add(boost::shared_ptr<T> obj) {
142  mVector.push_back(obj);
143  };
146  void add(T& obj) {
147  boost::shared_ptr<T> p;
148  obj.getSharedPointer(p);
149  mVector.push_back(p);
150  };
153  void add(T* obj) {
154  boost::shared_ptr<T> p;
155  if (obj!=null) obj->getSharedPointer(p);
156  mVector.push_back(p);
157  };
158 
159 
160 
161 
164  virtual void addInList(const boost::shared_ptr<T>& obj) {
165  add(obj);
166  };
167 
170  template<class Q>
171  void merge(const CORE_SharedPointersList<Q>& array);
172 
173 
179  tBoolean remove(const boost::shared_ptr<T>& obj);
180 
186  tBoolean remove(const T& obj);
192  tBoolean remove(const T* obj);
193 
196  tBoolean removeAtIndex(const int& i);
197 
198 
201  inline tBoolean remove() {
202  return remove(size()-1);
203  };
206  inline void clear(){
207  setSize(0);
208  mVector.clear();
209  };
210 
211  // GET
212 
215  inline T* getLastElement() {
216  if (mVector.size()==0) return null;
217  return mVector[mVector.size()-1].get();
218  };
221  inline const T* getLastElement() const {
222  if (mVector.size()==0) return null;
223  return mVector[mVector.size()-1].get();
224  };
225 
230  inline const T* get(const int& i) const {
231  if (i<0) return null;
232  if (i>=((int)mVector.size())) return null;
233  return mVector[i].get();
234  };
235 
240  inline T* get(const int& i) {
241  if (i<0) return null;
242  if (i>=((int)mVector.size())) return null;
243  return mVector[i].get();
244  };
245 
247  inline int size() const {return mVector.size();};
248 
250  inline int getSize() const {return mVector.size();};
251 
252 
255  tBoolean exists(const boost::shared_ptr<T>& obj) const;
258  tBoolean exists(const T& obj) const;
259 
262  tBoolean exists(const T* obj) const;
263 
264 
265 
268  void getValues(vector<T*>& vs);
271  void getValues(vector<const T*>& vs) const;
272 
273 
274  // OTHERS
275 
276 
279  void permute(const int& i,const int&j);
280 
283  void reverse();
284 
285 
288  inline void begin() {
289  mIterator=0;
290  };
291 
294  inline tBoolean hasNext() const {
295  return ((int)mIterator<(int)mVector.size());
296  };
299  inline boost::shared_ptr<T> next() {
300  boost::shared_ptr<T> ret=mVector[mIterator];
301  mIterator++;
302  return ret;
303  };
304 
305 
306 };
308 #endif
void copy(const CORE_SharedPointersList< Q > &cpy)
New copy constructor of a shared pointers list.
Definition: CORE_SharedPointersList.hpp:26
boost::shared_ptr< T > & operator[](const int &i)
get the i-th element shared pointer ASSERT_IN(i>-1); ASSERT_IN(i<((int)mVector.size())); ...
Definition: CORE_SharedPointersList.h:50
class CORE_SharedPointersList is a list of shared pointers
Definition: CORE_SharedPointersList.h:11
void clear()
clear the array
Definition: CORE_SharedPointersList.h:206
static boost::shared_ptr< CORE_SharedPointersList< T > > New()
New constructor of a shared pointers list.
Definition: CORE_SharedPointersList.h:79
void add(T &obj)
add an element at the end
Definition: CORE_SharedPointersList.h:146
void add(boost::shared_ptr< T > obj)
add an element at the end
Definition: CORE_SharedPointersList.h:141
tBoolean insert(const int &i, T *obj)
insert the pointer at index i the old element i become the element i+1
Definition: CORE_SharedPointersList.h:124
tBoolean hasNext() const
return true if the array has next element
Definition: CORE_SharedPointersList.h:294
#define tBoolean
Definition: types.h:35
tBoolean insert(const int &i, boost::shared_ptr< T > obj)
insert the pointer at index i the old element i become the element i+1
Definition: CORE_SharedPointersList.hpp:114
void add(T *obj)
add an element at the end
Definition: CORE_SharedPointersList.h:153
void merge(const CORE_SharedPointersList< Q > &array)
merge the array in this
Definition: CORE_SharedPointersList.hpp:150
virtual ~CORE_SharedPointersList()
destroy an array of T*
Definition: CORE_SharedPointersList.hpp:19
void setSize(const int &n)
set the size of the shared pointers list
Definition: CORE_SharedPointersList.h:94
void reverse()
reverse the vector
Definition: CORE_SharedPointersList.hpp:137
#define null
Definition: types.h:13
void begin()
put the iterator on begin
Definition: CORE_SharedPointersList.h:288
void getValues(vector< T * > &vs)
get values
Definition: CORE_SharedPointersList.hpp:160
void permute(const int &i, const int &j)
permute
Definition: CORE_SharedPointersList.hpp:106
tBoolean set(const int &i, T *obj)
set the pointer at the index i
Definition: CORE_SharedPointersList.h:110
CORE_SharedPointersList()
internal constructor of a shared pointers list
Definition: CORE_SharedPointersList.hpp:7
tBoolean removeAtIndex(const int &i)
remove the pointer at index i
Definition: CORE_SharedPointersList.hpp:97
virtual void addInList(const boost::shared_ptr< T > &obj)
add a core object
Definition: CORE_SharedPointersList.h:164
const T & operator()(const int &i) const
get the i-th element ASSERT_IN(i>-1); ASSERT_IN(i<((int)mVector.size()));
Definition: CORE_SharedPointersList.h:59
int getSize() const
return the size of the array
Definition: CORE_SharedPointersList.h:250
const T * getLastElement() const
get last element pointer
Definition: CORE_SharedPointersList.h:221
T & operator()(const int &i)
get the i-th element ASSERT_IN(i>-1); ASSERT_IN(i<((int)mVector.size()));
Definition: CORE_SharedPointersList.h:69
int size() const
return the size of the array
Definition: CORE_SharedPointersList.h:247
abstract base class for most classes.
Definition: CORE_Object.h:30
T * getLastElement()
get last element pointer
Definition: CORE_SharedPointersList.h:215
tBoolean insert(const int &i, T &obj)
insert the pointer at index i the old element i become the element i+1
Definition: CORE_SharedPointersList.h:132
tBoolean set(const int &i, T &obj)
set the pointer at the index i
Definition: CORE_SharedPointersList.h:103
boost::shared_ptr< T > next()
return the next element
Definition: CORE_SharedPointersList.h:299
const boost::shared_ptr< T > & operator[](const int &i) const
get the i-th element shared pointer ASSERT_IN(i>-1); ASSERT_IN(i<((int)mVector.size())); ...
Definition: CORE_SharedPointersList.h:41
tBoolean set(const int &i, const boost::shared_ptr< T > &obj)
set the pointer at the index i
Definition: CORE_SharedPointersList.hpp:127
#define ASSERT_IN(a)
Definition: types.h:82
tBoolean exists(const boost::shared_ptr< T > &obj) const
exists
Definition: CORE_SharedPointersList.hpp:33
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:106