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