xref: /trunk/main/chart2/source/inc/ContainerHelper.hxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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_CONTAINERHELPER_HXX
24 #define CHART2_CONTAINERHELPER_HXX
25 
26 #include <vector>
27 #include <set>
28 #include <map>
29 
30 #include <algorithm>
31 #include <functional>
32 #include <iterator>
33 
34 namespace chart
35 {
36 namespace ContainerHelper
37 {
38 
39 /** converts a standard container into a sequence of the same type
40 
41     input:   standard container
42     output:  css::uno::Sequence< container::value_type >
43 
44     example:
45 
46     ::std::vector< sal_Int32 > aVector;
47     Sequence< sal_Int32 > aSequence( ContainerHelper::ContainerToSequence( aVector ));
48  */
49 template< class Container >
50     ::com::sun::star::uno::Sequence< typename Container::value_type >
ContainerToSequence(const Container & rCont)51     ContainerToSequence( const Container & rCont )
52 {
53     ::com::sun::star::uno::Sequence< typename Container::value_type > aResult( rCont.size());
54     ::std::copy( rCont.begin(), rCont.end(), aResult.getArray());
55     return aResult;
56 }
57 
58 /** converts a UNO sequence into a standard "Sequence" container. For
59     convenience see the methods SequenceToVector, etc. below.
60 
61     input:  uno::Sequence
62     output: a standard container of the same value type implementing the Concept
63             of a Sequence (vector, deque, list, slist)
64 
65     example:
66 
67     Sequence< sal_Int32 > aSequence;
68     ::std::vector< sal_Int32 > aVector(
69         ContainerToSequence::SequenceToSTLSequenceContainer< ::std::vector< sal_Int32 > >( aSequence );
70 */
71 template< class Container >
72     Container
SequenceToSTLSequenceContainer(const::com::sun::star::uno::Sequence<typename Container::value_type> & rSeq)73     SequenceToSTLSequenceContainer( const ::com::sun::star::uno::Sequence< typename Container::value_type > & rSeq )
74 {
75     Container aResult( rSeq.getLength());
76     ::std::copy( rSeq.getConstArray(), rSeq.getConstArray() + rSeq.getLength(),
77                  aResult.begin() );
78     return aResult;
79 }
80 
81 /** converts a UNO sequence into a standard container. For convenience see the
82     methods SequenceToVector, etc. below. (In contrast to
83     SequenceToSTLSequenceContainer this works for all standard containers)
84 
85     input:  uno::Sequence
86     output: a standard container that has an insert( iterator, key ) method (all
87             standard containers)
88     note:   for containers implementing the Concept of a Sequence (vector, deque,
89             list, slist) use SequenceToSTLSequenceContainer for better speed
90 
91     example:
92 
93     Sequence< sal_Int32 > aSequence;
94     ::std::set< sal_Int32 > aVector(
95         ContainerToSequence::SequenceToSTLContainer< ::std::set< sal_Int32 > >( aSequence );
96 */
97 template< class Container >
98     Container
SequenceToSTLContainer(const::com::sun::star::uno::Sequence<typename Container::value_type> & rSeq)99     SequenceToSTLContainer( const ::com::sun::star::uno::Sequence< typename Container::value_type > & rSeq )
100 {
101     Container aResult;
102     ::std::copy( rSeq.getConstArray(), rSeq.getConstArray() + rSeq.getLength(),
103                  ::std::inserter< Container >( aResult, aResult.begin()));
104     return aResult;
105 }
106 
107 // concrete container methods for convenience
108 
109 /** converts a UNO sequence into a standard vector of same value type
110 
111     example:
112 
113     Sequence< sal_Int32 > aSequence;
114     ::std::vector< sal_Int32 > aVector( ContainerHelper::SequenceToVector( aSequence ));
115  */
116 template< typename T >
117     ::std::vector< T >
SequenceToVector(const::com::sun::star::uno::Sequence<T> & rSeq)118     SequenceToVector( const ::com::sun::star::uno::Sequence< T > & rSeq )
119 {
120     return SequenceToSTLSequenceContainer< ::std::vector< T > >( rSeq );
121 }
122 
123 /** converts a UNO sequence into a standard set of same value type
124 
125     example:
126 
127     Sequence< sal_Int32 > aSequence;
128     ::std::set< sal_Int32 > aVector( ContainerHelper::SequenceToSet( aSequence ));
129  */
130 template< typename T >
131     ::std::set< T >
SequenceToSet(const::com::sun::star::uno::Sequence<T> & rSeq)132     SequenceToSet( const ::com::sun::star::uno::Sequence< T > & rSeq )
133 {
134     return SequenceToSTLContainer< ::std::set< T > >( rSeq );
135 }
136 
137 // ----------------------------------------
138 
139 /** converts the keys of a Pair Associative Container into a UNO sequence
140 
141     example:
142 
143     ::std::multimap< sal_Int32, ::rtl::OUString > aMyMultiMap;
144     uno::Sequence< sal_Int32 > aMyKeys( ContainerHelper::MapKeysToSequence( aMyMultiMap ));
145     // note: aMyKeys may contain duplicate keys here
146  */
147 template< class Map >
MapKeysToSequence(const Map & rCont)148 ::com::sun::star::uno::Sequence< typename Map::key_type > MapKeysToSequence(
149     const Map & rCont )
150 {
151     ::com::sun::star::uno::Sequence< typename Map::key_type > aResult( rCont.size());
152     ::std::transform( rCont.begin(), rCont.end(), aResult.getArray(),
153                       ::std::select1st< typename Map::value_type >());
154     return aResult;
155 }
156 
157 /** converts the values of a Pair Associative Container into a UNO sequence
158 
159     example:
160 
161     ::std::map< sal_Int32, ::rtl::OUString > aMyMultiMap;
162     uno::Sequence< ::rtl::OUString > aMyValues( ContainerHelper::MapValuesToSequence( aMyMultiMap ));
163  */
164 template< class Map >
MapValuesToSequence(const Map & rCont)165 ::com::sun::star::uno::Sequence< typename Map::mapped_type > MapValuesToSequence(
166     const Map & rCont )
167 {
168     ::com::sun::star::uno::Sequence< typename Map::mapped_type > aResult( rCont.size());
169     ::std::transform( rCont.begin(), rCont.end(), aResult.getArray(),
170                       ::std::select2nd< typename Map::value_type >());
171     return aResult;
172 }
173 
174 } // namespace ContainerHelper
175 } //  namespace chart
176 
177 // CHART2_CONTAINERHELPER_HXX
178 #endif
179