1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir #ifndef CHART2_CONTAINERHELPER_HXX
28*cdf0e10cSrcweir #define CHART2_CONTAINERHELPER_HXX
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include <vector>
31*cdf0e10cSrcweir #include <set>
32*cdf0e10cSrcweir #include <map>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include <algorithm>
35*cdf0e10cSrcweir #include <functional>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir namespace chart
38*cdf0e10cSrcweir {
39*cdf0e10cSrcweir namespace ContainerHelper
40*cdf0e10cSrcweir {
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir /** converts a standard container into a sequence of the same type
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir     input:   standard container
45*cdf0e10cSrcweir     output:  css::uno::Sequence< container::value_type >
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir     example:
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir     ::std::vector< sal_Int32 > aVector;
50*cdf0e10cSrcweir     Sequence< sal_Int32 > aSequence( ContainerHelper::ContainerToSequence( aVector ));
51*cdf0e10cSrcweir  */
52*cdf0e10cSrcweir template< class Container >
53*cdf0e10cSrcweir     ::com::sun::star::uno::Sequence< typename Container::value_type >
54*cdf0e10cSrcweir     ContainerToSequence( const Container & rCont )
55*cdf0e10cSrcweir {
56*cdf0e10cSrcweir     ::com::sun::star::uno::Sequence< typename Container::value_type > aResult( rCont.size());
57*cdf0e10cSrcweir     ::std::copy( rCont.begin(), rCont.end(), aResult.getArray());
58*cdf0e10cSrcweir     return aResult;
59*cdf0e10cSrcweir }
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir /** converts a UNO sequence into a standard "Sequence" container. For
62*cdf0e10cSrcweir     convenience see the methods SequenceToVector, etc. below.
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir     input:  uno::Sequence
65*cdf0e10cSrcweir     output: a standard container of the same value type implementing the Concept
66*cdf0e10cSrcweir             of a Sequence (vector, deque, list, slist)
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir     example:
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir     Sequence< sal_Int32 > aSequence;
71*cdf0e10cSrcweir     ::std::vector< sal_Int32 > aVector(
72*cdf0e10cSrcweir         ContainerToSequence::SequenceToSTLSequenceContainer< ::std::vector< sal_Int32 > >( aSequence );
73*cdf0e10cSrcweir */
74*cdf0e10cSrcweir template< class Container >
75*cdf0e10cSrcweir     Container
76*cdf0e10cSrcweir     SequenceToSTLSequenceContainer( const ::com::sun::star::uno::Sequence< typename Container::value_type > & rSeq )
77*cdf0e10cSrcweir {
78*cdf0e10cSrcweir     Container aResult( rSeq.getLength());
79*cdf0e10cSrcweir     ::std::copy( rSeq.getConstArray(), rSeq.getConstArray() + rSeq.getLength(),
80*cdf0e10cSrcweir                  aResult.begin() );
81*cdf0e10cSrcweir     return aResult;
82*cdf0e10cSrcweir }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir /** converts a UNO sequence into a standard container. For convenience see the
85*cdf0e10cSrcweir     methods SequenceToVector, etc. below. (In contrast to
86*cdf0e10cSrcweir     SequenceToSTLSequenceContainer this works for all standard containers)
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir     input:  uno::Sequence
89*cdf0e10cSrcweir     output: a standard container that has an insert( iterator, key ) method (all
90*cdf0e10cSrcweir             standard containers)
91*cdf0e10cSrcweir     note:   for containers implementing the Concept of a Sequence (vector, deque,
92*cdf0e10cSrcweir             list, slist) use SequenceToSTLSequenceContainer for better speed
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir     example:
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir     Sequence< sal_Int32 > aSequence;
97*cdf0e10cSrcweir     ::std::set< sal_Int32 > aVector(
98*cdf0e10cSrcweir         ContainerToSequence::SequenceToSTLContainer< ::std::set< sal_Int32 > >( aSequence );
99*cdf0e10cSrcweir */
100*cdf0e10cSrcweir template< class Container >
101*cdf0e10cSrcweir     Container
102*cdf0e10cSrcweir     SequenceToSTLContainer( const ::com::sun::star::uno::Sequence< typename Container::value_type > & rSeq )
103*cdf0e10cSrcweir {
104*cdf0e10cSrcweir     Container aResult;
105*cdf0e10cSrcweir     ::std::copy( rSeq.getConstArray(), rSeq.getConstArray() + rSeq.getLength(),
106*cdf0e10cSrcweir                  ::std::inserter< Container >( aResult, aResult.begin()));
107*cdf0e10cSrcweir     return aResult;
108*cdf0e10cSrcweir }
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir // concrete container methods for convenience
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir /** converts a UNO sequence into a standard vector of same value type
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir     example:
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir     Sequence< sal_Int32 > aSequence;
117*cdf0e10cSrcweir     ::std::vector< sal_Int32 > aVector( ContainerHelper::SequenceToVector( aSequence ));
118*cdf0e10cSrcweir  */
119*cdf0e10cSrcweir template< typename T >
120*cdf0e10cSrcweir     ::std::vector< T >
121*cdf0e10cSrcweir     SequenceToVector( const ::com::sun::star::uno::Sequence< T > & rSeq )
122*cdf0e10cSrcweir {
123*cdf0e10cSrcweir     return SequenceToSTLSequenceContainer< ::std::vector< T > >( rSeq );
124*cdf0e10cSrcweir }
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir /** converts a UNO sequence into a standard set of same value type
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir     example:
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir     Sequence< sal_Int32 > aSequence;
131*cdf0e10cSrcweir     ::std::set< sal_Int32 > aVector( ContainerHelper::SequenceToSet( aSequence ));
132*cdf0e10cSrcweir  */
133*cdf0e10cSrcweir template< typename T >
134*cdf0e10cSrcweir     ::std::set< T >
135*cdf0e10cSrcweir     SequenceToSet( const ::com::sun::star::uno::Sequence< T > & rSeq )
136*cdf0e10cSrcweir {
137*cdf0e10cSrcweir     return SequenceToSTLContainer< ::std::set< T > >( rSeq );
138*cdf0e10cSrcweir }
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir // ----------------------------------------
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir /** converts the keys of a Pair Associative Container into a UNO sequence
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir     example:
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir     ::std::multimap< sal_Int32, ::rtl::OUString > aMyMultiMap;
147*cdf0e10cSrcweir     uno::Sequence< sal_Int32 > aMyKeys( ContainerHelper::MapKeysToSequence( aMyMultiMap ));
148*cdf0e10cSrcweir     // note: aMyKeys may contain duplicate keys here
149*cdf0e10cSrcweir  */
150*cdf0e10cSrcweir template< class Map >
151*cdf0e10cSrcweir ::com::sun::star::uno::Sequence< typename Map::key_type > MapKeysToSequence(
152*cdf0e10cSrcweir     const Map & rCont )
153*cdf0e10cSrcweir {
154*cdf0e10cSrcweir     ::com::sun::star::uno::Sequence< typename Map::key_type > aResult( rCont.size());
155*cdf0e10cSrcweir     ::std::transform( rCont.begin(), rCont.end(), aResult.getArray(),
156*cdf0e10cSrcweir                       ::std::select1st< typename Map::value_type >());
157*cdf0e10cSrcweir     return aResult;
158*cdf0e10cSrcweir }
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir /** converts the values of a Pair Associative Container into a UNO sequence
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir     example:
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir     ::std::map< sal_Int32, ::rtl::OUString > aMyMultiMap;
165*cdf0e10cSrcweir     uno::Sequence< ::rtl::OUString > aMyValues( ContainerHelper::MapValuesToSequence( aMyMultiMap ));
166*cdf0e10cSrcweir  */
167*cdf0e10cSrcweir template< class Map >
168*cdf0e10cSrcweir ::com::sun::star::uno::Sequence< typename Map::mapped_type > MapValuesToSequence(
169*cdf0e10cSrcweir     const Map & rCont )
170*cdf0e10cSrcweir {
171*cdf0e10cSrcweir     ::com::sun::star::uno::Sequence< typename Map::mapped_type > aResult( rCont.size());
172*cdf0e10cSrcweir     ::std::transform( rCont.begin(), rCont.end(), aResult.getArray(),
173*cdf0e10cSrcweir                       ::std::select2nd< typename Map::value_type >());
174*cdf0e10cSrcweir     return aResult;
175*cdf0e10cSrcweir }
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir } // namespace ContainerHelper
178*cdf0e10cSrcweir } //  namespace chart
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir // CHART2_CONTAINERHELPER_HXX
181*cdf0e10cSrcweir #endif
182