C++ main module for emicrom 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 
19  //ASSOCIATION
20 
21 public:
22 
23  //CONSTRUCTORS
27 
31  template<class Q>
33 
34  // DESTRUCTORS
35 public:
38  virtual ~CORE_WeakPointersList();
39  // OPERATORS
40 
45  inline boost::shared_ptr<const T> operator[](const tUIndex& i) const {
46  ASSERT_IN(i<mVector.size());
47  boost::shared_ptr<const T> v=mVector[(int)i].lock();
48  if (v) return v;
49  else {
50  boost::shared_ptr<const T> temp;
51  return temp;
52  }
53  };
56  inline boost::shared_ptr<T> operator[](const tUIndex& i) {
57  ASSERT_IN(i<mVector.size());
58  boost::shared_ptr<T> v=mVector[(int)i].lock();
59  if (v) return v;
60  else {
61  boost::shared_ptr<T> temp;
62  return temp;
63  }
64  };
65  // New
68  static inline boost::shared_ptr<CORE_WeakPointersList<T> > New() {
69  boost::shared_ptr<CORE_WeakPointersList<T> > p(new CORE_WeakPointersList<T>(),
71  return p;
72  };
73 
74  // COPY
77  template<class Q>
78  void copy(const CORE_WeakPointersList<Q>& cpy);
79 
82  template<class Q>
83  void copy(const CORE_SharedPointersList<Q>& cpy);
84 
85  // SET
88  inline void setSize(const int& n) {
89  mVector.resize(n);
90  };
91 
94  tBoolean set(const tUIndex& i,const boost::shared_ptr<T>& obj);
95 
99  tBoolean insert(const tUIndex& i,const boost::shared_ptr<T>& obj);
100 
101 
104  void add(const boost::shared_ptr<T>& obj) {
105  boost::weak_ptr<T> wobj=obj;
106  mVector.push_back(wobj);
107  };
110  void add(T& obj) {
111  boost::shared_ptr<T> p;
112  obj.getSharedPointer(p);
113  add(p);
114 
115  };
116 
119  virtual void addInList(const boost::shared_ptr<T>& obj) {
120  add(obj);
121  };
122 
125  template<class Q>
126  void merge(const CORE_WeakPointersList<Q>& array);
127 
128 
134  tBoolean remove(const boost::shared_ptr<T>& obj) {
135  return remove(obj.get());
136  }
137 
143  tBoolean remove(const T& obj);
149  tBoolean remove(const T* obj) {
150  if (obj!=null) return remove(*obj);
151  return false;
152  }
153 
156  tBoolean removeAtIndex(const tUIndex& i);
157 
158 
161  inline tBoolean remove() {
162  if (size()==0) return false;
163  return remove(size()-1);
164  };
167  inline void clear(){
168  setSize(0);
169  mVector.clear();
170  };
171 
172  // GET
173 
176  inline boost::shared_ptr<T> getLastElement() {
177  ASSERT_IN(mVector.size()>0);
178  boost::shared_ptr<T> obj=mVector[mVector.size()-1].lock();
179  if (obj) return obj;
180  else {
181  boost::shared_ptr<T> p;
182  return p;
183  }
184  };
185 
190  inline const T* get(const tUIndex& i) const {
191  ASSERT_IN(i<mVector.size());
192  boost::shared_ptr<T> obj=mVector[i].lock();
193  if (obj) return obj.get();
194  else {
195  return null;
196  }
197  };
198 
203  inline T* get(const tUIndex& i) {
204  ASSERT_IN(i<mVector.size());
205  boost::shared_ptr<T> obj=mVector[i].lock();
206  if (obj) return obj.get();
207  else {
208  return null;
209  }
210  };
211 
213  inline tUIndex size() const {return mVector.size();};
214 
216  inline tUIndex getSize() const {return mVector.size();};
217 
218 
221  tBoolean exists(const boost::shared_ptr<T>& obj) const {
222  return exists(obj.get());
223  }
226  tBoolean exists(const T& obj) const;
227 
230  tBoolean exists(const T* obj) const {
231  if (obj==null) return false;
232  return exists(*obj);
233  }
234 
235 
240  tUIndex i,n=getSize();
241  for (i=0;i<n;i++) {
242  ret.add(get(i));
243  }
244  return ret;
245  };
246 
247  // OTHERS
248 
249 
252  void permute(const tUIndex& i,const tUIndex &j);
253 
256  void reverse();
257 
260  inline typename vector<boost::weak_ptr<T> >::const_iterator begin() const {
261  return mVector.begin();
262  };
265  inline typename vector<boost::weak_ptr<T> >::iterator begin() {
266  return mVector.begin();
267  };
268 
271  inline typename vector<boost::weak_ptr<T> >::const_iterator end() const {
272  return mVector.end();
273  };
276  inline typename vector<boost::weak_ptr<T> >::iterator end() {
277  return mVector.end();
278  };
279 
280 
281 };
282 #include "CORE_WeakPointersList.hpp"
283 
284 #endif
boost::shared_ptr< T > getLastElement()
get last element pointer
Definition: CORE_WeakPointersList.h:176
class CORE_SharedPointersList is a list of shared pointers
Definition: CORE_SharedPointersList.h:11
void permute(const tUIndex &i, const tUIndex &j)
permute
Definition: CORE_WeakPointersList.hpp:88
CORE_SharedPointersList< T > toSharedVector()
to shared vector
Definition: CORE_WeakPointersList.h:238
void add(boost::shared_ptr< T > obj)
add an element at the end
Definition: CORE_SharedPointersList.h:134
tBoolean removeAtIndex(const tUIndex &i)
remove the pointer at index i
Definition: CORE_WeakPointersList.hpp:80
vector< boost::weak_ptr< T > >::const_iterator end() const
get iterator
Definition: CORE_WeakPointersList.h:271
#define tBoolean
Definition: types.h:139
vector< boost::weak_ptr< T > > mVector
Definition: CORE_WeakPointersList.h:16
CORE_WeakPointersList()
internal constructor
Definition: CORE_WeakPointersList.hpp:7
boost::shared_ptr< const T > operator[](const tUIndex &i) const
get the i-th element ASSERT_IN(i>-1); ASSERT_IN(i<((int)mVector.size()));
Definition: CORE_WeakPointersList.h:45
tUIndex size() const
return the size of the array
Definition: CORE_WeakPointersList.h:213
#define null
Definition: types.h:144
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:104
void setSize(const int &n)
set the size of the weak pointer list
Definition: CORE_WeakPointersList.h:88
tUIndex getSize() const
return the size of the array
Definition: CORE_WeakPointersList.h:216
void reverse()
reverse the vector
Definition: CORE_WeakPointersList.hpp:123
tBoolean insert(const tUIndex &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:97
virtual void addInList(const boost::shared_ptr< T > &obj)
add a core object
Definition: CORE_WeakPointersList.h:119
void add(T &obj)
add an element at the end
Definition: CORE_WeakPointersList.h:110
boost::shared_ptr< T > operator[](const tUIndex &i)
return the shared pointer at index i
Definition: CORE_WeakPointersList.h:56
static boost::shared_ptr< CORE_WeakPointersList< T > > New()
New constructor of a shared pointers list.
Definition: CORE_WeakPointersList.h:68
vector< boost::weak_ptr< T > >::iterator end()
get iterator
Definition: CORE_WeakPointersList.h:276
#define tUIndex
Definition: types.h:126
class CORE_WeakPointersList is a list of weak shared pointers
Definition: CORE_WeakPointersList.h:11
vector< boost::weak_ptr< T > >::iterator begin()
get iterator
Definition: CORE_WeakPointersList.h:265
abstract base class for most classes.
Definition: CORE_Object.h:53
void clear()
clear the array
Definition: CORE_WeakPointersList.h:167
void copy(const CORE_WeakPointersList< Q > &cpy)
copy the weak pointers list cpy
Definition: CORE_WeakPointersList.hpp:26
tBoolean exists(const boost::shared_ptr< T > &obj) const
exists
Definition: CORE_WeakPointersList.h:221
vector< boost::weak_ptr< T > >::const_iterator begin() const
get iterator
Definition: CORE_WeakPointersList.h:260
void merge(const CORE_WeakPointersList< Q > &array)
merge the array in this
Definition: CORE_WeakPointersList.hpp:135
#define ASSERT_IN(a)
Definition: types.h:196
tBoolean exists(const T *obj) const
exists
Definition: CORE_WeakPointersList.h:230
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:141