1*de7b3f82SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*de7b3f82SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*de7b3f82SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*de7b3f82SAndrew Rist * distributed with this work for additional information 6*de7b3f82SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*de7b3f82SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*de7b3f82SAndrew Rist * "License"); you may not use this file except in compliance 9*de7b3f82SAndrew Rist * with the License. You may obtain a copy of the License at 10*de7b3f82SAndrew Rist * 11*de7b3f82SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*de7b3f82SAndrew Rist * 13*de7b3f82SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*de7b3f82SAndrew Rist * software distributed under the License is distributed on an 15*de7b3f82SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*de7b3f82SAndrew Rist * KIND, either express or implied. See the License for the 17*de7b3f82SAndrew Rist * specific language governing permissions and limitations 18*de7b3f82SAndrew Rist * under the License. 19*de7b3f82SAndrew Rist * 20*de7b3f82SAndrew Rist *************************************************************/ 21*de7b3f82SAndrew Rist 22*de7b3f82SAndrew Rist 23cdf0e10cSrcweir #ifndef CHART2_CLONEHELPER_HXX 24cdf0e10cSrcweir #define CHART2_CLONEHELPER_HXX 25cdf0e10cSrcweir 26cdf0e10cSrcweir #include <com/sun/star/util/XCloneable.hpp> 27cdf0e10cSrcweir 28cdf0e10cSrcweir #include <map> 29cdf0e10cSrcweir #include <functional> 30cdf0e10cSrcweir #include <algorithm> 31cdf0e10cSrcweir 32cdf0e10cSrcweir namespace chart 33cdf0e10cSrcweir { 34cdf0e10cSrcweir namespace CloneHelper 35cdf0e10cSrcweir { 36cdf0e10cSrcweir 37cdf0e10cSrcweir /// functor that clones a UNO-Reference 38cdf0e10cSrcweir template< class Interface > 39cdf0e10cSrcweir struct CreateRefClone : public ::std::unary_function< Interface, Interface > 40cdf0e10cSrcweir { operator ()chart::CloneHelper::CreateRefClone41cdf0e10cSrcweir Interface operator() ( const Interface & xOther ) 42cdf0e10cSrcweir { 43cdf0e10cSrcweir Interface xResult; 44cdf0e10cSrcweir ::com::sun::star::uno::Reference< ::com::sun::star::util::XCloneable > 45cdf0e10cSrcweir xCloneable( xOther, ::com::sun::star::uno::UNO_QUERY ); 46cdf0e10cSrcweir if( xCloneable.is()) 47cdf0e10cSrcweir xResult.set( xCloneable->createClone(), ::com::sun::star::uno::UNO_QUERY ); 48cdf0e10cSrcweir 49cdf0e10cSrcweir return xResult; 50cdf0e10cSrcweir } 51cdf0e10cSrcweir }; 52cdf0e10cSrcweir 53cdf0e10cSrcweir /// functor that clones a map element with a UNO-Reference as value 54cdf0e10cSrcweir template< typename Key, class Interface > 55cdf0e10cSrcweir struct CreateRefWithKeyClone : public ::std::unary_function< 56cdf0e10cSrcweir ::std::pair< Key, Interface >, 57cdf0e10cSrcweir ::std::pair< Key, Interface > > 58cdf0e10cSrcweir { operator ()chart::CloneHelper::CreateRefWithKeyClone59cdf0e10cSrcweir ::std::pair< Key, Interface > operator() ( 60cdf0e10cSrcweir const ::std::pair< Key, Interface > & rOther ) 61cdf0e10cSrcweir { 62cdf0e10cSrcweir Interface xResult; 63cdf0e10cSrcweir ::com::sun::star::uno::Reference< ::com::sun::star::util::XCloneable > 64cdf0e10cSrcweir xCloneable( rOther.second, ::com::sun::star::uno::UNO_QUERY ); 65cdf0e10cSrcweir if( xCloneable.is()) 66cdf0e10cSrcweir xResult.set( xCloneable->createClone(), ::com::sun::star::uno::UNO_QUERY ); 67cdf0e10cSrcweir 68cdf0e10cSrcweir return ::std::make_pair< Key, Interface >( rOther.first, xResult ); 69cdf0e10cSrcweir } 70cdf0e10cSrcweir }; 71cdf0e10cSrcweir 72cdf0e10cSrcweir /// clones a vector of UNO-References 73cdf0e10cSrcweir template< class Interface > CloneRefVector(const::std::vector<Interface> & rSource,::std::vector<Interface> & rDestination)74cdf0e10cSrcweir void CloneRefVector( 75cdf0e10cSrcweir const ::std::vector< Interface > & rSource, 76cdf0e10cSrcweir ::std::vector< Interface > & rDestination ) 77cdf0e10cSrcweir { 78cdf0e10cSrcweir ::std::transform( rSource.begin(), rSource.end(), 79cdf0e10cSrcweir ::std::back_inserter( rDestination ), 80cdf0e10cSrcweir CreateRefClone< Interface >()); 81cdf0e10cSrcweir } 82cdf0e10cSrcweir 83cdf0e10cSrcweir template< typename Key, class Interface > CloneRefPairVector(const::std::vector<::std::pair<Key,Interface>> & rSource,::std::vector<::std::pair<Key,Interface>> & rDestination)84cdf0e10cSrcweir void CloneRefPairVector( 85cdf0e10cSrcweir const ::std::vector< ::std::pair< Key, Interface > > & rSource, 86cdf0e10cSrcweir ::std::vector< ::std::pair< Key, Interface > > & rDestination ) 87cdf0e10cSrcweir { 88cdf0e10cSrcweir ::std::transform( rSource.begin(), rSource.end(), 89cdf0e10cSrcweir ::std::back_inserter( rDestination ), 90cdf0e10cSrcweir CreateRefWithKeyClone< Key, Interface >()); 91cdf0e10cSrcweir } 92cdf0e10cSrcweir 93cdf0e10cSrcweir /// clones a map of elements with a UNO-Reference as value 94cdf0e10cSrcweir template< typename Key, class Interface > CloneRefMap(const::std::map<Key,Interface> & rSource,::std::map<Key,Interface> & rDestination)95cdf0e10cSrcweir void CloneRefMap( 96cdf0e10cSrcweir const ::std::map< Key, Interface > & rSource, 97cdf0e10cSrcweir ::std::map< Key, Interface > & rDestination ) 98cdf0e10cSrcweir { 99cdf0e10cSrcweir ::std::transform( rSource.begin(), rSource.end(), 100cdf0e10cSrcweir ::std::inserter( rDestination, rDestination.begin() ), 101cdf0e10cSrcweir CreateRefWithKeyClone< const Key, Interface >()); 102cdf0e10cSrcweir } 103cdf0e10cSrcweir 104cdf0e10cSrcweir /// clones a UNO-sequence of UNO-References 105cdf0e10cSrcweir template< class Interface > CloneRefSequence(const::com::sun::star::uno::Sequence<Interface> & rSource,::com::sun::star::uno::Sequence<Interface> & rDestination)106cdf0e10cSrcweir void CloneRefSequence( 107cdf0e10cSrcweir const ::com::sun::star::uno::Sequence< Interface > & rSource, 108cdf0e10cSrcweir ::com::sun::star::uno::Sequence< Interface > & rDestination ) 109cdf0e10cSrcweir { 110cdf0e10cSrcweir rDestination.realloc( rSource.getLength()); 111cdf0e10cSrcweir ::std::transform( rSource.getConstArray(), rSource.getConstArray() + rSource.getLength(), 112cdf0e10cSrcweir rDestination.getArray(), 113cdf0e10cSrcweir CreateRefClone< Interface >()); 114cdf0e10cSrcweir } 115cdf0e10cSrcweir 116cdf0e10cSrcweir } // namespace CloneHelper 117cdf0e10cSrcweir } // namespace chart 118cdf0e10cSrcweir 119cdf0e10cSrcweir // CHART2_CLONEHELPER_HXX 120cdf0e10cSrcweir #endif 121