Xalan-C++ API Reference 1.12.0
XalanMemMgrAutoPtr.hpp
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18#if !defined(XALANMEMMGRAUTOPTR_HEADER_GUARD_1357924680)
19#define XALANMEMMGRAUTOPTR_HEADER_GUARD_1357924680
20
21
22
23// Base include file. Must be first.
25
26
27
29
30#include <cstddef>
31
32#include <cassert>
33
34#include <utility>
35
36
37
38namespace XALAN_CPP_NAMESPACE {
39
40
41
42using xercesc::MemoryManager;
43
44// An auto_ptr-like class that supports the MemoryManager class.
45template<class Type>
47{
48public:
49
50 typedef std::pair<MemoryManager*, Type*> AutoPtrPairType;
51
53 {
54 public:
55
60
62 MemoryManager* memoryManager,
65 {
66 invariants();
67 }
68
69 bool
71 {
72 return this->first != 0 && this->second != 0;
73 }
74
75 void
77 {
78 invariants();
79
80 if (isInitialized())
81 {
82 this->second->~Type();
83
84 this->first->deallocate(this->second);
85 }
86 }
87
88 void
90 MemoryManager* memoryManager,
92 {
93 invariants();
94
95 this->first = memoryManager;
96
97 this->second = dataPointer;
98
99 invariants();
100 }
101
102 private:
103
104 void
105 invariants() const
106 {
107 assert(
108 isInitialized() ||
109 (this->first == 0 && this->second == 0));
110 }
111 };
112
113
115 MemoryManager& theManager,
116 Type* ptr) :
117 m_pointerInfo(&theManager, ptr)
118 {
119 }
120
122 m_pointerInfo()
123 {
124 }
125
130
133 {
134 if (this != &theRHS)
135 {
136 m_pointerInfo.deallocate();
137
138 m_pointerInfo = theRHS.release();
139 }
140
141 return *this;
142 }
143
145 {
146 m_pointerInfo.deallocate();
147 }
148
149 Type&
150 operator*() const
151 {
152 return *m_pointerInfo.second;
153 }
154
155 Type*
157 {
158 return m_pointerInfo.second;
159 }
160
161 Type*
162 get() const
163 {
164 return m_pointerInfo.second;
165 }
166
167 MemoryManager*
169 {
170 return m_pointerInfo.first;
171 }
172
173 const MemoryManager*
175 {
176 return m_pointerInfo.first;
177 }
178
179 MemMgrAutoPtrData
181 {
182 MemMgrAutoPtrData tmp = m_pointerInfo;
183
184 m_pointerInfo.reset(0, 0);
185
186 return MemMgrAutoPtrData(tmp);
187 }
188
189 Type*
191 {
192 MemMgrAutoPtrData tmp = release();
193
194 return tmp.second;
195 }
196
197 void
199 MemoryManager* theManager = 0,
200 Type* thePointer = 0)
201 {
202 m_pointerInfo.deallocate();
203
204 m_pointerInfo.reset(theManager, thePointer);
205 }
206
207private:
208
209 // data member
210 MemMgrAutoPtrData m_pointerInfo;
211};
212
213
214
215
216template<class Type>
218{
219public:
220
221 typedef std::size_t size_type;
222
224 {
225 public:
226
228 m_memoryManager(0),
229 m_dataArray(0),
230 m_size(0)
231 {
232 }
233
235 MemoryManager* memoryManager,
237 size_type size):
238 m_memoryManager(memoryManager),
239 m_dataArray(dataPointer),
240 m_size(size)
241 {
242 invariants();
243 }
244
245 bool
247 {
248 return m_memoryManager != 0 && m_dataArray != 0 && m_size != 0;
249 }
250
251 void
253 {
254 invariants();
255
256 if ( isInitilized() )
257 {
258 assert ( m_dataArray != 0 );
259
260 for ( size_type i = 0; i < m_size ; ++i )
261 {
262 m_dataArray[i].~Type();
263 }
264
265 m_memoryManager->deallocate(m_dataArray);
266 }
267 }
268
269 void
271 MemoryManager* theMemoryManager,
273 size_type size)
274 {
275 invariants();
276
277 m_memoryManager = theMemoryManager;
278
279 m_dataArray = thePointer;
280
281 m_size = size;
282
283 invariants();
284 }
285
286 MemoryManager* m_memoryManager;
287
289
291
292 private:
293
294 void
295 invariants()const
296 {
297 assert(
298 isInitilized() ||
299 (m_memoryManager == 0 && m_dataArray == 0 && m_size == 0));
300 }
301 };
302
304 MemoryManager& theManager,
305 Type* ptr,
306 size_type size) :
307 m_pointerInfo(
308 &theManager,
309 ptr,
310 size)
311 {
312 }
313
315 m_pointerInfo()
316 {
317 }
318
323
326 {
327 if (this != &theRHS)
328 {
329 m_pointerInfo.deallocate();
330
331 m_pointerInfo = theRHS.release();
332 }
333
334 return *this;
335 }
336
338 {
339 m_pointerInfo.deallocate();
340 }
341
342 Type&
343 operator*() const
344 {
345 return *m_pointerInfo.m_dataArray;
346 }
347
348 Type*
350 {
351 return m_pointerInfo.m_dataArray;
352 }
353
354 Type*
355 get() const
356 {
357 return m_pointerInfo.m_dataArray;
358 }
359
361 getSize()const
362 {
363 return m_pointerInfo.m_size;
364 }
365
366 MemoryManager*
368 {
369 return m_pointerInfo.m_memoryManager;
370 }
371
372 const MemoryManager*
374 {
375 return m_pointerInfo.m_memoryManager;
376 }
377
380 {
381 ++m_pointerInfo.m_size;
382
383 return *this;
384 }
385
386 /* Since this class is not reference-counted, I don't see how this
387 could work, since the destruction of the temporary will free
388 the controlled pointer.
389 XalanMemMgrAutoPtrArray<Type>
390 operator++ (int)
391 {
392 XalanMemMgrAutoPtrArray<Type> temp = *this;
393 ++*this;
394
395 return temp;
396 }
397 */
398
399 MemMgrAutoPtrArrayData
401 {
402 MemMgrAutoPtrArrayData tmp = m_pointerInfo;
403
404 m_pointerInfo.reset(0, 0, 0);
405
407 }
408
409 Type*
411 {
412 MemMgrAutoPtrArrayData tmp = release();
413
414 return tmp.m_dataArray;
415 }
416
417 void
419 MemoryManager* theManager = 0,
420 Type* thePointer = 0 ,
421 size_type size = 0)
422 {
423 m_pointerInfo.deallocate();
424
425 m_pointerInfo.reset(theManager, thePointer, size);
426 }
427
428 Type&
430 {
431 return m_pointerInfo.m_dataArray[index];
432 }
433
434private:
435
436 // data member
437 MemMgrAutoPtrArrayData m_pointerInfo;
438};
439
440
441
442
443}
444
445
446
447#endif // if !defined(XALANMEMMGRAUTOPTR_HEADER_GUARD_1357924680)
#define XALAN_CPP_NAMESPACE
Xalan-C++ namespace, including major and minor version.
void reset(MemoryManager *theMemoryManager, Type *thePointer, size_type size)
MemMgrAutoPtrArrayData(MemoryManager *memoryManager, Type *dataPointer, size_type size)
void reset(MemoryManager *theManager=0, Type *thePointer=0, size_type size=0)
XalanMemMgrAutoPtrArray(const XalanMemMgrAutoPtrArray< Type > &theSource)
XalanMemMgrAutoPtrArray(MemoryManager &theManager, Type *ptr, size_type size)
XalanMemMgrAutoPtrArray< Type > & operator=(XalanMemMgrAutoPtrArray< Type > &theRHS)
const MemoryManager * getMemoryManager() const
Type & operator[](size_type index) const
MemMgrAutoPtrData(MemoryManager *memoryManager, Type *dataPointer)
void reset(MemoryManager *memoryManager, Type *dataPointer)
XalanMemMgrAutoPtr(const XalanMemMgrAutoPtr< Type > &theSource)
std::pair< MemoryManager *, Type * > AutoPtrPairType
XalanMemMgrAutoPtr(MemoryManager &theManager, Type *ptr)
const MemoryManager * getMemoryManager() const
XalanMemMgrAutoPtr< Type > & operator=(XalanMemMgrAutoPtr< Type > &theRHS)
void reset(MemoryManager *theManager=0, Type *thePointer=0)
size_t size_type
Definition XalanMap.hpp:46