Branch data 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 : :
20 : : #ifndef _COMPHELPER_SEQUENCEASVECTOR_HXX_
21 : : #define _COMPHELPER_SEQUENCEASVECTOR_HXX_
22 : :
23 : : #include <vector>
24 : : #include <algorithm>
25 : : #include <com/sun/star/uno/Sequence.hxx>
26 : :
27 : : #include <com/sun/star/beans/IllegalTypeException.hpp>
28 : :
29 : :
30 : : namespace comphelper{
31 : :
32 : :
33 : : /** @short Implements a stl vector on top of any
34 : : uno sequence.
35 : :
36 : : @descr That provides the possibility to modify
37 : : sequences very easy ...
38 : : Of course this can be usefull only, if
39 : : count of modifications is high, so copying
40 : : of the sequence make sense!
41 : : */
42 : : template< class TElementType >
43 : 162560 : class SequenceAsVector : public ::std::vector< TElementType >
44 : : {
45 : : //-------------------------------------------
46 : : // types
47 : :
48 : : public:
49 : :
50 : : //---------------------------------------
51 : : /** @short When inheriting from a template using typename is generally required when using
52 : : types from the base! */
53 : : typedef typename ::std::vector< TElementType >::const_iterator const_iterator;
54 : :
55 : : //---------------------------------------
56 : : /** @short When inheriting from a template using typename is generally required when using
57 : : types from the base! */
58 : : typedef typename ::std::vector< TElementType >::iterator iterator;
59 : :
60 : : //-------------------------------------------
61 : : // interface
62 : : public:
63 : :
64 : : //---------------------------------------
65 : : /** @short default ctor, to create an empty list.
66 : : */
67 : 1315639 : SequenceAsVector()
68 : 1315639 : {}
69 : :
70 : : //---------------------------------------
71 : : /** @short default dtor
72 : : */
73 : 1528967 : ~SequenceAsVector()
74 : 1528967 : {}
75 : :
76 : : //---------------------------------------
77 : : /** @short creates a new vector with the given length.
78 : :
79 : : @param nLength
80 : : the number of elements for the new vector.
81 : : */
82 : 5 : explicit SequenceAsVector(sal_Int32 nLength) :
83 : 5 : ::std::vector< TElementType >( static_cast< size_t >( nLength ) )
84 : : {
85 : 5 : }
86 : :
87 : : //---------------------------------------
88 : : /** @short creates a new deque from the given uno sequence.
89 : :
90 : : @param lSource
91 : : contains the new items for this deque.
92 : : */
93 : 81693 : SequenceAsVector(const ::com::sun::star::uno::Sequence< TElementType >& lSource)
94 : 81693 : {
95 [ + - # # ]: 81693 : (*this) << lSource;
96 : 81693 : }
97 : :
98 : : //---------------------------------------
99 : : /** @short creates a new instance from the given Any, which
100 : : of course must contain a valid sequence using the
101 : : right element type for every item.
102 : :
103 : : @attention If the given Any is an empty one
104 : : (if its set to VOID), no exception
105 : : is thrown. In such case this instance will
106 : : be created as an empty one too!
107 : :
108 : : @param aSource
109 : : this any must contain a suitable sequence. :-)
110 : :
111 : : @throw A <type scope="com::sun::star::beans">IllegalTypeException</type>
112 : : if an unsupported element inside this Any
113 : : is given. An empty Any reset this instance!
114 : : */
115 : 71196 : SequenceAsVector(const ::com::sun::star::uno::Any& aSource)
116 : 71196 : {
117 [ + - ]: 71196 : (*this) << aSource;
118 : 71196 : }
119 : :
120 : : //---------------------------------------
121 : : /** @short fill this instance from the given uno sequence.
122 : :
123 : : @param lSource
124 : : contains the new items for this deque.
125 : : */
126 : 170120 : void operator<<(const ::com::sun::star::uno::Sequence< TElementType >& lSource)
127 : : {
128 : 170120 : this->clear();
129 : :
130 : 170120 : sal_Int32 c = lSource.getLength();
131 : 170120 : const TElementType* pSource = lSource.getConstArray();
132 : :
133 [ + + ][ # # ]: 2195225 : for (sal_Int32 i=0; i<c; ++i)
134 : 2025105 : this->push_back(pSource[i]);
135 : 170120 : }
136 : :
137 : : //---------------------------------------
138 : : /** @short fill this instance from the given Any, which
139 : : of course must contain a valid sequence using the
140 : : right element type for every item.
141 : :
142 : : @attention If the given Any is an empty one
143 : : (if its set to VOID), no exception
144 : : is thrown. In such case this instance will
145 : : be created as an empty one too!
146 : :
147 : : @param aSource
148 : : this any must contain a suitable sequence. :-)
149 : :
150 : : @throw A <type scope="com::sun::star::beans">IllegalTypeException</type>
151 : : if an unsupported element inside this Any
152 : : is given. An empty Any reset this instance!
153 : : */
154 : 76788 : void operator<<(const ::com::sun::star::uno::Any& aSource)
155 : : {
156 : : // An empty Any reset this instance!
157 [ + + ]: 76788 : if (!aSource.hasValue())
158 : : {
159 : 8154 : this->clear();
160 : 76788 : return;
161 : : }
162 : :
163 [ + - ]: 68634 : ::com::sun::star::uno::Sequence< TElementType > lSource;
164 [ + - ][ - + ]: 68634 : if (!(aSource >>= lSource))
165 : : throw ::com::sun::star::beans::IllegalTypeException(
166 : : ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("SequenceAsVector operator<<(Any) was called with an unsupported Any type.")),
167 [ # # ][ # # ]: 0 : ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >());
168 : :
169 [ + - ][ + - ]: 68634 : (*this) << lSource;
170 : : }
171 : :
172 : : //---------------------------------------
173 : : /** @short converts this instance to an uno sequence.
174 : :
175 : : @param lDestination
176 : : target sequence for converting.
177 : : */
178 : 498483 : void operator>>(::com::sun::star::uno::Sequence< TElementType >& lDestination) const
179 : : {
180 : 498483 : sal_Int32 c = (sal_Int32)this->size();
181 : 498483 : lDestination.realloc(c);
182 : 498483 : TElementType* pDestination = lDestination.getArray();
183 : :
184 : 498483 : sal_Int32 i = 0;
185 [ + - + + : 2483234 : for (typename std::vector<TElementType>::const_iterator pThis = this->begin();
+ ][ + - +
+ + # # ]
[ + + + +
- # # ]
[ + + ][ # # ]
[ # # # #
# ][ + - #
# # + - ]
[ + + + + ]
[ # # ]
[ # # + - ]
[ # # + + ]
[ + - ][ + - ]
[ + + ][ + - ]
[ + - ][ + + ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ + - ][ + - ]
[ + + ]
186 : : pThis != this->end() ;
187 : : ++pThis )
188 : : {
189 [ + + - ]: 1984751 : pDestination[i] = *pThis;
[ + - ][ # # ]
[ + - ][ + - ]
[ # # ][ # # ]
[ + - ]
190 : 1984751 : ++i;
191 : : }
192 : 498483 : }
193 : :
194 : : //---------------------------------------
195 : : /** @short converts this instance to an uno any
196 : : which contains a suitable sequence
197 : : of items of this stl struct.
198 : :
199 : : @param aDestination
200 : : target any for converting.
201 : : */
202 : : void operator>>(::com::sun::star::uno::Any& aDestination) const
203 : : {
204 : : sal_Int32 c = (sal_Int32)this->size();
205 : : ::com::sun::star::uno::Sequence< TElementType > lDestination(c);
206 : : TElementType* pDestination = lDestination.getArray();
207 : :
208 : : sal_Int32 i = 0;
209 : : for (typename std::vector<TElementType>::const_iterator pThis = this->begin();
210 : : pThis != this->end() ;
211 : : ++pThis )
212 : : {
213 : : pDestination[i] = *pThis;
214 : : ++i;
215 : : }
216 : :
217 : : aDestination <<= lDestination;
218 : : }
219 : :
220 : : //---------------------------------------
221 : : /** @short converts this deque to a suitable uno
222 : : sequence which contains all items.
223 : :
224 : : @attention It return a const sequence to prevent
225 : : the outside code against using of this
226 : : return value as [in/]out parameter for
227 : : direct function calls!
228 : : Of course it can be casted to non const
229 : : ... but then its a problem of the outside
230 : : code :-)
231 : :
232 : : @return A (const!) sequence, which contains all items of
233 : : this deque.
234 : : */
235 : 490409 : const ::com::sun::star::uno::Sequence< TElementType > getAsConstList() const
236 : : {
237 : 490409 : ::com::sun::star::uno::Sequence< TElementType > lDestination;
238 [ + - + - : 490409 : (*this) >> lDestination;
+ - + - +
- # # # #
+ - ]
239 : 490409 : return lDestination;
240 : : }
241 : : };
242 : :
243 : : } // namespace comphelper
244 : :
245 : : #endif // _COMPHELPER_SEQUENCEASVECTOR_HXX_
246 : :
247 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|