1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 #ifndef CHART2_CONTAINERHELPER_HXX
28 #define CHART2_CONTAINERHELPER_HXX
29 
30 #include <vector>
31 #include <set>
32 #include <map>
33 
34 #include <algorithm>
35 #include <functional>
36 
37 namespace chart
38 {
39 namespace ContainerHelper
40 {
41 
42 /** converts a standard container into a sequence of the same type
43 
44     input:   standard container
45     output:  css::uno::Sequence< container::value_type >
46 
47     example:
48 
49     ::std::vector< sal_Int32 > aVector;
50     Sequence< sal_Int32 > aSequence( ContainerHelper::ContainerToSequence( aVector ));
51  */
52 template< class Container >
53     ::com::sun::star::uno::Sequence< typename Container::value_type >
54     ContainerToSequence( const Container & rCont )
55 {
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;
59 }
60 
61 /** converts a UNO sequence into a standard "Sequence" container. For
62     convenience see the methods SequenceToVector, etc. below.
63 
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)
67 
68     example:
69 
70     Sequence< sal_Int32 > aSequence;
71     ::std::vector< sal_Int32 > aVector(
72         ContainerToSequence::SequenceToSTLSequenceContainer< ::std::vector< sal_Int32 > >( aSequence );
73 */
74 template< class Container >
75     Container
76     SequenceToSTLSequenceContainer( const ::com::sun::star::uno::Sequence< typename Container::value_type > & rSeq )
77 {
78     Container aResult( rSeq.getLength());
79     ::std::copy( rSeq.getConstArray(), rSeq.getConstArray() + rSeq.getLength(),
80                  aResult.begin() );
81     return aResult;
82 }
83 
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)
87 
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
93 
94     example:
95 
96     Sequence< sal_Int32 > aSequence;
97     ::std::set< sal_Int32 > aVector(
98         ContainerToSequence::SequenceToSTLContainer< ::std::set< sal_Int32 > >( aSequence );
99 */
100 template< class Container >
101     Container
102     SequenceToSTLContainer( const ::com::sun::star::uno::Sequence< typename Container::value_type > & rSeq )
103 {
104     Container aResult;
105     ::std::copy( rSeq.getConstArray(), rSeq.getConstArray() + rSeq.getLength(),
106                  ::std::inserter< Container >( aResult, aResult.begin()));
107     return aResult;
108 }
109 
110 // concrete container methods for convenience
111 
112 /** converts a UNO sequence into a standard vector of same value type
113 
114     example:
115 
116     Sequence< sal_Int32 > aSequence;
117     ::std::vector< sal_Int32 > aVector( ContainerHelper::SequenceToVector( aSequence ));
118  */
119 template< typename T >
120     ::std::vector< T >
121     SequenceToVector( const ::com::sun::star::uno::Sequence< T > & rSeq )
122 {
123     return SequenceToSTLSequenceContainer< ::std::vector< T > >( rSeq );
124 }
125 
126 /** converts a UNO sequence into a standard set of same value type
127 
128     example:
129 
130     Sequence< sal_Int32 > aSequence;
131     ::std::set< sal_Int32 > aVector( ContainerHelper::SequenceToSet( aSequence ));
132  */
133 template< typename T >
134     ::std::set< T >
135     SequenceToSet( const ::com::sun::star::uno::Sequence< T > & rSeq )
136 {
137     return SequenceToSTLContainer< ::std::set< T > >( rSeq );
138 }
139 
140 // ----------------------------------------
141 
142 /** converts the keys of a Pair Associative Container into a UNO sequence
143 
144     example:
145 
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
149  */
150 template< class Map >
151 ::com::sun::star::uno::Sequence< typename Map::key_type > MapKeysToSequence(
152     const Map & rCont )
153 {
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;
158 }
159 
160 /** converts the values of a Pair Associative Container into a UNO sequence
161 
162     example:
163 
164     ::std::map< sal_Int32, ::rtl::OUString > aMyMultiMap;
165     uno::Sequence< ::rtl::OUString > aMyValues( ContainerHelper::MapValuesToSequence( aMyMultiMap ));
166  */
167 template< class Map >
168 ::com::sun::star::uno::Sequence< typename Map::mapped_type > MapValuesToSequence(
169     const Map & rCont )
170 {
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;
175 }
176 
177 } // namespace ContainerHelper
178 } //  namespace chart
179 
180 // CHART2_CONTAINERHELPER_HXX
181 #endif
182