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 INCLUDED_COMPHELPER_SEQUENCE_HXX
21 : #define INCLUDED_COMPHELPER_SEQUENCE_HXX
22 :
23 : #include <algorithm>
24 : #include <com/sun/star/uno/Sequence.hxx>
25 : #include <osl/diagnose.h>
26 : #include <comphelper/comphelperdllapi.h>
27 :
28 : #include <vector>
29 :
30 : namespace comphelper
31 : {
32 : /** search the given string within the given sequence, return the positions where it was found.
33 : if _bOnlyFirst is sal_True, only the first occurrence will be returned.
34 : */
35 : COMPHELPER_DLLPUBLIC css::uno::Sequence<sal_Int16> findValue(const css::uno::Sequence< OUString >& _rList, const OUString& _rValue, bool _bOnlyFirst = false);
36 :
37 : namespace internal
38 : {
39 : template <class T>
40 5797 : inline void implCopySequence(const T* _pSource, T*& _pDest, sal_Int32 _nSourceLen)
41 : {
42 73056 : for (sal_Int32 i=0; i<_nSourceLen; ++i, ++_pSource, ++_pDest)
43 67259 : *_pDest = *_pSource;
44 5797 : }
45 : }
46 :
47 : /// concat two sequences
48 : template <class T>
49 2189 : inline css::uno::Sequence<T> concatSequences(const css::uno::Sequence<T>& _rLeft, const css::uno::Sequence<T>& _rRight)
50 : {
51 2189 : sal_Int32 nLeft(_rLeft.getLength()), nRight(_rRight.getLength());
52 2189 : const T* pLeft = _rLeft.getConstArray();
53 2189 : const T* pRight = _rRight.getConstArray();
54 :
55 2189 : sal_Int32 nReturnLen(nLeft + nRight);
56 2189 : css::uno::Sequence<T> aReturn(nReturnLen);
57 2189 : T* pReturn = aReturn.getArray();
58 :
59 2189 : internal::implCopySequence(pLeft, pReturn, nLeft);
60 2189 : internal::implCopySequence(pRight, pReturn, nRight);
61 :
62 2189 : return aReturn;
63 : }
64 :
65 : /// concat additional elements from right sequence to left sequence
66 : ///
67 : /// be aware that this takes time O(|left| * |right|)
68 345 : template<typename T> inline css::uno::Sequence<T> combineSequences(
69 : css::uno::Sequence<T> const & left, css::uno::Sequence<T> const & right)
70 : {
71 345 : sal_Int32 n1 = left.getLength();
72 345 : css::uno::Sequence<T> ret(n1 + right.getLength());
73 : //TODO: check for overflow
74 345 : T * p = ret.getArray();
75 345 : internal::implCopySequence(left.getConstArray(), p, n1);
76 345 : sal_Int32 n2 = n1;
77 1771 : for (sal_Int32 i = 0; i != right.getLength(); ++i) {
78 1426 : bool found = false;
79 6355 : for (sal_Int32 j = 0; j != n1; ++j) {
80 5197 : if (right[i] == left[j]) {
81 268 : found = true;
82 268 : break;
83 : }
84 : }
85 1426 : if (!found) {
86 1158 : ret[n2++] = right[i];
87 : }
88 : }
89 345 : ret.realloc(n2);
90 345 : return ret;
91 : }
92 :
93 : /// concat three sequences
94 : template <class T>
95 358 : inline css::uno::Sequence<T> concatSequences(const css::uno::Sequence<T>& _rLeft, const css::uno::Sequence<T>& _rMiddle, const css::uno::Sequence<T>& _rRight)
96 : {
97 358 : sal_Int32 nLeft(_rLeft.getLength()), nMiddle(_rMiddle.getLength()), nRight(_rRight.getLength());
98 358 : const T* pLeft = _rLeft.getConstArray();
99 358 : const T* pMiddle = _rMiddle.getConstArray();
100 358 : const T* pRight = _rRight.getConstArray();
101 :
102 358 : sal_Int32 nReturnLen(nLeft + nMiddle + nRight);
103 358 : css::uno::Sequence<T> aReturn(nReturnLen);
104 358 : T* pReturn = aReturn.getArray();
105 :
106 358 : internal::implCopySequence(pLeft, pReturn, nLeft);
107 358 : internal::implCopySequence(pMiddle, pReturn, nMiddle);
108 358 : internal::implCopySequence(pRight, pReturn, nRight);
109 :
110 358 : return aReturn;
111 : }
112 :
113 :
114 : /// remove a specified element from a sequences
115 : template<class T>
116 3767 : inline void removeElementAt(css::uno::Sequence<T>& _rSeq, sal_Int32 _nPos)
117 : {
118 3767 : sal_Int32 nLength = _rSeq.getLength();
119 :
120 : OSL_ENSURE(0 <= _nPos && _nPos < nLength, "invalid index");
121 :
122 45756 : for (sal_Int32 i = _nPos + 1; i < nLength; ++i)
123 : {
124 41989 : _rSeq[i-1] = _rSeq[i];
125 : }
126 :
127 3767 : _rSeq.realloc(nLength-1);
128 3767 : }
129 :
130 :
131 : //= iterating through sequences
132 :
133 : /** a helper class for iterating through a sequence
134 : */
135 : template <class TYPE>
136 : class OSequenceIterator
137 : {
138 : const TYPE* m_pElements;
139 : sal_Int32 m_nLen;
140 : const TYPE* m_pCurrent;
141 :
142 : public:
143 : /** contrcuct a sequence iterator from a sequence
144 : */
145 : OSequenceIterator(const ::com::sun::star::uno::Sequence< TYPE >& _rSeq);
146 : /** contrcuct a sequence iterator from a Any containing a sequence
147 : */
148 : OSequenceIterator(const ::com::sun::star::uno::Any& _rSequenceAny);
149 :
150 : bool hasMoreElements() const;
151 : ::com::sun::star::uno::Any nextElement();
152 :
153 : private:
154 : inline void construct(const ::com::sun::star::uno::Sequence< TYPE >& _rSeq);
155 : };
156 :
157 :
158 : template <class TYPE>
159 82862 : inline OSequenceIterator<TYPE>::OSequenceIterator(const ::com::sun::star::uno::Sequence< TYPE >& _rSeq)
160 : :m_pElements(NULL)
161 : ,m_nLen(0)
162 82862 : ,m_pCurrent(NULL)
163 : {
164 82862 : construct(_rSeq);
165 82862 : }
166 :
167 :
168 : template <class TYPE>
169 0 : inline OSequenceIterator<TYPE>::OSequenceIterator(const ::com::sun::star::uno::Any& _rSequenceAny)
170 : :m_pElements(NULL)
171 : ,m_nLen(0)
172 0 : ,m_pCurrent(NULL)
173 : {
174 0 : ::com::sun::star::uno::Sequence< TYPE > aContainer;
175 0 : bool bSuccess = _rSequenceAny >>= aContainer;
176 : OSL_ENSURE(bSuccess, "OSequenceIterator::OSequenceIterator: invalid Any!");
177 : (void)bSuccess;
178 0 : construct(aContainer);
179 0 : }
180 :
181 :
182 : template <class TYPE>
183 82862 : void OSequenceIterator<TYPE>::construct(const ::com::sun::star::uno::Sequence< TYPE >& _rSeq)
184 : {
185 82862 : m_pElements = _rSeq.getConstArray();
186 82862 : m_nLen = _rSeq.getLength();
187 82862 : m_pCurrent = m_pElements;
188 82862 : }
189 :
190 :
191 : template <class TYPE>
192 366591 : inline bool OSequenceIterator<TYPE>::hasMoreElements() const
193 : {
194 366591 : return m_pCurrent - m_pElements < m_nLen;
195 : }
196 :
197 :
198 : template <class TYPE>
199 283729 : inline ::com::sun::star::uno::Any OSequenceIterator<TYPE>::nextElement()
200 : {
201 283729 : return ::com::sun::star::uno::toAny(*m_pCurrent++);
202 : }
203 :
204 : /** Copy from a plain C/C++ array into a Sequence.
205 :
206 : @tpl SrcType
207 : Array element type. Must be assignable to DstType
208 :
209 : @tpl DstType
210 : Sequence element type. Must be assignable from SrcType
211 :
212 : @param i_pArray
213 : Valid pointer to at least num elements of type SrcType
214 :
215 : @param nNum
216 : Number of array elements to copy
217 :
218 : @return the resulting Sequence
219 :
220 : @attention when copying from e.g. a double array to a
221 : Sequence<int>, no proper rounding will be performed, but the
222 : values will be truncated. There's currently no measure to
223 : prevent or detect precision loss, overflow or truncation.
224 : */
225 : template < typename DstType, typename SrcType >
226 0 : inline ::com::sun::star::uno::Sequence< DstType > arrayToSequence( const SrcType* i_pArray, sal_Int32 nNum )
227 : {
228 0 : ::com::sun::star::uno::Sequence< DstType > result( nNum );
229 0 : ::std::copy( i_pArray, i_pArray+nNum, result.getArray() );
230 0 : return result;
231 : }
232 :
233 :
234 : /** Copy from a Sequence into a plain C/C++ array
235 :
236 : @tpl SrcType
237 : Sequence element type. Must be assignable to DstType
238 :
239 : @tpl DstType
240 : Array element type. Must be assignable from SrcType
241 :
242 : @param io_pArray
243 : Valid pointer to at least i_Sequence.getLength() elements of
244 : type DstType
245 :
246 : @param i_Sequence
247 : Reference to a Sequence of SrcType elements
248 :
249 : @return a pointer to the array
250 :
251 : @attention when copying from e.g. a Sequence<double> to an int
252 : array, no proper rounding will be performed, but the values
253 : will be truncated. There's currently no measure to prevent or
254 : detect precision loss, overflow or truncation.
255 : */
256 : template < typename DstType, typename SrcType >
257 0 : inline DstType* sequenceToArray( DstType* io_pArray, const ::com::sun::star::uno::Sequence< SrcType >& i_Sequence )
258 : {
259 0 : ::std::copy( i_Sequence.begin(), i_Sequence.end(), io_pArray );
260 0 : return io_pArray;
261 : }
262 :
263 :
264 : /** Copy from a container into a Sequence
265 :
266 : @tpl SrcType
267 : Container type. This type must fulfill the STL container
268 : concept, in particular, the size(), begin() and end() methods
269 : must be available and have the usual semantics.
270 :
271 : @tpl DstType
272 : Sequence element type. Must be assignable from SrcType's
273 : elements
274 :
275 : @param i_Container
276 : Reference to the input contain with elements of type SrcType
277 :
278 : @return the generated Sequence
279 :
280 : @attention this function always performs a copy. Furthermore,
281 : when copying from e.g. a vector<double> to a Sequence<int>, no
282 : proper rounding will be performed, but the values will be
283 : truncated. There's currently no measure to prevent or detect
284 : precision loss, overflow or truncation.
285 : */
286 : template < typename DstType, typename SrcType >
287 4 : inline ::com::sun::star::uno::Sequence< DstType > containerToSequence( const SrcType& i_Container )
288 : {
289 4 : ::com::sun::star::uno::Sequence< DstType > result( i_Container.size() );
290 4 : ::std::copy( i_Container.begin(), i_Container.end(), result.getArray() );
291 4 : return result;
292 : }
293 :
294 : template <typename T>
295 27754588 : inline ::com::sun::star::uno::Sequence<T> containerToSequence(
296 : ::std::vector<T> const& v )
297 : {
298 : return ::com::sun::star::uno::Sequence<T>(
299 27754588 : v.data(), static_cast<sal_Int32>(v.size()) );
300 : }
301 :
302 :
303 : /** Copy from a Sequence into a container
304 :
305 : @tpl SrcType
306 : Sequence element type. Must be assignable to SrcType's
307 : elements
308 :
309 : @tpl DstType
310 : Container type. This type must fulfill the STL container and
311 : sequence concepts, in particular, the begin(), end() and the
312 : unary constructor DstType(int) methods must be available and
313 : have the usual semantics.
314 :
315 : @param i_Sequence
316 : Reference to a Sequence of SrcType elements
317 :
318 : @return the generated container
319 :
320 : @attention this function always performs a copy. Furthermore,
321 : when copying from e.g. a Sequence<double> to a vector<int>, no
322 : proper rounding will be performed, but the values will be
323 : truncated. There's currently no measure to prevent or detect
324 : precision loss, overflow or truncation.
325 : */
326 : template < typename DstType, typename SrcType >
327 947919 : inline DstType sequenceToContainer( const ::com::sun::star::uno::Sequence< SrcType >& i_Sequence )
328 : {
329 947919 : DstType result( i_Sequence.getLength() );
330 947919 : ::std::copy( i_Sequence.begin(), i_Sequence.end(), result.begin() );
331 947919 : return result;
332 : }
333 :
334 : /** Copy from a Sequence into an existing container
335 :
336 : This potentially saves a needless extra copy operation over
337 : the whole container, as it passes the target object by
338 : reference.
339 :
340 : @tpl SrcType
341 : Sequence element type. Must be assignable to SrcType's
342 : elements
343 :
344 : @tpl DstType
345 : Container type. This type must fulfill the STL container and
346 : sequence concepts, in particular, the begin(), end() and
347 : resize(int) methods must be available and have the usual
348 : semantics.
349 :
350 : @param o_Output
351 : Reference to the target container
352 :
353 : @param i_Sequence
354 : Reference to a Sequence of SrcType elements
355 :
356 : @return a non-const reference to the given container
357 :
358 : @attention this function always performs a copy. Furthermore,
359 : when copying from e.g. a Sequence<double> to a vector<int>, no
360 : proper rounding will be performed, but the values will be
361 : truncated. There's currently no measure to prevent or detect
362 : precision loss, overflow or truncation.
363 : */
364 : template < typename DstType, typename SrcType >
365 6 : inline DstType& sequenceToContainer( DstType& o_Output, const ::com::sun::star::uno::Sequence< SrcType >& i_Sequence )
366 : {
367 6 : o_Output.resize( i_Sequence.getLength() );
368 6 : ::std::copy( i_Sequence.begin(), i_Sequence.end(), o_Output.begin() );
369 6 : return o_Output;
370 : }
371 :
372 :
373 : } // namespace comphelper
374 :
375 :
376 :
377 : #endif // INCLUDED_COMPHELPER_SEQUENCE_HXX
378 :
379 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|