C++ main module for emicrom Package  1.0
CORE_SharedPointersArray.hpp
Go to the documentation of this file.
1 #ifndef CORE_SharedPointersArray_CPP
2 #define CORE_SharedPointersArray_CPP
3 
5 #include "CORE_Exception.h"
6 
7 template<class T>
9  mSize=0;
10  mCapacity=1;
12  mValues=new boost::shared_ptr<T>[mCapacity];
13 }
14 
15 template<class T>
16 template<class Q>
18  copy(cpy);
19 }
20 
21 
22 template<class T>
24  mSize=0;
25  mCapacity=0;
27  if (mValues!=null) delete[] mValues;
28 }
29 
30 
31 template<class T>
32 template<class Q>
34  tUIndex i,n=cpy.getSize();
36  setSize(n);
37  boost::shared_ptr<T> *pValues=mValues;
38  const boost::shared_ptr<T> *cValues=&cpy[0];
39  for (i=0;i<n;i++) {
40  *pValues=boost::dynamic_pointer_cast<T>(*cValues);
41  pValues++;
42  cValues++;
43  }
44 
45 }
46 template<class T>
48  // no reallocation
49  if (cap<mCapacity) return;
50  //set capacity
51  if (mCapacityFactor<=1) mCapacity=cap+1;
52  else while (mCapacity<=cap) mCapacity*=mCapacityFactor;
53  //reallocation
54  boost::shared_ptr<T> *newValues=null;
55  try {
56  newValues=new boost::shared_ptr<T>[mCapacity];
57  } catch (std::exception e) {
58  cout << e.what()<<"\n";
59  throw CORE_Exception("common/core",
60  "CORE_SharedPointersArray<T>::resize(cap)",
61  "not enought memory to allocate new array");
62  }
63 
64  //copy the array
65  if (mSize>0) {
66  const boost::shared_ptr<T> *cValues=&(*this)[0];
67  for (tUIndex i=0;i<mSize;i++) {
68  *newValues=boost::dynamic_pointer_cast<T>(*cValues);
69  newValues++;
70  cValues++;
71  }
72  }
73  //delete old values
74  if (mValues!=null) delete[] mValues;
75 
76  //make the pointer link
77  mValues=newValues;
78 
79 }
80 template<class T>
82  // no reallocation
83  if (mSize+1==mCapacity) return;
84  //set capacity
85  mCapacity=mSize+1;
86  //reallocation
87  boost::shared_ptr<T> *newValues=null;
88  try {
89  newValues =new boost::shared_ptr<T>[mCapacity];
90  } catch (std::exception e) {
91  cout << e.what()<<"\n";
92  throw CORE_Exception("common/core","CORE_SharedPointersArray<T>::resize(cap)",
93  "not enought memory to allocate new array");
94  }
95  //copy
96  if (mSize>0) {
97  const boost::shared_ptr<T> *cValues=&(*this)[0];
98  for (tUIndex i=0;i<mSize;i++) {
99  *newValues=boost::dynamic_pointer_cast<T>(*cValues);
100  newValues++;
101  cValues++;
102  }
103  }
104  //delete old values
105  if (mValues!=null) delete[] mValues;
106 
107  //make the pointer link
108  mValues=newValues;
109 }
110 
111 
112 
113 template<class T>
114 void CORE_SharedPointersArray<T>::set(const tUIndex & index,boost::shared_ptr<T> obj) {
115  if (index>=mSize) {
116  setSize(index+1);
117  }
118  mValues[index]=obj;
119 }
120 
121 template<class T>
122 void CORE_SharedPointersArray<T>::insert(const tUIndex& index,boost::shared_ptr<T> obj) {
123  if (index>=mSize) add(obj);
124  else {
125  setSize(mSize+1);
126  boost::shared_ptr<T> *pValues=&(*this)[mSize];
127  const boost::shared_ptr<T> *cValues=null;
128  if (mSize>0) {
129  cValues=&(*this)[mSize-1];
130  for (tUIndex i=mSize;i>index;i--) {
131  *pValues=*cValues;
132  pValues--;
133  cValues--;
134  }
135  }
136  *pValues=obj;
137  }
138 }
139 
140 template<class T>
142 
143  tBoolean exists=false;
144  if (mSize==0) return false;
145  tUIndex i=mSize-1;
146  boost::shared_ptr<T> *pValues=&(*this)[i];
147  if ((*pValues).get()==obj) removeAtIndex(i);
148  while (i>0) {
149  i--;
150  pValues--;
151  if ((*pValues).get()==obj) {
152  removeAtIndex(i);
153  exists=true;
154  }
155  }
156  return exists;
157 }
158 
159 template<class T>
161  return remove(&obj);
162 }
163 
164 
165 template<class T>
167 
168  if ((i<0) || (i>=mSize)) return false;
169 
170 
171  boost::shared_ptr<T> *pValues=&(*this)[i];
172  tUIndex k=i+1;
173  const boost::shared_ptr<T> *cValues=null;
174  if (k<mSize) {
175 
176  cValues=&(*this)[k];
177  while (k<mSize) {
178  *pValues=*cValues;
179  pValues++;
180  cValues++;
181  k++;
182  }
183  }
184  mSize--;
185  return true;
186 }
187 
188 
189 
190 
191 template<class T>
193  return exists(&obj);
194 };
195 template<class T>
197  if (mSize==0) return false;
198  const boost::shared_ptr<T> *pValues=&(*this)[0];
199  for (tUIndex i=0;i<mSize;i++) {
200  if ((*pValues).get()==obj) return true;
201  pValues++;
202  }
203  return false;
204 }
205 
206 
207 #endif
void copy(const CORE_SharedPointersArray< Q > &cpy)
New copy constructor of a shared pointers list.
Definition: CORE_SharedPointersArray.hpp:33
tBoolean removeAtIndex(const tUIndex &i)
remove the pointer at index i
Definition: CORE_SharedPointersArray.hpp:166
tUIndex mSize
Definition: CORE_SharedPointersArray.h:16
void resize(const tUIndex &cap)
resize the array
Definition: CORE_SharedPointersArray.hpp:47
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_SharedPointersArray.hpp:122
tBoolean remove()
remove the last pointer
Definition: CORE_SharedPointersArray.h:217
void set(const tUIndex &i, boost::shared_ptr< T > obj)
set the pointer at the index i
Definition: CORE_SharedPointersArray.hpp:114
#define tBoolean
Definition: types.h:139
boost::shared_ptr< T > * mValues
Definition: CORE_SharedPointersArray.h:25
tFlag mCapacityFactor
Definition: CORE_SharedPointersArray.h:22
const tUIndex & getSize() const
return the size of the array
Definition: CORE_SharedPointersArray.h:254
#define null
Definition: types.h:144
virtual ~CORE_SharedPointersArray()
destroy an array of T*
Definition: CORE_SharedPointersArray.hpp:23
CORE_SharedPointersArray()
internal constructor of a shared pointers list
Definition: CORE_SharedPointersArray.hpp:8
tUIndex mCapacity
Definition: CORE_SharedPointersArray.h:19
this class describes the exceptions raised for CORE package
Definition: CORE_Exception.h:15
void fit()
fit the capacity to size
Definition: CORE_SharedPointersArray.hpp:81
#define tUIndex
Definition: types.h:126
abstract base class for most classes.
Definition: CORE_Object.h:53
void setSize(const tUIndex &n)
set the size of the shared pointers list
Definition: CORE_SharedPointersArray.h:111
void add(boost::shared_ptr< T > obj)
add an element at the end
Definition: CORE_SharedPointersArray.h:167
tBoolean exists(const T &obj) const
exists
Definition: CORE_SharedPointersArray.hpp:192
class CORE_SharedPointersArray is a list of shared pointers
Definition: CORE_SharedPointersArray.h:12