1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 #ifndef CHART2_CLONEHELPER_HXX 28 #define CHART2_CLONEHELPER_HXX 29 30 #include <com/sun/star/util/XCloneable.hpp> 31 32 #include <map> 33 #include <functional> 34 #include <algorithm> 35 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 { 45 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 { 63 ::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 > 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 > 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 > 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 > 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