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