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 
38 namespace XALAN_CPP_NAMESPACE {
39 
40 
41 
42 using xercesc::MemoryManager;
43 
44 // An auto_ptr-like class that supports the MemoryManager class.
45 template<class Type>
47 {
48 public:
49 
50  typedef std::pair<MemoryManager*, Type*> AutoPtrPairType;
51 
53  {
54  public:
55 
58  {
59  }
60 
62  MemoryManager* memoryManager,
63  Type* dataPointer):
64  AutoPtrPairType(memoryManager, dataPointer)
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,
91  Type* dataPointer)
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 
127  m_pointerInfo(const_cast<XalanMemMgrAutoPtr<Type>&>(theSource).release())
128  {
129  }
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*
156  operator->() const
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 
207 private:
208 
209  // data member
210  MemMgrAutoPtrData m_pointerInfo;
211 };
212 
213 
214 
215 
216 template<class Type>
218 {
219 public:
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,
236  Type* dataPointer,
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,
272  Type* thePointer,
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 
288  Type* m_dataArray;
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 
320  m_pointerInfo(((XalanMemMgrAutoPtr<Type>&)theSource).release())
321  {
322  }
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*
349  operator->() const
350  {
351  return m_pointerInfo.m_dataArray;
352  }
353 
354  Type*
355  get() const
356  {
357  return m_pointerInfo.m_dataArray;
358  }
359 
360  size_type
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 
379  operator++ ()
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 
406  return MemMgrAutoPtrArrayData(tmp);
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&
429  operator[](size_type index) const
430  {
431  return m_pointerInfo.m_dataArray[index];
432  }
433 
434 private:
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)
xalanc::XalanMemMgrAutoPtr::operator->
Type * operator->() const
Definition: XalanMemMgrAutoPtr.hpp:156
xalanc::XalanMemMgrAutoPtrArray::get
Type * get() const
Definition: XalanMemMgrAutoPtr.hpp:355
xalanc::XalanMemMgrAutoPtr::XalanMemMgrAutoPtr
XalanMemMgrAutoPtr(MemoryManager &theManager, Type *ptr)
Definition: XalanMemMgrAutoPtr.hpp:114
xalanc::XalanMemMgrAutoPtr::reset
void reset(MemoryManager *theManager=0, Type *thePointer=0)
Definition: XalanMemMgrAutoPtr.hpp:198
xalanc::XalanMemMgrAutoPtrArray
Definition: XalanMemMgrAutoPtr.hpp:217
xalanc::XalanMemMgrAutoPtrArray::reset
void reset(MemoryManager *theManager=0, Type *thePointer=0, size_type size=0)
Definition: XalanMemMgrAutoPtr.hpp:418
XALAN_CPP_NAMESPACE
#define XALAN_CPP_NAMESPACE
Xalan-C++ namespace, including major and minor version.
Definition: XalanVersion.hpp:76
xalanc::XalanMemMgrAutoPtr::MemMgrAutoPtrData::MemMgrAutoPtrData
MemMgrAutoPtrData()
Definition: XalanMemMgrAutoPtr.hpp:56
xalanc::XalanMemMgrAutoPtrArray::MemMgrAutoPtrArrayData::isInitilized
bool isInitilized() const
Definition: XalanMemMgrAutoPtr.hpp:246
xalanc::XalanMemMgrAutoPtr::operator=
XalanMemMgrAutoPtr< Type > & operator=(XalanMemMgrAutoPtr< Type > &theRHS)
Definition: XalanMemMgrAutoPtr.hpp:132
xalanc::XalanMemMgrAutoPtr::MemMgrAutoPtrData::MemMgrAutoPtrData
MemMgrAutoPtrData(MemoryManager *memoryManager, Type *dataPointer)
Definition: XalanMemMgrAutoPtr.hpp:61
xalanc::size_type
size_t size_type
Definition: XalanMap.hpp:46
xalanc::XalanMemMgrAutoPtrArray::MemMgrAutoPtrArrayData::reset
void reset(MemoryManager *theMemoryManager, Type *thePointer, size_type size)
Definition: XalanMemMgrAutoPtr.hpp:270
xalanc::XalanMemMgrAutoPtr::getMemoryManager
MemoryManager * getMemoryManager()
Definition: XalanMemMgrAutoPtr.hpp:168
xalanc::XalanMemMgrAutoPtrArray::release
MemMgrAutoPtrArrayData release()
Definition: XalanMemMgrAutoPtr.hpp:400
xalanc::XalanMemMgrAutoPtrArray::operator=
XalanMemMgrAutoPtrArray< Type > & operator=(XalanMemMgrAutoPtrArray< Type > &theRHS)
Definition: XalanMemMgrAutoPtr.hpp:325
xalanc::XalanMemMgrAutoPtrArray::releasePtr
Type * releasePtr()
Definition: XalanMemMgrAutoPtr.hpp:410
xalanc::XalanMemMgrAutoPtr::releasePtr
Type * releasePtr()
Definition: XalanMemMgrAutoPtr.hpp:190
xalanc::XalanMemMgrAutoPtr::MemMgrAutoPtrData::reset
void reset(MemoryManager *memoryManager, Type *dataPointer)
Definition: XalanMemMgrAutoPtr.hpp:89
xalanc::XalanMemMgrAutoPtr::MemMgrAutoPtrData
Definition: XalanMemMgrAutoPtr.hpp:52
xalanc::XalanMemMgrAutoPtrArray::getSize
size_type getSize() const
Definition: XalanMemMgrAutoPtr.hpp:361
xalanc::XalanMemMgrAutoPtr::MemMgrAutoPtrData::isInitialized
bool isInitialized() const
Definition: XalanMemMgrAutoPtr.hpp:70
PlatformSupportDefinitions.hpp
xalanc::XalanMemMgrAutoPtr::operator*
Type & operator*() const
Definition: XalanMemMgrAutoPtr.hpp:150
xalanc::XalanMemMgrAutoPtrArray::MemMgrAutoPtrArrayData::MemMgrAutoPtrArrayData
MemMgrAutoPtrArrayData()
Definition: XalanMemMgrAutoPtr.hpp:227
xalanc::XalanMemMgrAutoPtr::XalanMemMgrAutoPtr
XalanMemMgrAutoPtr(const XalanMemMgrAutoPtr< Type > &theSource)
Definition: XalanMemMgrAutoPtr.hpp:126
xalanc::XalanMemMgrAutoPtrArray::MemMgrAutoPtrArrayData::MemMgrAutoPtrArrayData
MemMgrAutoPtrArrayData(MemoryManager *memoryManager, Type *dataPointer, size_type size)
Definition: XalanMemMgrAutoPtr.hpp:234
XalanMemoryManagement.hpp
xalanc::XalanMemMgrAutoPtrArray::MemMgrAutoPtrArrayData::deallocate
void deallocate()
Definition: XalanMemMgrAutoPtr.hpp:252
xalanc::XalanMemMgrAutoPtr::get
Type * get() const
Definition: XalanMemMgrAutoPtr.hpp:162
xalanc::XalanMemMgrAutoPtrArray::getMemoryManager
const MemoryManager * getMemoryManager() const
Definition: XalanMemMgrAutoPtr.hpp:373
xalanc::XalanMemMgrAutoPtr::release
MemMgrAutoPtrData release()
Definition: XalanMemMgrAutoPtr.hpp:180
xalanc::XalanMemMgrAutoPtrArray::~XalanMemMgrAutoPtrArray
~XalanMemMgrAutoPtrArray()
Definition: XalanMemMgrAutoPtr.hpp:337
xalanc::XalanMemMgrAutoPtr::MemMgrAutoPtrData::deallocate
void deallocate()
Definition: XalanMemMgrAutoPtr.hpp:76
xalanc::XalanMemMgrAutoPtrArray::size_type
std::size_t size_type
Definition: XalanMemMgrAutoPtr.hpp:221
xalanc::XalanMemMgrAutoPtrArray::getMemoryManager
MemoryManager * getMemoryManager()
Definition: XalanMemMgrAutoPtr.hpp:367
xalanc::XalanMemMgrAutoPtrArray::operator[]
Type & operator[](size_type index) const
Definition: XalanMemMgrAutoPtr.hpp:429
xalanc::XalanMemMgrAutoPtrArray::MemMgrAutoPtrArrayData
Definition: XalanMemMgrAutoPtr.hpp:223
xalanc::XalanMemMgrAutoPtr::~XalanMemMgrAutoPtr
~XalanMemMgrAutoPtr()
Definition: XalanMemMgrAutoPtr.hpp:144
xalanc::XalanMemMgrAutoPtrArray::XalanMemMgrAutoPtrArray
XalanMemMgrAutoPtrArray(MemoryManager &theManager, Type *ptr, size_type size)
Definition: XalanMemMgrAutoPtr.hpp:303
xalanc::XalanMemMgrAutoPtrArray::XalanMemMgrAutoPtrArray
XalanMemMgrAutoPtrArray()
Definition: XalanMemMgrAutoPtr.hpp:314
xalanc::XalanMemMgrAutoPtr::getMemoryManager
const MemoryManager * getMemoryManager() const
Definition: XalanMemMgrAutoPtr.hpp:174
xalanc::XalanMemMgrAutoPtrArray::MemMgrAutoPtrArrayData::m_dataArray
Type * m_dataArray
Definition: XalanMemMgrAutoPtr.hpp:288
xalanc::XalanMemMgrAutoPtr::XalanMemMgrAutoPtr
XalanMemMgrAutoPtr()
Definition: XalanMemMgrAutoPtr.hpp:121
xalanc::XalanMemMgrAutoPtr
Definition: XalanMemMgrAutoPtr.hpp:46
xalanc::XalanMemMgrAutoPtrArray::operator->
Type * operator->() const
Definition: XalanMemMgrAutoPtr.hpp:349
xalanc::XalanMemMgrAutoPtrArray::MemMgrAutoPtrArrayData::m_size
size_type m_size
Definition: XalanMemMgrAutoPtr.hpp:290
xalanc::XalanMemMgrAutoPtrArray::XalanMemMgrAutoPtrArray
XalanMemMgrAutoPtrArray(const XalanMemMgrAutoPtrArray< Type > &theSource)
Definition: XalanMemMgrAutoPtr.hpp:319
xalanc::XalanMemMgrAutoPtrArray::MemMgrAutoPtrArrayData::m_memoryManager
MemoryManager * m_memoryManager
Definition: XalanMemMgrAutoPtr.hpp:286
xalanc::XalanMemMgrAutoPtr::AutoPtrPairType
std::pair< MemoryManager *, Type * > AutoPtrPairType
Definition: XalanMemMgrAutoPtr.hpp:50
xalanc::XalanMemMgrAutoPtrArray::operator*
Type & operator*() const
Definition: XalanMemMgrAutoPtr.hpp:343