1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 **************************************************************/ 21 22 // MARKER(update_precomp.py): autogen include statement, do not remove 23 //This file is about the conversion of the UOF v2.0 and ODF document format 24 #ifndef FILTER_SOURCE_XSLTFILTER_CONTAINERHELPER_HXX 25 #define FILTER_SOURCE_XSLTFILTER_CONTAINERHELPER_HXX 26 27 #include <map> 28 #include <boost/shared_ptr.hpp> 29 #include <boost/bind.hpp> 30 31 namespace XSLT{ 32 33 template< typename KeyType, typename ObjType, typename CompType = ::std::less< KeyType > > 34 class RefMap : public ::std::map< KeyType, ::boost::shared_ptr< ObjType >, CompType > 35 { 36 public: 37 typedef ::std::map< KeyType, ::boost::shared_ptr< ObjType >, CompType > container_type; 38 typedef typename container_type::key_type key_type; 39 typedef typename container_type::mapped_type mapped_type; 40 typedef typename container_type::value_type value_type; 41 typedef typename container_type::key_compare key_compare; 42 has(key_type nKey) const43 inline bool has( key_type nKey ) const 44 { 45 const mapped_type* pxRef = getRef(nKey); 46 return pxRef && pxRef->get(); 47 } 48 get(key_type nKey) const49 inline mapped_type get( key_type nKey ) const 50 { 51 if(const mapped_type* pxRef = getRef(nKey) ) return *pxRef; 52 return mapped_type(); 53 } 54 55 template<typename FunctorType > forEach(const FunctorType & rFunctor) const56 inline void forEach( const FunctorType& rFunctor) const 57 { 58 ::std::for_each(this->begin(), this->end(), ForEachFunctor< FunctorType >(rFunctor)); 59 } 60 61 template< typename FuncType > forEachMem(FuncType pFunc)62 inline void forEachMem(FuncType pFunc) 63 { 64 forEach( ::boost::bind(pFunc, _1 )); 65 } 66 67 template<typename FuncType, typename ParamType> forEachMem(FuncType pFunc,ParamType aParam) const68 inline void forEachMem(FuncType pFunc, ParamType aParam) const 69 { 70 forEach( ::boost::bind(pFunc, _1, aParam)); 71 } 72 73 template<typename FuncType, typename ParamType1, typename ParamType2> forEachMem(FuncType pFunc,ParamType1 aParam1,ParamType2 aParam2) const74 inline void forEachMem(FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2) const 75 { 76 forEach( ::boost::bind(pFunc, -1, aParam1, aParam2 )); 77 } 78 79 template<typename FuncType, typename ParamType1, typename ParamType2, typename ParamType3> forEachMem(FuncType pFunc,ParamType1 aParam1,ParamType2 aParam2,ParamType3 aParam3) const80 inline void forEachMem( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2, ParamType3 aParam3 ) const 81 { 82 forEach( ::boost::bind(pFunc, _1, aParam2, aParam2, aParam3 )); 83 } 84 85 template<typename FuncType> forEachWithKey(const FuncType & rFunctor) const86 inline void forEachWithKey(const FuncType& rFunctor) const 87 { 88 ::std::for_each( this->begin(), this->end(), ForEachFunctorWithKey< FuncType >(rFunctor)); 89 } 90 91 template<typename FuncType> forEachMemWithKey(FuncType pFunc) const92 inline void forEachMemWithKey(FuncType pFunc) const 93 { 94 forEachWithKey( ::boost::bind(pFunc, _2, _1)); 95 } 96 97 template<typename FuncType, typename ParamType> forEachMemWithKey(FuncType pFunc,ParamType aParam1) const98 inline void forEachMemWithKey(FuncType pFunc, ParamType aParam1) const 99 { 100 forEachWithKey( ::boost::bind(pFunc, _2, _1, aParam1) ); 101 } 102 103 template<typename FuncType, typename ParamType1, typename ParamType2> forEachMemWithKey(FuncType pFunc,ParamType1 aParam1,ParamType2 aParam2) const104 inline void forEachMemWithKey(FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2) const 105 { 106 forEachWithKey( ::boost::bind(pFunc, _2, _1, aParam1, aParam2) ); 107 } 108 109 template<typename FuncType, typename ParamType1, typename ParamType2, typename ParamType3> forEachMemWithKey(FuncType pFunc,ParamType1 aParam1,ParamType2 aParam2,ParamType3 aParam3) const110 inline void forEachMemWithKey(FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2, ParamType3 aParam3) const 111 { 112 forEachWithKey( ::boost::bind(pFunc, _2, _1, aParam1, aParam2, aParam3) ); 113 } 114 private: 115 template<typename FunctorType> 116 struct ForEachFunctor 117 { 118 FunctorType m_aFunctor; ForEachFunctorXSLT::RefMap::ForEachFunctor119 inline explicit ForEachFunctor( const FunctorType& rFunctor): m_aFunctor(rFunctor){} operator ()XSLT::RefMap::ForEachFunctor120 inline void operator()(const value_type& rValue) 121 { 122 if(rValue.second.get()) 123 m_aFunctor(*rValue.second); 124 } 125 }; 126 127 template<typename FunctorType> 128 struct ForEachFunctorWithKey 129 { 130 FunctorType m_aFunctor; ForEachFunctorWithKeyXSLT::RefMap::ForEachFunctorWithKey131 inline explicit ForEachFunctorWithKey( const FunctorType& rFunctor) : m_aFunctor(rFunctor){} operator ()XSLT::RefMap::ForEachFunctorWithKey132 inline void operator()(const value_type& rValue) 133 { 134 if(rValue.second.get()) 135 m_aFunctor(rValue.first, *rValue.second); 136 } 137 }; 138 getRef(key_type nKey) const139 inline const mapped_type* getRef( key_type nKey ) const 140 { 141 typename container_type::const_iterator aIt = find(nKey); 142 return (aIt == this->end())? 0 : &aIt->second; 143 } 144 }; 145 146 } 147 148 #endif 149