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