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