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 44 { 45 typedef Interface argument_type; 46 typedef Interface result_type; operator ()chart::CloneHelper::CreateRefClone47 Interface operator() ( const Interface & xOther ) 48 { 49 Interface xResult; 50 ::com::sun::star::uno::Reference< ::com::sun::star::util::XCloneable > 51 xCloneable( xOther, ::com::sun::star::uno::UNO_QUERY ); 52 if( xCloneable.is()) 53 xResult.set( xCloneable->createClone(), ::com::sun::star::uno::UNO_QUERY ); 54 55 return xResult; 56 } 57 }; 58 59 /// functor that clones a map element with a UNO-Reference as value 60 template< typename Key, class Interface > 61 struct CreateRefWithKeyClone 62 { 63 typedef ::std::pair< Key, Interface > argument_type; 64 typedef ::std::pair< Key, Interface > result_type; operator ()chart::CloneHelper::CreateRefWithKeyClone65 ::std::pair< Key, Interface > operator() ( 66 const ::std::pair< Key, Interface > & rOther ) 67 { 68 Interface xResult; 69 ::com::sun::star::uno::Reference< ::com::sun::star::util::XCloneable > 70 xCloneable( rOther.second, ::com::sun::star::uno::UNO_QUERY ); 71 if( xCloneable.is()) 72 xResult.set( xCloneable->createClone(), ::com::sun::star::uno::UNO_QUERY ); 73 74 return ::std::make_pair< Key, Interface >( rOther.first, xResult ); 75 } 76 }; 77 78 /// clones a vector of UNO-References 79 template< class Interface > CloneRefVector(const::std::vector<Interface> & rSource,::std::vector<Interface> & rDestination)80 void CloneRefVector( 81 const ::std::vector< Interface > & rSource, 82 ::std::vector< Interface > & rDestination ) 83 { 84 ::std::transform( rSource.begin(), rSource.end(), 85 ::std::back_inserter( rDestination ), 86 CreateRefClone< Interface >()); 87 } 88 89 template< typename Key, class Interface > CloneRefPairVector(const::std::vector<::std::pair<Key,Interface>> & rSource,::std::vector<::std::pair<Key,Interface>> & rDestination)90 void CloneRefPairVector( 91 const ::std::vector< ::std::pair< Key, Interface > > & rSource, 92 ::std::vector< ::std::pair< Key, Interface > > & rDestination ) 93 { 94 ::std::transform( rSource.begin(), rSource.end(), 95 ::std::back_inserter( rDestination ), 96 CreateRefWithKeyClone< Key, Interface >()); 97 } 98 99 /// clones a map of elements with a UNO-Reference as value 100 template< typename Key, class Interface > CloneRefMap(const::std::map<Key,Interface> & rSource,::std::map<Key,Interface> & rDestination)101 void CloneRefMap( 102 const ::std::map< Key, Interface > & rSource, 103 ::std::map< Key, Interface > & rDestination ) 104 { 105 ::std::transform( rSource.begin(), rSource.end(), 106 ::std::inserter( rDestination, rDestination.begin() ), 107 CreateRefWithKeyClone< const Key, Interface >()); 108 } 109 110 /// clones a UNO-sequence of UNO-References 111 template< class Interface > CloneRefSequence(const::com::sun::star::uno::Sequence<Interface> & rSource,::com::sun::star::uno::Sequence<Interface> & rDestination)112 void CloneRefSequence( 113 const ::com::sun::star::uno::Sequence< Interface > & rSource, 114 ::com::sun::star::uno::Sequence< Interface > & rDestination ) 115 { 116 rDestination.realloc( rSource.getLength()); 117 ::std::transform( rSource.getConstArray(), rSource.getConstArray() + rSource.getLength(), 118 rDestination.getArray(), 119 CreateRefClone< Interface >()); 120 } 121 122 } // namespace CloneHelper 123 } // namespace chart 124 125 // CHART2_CLONEHELPER_HXX 126 #endif 127