Xalan-C++ API Reference 1.12.0
STLHelper.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(STLHELPERS_HEADER_GUARD_1357924680)
19#define STLHELPERS_HEADER_GUARD_1357924680
20
21
22
23// Base include file. Must be first.
25
26
27
28#include <algorithm>
29#include <functional>
30
31
32
34
35
36
37namespace XALAN_CPP_NAMESPACE {
38
39
40
41template<class Type>
42struct
44{
45 void
47 {
48 theArg.~Type();
49 }
50
51 void
53 {
54 theArg->~Type();
55 }
56
57 void
59 {
60 (*this)(const_cast<Type*>(theArg));
61 }
62
63 void
65 Type* theArg,
66 MemoryManager& theMemoryManager)
67 {
68 if (theArg != 0)
69 {
70 (*this)(*theArg);
71
72 theMemoryManager.deallocate(theArg);
73 }
74 }
75
76 void
78 const Type* theArg,
79 MemoryManager& theMemoryManager)
80 {
81 (*this)(const_cast<Type*>(theArg), theMemoryManager);
82 }
83};
84
85
86
87template<class Type>
88XalanDestroyFunctor<Type>
89makeXalanDestroyFunctor(const Type* /* theType */)
90{
92}
93
94
95
96/**
97 * Functor to delete objects, used in STL iteration algorithms.
98 */
99template <class Type>
101{
102 DeleteFunctor(MemoryManager& theManager) :
103 m_memoryManager(theManager)
104 {
105 }
106
107 /**
108 * Delete the object pointed to by argument.
109 *
110 * @param thePointer pointer to object to be deleted
111 */
112 void
114 {
115 return makeXalanDestroyFunctor(thePointer)(thePointer, m_memoryManager);
116 }
117
118private:
119
120 MemoryManager& m_memoryManager;
121};
122
123
124/**
125 * Functor to call a clear() member function on its argument.
126 */
127template <class Type>
129{
130 /**
131 * Retrieve the value of a key-value pair.
132 *
133 * @param thePair key-value pair
134 * @return value
135 */
136 void
138 {
139 return theArg.clear();
140 }
141};
142
143
144
145/**
146 * Functor to delete value objects in maps, used in STL iteration algorithms.
147 */
148template <class T>
150{
152 m_memoryManager(theManager)
153 {
154 }
155
156 /**
157 * Delete the value object in a map value pair. The value of the pair must
158 * be of pointer type.
159 *
160 * @param thePair key-value pair
161 */
162 void
163 operator()(const typename T::value_type& thePair) const
164 {
165 return makeXalanDestroyFunctor(thePair.second)(thePair.second, m_memoryManager);
166 }
167
168private:
169
170 MemoryManager& m_memoryManager;
171};
172
173
174
175template<class MapType>
176MapValueDeleteFunctor<MapType>
181
182
183
184/**
185 * This functor is designed to compare 0-terminated arrays. It substitutes
186 * for the default less<type*> so that pointers to arrays can be compared,
187 * rather than copies of arrays. For example, you might want to use C-style
188 * strings as keys in a map, rather than string objects. The default
189 * algorithm less<const char*> would just compare the pointers, and not the
190 * vector of characters to which it points. Using this algorithm instead of
191 * the default will allow the map to work as expected.
192 */
193template<class T>
195{
196 /**
197 * Compare the values of two objects.
198 *
199 *
200 * @param theLHS first object to compare
201 * @param theRHS second object to compare
202 * @return true if objects are the same
203 */
204 bool
206 const T* theLHS,
207 const T* theRHS) const
208 {
209 while(*theLHS && *theRHS)
210 {
211 if (*theLHS != *theRHS)
212 {
213 break;
214 }
215 else
216 {
217 theLHS++;
218 theRHS++;
219 }
220 }
221
222 return *theLHS < *theRHS ? true : false;
223 }
224};
225
226
227
228template<class T>
230{
231 /**
232 * Compare the values of two objects.
233 *
234 *
235 * @param theLHS first object to compare
236 * @param theRHS second object to compare
237 * @return true if objects are the same
238 */
239 bool
241 const T* theLHS,
242 const T* theRHS) const
243 {
244 while(*theLHS && *theRHS)
245 {
246 if (*theLHS != *theRHS)
247 {
248 return false;
249 }
250 else
251 {
252 ++theLHS;
253 ++theRHS;
254 }
255 }
256
257 if (*theLHS || *theRHS)
258 {
259 return false;
260 }
261 else
262 {
263 return true;
264 }
265 }
266};
267
268
269
270template <class ScalarType>
271inline size_t
274 size_t theResult)
275{
276 return (theResult * 37) + (theResult >> 24) + size_type(theValue);
277}
278
279
280
281template <class T>
283{
284 size_t
286 const T* theKey,
287 size_t theLength,
288 size_t theInitialValue = 0) const
289 {
291
292 const T* theEnd =
294
295 while (theKey != theEnd)
296 {
298
299 ++theKey;
300 }
301
302 return ++theHashValue;
303 }
304};
305
306
307
308template <class T>
310{
311 size_t
313 const T* theKey,
314 size_t theInitialValue = 0) const
315 {
317
318 while (*theKey)
319 {
321
322 ++theKey;
323 }
324
325 return ++theHashValue;
326 }
327};
328
329
330
331template<>
337
338
339
340template<class CollectionType>
342{
343public:
344
346 m_collection(&theCollection)
347 {
348 }
349
351 {
352 if (m_collection != 0)
353 {
354 m_collection->clear();
355 }
356 }
357
358 void
360 {
361 m_collection = 0;
362 }
363
364private:
365
366 // Not implemented...
368
370 operator=(const CollectionClearGuard<CollectionType>&);
371
372 // Data members...
373 CollectionType* m_collection;
374};
375
376
377
378template<class CollectionType, class DeleteFunctorType>
380{
381public:
382
384 m_collection(&theCollection)
385 {
386 }
387
389 {
390 if (m_collection != 0)
391 {
392 using std::for_each;
393
394 // Delete all of the objects in the temp vector.
395 for_each(m_collection->begin(),
396 m_collection->end(),
397 DeleteFunctorType(m_collection->getMemoryManager()));
398 }
399 }
400
401 void
403 {
404 m_collection = 0;
405 }
406
407private:
408
409 // Not implemented...
411
414
415 // Data members...
416 CollectionType* m_collection;
417};
418
419
420
421template<class T>
423{
424 bool
426 const T* theLHS,
427 const T* theRHS) const
428 {
429 assert(theLHS != 0 && theRHS != 0);
430
431 return *theLHS == *theRHS;
432 }
433};
434
435
436
437template<class T>
439{
441 m_arg(theArg)
442 {
443 }
444
445 bool
447 const T* theOther) const
448 {
449 assert(theOther != 0);
450
451 return *theOther == *m_arg;
452 }
453
454private:
455
456 const T* m_arg;
457};
458
459
460
461template<class T>
463{
464 bool
466 const T* theLHS,
467 const T* theRHS) const
468 {
469 assert(theLHS != 0 && theRHS != 0);
470
471 using std::less;
472
473 return less<T>()(*theLHS, *theRHS);
474 }
475};
476
477
478
479template<class T>
481{
482 bool
484 const T* theLHS,
485 const T* theRHS) const
486 {
487 assert(theLHS != 0 && theRHS != 0);
488 return std::equal_to<T>()(*theLHS, *theRHS);
489 }
490};
491
492
493
494
495}
496
497
498
499#endif // STLHELPERS_HEADER_GUARD_1357924680
#define XALAN_CPP_NAMESPACE
Xalan-C++ namespace, including major and minor version.
CollectionClearGuard(CollectionType &theCollection)
CollectionDeleteGuard(CollectionType &theCollection)
MapValueDeleteFunctor< MapType > makeMapValueDeleteFunctor(MapType &theMap)
size_t size_type
Definition XalanMap.hpp:46
XalanDestroyFunctor< Type > makeXalanDestroyFunctor(const Type *)
Definition STLHelper.hpp:89
size_t XalanScalarHash(ScalarType theValue, size_t theResult)
Functor to call a clear() member function on its argument.
void operator()(Type &theArg) const
Retrieve the value of a key-value pair.
Functor to delete objects, used in STL iteration algorithms.
void operator()(const Type *thePointer) const
Delete the object pointed to by argument.
DeleteFunctor(MemoryManager &theManager)
Functor to delete value objects in maps, used in STL iteration algorithms.
void operator()(const typename T::value_type &thePair) const
Delete the value object in a map value pair.
MapValueDeleteFunctor(MemoryManager &theManager)
void operator()(Type *theArg)
Definition STLHelper.hpp:52
void operator()(Type *theArg, MemoryManager &theMemoryManager)
Definition STLHelper.hpp:64
void operator()(const Type *theArg)
Definition STLHelper.hpp:58
void operator()(const Type *theArg, MemoryManager &theMemoryManager)
Definition STLHelper.hpp:77
void operator()(Type &theArg)
Definition STLHelper.hpp:46
hash_null_terminated_array< XalanDOMChar > Hasher
equal_null_terminated_arrays< XalanDOMChar > Comparator
bool operator()(const T *theLHS, const T *theRHS) const
Compare the values of two objects.
This functor is designed to compare 0-terminated arrays.
bool operator()(const T *theLHS, const T *theRHS) const
Compare the values of two objects.
bool operator()(const T *theLHS, const T *theRHS) const
bool operator()(const T *theOther) const
pointer_equals_predicate(const T *theArg)
bool operator()(const T *theLHS, const T *theRHS) const
bool operator()(const T *theLHS, const T *theRHS) const