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