Update ooo320-m1
[ooovba.git] / chart2 / source / inc / ContainerHelper.hxx
blob94036e02dece88c24780ab6eb53cfebe3e3546c4
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ContainerHelper.hxx,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #ifndef CHART2_CONTAINERHELPER_HXX
31 #define CHART2_CONTAINERHELPER_HXX
33 #include <vector>
34 #include <set>
35 #include <map>
37 #include <algorithm>
38 #include <functional>
40 namespace chart
42 namespace ContainerHelper
45 /** converts a standard container into a sequence of the same type
47 input: standard container
48 output: css::uno::Sequence< container::value_type >
50 example:
52 ::std::vector< sal_Int32 > aVector;
53 Sequence< sal_Int32 > aSequence( ContainerHelper::ContainerToSequence( aVector ));
55 template< class Container >
56 ::com::sun::star::uno::Sequence< typename Container::value_type >
57 ContainerToSequence( const Container & rCont )
59 ::com::sun::star::uno::Sequence< typename Container::value_type > aResult( rCont.size());
60 ::std::copy( rCont.begin(), rCont.end(), aResult.getArray());
61 return aResult;
64 /** converts a UNO sequence into a standard "Sequence" container. For
65 convenience see the methods SequenceToVector, etc. below.
67 input: uno::Sequence
68 output: a standard container of the same value type implementing the Concept
69 of a Sequence (vector, deque, list, slist)
71 example:
73 Sequence< sal_Int32 > aSequence;
74 ::std::vector< sal_Int32 > aVector(
75 ContainerToSequence::SequenceToSTLSequenceContainer< ::std::vector< sal_Int32 > >( aSequence );
77 template< class Container >
78 Container
79 SequenceToSTLSequenceContainer( const ::com::sun::star::uno::Sequence< typename Container::value_type > & rSeq )
81 Container aResult( rSeq.getLength());
82 ::std::copy( rSeq.getConstArray(), rSeq.getConstArray() + rSeq.getLength(),
83 aResult.begin() );
84 return aResult;
87 /** converts a UNO sequence into a standard container. For convenience see the
88 methods SequenceToVector, etc. below. (In contrast to
89 SequenceToSTLSequenceContainer this works for all standard containers)
91 input: uno::Sequence
92 output: a standard container that has an insert( iterator, key ) method (all
93 standard containers)
94 note: for containers implementing the Concept of a Sequence (vector, deque,
95 list, slist) use SequenceToSTLSequenceContainer for better speed
97 example:
99 Sequence< sal_Int32 > aSequence;
100 ::std::set< sal_Int32 > aVector(
101 ContainerToSequence::SequenceToSTLContainer< ::std::set< sal_Int32 > >( aSequence );
103 template< class Container >
104 Container
105 SequenceToSTLContainer( const ::com::sun::star::uno::Sequence< typename Container::value_type > & rSeq )
107 Container aResult;
108 ::std::copy( rSeq.getConstArray(), rSeq.getConstArray() + rSeq.getLength(),
109 ::std::inserter< Container >( aResult, aResult.begin()));
110 return aResult;
113 // concrete container methods for convenience
115 /** converts a UNO sequence into a standard vector of same value type
117 example:
119 Sequence< sal_Int32 > aSequence;
120 ::std::vector< sal_Int32 > aVector( ContainerHelper::SequenceToVector( aSequence ));
122 template< typename T >
123 ::std::vector< T >
124 SequenceToVector( const ::com::sun::star::uno::Sequence< T > & rSeq )
126 return SequenceToSTLSequenceContainer< ::std::vector< T > >( rSeq );
129 /** converts a UNO sequence into a standard set of same value type
131 example:
133 Sequence< sal_Int32 > aSequence;
134 ::std::set< sal_Int32 > aVector( ContainerHelper::SequenceToSet( aSequence ));
136 template< typename T >
137 ::std::set< T >
138 SequenceToSet( const ::com::sun::star::uno::Sequence< T > & rSeq )
140 return SequenceToSTLContainer< ::std::set< T > >( rSeq );
143 // ----------------------------------------
145 /** converts the keys of a Pair Associative Container into a UNO sequence
147 example:
149 ::std::multimap< sal_Int32, ::rtl::OUString > aMyMultiMap;
150 uno::Sequence< sal_Int32 > aMyKeys( ContainerHelper::MapKeysToSequence( aMyMultiMap ));
151 // note: aMyKeys may contain duplicate keys here
153 template< class Map >
154 ::com::sun::star::uno::Sequence< typename Map::key_type > MapKeysToSequence(
155 const Map & rCont )
157 ::com::sun::star::uno::Sequence< typename Map::key_type > aResult( rCont.size());
158 ::std::transform( rCont.begin(), rCont.end(), aResult.getArray(),
159 ::std::select1st< typename Map::value_type >());
160 return aResult;
163 /** converts the values of a Pair Associative Container into a UNO sequence
165 example:
167 ::std::map< sal_Int32, ::rtl::OUString > aMyMultiMap;
168 uno::Sequence< ::rtl::OUString > aMyValues( ContainerHelper::MapValuesToSequence( aMyMultiMap ));
170 template< class Map >
171 ::com::sun::star::uno::Sequence< typename Map::mapped_type > MapValuesToSequence(
172 const Map & rCont )
174 ::com::sun::star::uno::Sequence< typename Map::mapped_type > aResult( rCont.size());
175 ::std::transform( rCont.begin(), rCont.end(), aResult.getArray(),
176 ::std::select2nd< typename Map::value_type >());
177 return aResult;
180 } // namespace ContainerHelper
181 } // namespace chart
183 // CHART2_CONTAINERHELPER_HXX
184 #endif