Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 : #ifndef INCLUDED_CHART2_SOURCE_INC_CONTAINERHELPER_HXX
20 : #define INCLUDED_CHART2_SOURCE_INC_CONTAINERHELPER_HXX
21 :
22 : #include <vector>
23 : #include <set>
24 : #include <map>
25 :
26 : #include <algorithm>
27 : #include <functional>
28 : #include <o3tl/compat_functional.hxx>
29 :
30 : namespace chart
31 : {
32 : namespace ContainerHelper
33 : {
34 :
35 : /** converts a standard container into a sequence of the same type
36 :
37 : input: standard container
38 : output: css::uno::Sequence< container::value_type >
39 :
40 : example:
41 :
42 : ::std::vector< sal_Int32 > aVector;
43 : Sequence< sal_Int32 > aSequence( ContainerHelper::ContainerToSequence( aVector ));
44 : */
45 : template< class Container >
46 : ::com::sun::star::uno::Sequence< typename Container::value_type >
47 0 : ContainerToSequence( const Container & rCont )
48 : {
49 0 : ::com::sun::star::uno::Sequence< typename Container::value_type > aResult( rCont.size());
50 0 : ::std::copy( rCont.begin(), rCont.end(), aResult.getArray());
51 0 : return aResult;
52 : }
53 :
54 : /** converts a UNO sequence into a standard "Sequence" container. For
55 : convenience see the methods SequenceToVector, etc. below.
56 :
57 : input: uno::Sequence
58 : output: a standard container of the same value type implementing the Concept
59 : of a Sequence (vector, deque, list, slist)
60 :
61 : example:
62 :
63 : Sequence< sal_Int32 > aSequence;
64 : ::std::vector< sal_Int32 > aVector(
65 : ContainerToSequence::SequenceToSTLSequenceContainer< ::std::vector< sal_Int32 > >( aSequence );
66 : */
67 : template< class Container >
68 : Container
69 0 : SequenceToSTLSequenceContainer( const ::com::sun::star::uno::Sequence< typename Container::value_type > & rSeq )
70 : {
71 0 : Container aResult( rSeq.getLength());
72 0 : ::std::copy( rSeq.getConstArray(), rSeq.getConstArray() + rSeq.getLength(),
73 0 : aResult.begin() );
74 0 : return aResult;
75 : }
76 :
77 : /** converts a UNO sequence into a standard container. For convenience see the
78 : methods SequenceToVector, etc. below. (In contrast to
79 : SequenceToSTLSequenceContainer this works for all standard containers)
80 :
81 : input: uno::Sequence
82 : output: a standard container that has an insert( iterator, key ) method (all
83 : standard containers)
84 : note: for containers implementing the Concept of a Sequence (vector, deque,
85 : list, slist) use SequenceToSTLSequenceContainer for better speed
86 :
87 : example:
88 :
89 : Sequence< sal_Int32 > aSequence;
90 : ::std::set< sal_Int32 > aVector(
91 : ContainerToSequence::SequenceToSTLContainer< ::std::set< sal_Int32 > >( aSequence );
92 : */
93 : template< class Container >
94 : Container
95 : SequenceToSTLContainer( const ::com::sun::star::uno::Sequence< typename Container::value_type > & rSeq )
96 : {
97 : Container aResult;
98 : ::std::copy( rSeq.getConstArray(), rSeq.getConstArray() + rSeq.getLength(),
99 : ::std::inserter< Container >( aResult, aResult.begin()));
100 : return aResult;
101 : }
102 :
103 : // concrete container methods for convenience
104 :
105 : /** converts a UNO sequence into a standard vector of same value type
106 :
107 : example:
108 :
109 : Sequence< sal_Int32 > aSequence;
110 : ::std::vector< sal_Int32 > aVector( ContainerHelper::SequenceToVector( aSequence ));
111 : */
112 : template< typename T >
113 : ::std::vector< T >
114 0 : SequenceToVector( const ::com::sun::star::uno::Sequence< T > & rSeq )
115 : {
116 0 : return SequenceToSTLSequenceContainer< ::std::vector< T > >( rSeq );
117 : }
118 :
119 : /** converts a UNO sequence into a standard set of same value type
120 :
121 : example:
122 :
123 : Sequence< sal_Int32 > aSequence;
124 : ::std::set< sal_Int32 > aVector( ContainerHelper::SequenceToSet( aSequence ));
125 : */
126 : template< typename T >
127 : ::std::set< T >
128 : SequenceToSet( const ::com::sun::star::uno::Sequence< T > & rSeq )
129 : {
130 : return SequenceToSTLContainer< ::std::set< T > >( rSeq );
131 : }
132 :
133 : /** converts the keys of a Pair Associative Container into a UNO sequence
134 :
135 : example:
136 :
137 : ::std::multimap< sal_Int32, OUString > aMyMultiMap;
138 : uno::Sequence< sal_Int32 > aMyKeys( ContainerHelper::MapKeysToSequence( aMyMultiMap ));
139 : // note: aMyKeys may contain duplicate keys here
140 : */
141 : template< class Map >
142 0 : ::com::sun::star::uno::Sequence< typename Map::key_type > MapKeysToSequence(
143 : const Map & rCont )
144 : {
145 0 : ::com::sun::star::uno::Sequence< typename Map::key_type > aResult( rCont.size());
146 0 : ::std::transform( rCont.begin(), rCont.end(), aResult.getArray(),
147 0 : ::o3tl::select1st< typename Map::value_type >());
148 0 : return aResult;
149 : }
150 :
151 : /** converts the values of a Pair Associative Container into a UNO sequence
152 :
153 : example:
154 :
155 : ::std::map< sal_Int32, OUString > aMyMultiMap;
156 : uno::Sequence< OUString > aMyValues( ContainerHelper::MapValuesToSequence( aMyMultiMap ));
157 : */
158 : template< class Map >
159 0 : ::com::sun::star::uno::Sequence< typename Map::mapped_type > MapValuesToSequence(
160 : const Map & rCont )
161 : {
162 0 : ::com::sun::star::uno::Sequence< typename Map::mapped_type > aResult( rCont.size());
163 0 : ::std::transform( rCont.begin(), rCont.end(), aResult.getArray(),
164 0 : ::o3tl::select2nd< typename Map::value_type >());
165 0 : return aResult;
166 : }
167 :
168 : } // namespace ContainerHelper
169 : } // namespace chart
170 :
171 : // INCLUDED_CHART2_SOURCE_INC_CONTAINERHELPER_HXX
172 : #endif
173 :
174 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|