C++ main module for gpm Package  1.0
CORE_WeakPointersList.h
Go to the documentation of this file.
1 #ifndef CORE_WeakPointersList_H
2 #define CORE_WeakPointersList_H
3 
4 #include "boost/shared_ptr.hpp"
5 #include "boost/weak_ptr.hpp"
6 #include "CORE_Vector.h"
10 template <class T>
12 
13 
14  //ATTRIBUTES
15  private:
16  vector<boost::weak_ptr<T> > mVector;
17 
18  int mIterator;
19 
20  //ASSOCIATION
21 
22  public:
23 
24  //CONSTRUCTORS
28 
32  template<class Q>
34 
35  // DESTRUCTORS
36  public:
39  virtual ~CORE_WeakPointersList();
40  // OPERATORS
41 
46  inline boost::shared_ptr<const T> operator[](int i) const {
47  ASSERT_IN(i>-1);
48  ASSERT_IN(i<((int)mVector.size()));
49  boost::shared_ptr<const T> v=mVector[(int)i].lock();
50  if (v) return v;
51  else {
52  boost::shared_ptr<const T> temp;
53  return temp;
54  }
55  };
58  inline boost::shared_ptr<T> operator[](int i) {
59  ASSERT_IN(i>-1);
60  ASSERT_IN(i<((int)mVector.size()));
61  boost::shared_ptr<T> v=mVector[(int)i].lock();
62  if (v) return v;
63  else {
64  boost::shared_ptr<T> temp;
65  return temp;
66  }
67  };
68  // New
71  static inline boost::shared_ptr<CORE_WeakPointersList<T> > New() {
72  boost::shared_ptr<CORE_WeakPointersList<T> > p(new CORE_WeakPointersList<T>(),
74  return p;
75  };
76 
77  // COPY
80  template<class Q>
81  void copy(const CORE_WeakPointersList<Q>& cpy);
82 
85  template<class Q>
86  void copy(const CORE_SharedPointersList<Q>& cpy);
87 
88  // SET
91  inline void setSize(const int& n) {
92  mVector.resize(n);
93  };
94 
97  tBoolean set(int i,const boost::shared_ptr<T>& obj);
98 
102  tBoolean insert(int i,const boost::shared_ptr<T>& obj);
103 
104 
107  void add(const boost::shared_ptr<T>& obj) {
108  boost::weak_ptr<T> wobj=obj;
109  mVector.push_back(wobj);
110  };
113  void add(T& obj) {
114  boost::shared_ptr<T> p;
115  obj.getSharedPointer(p);
116  add(p);
117 
118  };
119 
122  virtual void addInList(const boost::shared_ptr<T>& obj) {
123  add(obj);
124  };
125 
128  template<class Q>
129  void merge(const CORE_WeakPointersList<Q>& array);
130 
131 
137  tBoolean remove(const boost::shared_ptr<T>& obj);
138 
144  tBoolean remove(const T& obj);
150  tBoolean remove(const T* obj);
151 
154  tBoolean removeAtIndex(const int& i);
155 
156 
159  inline tBoolean remove() {
160  return remove(size()-1);
161  };
164  inline void clear(){
165  setSize(0);
166  mVector.clear();
167  };
168 
169  // GET
170 
173  inline boost::shared_ptr<T> getLastElement() {
174  ASSERT_IN(mVector.size()>0);
175  boost::shared_ptr<T> obj=mVector[mVector.size()-1].lock();
176  if (obj) return obj;
177  else {
178  boost::shared_ptr<T> p;
179  return p;
180  }
181  };
182 
187  inline const T* get(int i) const {
188  ASSERT_IN(i>-1);
189  ASSERT_IN(i<((int)mVector.size()));
190  boost::shared_ptr<T> obj=mVector[i].lock();
191  if (obj) return obj.get();
192  else {
193  return null;
194  }
195  };
196 
201  inline T* get(int i) {
202  ASSERT_IN(i>-1);
203  ASSERT_IN(i<((int)mVector.size()));
204  boost::shared_ptr<T> obj=mVector[i].lock();
205  if (obj) return obj.get();
206  else {
207  return null;
208  }
209  };
210 
212  inline int size() const {return mVector.size();};
213 
215  inline int getSize() const {return mVector.size();};
216 
217 
220  tBoolean exists(const boost::shared_ptr<T>& obj) const;
223  tBoolean exists(const T& obj) const;
224 
227  tBoolean exists(const T* obj) const;
228 
229 
234  int n=getSize();
235  for (int i=0;i<n;i++) {
236  ret.add(get(i));
237  }
238  return ret;
239  };
240 
241  // OTHERS
242 
243 
246  void permute(const int& i,const int&j);
247 
250  void reverse();
251 
252 
255  inline void begin() {
256  mIterator=0;
257  };
258 
261  inline tBoolean hasNext() const {
262  return (mIterator<mVector.size());
263  };
266  inline boost::shared_ptr<T> next() {
267  boost::shared_ptr<T> ret=mVector[mIterator].lock();
268  mIterator++;
269  if (ret) return ret;
270  else {
271  boost::shared_ptr<T> p;
272  return p;
273  }
274  };
275 };
276 #include "CORE_WeakPointersList.hpp"
277 
278 #endif
int getSize() const
return the size of the array
Definition: CORE_WeakPointersList.h:215
boost::shared_ptr< const T > operator[](int i) const
get the i-th element ASSERT_IN(i>-1); ASSERT_IN(i<((int)mVector.size()));
Definition: CORE_WeakPointersList.h:46
boost::shared_ptr< T > getLastElement()
get last element pointer
Definition: CORE_WeakPointersList.h:173
class CORE_SharedPointersList is a list of shared pointers
Definition: CORE_SharedPointersList.h:11
void begin()
put the iterator on begin
Definition: CORE_WeakPointersList.h:255
tBoolean insert(int i, const boost::shared_ptr< T > &obj)
insert the pointer at index i the old element i become the element i+1
Definition: CORE_WeakPointersList.hpp:129
CORE_SharedPointersList< T > toSharedVector()
to shared vector
Definition: CORE_WeakPointersList.h:232
void add(boost::shared_ptr< T > obj)
add an element at the end
Definition: CORE_SharedPointersList.h:141
boost::shared_ptr< T > operator[](int i)
return the shared pointer at index i
Definition: CORE_WeakPointersList.h:58
#define tBoolean
Definition: types.h:35
boost::shared_ptr< T > next()
return the next element
Definition: CORE_WeakPointersList.h:266
CORE_WeakPointersList()
internal constructor
Definition: CORE_WeakPointersList.hpp:7
tBoolean exists(const boost::shared_ptr< T > &obj) const
exists
Definition: CORE_WeakPointersList.hpp:40
void permute(const int &i, const int &j)
permute
Definition: CORE_WeakPointersList.hpp:121
tBoolean hasNext() const
return true if the array has next element
Definition: CORE_WeakPointersList.h:261
tBoolean set(int i, const boost::shared_ptr< T > &obj)
set the pointer at the index i
Definition: CORE_WeakPointersList.hpp:143
#define null
Definition: types.h:13
virtual ~CORE_WeakPointersList()
destroy an array of T*
Definition: CORE_WeakPointersList.hpp:19
void add(const boost::shared_ptr< T > &obj)
add an element at the end
Definition: CORE_WeakPointersList.h:107
void setSize(const int &n)
set the size of the weak pointer list
Definition: CORE_WeakPointersList.h:91
void reverse()
reverse the vector
Definition: CORE_WeakPointersList.hpp:154
virtual void addInList(const boost::shared_ptr< T > &obj)
add a core object
Definition: CORE_WeakPointersList.h:122
void add(T &obj)
add an element at the end
Definition: CORE_WeakPointersList.h:113
static boost::shared_ptr< CORE_WeakPointersList< T > > New()
New constructor of a shared pointers list.
Definition: CORE_WeakPointersList.h:71
class CORE_WeakPointersList is a list of weak shared pointers
Definition: CORE_WeakPointersList.h:11
int size() const
return the size of the array
Definition: CORE_WeakPointersList.h:212
abstract base class for most classes.
Definition: CORE_Object.h:30
void clear()
clear the array
Definition: CORE_WeakPointersList.h:164
void copy(const CORE_WeakPointersList< Q > &cpy)
copy the weak pointers list cpy
Definition: CORE_WeakPointersList.hpp:26
tBoolean removeAtIndex(const int &i)
remove the pointer at index i
Definition: CORE_WeakPointersList.hpp:112
void merge(const CORE_WeakPointersList< Q > &array)
merge the array in this
Definition: CORE_WeakPointersList.hpp:167
#define ASSERT_IN(a)
Definition: types.h:82
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:106