C++ main module for mmsd Package  1.0
CORE_WeakPointersArray.h
Go to the documentation of this file.
1 #ifndef CORE_WeakPointersArray_H
2 #define CORE_WeakPointersArray_H
3 
4 #include "boost/shared_ptr.hpp"
5 #include "boost/weak_ptr.hpp"
6 
7 #include "CORE_Object.h"
8 
11 template <class T>
13  //ATTRIBUTES
14 
15  //size of the array
16  tArrayIndex mSize;
17  //capacity of the array
18  tArrayIndex mCapacity;
19  //capacity factor
20  tFlag mCapacityFactor;
21 
22  //weak pointer array
23  boost::weak_ptr<T> *mValues;
24 
25  //ASSOCIATION
26 
27 public:
28 
29  //CONSTRUCTORS
36  template<class Q>
38  // DESTRUCTORS
39 
40 public:
43  virtual ~CORE_WeakPointersArray();
44  // OPERATORS
45 
50  inline boost::shared_ptr<const T> operator[](const tArrayIndex & i) const {
51  ASSERT_IN(i>=0);
52  ASSERT_IN(i<mSize);
53  boost::shared_ptr<const T> v=mValues[i].lock();
54  if (v) return v;
55  else {
56  boost::shared_ptr<const T> temp;
57  return temp;
58  }
59  };
64  inline boost::shared_ptr<T> operator[](const tArrayIndex & i) {
65  ASSERT_IN(i>=0);
66  ASSERT_IN(i<mSize);
67  boost::shared_ptr<T> v=mValues[i].lock();
68  if (v) return v;
69  else {
70  boost::shared_ptr<T> temp;
71  return temp;
72  }
73  };
74 
79  inline const T& operator()(const tArrayIndex & i) const {
80  boost::shared_ptr<T> p=(*this)[i];
81  ASSERT_IN(p.get()!=null);
82  return *(p.get());
83  };
88  inline T& operator()(const int & i) {
89  boost::shared_ptr<T> p=(*this)[i];
90  ASSERT_IN(p.get()!=null);
91  return *(p.get());
92  };
93 
94  // New
97  static inline boost::shared_ptr<CORE_WeakPointersArray<T> > New() {
98  boost::shared_ptr<CORE_WeakPointersArray<T> > p(new CORE_WeakPointersArray<T>(),
100  return p;
101  };
102  //COPY
106  template<class Q>
107  void copy(const CORE_WeakPointersArray<Q>& cpy);
108 
109  // SET
110 public:
113  void resize(const tArrayIndex & cap);
114 
115 public:
118  void fit();
121  inline void clear(){
122  mSize=0;
123  };
126  void setSize(const tArrayIndex& n) {
127  if (mCapacity>=n) mSize=n;
128  else {
129  resize(n);
130  mSize=n;
131  }
132  };
135  inline void setCapacityFactor(const unsigned int& cap) {
136  mCapacityFactor=cap;
137  }
138 
141  void set(const tArrayIndex& i,boost::shared_ptr<T> obj);
142 
143 
147  void insert(const tArrayIndex& i,boost::shared_ptr<T> obj);
148 
149 
150 
151 
154  void add(boost::shared_ptr<T> obj) {
155  setSize(mSize+1);
156  boost::weak_ptr<T> wobj=obj;
157  mValues[mSize-1]=wobj;
158  };
161  void add(T& obj) {
162  boost::shared_ptr<T> p;
163  obj.getSharedPointer(p);
164  add(p);
165  };
166 
167 
168 
174  tBoolean remove(boost::shared_ptr<T> obj) {
175  if (obj.get()==null) return false;
176  return remove(obj.get());
177  };
178 
184  tBoolean remove(const T& obj);
190  tBoolean remove(const T* obj);
191 
195 
196 
199  inline tBoolean remove() {
200  return remove(mSize-1);
201  };
202 
203 
204  // GET
205 
208  inline boost::shared_ptr<T> getLastElement() {
209  if (mSize==0) {
210  boost::shared_ptr<T> p;
211  return p;
212  }
213  boost::shared_ptr<T> obj=mValues[mSize-1].lock();
214  if (obj) return obj;
215  else {
216  boost::shared_ptr<T> p;
217  return p;
218  }
219 
220  };
221 
222 
223 
228  inline const T* get(const tArrayIndex& i) const {
229  ASSERT_IN(i>=0);
230  ASSERT_IN(i<mSize);
231  boost::shared_ptr<T> obj=mValues[i].lock();
232  if (obj) return obj.get();
233  else {
234  return null;
235  }
236  };
241  inline T* get(const tArrayIndex& i) {
242  ASSERT_IN(i>=0);
243  ASSERT_IN(i<mSize);
244  boost::shared_ptr<T> obj=mValues[i].lock();
245  if (obj) return obj.get();
246  else {
247  return null;
248  }
249  };
250 
251 
252 
254  inline const tArrayIndex& getSize() const {
255  return mSize;
256  };
257 
258 
261  tBoolean exists(boost::shared_ptr<T> obj) const {
262  if (obj.get()==null) return false;
263  return exist(obj.get());
264  };
267  tBoolean exists(const T& obj) const;
268 
271  tBoolean exists(const T* obj) const;
272 
273 
274 
275 
276 
277 
278 };
280 #endif
const T & operator()(const tArrayIndex &i) const
get the i-th element ASSERT_IN(i>-1); ASSERT_IN(i<((int)mVector.size()));
Definition: CORE_WeakPointersArray.h:79
boost::shared_ptr< const T > operator[](const tArrayIndex &i) const
get the i-th element weak pointer ASSERT_IN(i>-1); ASSERT_IN(i<((int)mVector.size())); ...
Definition: CORE_WeakPointersArray.h:50
#define tArrayIndex
Definition: types.h:39
tBoolean removeAtIndex(const tArrayIndex &i)
remove the pointer at index i
Definition: CORE_WeakPointersArray.hpp:163
void resize(const tArrayIndex &cap)
resize the array
Definition: CORE_WeakPointersArray.hpp:44
void copy(const CORE_WeakPointersArray< Q > &cpy)
New copy constructor of a weak pointers list.
Definition: CORE_WeakPointersArray.hpp:33
CORE_WeakPointersArray()
internal constructor of a weak pointers array
Definition: CORE_WeakPointersArray.hpp:8
const tArrayIndex & getSize() const
return the size of the array
Definition: CORE_WeakPointersArray.h:254
void set(const tArrayIndex &i, boost::shared_ptr< T > obj)
set the pointer at the index i
Definition: CORE_WeakPointersArray.hpp:111
#define tBoolean
Definition: types.h:48
#define null
Definition: types.h:13
virtual ~CORE_WeakPointersArray()
destroy an array of T*
Definition: CORE_WeakPointersArray.hpp:23
class CORE_WeakPointersArray is an array of weak pointers
Definition: CORE_WeakPointersArray.h:12
tBoolean exists(boost::shared_ptr< T > obj) const
exists
Definition: CORE_WeakPointersArray.h:261
void setCapacityFactor(const unsigned int &cap)
set the capcity factor
Definition: CORE_WeakPointersArray.h:135
boost::shared_ptr< T > operator[](const tArrayIndex &i)
get the i-th element weak pointer ASSERT_IN(i>-1); ASSERT_IN(i<((int)mVector.size())); ...
Definition: CORE_WeakPointersArray.h:64
void add(T &obj)
add an element at the end
Definition: CORE_WeakPointersArray.h:161
void clear()
clear the array
Definition: CORE_WeakPointersArray.h:121
T & operator()(const int &i)
get the i-th element ASSERT_IN(i>-1); ASSERT_IN(i<((int)mVector.size()));
Definition: CORE_WeakPointersArray.h:88
void insert(const tArrayIndex &i, boost::shared_ptr< T > obj)
insert the pointer at index i the old element i become the element i+1
Definition: CORE_WeakPointersArray.hpp:119
abstract base class for most classes.
Definition: CORE_Object.h:30
boost::shared_ptr< T > getLastElement()
get last element pointer
Definition: CORE_WeakPointersArray.h:208
void fit()
fit the capacity to size
Definition: CORE_WeakPointersArray.hpp:78
void add(boost::shared_ptr< T > obj)
add an element at the end
Definition: CORE_WeakPointersArray.h:154
static boost::shared_ptr< CORE_WeakPointersArray< T > > New()
New constructor of a weak pointers list.
Definition: CORE_WeakPointersArray.h:97
void setSize(const tArrayIndex &n)
set the size of the weak pointers list
Definition: CORE_WeakPointersArray.h:126
#define ASSERT_IN(a)
Definition: types.h:96
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:106
#define tFlag
Definition: types.h:14