DCL 4.0
Loading...
Searching...
No Matches
__ARRAY.cpp
Go to the documentation of this file.
1#ifdef __DCL_INTERNAL__
2
3#ifdef __COMPILE_StringArray__
4 #define THIS_NAME __szStringArray_cpp__
5 #define THIS_VALUE __T("dcl/__ARRAY.cpp/String")
6 #define ARRAY_T StringArray
7 #define ELEMENT_T String
8 #define CONST_ELEMENT_REF const String&
9 #define HAVE_CONSTRUCTOR 1
10#elif defined(__COMPILE_ByteStringArray__)
11 #define THIS_NAME __szByteStringArray_cpp__
12 #define THIS_VALUE __T("dcl/__ARRAY.cpp/ByteString")
13 #define ARRAY_T ByteStringArray
14 #define ELEMENT_T ByteString
15 #define CONST_ELEMENT_REF const ByteString&
16 #define HAVE_CONSTRUCTOR 1
17#elif defined(__COMPILE_PointerArray__)
18 #define THIS_NAME __szPointerArray_cpp__
19 #define THIS_VALUE __T("dcl/__ARRAY.cpp/void*")
20 #define ARRAY_T PointerArray
21 #define ELEMENT_T void*
22 #define CONST_ELEMENT_REF const void*
23 #define HAVE_CONSTRUCTOR 0
24#elif defined(__COMPILE_StringStringArray__)
25 #define THIS_NAME __szStringStringArray_cpp__
26 #define THIS_VALUE __T("dcl/__ARRAY.cpp/StringString")
27 #define ARRAY_T StringStringArray
28 #define ELEMENT_T StringString
29 #define CONST_ELEMENT_REF const StringString&
30 #define HAVE_CONSTRUCTOR 1
31#endif
32
33#if __DCL_HAVE_THIS_FILE__
34 static const char_t THIS_NAME[] = THIS_VALUE;
35 #undef __THIS_FILE__
36 #define __THIS_FILE__ THIS_NAME
37#endif
38
39#if __DCL_HAVE_ALLOC_DEBUG
40 #undef __DCL_ALLOC_LEVEL
41 #define __DCL_ALLOC_LEVEL __DCL_ALLOC_INTERNAL
42#endif
43
45
47
48String ARRAY_T::toString() const
49{
50 StringBuilder r = __T("{");
51 for (ConstIterator it = begin(); it != end(); it++)
52 {
53 if (it != begin())
54 r += __T(", ");
55#ifdef __COMPILE_StringArray__
56 r += __T("\"");
57 r += String::escape((*it), (*it).length());
58 r += __T("\"");
59#elif defined(__COMPILE_ByteStringArray__)
60 r += __T("\"");
61 r += String::tryString((*it), 8);
62 r += __T("\"");
63#elif defined(__COMPILE_PointerArray__)
64 r.format(__T("%p"), (*it));
65#else
66 r += (*it).toString();
67#endif
68 }
69 r += __T("}");
70 return r;
71}
72
73#undef constructElements
74#undef destructElements
75
76#if HAVE_CONSTRUCTOR
77inline
78void
79ARRAY_T::constructElements(ELEMENT_T* _pElements, size_t _size)
80{
81 for(; _size; _size--, _pElements++)
82 {
83#if __DCL_HAVE_ALLOC_DEBUG
84 #undef new
85#endif
86
87 new((void*)_pElements) ELEMENT_T;
88
89#if __DCL_HAVE_ALLOC_DEBUG
90 #define new __DCL_DEBUG_NEW
91#endif
92 }
93}
94
95inline
96void
97ARRAY_T::destructElements(ELEMENT_T* _pElements, size_t _size)
98{
99 for( ; _size; _size--, _pElements++)
100 {
101 _pElements->~ELEMENT_T();
102 }
103}
104#else
105 #define constructElements(_pElements, _size)
106 #define destructElements(_pElements, _size)
107#endif
108
109ARRAY_T::ARRAY_T(size_t _size)
110{
111 __pData = NULL;
112 resize(_size);
113}
114
116{
117 __pData = NULL;
118 *this = _src;
119}
120
122{
123 clear();
124 free(__buf());
125}
126
127const
128ARRAY_T&
130{
131 if (&_src != this)
132 {
133 resize(_src.size());
134 for(size_t i = 0; i < _src.size(); i++)
135 __pData[i] = _src.__pData[i];
136 }
137 return *this;
138}
139
140void
142{
143 if (size() > 0)
144 {
146 __size() = 0;
147 }
148}
149
150void
152{
153 if (size() <__maxSize())
154 {
155 Buffer* buf = (Buffer*) realloc(__buf(), sizeof(Buffer) + size() * sizeof(ELEMENT_T));
156 // FIXME!
157 // if (buf == NULL)
158 // throw new BadAllocException();
160
161 buf->__maxSize = buf->__size;
162 __pData = buf->data();
163 }
164}
165
166void
167ARRAY_T::resize(size_t _size)
168{
169 if (__pData != NULL)
170 {
171 if (size() == _size)
172 return;
173
174 if (size() > _size)
175 {
176 destructElements(__pData + _size, size() - _size);
177 __size() = _size;
178 return;
179 }
180 }
181
182 if (__pData == NULL || __maxSize() < _size)
183 {
184 Buffer* buf = NULL;
185 if (__pData == NULL)
186 {
187 buf = (Buffer*) malloc(sizeof(Buffer) + _size * sizeof(ELEMENT_T));
188 // FIXME!
189 // if (buf == NULL)
190 // throw new BadAllocException();
192
193 buf->__size = 0;
194 }
195 else
196 {
197 buf = (Buffer*) realloc(__buf(), sizeof(Buffer) + _size * sizeof(ELEMENT_T));
198 // FIXME!
199 // if (buf == NULL)
200 // throw new BadAllocException();
202 }
203
204 buf->__maxSize = _size;
205 __pData = buf->data();
206 memset((void*)(__pData + buf->__size), 0, (_size - buf->__size) * sizeof(ELEMENT_T));
207 }
208
209 constructElements(__pData + size(), _size - size());
210 __size() = _size;
211}
212
213ARRAY_T::Iterator
214ARRAY_T::insert(Iterator _pos, CONST_ELEMENT_REF _element)
215{
216 __DCL_ASSERT_PARAM(begin() <= _pos);
217 __DCL_ASSERT_PARAM(_pos <= end());
218
219 size_t index = _pos - __pData;
220 insert(index, _element);
221 return __pData + index + 1;
222}
223
224ARRAY_T&
225ARRAY_T::insert(size_t _index, CONST_ELEMENT_REF _element)
226{
228 __DCL_ASSERT_PARAM(_index <= size());
229
230 size_t newSize = size() + 1;
231 if (__maxSize() < newSize)
232 {
233 Buffer* buf = (Buffer*) realloc(__buf(), sizeof(Buffer) + newSize * sizeof(ELEMENT_T));
234 // FIXME!
235 // if (buf == NULL)
236 // throw new BadAllocException();
238
239 buf->__maxSize = newSize;
240 __pData = buf->data();
241
242 if (_index < buf->__size)
243 memmove((void*)(__pData + _index + 1), (const void*)(__pData + _index),
244 sizeof(ELEMENT_T) * (buf->__size - _index));
245 }
246
247 constructElements(__pData + _index, 1);
248 __size() = newSize;
249 __pData[_index] = (ELEMENT_T) _element;
250 return *this;
251}
252
253ARRAY_T&
255{
256 if (_src.size() > 0)
257 {
258 size_t newSize = size() + _src.size();
259 if (__maxSize() < newSize)
260 {
261 Buffer* buf = (Buffer*) realloc(__buf(), sizeof(Buffer) + newSize * sizeof(ELEMENT_T));
262 // FIXME!
263 // if (buf == NULL)
264 // throw new BadAllocException();
266
267 __pData = buf->data();
268 }
269
270 constructElements(__pData + size(), _src.size());
271 for(size_t i = 0; i < _src.size(); i++)
272 __pData[size() + i] = _src.__pData[i];
273
274 __maxSize() = __size() = newSize;
275 }
276 return *this;
277}
278
279ARRAY_T::Iterator
280ARRAY_T::find(CONST_ELEMENT_REF _element)
281{
282 Iterator it = begin();
283 for ( ; it != end(); it++)
284 {
285 if (*it == _element)
286 break;
287 }
288 return it;
289}
290
291ARRAY_T&
292ARRAY_T::erase(size_t _index, size_t _size)
293{
294 __DCL_ASSERT_PARAM(_index + _size <= size());
295 if (_size > 0)
296 {
297 destructElements(__pData + _index, _size);
298 if (_index + _size < size())
299 memmove((void*)(__pData + _index), (const void*)(__pData + _index + _size),
300 (size() - (_index + _size)) * sizeof(ELEMENT_T));
301 __size() -= _size;
302 }
303 return *this;
304}
305
306#if __DCL_HAVE_THIS_FILE__
307 #undef __THIS_FILE__
308 #define __THIS_FILE__ __T(__FILE__)
309#endif
310
311#undef THIS_NAME
312#undef THIS_VALUE
313#undef ARRAY_T
314#undef ELEMENT_T
315#undef CONST_ELEMENT_REF
316#undef HAVE_CONSTRUCTOR
317
318#endif // __DCL_INTERNAL__
#define constructElements(_pElements, _size)
#define destructElements(_pElements, _size)
#define ARRAY_T
Definition __STRING.h:25
#define NULL
Definition Config.h:340
wchar_t char_t
Definition Config.h:275
#define __DCL_ASSERT_PARAM(expr)
Definition Object.h:384
#define __DCL_ASSERT(expr)
Definition Object.h:371
#define IMPLEMENT_CLASSINFO(class_name, base_class_name)
Definition Object.h:228
#define __T(str)
Definition Object.h:44
#define __DCL_ASSERT_HANDLE(expr)
Definition Object.h:383
ByteString r
ByteBuffer * buf
void CharsetConvertException *__fields clear()
ELEMENT_T * __pData
Definition __ARRAY.h:104
void shrink()
Definition __ARRAY.cpp:151
size_t index(Iterator _pos) const
Definition __ARRAY.h:195
Iterator erase(Iterator _pos)
Definition __ARRAY.h:162
size_t size() const
Definition __ARRAY.h:203
void resize(size_t _size)
Definition __ARRAY.cpp:167
ELEMENT_T * Iterator
Definition __ARRAY.h:62
ARRAY_T & add(CONST_ELEMENT_REF _element)
Definition __ARRAY.h:155
ARRAY_T(size_t _size=0)
Buffer * __buf() const
Definition __ARRAY.h:113
size_t & __size() const
Definition __ARRAY.h:114
ConstIterator end() const
Definition __ARRAY.h:134
Iterator insert(Iterator _pos, CONST_ELEMENT_REF _element)
Definition __ARRAY.cpp:214
Iterator find(CONST_ELEMENT_REF _element)
Definition __ARRAY.cpp:280
size_t & __maxSize() const
Definition __ARRAY.h:115
const ARRAY_T & operator=(const ARRAY_T &_src)
Definition __ARRAY.cpp:129
ConstIterator begin() const
Definition __ARRAY.h:127
void clear()
Definition __ARRAY.cpp:141
virtual ~ARRAY_T()
Definition __ARRAY.cpp:121