C++ main module for emicrom 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
17  //capacity of the array
19  //capacity factor
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 tUIndex & 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 tUIndex & 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 tUIndex & 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 tUIndex & cap);
114 
115 public:
118  void fit();
121  inline void clear(){
122  mSize=0;
123  };
126  void setSize(const tUIndex& 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 tUIndex& i,boost::shared_ptr<T> obj);
142 
143 
147  void insert(const tUIndex& 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 
194  tBoolean removeAtIndex(const tUIndex& i);
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 tUIndex& i) const {
229  ASSERT_IN(i<mSize);
230  boost::shared_ptr<T> obj=mValues[i].lock();
231  if (obj) return obj.get();
232  else {
233  return null;
234  }
235  };
240  inline T* get(const tUIndex& i) {
241  ASSERT_IN(i<mSize);
242  boost::shared_ptr<T> obj=mValues[i].lock();
243  if (obj) return obj.get();
244  else {
245  return null;
246  }
247  };
248 
249 
250 
252  inline const tUIndex& getSize() const {
253  return mSize;
254  };
255 
256 
259  tBoolean exists(boost::shared_ptr<T> obj) const {
260  if (obj.get()==null) return false;
261  return exist(obj.get());
262  };
265  tBoolean exists(const T& obj) const;
266 
269  tBoolean exists(const T* obj) const;
270 
271 
272 
273 
274 
275 
276 };
278 #endif
boost::shared_ptr< T > operator[](const tUIndex &i)
get the i-th element weak pointer ASSERT_IN(i>-1); ASSERT_IN(i<((int)mVector.size())); ...
Definition: CORE_WeakPointersArray.h:64
void copy(const CORE_WeakPointersArray< Q > &cpy)
New copy constructor of a weak pointers list.
Definition: CORE_WeakPointersArray.hpp:33
void insert(const tUIndex &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
CORE_WeakPointersArray()
internal constructor of a weak pointers array
Definition: CORE_WeakPointersArray.hpp:8
#define tBoolean
Definition: types.h:139
boost::shared_ptr< const T > operator[](const tUIndex &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 null
Definition: types.h:144
void setSize(const tUIndex &n)
set the size of the weak pointers list
Definition: CORE_WeakPointersArray.h:126
tFlag mCapacityFactor
Definition: CORE_WeakPointersArray.h:20
virtual ~CORE_WeakPointersArray()
destroy an array of T*
Definition: CORE_WeakPointersArray.hpp:23
tUIndex mCapacity
Definition: CORE_WeakPointersArray.h:18
class CORE_WeakPointersArray is an array of weak pointers
Definition: CORE_WeakPointersArray.h:12
void setCapacityFactor(const unsigned int &cap)
set the capcity factor
Definition: CORE_WeakPointersArray.h:135
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
#define tUIndex
Definition: types.h:126
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
const tUIndex & getSize() const
return the size of the array
Definition: CORE_WeakPointersArray.h:252
abstract base class for most classes.
Definition: CORE_Object.h:53
boost::weak_ptr< T > * mValues
Definition: CORE_WeakPointersArray.h:23
tBoolean removeAtIndex(const tUIndex &i)
remove the pointer at index i
Definition: CORE_WeakPointersArray.hpp:163
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
tBoolean exists(boost::shared_ptr< T > obj) const
exists
Definition: CORE_WeakPointersArray.h:259
void resize(const tUIndex &cap)
resize the array
Definition: CORE_WeakPointersArray.hpp:44
const T & operator()(const tUIndex &i) const
get the i-th element ASSERT_IN(i>-1); ASSERT_IN(i<((int)mVector.size()));
Definition: CORE_WeakPointersArray.h:79
#define ASSERT_IN(a)
Definition: types.h:196
tUIndex mSize
Definition: CORE_WeakPointersArray.h:16
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:141
#define tFlag
Definition: types.h:74