00001 /* 00002 * Licensed to the Apache Software Foundation (ASF) under one 00003 * or more contributor license agreements. See the NOTICE file 00004 * distributed with this work for additional information 00005 * regarding copyright ownership. The ASF licenses this file 00006 * to you under the Apache License, Version 2.0 (the "License"); 00007 * you may not use this file except in compliance with the License. 00008 * You may obtain a copy of the License at 00009 * 00010 * http://www.apache.org/licenses/LICENSE-2.0 00011 * 00012 * Unless required by applicable law or agreed to in writing, software 00013 * distributed under the License is distributed on an "AS IS" BASIS, 00014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 * See the License for the specific language governing permissions and 00016 * limitations under the License. 00017 */ 00018 #if !defined(STLHELPERS_HEADER_GUARD_1357924680) 00019 #define STLHELPERS_HEADER_GUARD_1357924680 00020 00021 00022 00023 // Base include file. Must be first. 00024 #include <xalanc/Include/PlatformDefinitions.hpp> 00025 00026 00027 00028 #include <algorithm> 00029 #include <functional> 00030 00031 00032 00033 #include <xalanc/Include/XalanMap.hpp> 00034 00035 00036 00037 XALAN_CPP_NAMESPACE_BEGIN 00038 00039 00040 00041 template<class Type> 00042 struct 00043 XalanDestroyFunctor 00044 { 00045 void 00046 operator()(Type& theArg) 00047 { 00048 theArg.~Type(); 00049 } 00050 00051 void 00052 operator()(Type* theArg) 00053 { 00054 theArg->~Type(); 00055 } 00056 00057 void 00058 operator()(const Type* theArg) 00059 { 00060 (*this)(const_cast<Type*>(theArg)); 00061 } 00062 00063 void 00064 operator()( 00065 Type* theArg, 00066 MemoryManager& theMemoryManager) 00067 { 00068 if (theArg != 0) 00069 { 00070 (*this)(*theArg); 00071 00072 theMemoryManager.deallocate(theArg); 00073 } 00074 } 00075 00076 void 00077 operator()( 00078 const Type* theArg, 00079 MemoryManager& theMemoryManager) 00080 { 00081 (*this)(const_cast<Type*>(theArg), theMemoryManager); 00082 } 00083 }; 00084 00085 00086 00087 template<class Type> 00088 XalanDestroyFunctor<Type> 00089 makeXalanDestroyFunctor(const Type* /* theType */) 00090 { 00091 return XalanDestroyFunctor<Type>(); 00092 } 00093 00094 00095 00096 /** 00097 * Functor to delete objects, used in STL iteration algorithms. 00098 */ 00099 template <class Type> 00100 #if defined(XALAN_NO_STD_NAMESPACE) 00101 struct DeleteFunctor : public unary_function<const Type*, void> 00102 #else 00103 struct DeleteFunctor : public std::unary_function<const Type*, void> 00104 #endif 00105 { 00106 #if defined(XALAN_NO_STD_NAMESPACE) 00107 typedef unary_function<const Type*, void> BaseClassType; 00108 #else 00109 typedef std::unary_function<const Type*, void> BaseClassType; 00110 #endif 00111 00112 typedef typename BaseClassType::result_type result_type; 00113 typedef typename BaseClassType::argument_type argument_type; 00114 00115 DeleteFunctor(MemoryManager& theManager) : 00116 m_memoryManager(theManager) 00117 { 00118 } 00119 00120 /** 00121 * Delete the object pointed to by argument. 00122 * 00123 * @param thePointer pointer to object to be deleted 00124 */ 00125 result_type 00126 operator()(argument_type thePointer) const 00127 { 00128 return makeXalanDestroyFunctor(thePointer)(thePointer, m_memoryManager); 00129 } 00130 00131 private: 00132 00133 MemoryManager& m_memoryManager; 00134 }; 00135 00136 00137 00138 #if !defined(XALAN_SGI_BASED_STL) 00139 00140 /** 00141 * Functor to retrieve the key of a key-value pair in a map, used in STL 00142 * iteration algorithms. 00143 */ 00144 template <class PairType> 00145 #if defined(XALAN_NO_STD_NAMESPACE) 00146 struct select1st : public unary_function<PairType, PairType::first_type> 00147 #else 00148 struct select1st : public std::unary_function<PairType, typename PairType::first_type> 00149 #endif 00150 { 00151 #if defined(XALAN_NO_STD_NAMESPACE) 00152 typedef unary_function<PairType, PairType::first_type> BaseClassType; 00153 #else 00154 typedef std::unary_function<PairType, typename PairType::first_type> BaseClassType; 00155 #endif 00156 00157 typedef typename BaseClassType::result_type result_type; 00158 typedef typename BaseClassType::argument_type argument_type; 00159 00160 typedef PairType value_type; 00161 00162 /** 00163 * Retrieve the key of a key-value pair. 00164 * 00165 * @param thePair key-value pair 00166 * @return key 00167 */ 00168 result_type 00169 operator()(const argument_type& thePair) const 00170 { 00171 return thePair.first; 00172 } 00173 }; 00174 00175 00176 00177 /** 00178 * Functor to retrieve the value of a key-value pair in a map, used in STL 00179 * iteration algorithms. 00180 */ 00181 template <class PairType> 00182 #if defined(XALAN_NO_STD_NAMESPACE) 00183 struct select2nd : public unary_function<PairType, PairType::second_type> 00184 #else 00185 struct select2nd : public std::unary_function<PairType, typename PairType::second_type> 00186 #endif 00187 { 00188 #if defined(XALAN_NO_STD_NAMESPACE) 00189 typedef unary_function<PairType, PairType::second_type> BaseClassType; 00190 #else 00191 typedef std::unary_function<PairType, typename PairType::second_type> BaseClassType; 00192 #endif 00193 00194 typedef typename BaseClassType::result_type result_type; 00195 typedef typename BaseClassType::argument_type argument_type; 00196 00197 typedef PairType value_type; 00198 00199 /** 00200 * Retrieve the value of a key-value pair. 00201 * 00202 * @param thePair key-value pair 00203 * @return value 00204 */ 00205 result_type 00206 operator()(const argument_type& thePair) const 00207 { 00208 return thePair.second; 00209 } 00210 }; 00211 00212 #endif 00213 00214 00215 00216 /** 00217 * Functor to call a clear() member function on its argument. 00218 */ 00219 template <class Type> 00220 #if defined(XALAN_NO_STD_NAMESPACE) 00221 struct ClearFunctor : public unary_function<Type, void> 00222 #else 00223 struct ClearFunctor : public std::unary_function<Type, void> 00224 #endif 00225 { 00226 #if defined(XALAN_NO_STD_NAMESPACE) 00227 typedef unary_function<Type, void> BaseClassType; 00228 #else 00229 typedef std::unary_function<Type, void> BaseClassType; 00230 #endif 00231 00232 typedef typename BaseClassType::result_type result_type; 00233 typedef typename BaseClassType::argument_type argument_type; 00234 00235 typedef Type value_type; 00236 00237 /** 00238 * Retrieve the value of a key-value pair. 00239 * 00240 * @param thePair key-value pair 00241 * @return value 00242 */ 00243 result_type 00244 operator()(argument_type& theArg) const 00245 { 00246 return theArg.clear(); 00247 } 00248 }; 00249 00250 00251 00252 /** 00253 * Functor to delete value objects in maps, used in STL iteration algorithms. 00254 */ 00255 template <class T> 00256 #if defined(XALAN_NO_STD_NAMESPACE) 00257 struct MapValueDeleteFunctor : public unary_function<const typename T::value_type&, void> 00258 #else 00259 struct MapValueDeleteFunctor : public std::unary_function<const typename T::value_type&, void> 00260 #endif 00261 { 00262 #if defined(XALAN_NO_STD_NAMESPACE) 00263 typedef unary_function<const typename T::value_type&, void> BaseClassType; 00264 #else 00265 typedef std::unary_function<const typename T::value_type&, void> BaseClassType; 00266 #endif 00267 00268 typedef typename BaseClassType::result_type result_type; 00269 typedef typename BaseClassType::argument_type argument_type; 00270 00271 MapValueDeleteFunctor(MemoryManager& theManager) : 00272 m_memoryManager(theManager) 00273 { 00274 } 00275 00276 /** 00277 * Delete the value object in a map value pair. The value of the pair must 00278 * be of pointer type. 00279 * 00280 * @param thePair key-value pair 00281 */ 00282 result_type 00283 operator()(argument_type thePair) const 00284 { 00285 return makeXalanDestroyFunctor(thePair.second)(thePair.second, m_memoryManager); 00286 } 00287 00288 private: 00289 00290 MemoryManager& m_memoryManager; 00291 }; 00292 00293 00294 00295 template<class MapType> 00296 MapValueDeleteFunctor<MapType> 00297 makeMapValueDeleteFunctor(MapType& theMap) 00298 { 00299 return MapValueDeleteFunctor<MapType>(theMap.getMemoryManager()); 00300 } 00301 00302 00303 00304 /** 00305 * This functor is designed to compare 0-terminated arrays. It substitutes 00306 * for the default less<type*> so that pointers to arrays can be compared, 00307 * rather than copies of arrays. For example, you might want to use C-style 00308 * strings as keys in a map, rather than string objects. The default 00309 * algorithm less<const char*> would just compare the pointers, and not the 00310 * vector of characters to which it points. Using this algorithm instead of 00311 * the default will allow the map to work as expected. 00312 */ 00313 template<class T> 00314 #if defined(XALAN_NO_STD_NAMESPACE) 00315 struct less_null_terminated_arrays : public binary_function<const T*, const T*, bool> 00316 #else 00317 struct less_null_terminated_arrays : public std::binary_function<const T*, const T*, bool> 00318 #endif 00319 { 00320 #if defined(XALAN_NO_STD_NAMESPACE) 00321 typedef binary_function<const T*, const T*, bool> BaseClassType; 00322 #else 00323 typedef std::binary_function<const T*, const T*, bool> BaseClassType; 00324 #endif 00325 00326 typedef typename BaseClassType::result_type result_type; 00327 typedef typename BaseClassType::first_argument_type first_argument_type; 00328 typedef typename BaseClassType::second_argument_type second_argument_type; 00329 00330 /** 00331 * Compare the values of two objects. 00332 * 00333 * 00334 * @param theLHS first object to compare 00335 * @param theRHS second object to compare 00336 * @return true if objects are the same 00337 */ 00338 result_type 00339 operator()( 00340 first_argument_type theLHS, 00341 second_argument_type theRHS) const 00342 { 00343 while(*theLHS && *theRHS) 00344 { 00345 if (*theLHS != *theRHS) 00346 { 00347 break; 00348 } 00349 else 00350 { 00351 theLHS++; 00352 theRHS++; 00353 } 00354 } 00355 00356 return *theLHS < *theRHS ? true : false; 00357 } 00358 }; 00359 00360 00361 00362 template<class T> 00363 struct equal_null_terminated_arrays : public XALAN_STD_QUALIFIER binary_function<const T*, const T*, bool> 00364 { 00365 typedef XALAN_STD_QUALIFIER binary_function<const T*, const T*, bool> BaseClassType; 00366 00367 typedef typename BaseClassType::result_type result_type; 00368 typedef typename BaseClassType::first_argument_type first_argument_type; 00369 typedef typename BaseClassType::second_argument_type second_argument_type; 00370 /** 00371 * Compare the values of two objects. 00372 * 00373 * 00374 * @param theLHS first object to compare 00375 * @param theRHS second object to compare 00376 * @return true if objects are the same 00377 */ 00378 result_type 00379 operator()( 00380 first_argument_type theLHS, 00381 second_argument_type theRHS) const 00382 { 00383 while(*theLHS && *theRHS) 00384 { 00385 if (*theLHS != *theRHS) 00386 { 00387 return false; 00388 } 00389 else 00390 { 00391 ++theLHS; 00392 ++theRHS; 00393 } 00394 } 00395 00396 if (*theLHS || *theRHS) 00397 { 00398 return false; 00399 } 00400 else 00401 { 00402 return true; 00403 } 00404 } 00405 }; 00406 00407 00408 00409 template <class ScalarType> 00410 inline size_t 00411 XalanScalarHash( 00412 ScalarType theValue, 00413 size_t theResult) 00414 { 00415 return (theResult * 37) + (theResult >> 24) + size_type(theValue); 00416 } 00417 00418 00419 00420 template <class T> 00421 struct hash_non_terminated_array : public XALAN_STD_QUALIFIER unary_function<const T*, size_t> 00422 { 00423 typedef XALAN_STD_QUALIFIER unary_function<const T*, size_t> BaseClassType; 00424 00425 typedef typename BaseClassType::result_type result_type; 00426 typedef typename BaseClassType::argument_type argument_type; 00427 00428 result_type 00429 operator() ( 00430 argument_type theKey, 00431 result_type theLength, 00432 result_type theInitialValue = 0) const 00433 { 00434 result_type theHashValue = theInitialValue; 00435 00436 const argument_type theEnd = 00437 theKey + theLength; 00438 00439 while (theKey != theEnd) 00440 { 00441 theHashValue += XalanScalarHash(*theKey, theHashValue); 00442 00443 ++theKey; 00444 } 00445 00446 return ++theHashValue; 00447 } 00448 }; 00449 00450 00451 00452 template <class T> 00453 struct hash_null_terminated_array : public XALAN_STD_QUALIFIER unary_function<const T*, size_t> 00454 { 00455 typedef XALAN_STD_QUALIFIER unary_function<const T*, size_t> BaseClassType; 00456 00457 typedef typename BaseClassType::result_type result_type; 00458 typedef typename BaseClassType::argument_type argument_type; 00459 00460 result_type 00461 operator() ( 00462 argument_type theKey, 00463 result_type theInitialValue = 0) const 00464 { 00465 result_type theHashValue = theInitialValue; 00466 00467 while (*theKey) 00468 { 00469 theHashValue += XalanScalarHash(*theKey, theHashValue); 00470 00471 ++theKey; 00472 } 00473 00474 return ++theHashValue; 00475 } 00476 }; 00477 00478 00479 00480 template<> 00481 struct XalanMapKeyTraits<const XalanDOMChar*> 00482 { 00483 typedef hash_null_terminated_array<XalanDOMChar> Hasher; 00484 typedef equal_null_terminated_arrays<XalanDOMChar> Comparator; 00485 }; 00486 00487 00488 00489 template<class CollectionType> 00490 class CollectionClearGuard 00491 { 00492 public: 00493 00494 CollectionClearGuard(CollectionType& theCollection) : 00495 m_collection(&theCollection) 00496 { 00497 } 00498 00499 ~CollectionClearGuard() 00500 { 00501 if (m_collection != 0) 00502 { 00503 m_collection->clear(); 00504 } 00505 } 00506 00507 void 00508 release() 00509 { 00510 m_collection = 0; 00511 } 00512 00513 private: 00514 00515 // Not implemented... 00516 CollectionClearGuard(const CollectionClearGuard<CollectionType>&); 00517 00518 CollectionClearGuard<CollectionType>& 00519 operator=(const CollectionClearGuard<CollectionType>&); 00520 00521 // Data members... 00522 CollectionType* m_collection; 00523 }; 00524 00525 00526 00527 template<class CollectionType, class DeleteFunctorType> 00528 class CollectionDeleteGuard 00529 { 00530 public: 00531 00532 CollectionDeleteGuard(CollectionType& theCollection) : 00533 m_collection(&theCollection) 00534 { 00535 } 00536 00537 ~CollectionDeleteGuard() 00538 { 00539 if (m_collection != 0) 00540 { 00541 #if !defined(XALAN_NO_STD_NAMESPACE) 00542 using std::for_each; 00543 #endif 00544 00545 // Delete all of the objects in the temp vector. 00546 for_each(m_collection->begin(), 00547 m_collection->end(), 00548 DeleteFunctorType(m_collection->getMemoryManager())); 00549 } 00550 } 00551 00552 void 00553 release() 00554 { 00555 m_collection = 0; 00556 } 00557 00558 private: 00559 00560 // Not implemented... 00561 CollectionDeleteGuard(const CollectionDeleteGuard<CollectionType, DeleteFunctorType>&); 00562 00563 CollectionDeleteGuard<CollectionType, DeleteFunctorType>& 00564 operator=(const CollectionDeleteGuard<CollectionType, DeleteFunctorType>&); 00565 00566 // Data members... 00567 CollectionType* m_collection; 00568 }; 00569 00570 00571 00572 template<class T> 00573 #if defined(XALAN_NO_STD_NAMESPACE) 00574 struct pointer_equals : public binary_function<const T*, const T*, bool> 00575 #else 00576 struct pointer_equals : public std::binary_function<const T*, const T*, bool> 00577 #endif 00578 { 00579 #if defined(XALAN_NO_STD_NAMESPACE) 00580 typedef binary_function<const T*, const T*, bool> BaseClassType; 00581 #else 00582 typedef std::binary_function<const T*, const T*, bool> BaseClassType; 00583 #endif 00584 00585 typedef typename BaseClassType::result_type result_type; 00586 typedef typename BaseClassType::first_argument_type first_argument_type; 00587 typedef typename BaseClassType::second_argument_type second_argument_type; 00588 00589 result_type 00590 operator()( 00591 first_argument_type theLHS, 00592 second_argument_type theRHS) const 00593 { 00594 assert(theLHS != 0 && theRHS != 0); 00595 00596 return *theLHS == *theRHS; 00597 } 00598 }; 00599 00600 00601 00602 template<class T> 00603 #if defined(XALAN_NO_STD_NAMESPACE) 00604 struct pointer_equals_predicate : public unary_function<const T*, bool> 00605 #else 00606 struct pointer_equals_predicate : public std::unary_function<const T*, bool> 00607 #endif 00608 { 00609 #if defined(XALAN_NO_STD_NAMESPACE) 00610 typedef unary_function<const T*, bool> BaseClassType; 00611 #else 00612 typedef std::unary_function<const T*, bool> BaseClassType; 00613 #endif 00614 00615 typedef typename BaseClassType::result_type result_type; 00616 typedef typename BaseClassType::argument_type argument_type; 00617 00618 pointer_equals_predicate(argument_type theArg) : 00619 m_arg(theArg) 00620 { 00621 } 00622 00623 result_type 00624 operator()( 00625 argument_type theOther) const 00626 { 00627 assert(theOther != 0); 00628 00629 return *theOther == *m_arg; 00630 } 00631 00632 private: 00633 00634 const argument_type m_arg; 00635 }; 00636 00637 00638 00639 template<class T> 00640 #if defined(XALAN_NO_STD_NAMESPACE) 00641 struct pointer_less : public binary_function<const T*, const T*, bool> 00642 #else 00643 struct pointer_less : public std::binary_function<const T*, const T*, bool> 00644 #endif 00645 { 00646 #if defined(XALAN_NO_STD_NAMESPACE) 00647 typedef binary_function<const T*, const T*, bool> BaseClassType; 00648 #else 00649 typedef std::binary_function<const T*, const T*, bool> BaseClassType; 00650 #endif 00651 00652 typedef typename BaseClassType::result_type result_type; 00653 typedef typename BaseClassType::first_argument_type first_argument_type; 00654 typedef typename BaseClassType::second_argument_type second_argument_type; 00655 00656 result_type 00657 operator()( 00658 first_argument_type theLHS, 00659 second_argument_type theRHS) const 00660 { 00661 assert(theLHS != 0 && theRHS != 0); 00662 00663 #if !defined(XALAN_NO_STD_NAMESPACE) 00664 using std::less; 00665 #endif 00666 00667 return less<T>()(*theLHS, *theRHS); 00668 } 00669 }; 00670 00671 00672 00673 template<class T> 00674 struct pointer_equal : public XALAN_STD_QUALIFIER binary_function<const T*, const T*, bool> 00675 { 00676 typedef XALAN_STD_QUALIFIER binary_function<const T*, const T*, bool> BaseClassType; 00677 00678 typedef typename BaseClassType::result_type result_type; 00679 typedef typename BaseClassType::first_argument_type first_argument_type; 00680 typedef typename BaseClassType::second_argument_type second_argument_type; 00681 00682 result_type 00683 operator()( 00684 first_argument_type theLHS, 00685 second_argument_type theRHS) const 00686 { 00687 assert(theLHS != 0 && theRHS != 0); 00688 return XALAN_STD_QUALIFIER equal_to<T>()(*theLHS, *theRHS); 00689 } 00690 }; 00691 00692 00693 00694 00695 XALAN_CPP_NAMESPACE_END 00696 00697 00698 00699 #endif // STLHELPERS_HEADER_GUARD_1357924680
Doxygen and GraphViz are used to generate this API documentation from the Xalan-C header files.
Xalan-C++ XSLT Processor Version 1.11 |
|