C++ main module for mmsd Package  1.0
CORE_SharedPointersArray.h
Go to the documentation of this file.
1 #ifndef CORE_SharedPointersArray_H
2 #define CORE_SharedPointersArray_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 
18  //capacity of the array
19  tArrayIndex mCapacity;
20 
21  //capacity factor for reallocation
22  tFlag mCapacityFactor;
23 
24  //array of boost pointer
25  boost::shared_ptr<T> *mValues;
26 
27  //ASSOCIATION
28 
29 public:
30 
31  //CONSTRUCTORS
38  template<class Q>
40  // DESTRUCTORS
41 
42 public:
45  virtual ~CORE_SharedPointersArray();
46  // OPERATORS
47 
52  inline const boost::shared_ptr<T>& operator[](const tArrayIndex & i) const {
53  ASSERT_IN(i>=0);
54  ASSERT_IN(i<mSize);
55  return mValues[i];
56  };
61  inline boost::shared_ptr<T>& operator[](const tArrayIndex & i) {
62  ASSERT_IN(i>=0);
63  ASSERT_IN(i<mSize);
64  return mValues[i];
65  };
70  inline const T& operator()(const tArrayIndex & i) const {
71  ASSERT_IN(i>=0);
72  ASSERT_IN(i<mSize);
73  ASSERT_IN(mValues[i].get()!=null);
74  return *(mValues[i].get());
75  };
80  inline T& operator()(const int & i) {
81  ASSERT_IN(i>=0);
82  ASSERT_IN(i<mSize);
83  ASSERT_IN(mValues[i].get()!=null);
84  return *(mValues[i].get());
85  };
86 
87  // New
90  static inline boost::shared_ptr<CORE_SharedPointersArray<T> > New() {
91  boost::shared_ptr<CORE_SharedPointersArray<T> > p(new CORE_SharedPointersArray<T>(),
93  return p;
94  };
95  //COPY
99  template<class Q>
100  void copy(const CORE_SharedPointersArray<Q>& cpy);
101 
102  // SET
103 public:
106  void resize(const tArrayIndex & cap);
107 
108 public:
111  void fit();
114  inline void clear(){
115  mSize=0;
116  };
119  void setSize(const tArrayIndex& n) {
120  if (mCapacity>=n) mSize=n;
121  else {
122  resize(n);
123  mSize=n;
124  }
125  };
128  inline void setCapacityFactor(const tFlag& cap) {
129  mCapacityFactor=cap;
130  }
131 
134  void set(const tArrayIndex& i,boost::shared_ptr<T> obj);
137  inline void set(const tArrayIndex& i,T& obj) {
138  boost::shared_ptr<T> p;
139  obj.getSharedPointer(p);
140  set(i,p);
141  };
144  inline void set(const tArrayIndex & i,T* obj) {
145  boost::shared_ptr<T> p;
146  if (obj!=null) obj->getSharedPointer(p);
147  set(i,p);
148  };
149 
153  void insert(const tArrayIndex& i,boost::shared_ptr<T> obj);
154 
158  inline void insert(const tArrayIndex & i,T* obj) {
159  boost::shared_ptr<T> p;
160  if (obj!=null) obj->getSharedPointer(p);
161  insert(i,p);
162  };
166  inline void insert(const tArrayIndex& i,T& obj) {
167  boost::shared_ptr<T> p;
168  obj.getSharedPointer(p);
169  insert(i,p);
170  };
171 
172 
175  void add(boost::shared_ptr<T> obj) {
176  setSize(mSize+1);
177  mValues[mSize-1]=obj;
178  };
181  void add(T& obj) {
182  boost::shared_ptr<T> p;
183  obj.getSharedPointer(p);
184  add(p);
185  };
188  void add(T* obj) {
189  boost::shared_ptr<T> p;
190  if (obj!=null) obj->getSharedPointer(p);
191  add(p);
192  };
193 
194 
200  tBoolean remove(boost::shared_ptr<T> obj) {
201  if (obj.get()==null) return false;
202  return remove(obj.get());
203  };
204 
210  tBoolean remove(const T& obj);
216  tBoolean remove(const T* obj);
217 
221 
222 
225  inline tBoolean remove() {
226  return remove(mSize-1);
227  };
228 
229 
230  // GET
231 
234  inline T* getLastElement() {
235  return (mSize==0)?null:mValues[mSize-1].get();
236  };
239  inline const T* getLastElement() const {
240  return (mSize==0)?null:mValues[mSize-1].get();
241  };
242 
247  inline const T* get(const tArrayIndex& i) const {
248  return ( (i<0) || (i>=mSize) ) ? null:mValues[i].get();
249  };
250 
255  inline T* get(const tArrayIndex& i) {
256  return ((i<0) || (i>=mSize)) ? null:mValues[i].get();
257  };
258 
259 
260 
262  inline const tArrayIndex& getSize() const {
263  return mSize;
264  };
265 
266 
269  tBoolean exists(const T& obj) const;
270 
273  tBoolean exists(const T* obj) const;
274 
275 
276 
277 
278 
279 
280 };
282 #endif
static boost::shared_ptr< CORE_SharedPointersArray< T > > New()
New constructor of a shared pointers list.
Definition: CORE_SharedPointersArray.h:90
void resize(const tArrayIndex &cap)
resize the array
Definition: CORE_SharedPointersArray.hpp:47
void set(const tArrayIndex &i, T &obj)
set the pointer at the index i
Definition: CORE_SharedPointersArray.h:137
void copy(const CORE_SharedPointersArray< Q > &cpy)
New copy constructor of a shared pointers list.
Definition: CORE_SharedPointersArray.hpp:33
tBoolean exists(const T &obj) const
exists
Definition: CORE_SharedPointersArray.hpp:192
void add(T &obj)
add an element at the end
Definition: CORE_SharedPointersArray.h:181
#define tArrayIndex
Definition: types.h:39
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_SharedPointersArray.hpp:122
void add(T *obj)
add an element at the end
Definition: CORE_SharedPointersArray.h:188
T * getLastElement()
get last element pointer
Definition: CORE_SharedPointersArray.h:234
#define tBoolean
Definition: types.h:48
T & operator()(const int &i)
get the i-th element ASSERT_IN(i>-1); ASSERT_IN(i<((int)mVector.size()));
Definition: CORE_SharedPointersArray.h:80
const T & operator()(const tArrayIndex &i) const
get the i-th element ASSERT_IN(i>-1); ASSERT_IN(i<((int)mVector.size()));
Definition: CORE_SharedPointersArray.h:70
#define null
Definition: types.h:13
void insert(const tArrayIndex &i, T &obj)
insert the pointer at index i the old element i become the element i+1
Definition: CORE_SharedPointersArray.h:166
virtual ~CORE_SharedPointersArray()
destroy an array of T*
Definition: CORE_SharedPointersArray.hpp:23
void clear()
clear the array
Definition: CORE_SharedPointersArray.h:114
CORE_SharedPointersArray()
internal constructor of a shared pointers list
Definition: CORE_SharedPointersArray.hpp:8
void fit()
fit the capacity to size
Definition: CORE_SharedPointersArray.hpp:81
void set(const tArrayIndex &i, boost::shared_ptr< T > obj)
set the pointer at the index i
Definition: CORE_SharedPointersArray.hpp:114
const T * getLastElement() const
get last element pointer
Definition: CORE_SharedPointersArray.h:239
void setSize(const tArrayIndex &n)
set the size of the shared pointers list
Definition: CORE_SharedPointersArray.h:119
const boost::shared_ptr< T > & operator[](const tArrayIndex &i) const
get the i-th element shared pointer ASSERT_IN(i>-1); ASSERT_IN(i<((int)mVector.size())); ...
Definition: CORE_SharedPointersArray.h:52
tBoolean removeAtIndex(const tArrayIndex &i)
remove the pointer at index i
Definition: CORE_SharedPointersArray.hpp:166
abstract base class for most classes.
Definition: CORE_Object.h:30
void add(boost::shared_ptr< T > obj)
add an element at the end
Definition: CORE_SharedPointersArray.h:175
void insert(const tArrayIndex &i, T *obj)
insert the pointer at index i the old element i become the element i+1
Definition: CORE_SharedPointersArray.h:158
void setCapacityFactor(const tFlag &cap)
set the capcity factor
Definition: CORE_SharedPointersArray.h:128
boost::shared_ptr< T > & operator[](const tArrayIndex &i)
get the i-th element shared pointer ASSERT_IN(i>-1); ASSERT_IN(i<((int)mVector.size())); ...
Definition: CORE_SharedPointersArray.h:61
void set(const tArrayIndex &i, T *obj)
set the pointer at the index i
Definition: CORE_SharedPointersArray.h:144
class CORE_SharedPointersArray is a list of shared pointers
Definition: CORE_SharedPointersArray.h:12
#define ASSERT_IN(a)
Definition: types.h:96
const tArrayIndex & getSize() const
return the size of the array
Definition: CORE_SharedPointersArray.h:262
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:106
#define tFlag
Definition: types.h:14