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 23 #ifndef CHART2_CLONEHELPER_HXX 24 #define CHART2_CLONEHELPER_HXX 25 26 #include <com/sun/star/util/XCloneable.hpp> 27 28 #include <map> 29 #include <functional> 30 #include <algorithm> 31 32 33 // std::back_inserter lives in <iterator>. VC9's headers pulled it in 34 // transitively, a modern one does not. 35 #include <iterator> 36 namespace chart 37 { 38 namespace CloneHelper 39 { 40 41 /// functor that clones a UNO-Reference 42 template< class Interface > 43 struct CreateRefClone : public ::std::unary_function< Interface, Interface > 44 { operator ()chart::CloneHelper::CreateRefClone45 Interface operator() ( const Interface & xOther ) 46 { 47 Interface xResult; 48 ::com::sun::star::uno::Reference< ::com::sun::star::util::XCloneable > 49 xCloneable( xOther, ::com::sun::star::uno::UNO_QUERY ); 50 if( xCloneable.is()) 51 xResult.set( xCloneable->createClone(), ::com::sun::star::uno::UNO_QUERY ); 52 53 return xResult; 54 } 55 }; 56 57 /// functor that clones a map element with a UNO-Reference as value 58 template< typename Key, class Interface > 59 struct CreateRefWithKeyClone : public ::std::unary_function< 60 ::std::pair< Key, Interface >, 61 ::std::pair< Key, Interface > > 62 { operator ()chart::CloneHelper::CreateRefWithKeyClone63 ::std::pair< Key, Interface > operator() ( 64 const ::std::pair< Key, Interface > & rOther ) 65 { 66 Interface xResult; 67 ::com::sun::star::uno::Reference< ::com::sun::star::util::XCloneable > 68 xCloneable( rOther.second, ::com::sun::star::uno::UNO_QUERY ); 69 if( xCloneable.is()) 70 xResult.set( xCloneable->createClone(), ::com::sun::star::uno::UNO_QUERY ); 71 72 return ::std::make_pair< Key, Interface >( rOther.first, xResult ); 73 } 74 }; 75 76 /// clones a vector of UNO-References 77 template< class Interface > CloneRefVector(const::std::vector<Interface> & rSource,::std::vector<Interface> & rDestination)78 void CloneRefVector( 79 const ::std::vector< Interface > & rSource, 80 ::std::vector< Interface > & rDestination ) 81 { 82 ::std::transform( rSource.begin(), rSource.end(), 83 ::std::back_inserter( rDestination ), 84 CreateRefClone< Interface >()); 85 } 86 87 template< typename Key, class Interface > CloneRefPairVector(const::std::vector<::std::pair<Key,Interface>> & rSource,::std::vector<::std::pair<Key,Interface>> & rDestination)88 void CloneRefPairVector( 89 const ::std::vector< ::std::pair< Key, Interface > > & rSource, 90 ::std::vector< ::std::pair< Key, Interface > > & rDestination ) 91 { 92 ::std::transform( rSource.begin(), rSource.end(), 93 ::std::back_inserter( rDestination ), 94 CreateRefWithKeyClone< Key, Interface >()); 95 } 96 97 /// clones a map of elements with a UNO-Reference as value 98 template< typename Key, class Interface > CloneRefMap(const::std::map<Key,Interface> & rSource,::std::map<Key,Interface> & rDestination)99 void CloneRefMap( 100 const ::std::map< Key, Interface > & rSource, 101 ::std::map< Key, Interface > & rDestination ) 102 { 103 ::std::transform( rSource.begin(), rSource.end(), 104 ::std::inserter( rDestination, rDestination.begin() ), 105 CreateRefWithKeyClone< const Key, Interface >()); 106 } 107 108 /// clones a UNO-sequence of UNO-References 109 template< class Interface > CloneRefSequence(const::com::sun::star::uno::Sequence<Interface> & rSource,::com::sun::star::uno::Sequence<Interface> & rDestination)110 void CloneRefSequence( 111 const ::com::sun::star::uno::Sequence< Interface > & rSource, 112 ::com::sun::star::uno::Sequence< Interface > & rDestination ) 113 { 114 rDestination.realloc( rSource.getLength()); 115 ::std::transform( rSource.getConstArray(), rSource.getConstArray() + rSource.getLength(), 116 rDestination.getArray(), 117 CreateRefClone< Interface >()); 118 } 119 120 } // namespace CloneHelper 121 } // namespace chart 122 123 // CHART2_CLONEHELPER_HXX 124 #endif 125