1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef _COMPHELPER_SEQUENCE_HXX_
21 #define _COMPHELPER_SEQUENCE_HXX_
23 #include <algorithm> // copy algorithm
24 #include <com/sun/star/uno/Sequence.hxx>
25 #include <osl/diagnose.h>
26 #include "comphelper/comphelperdllapi.h"
30 //.........................................................................
33 //.........................................................................
35 namespace staruno
= ::com::sun::star::uno
;
37 //-------------------------------------------------------------------------
38 /** search the given string within the given sequence, return the positions where it was found.
39 if _bOnlyFirst is sal_True, only the first occurrence will be returned.
41 COMPHELPER_DLLPUBLIC
staruno::Sequence
<sal_Int16
> findValue(const staruno::Sequence
< OUString
>& _rList
, const OUString
& _rValue
, sal_Bool _bOnlyFirst
= sal_False
);
43 /** Checks if the name exists
45 * \param Value The value to search for.
46 * \param _aList The list in which to search for the value.
47 * \return <TRUE/> if the value can be found, otherwise <FALSE/>.
49 COMPHELPER_DLLPUBLIC sal_Bool
existsValue(const OUString
& Value
,const ::com::sun::star::uno::Sequence
< OUString
>& _aList
);
52 //-------------------------------------------------------------------------
56 void implCopySequence(const T
* _pSource
, T
*& _pDest
, sal_Int32 _nSourceLen
)
58 for (sal_Int32 i
=0; i
<_nSourceLen
; ++i
, ++_pSource
, ++_pDest
)
62 //-------------------------------------------------------------------------
63 /// concat two sequences
65 staruno::Sequence
<T
> concatSequences(const staruno::Sequence
<T
>& _rLeft
, const staruno::Sequence
<T
>& _rRight
)
67 sal_Int32
nLeft(_rLeft
.getLength()), nRight(_rRight
.getLength());
68 const T
* pLeft
= _rLeft
.getConstArray();
69 const T
* pRight
= _rRight
.getConstArray();
71 sal_Int32
nReturnLen(nLeft
+ nRight
);
72 staruno::Sequence
<T
> aReturn(nReturnLen
);
73 T
* pReturn
= aReturn
.getArray();
75 internal::implCopySequence(pLeft
, pReturn
, nLeft
);
76 internal::implCopySequence(pRight
, pReturn
, nRight
);
81 //-------------------------------------------------------------------------
82 /// concat three sequences
84 staruno::Sequence
<T
> concatSequences(const staruno::Sequence
<T
>& _rLeft
, const staruno::Sequence
<T
>& _rMiddle
, const staruno::Sequence
<T
>& _rRight
)
86 sal_Int32
nLeft(_rLeft
.getLength()), nMiddle(_rMiddle
.getLength()), nRight(_rRight
.getLength());
87 const T
* pLeft
= _rLeft
.getConstArray();
88 const T
* pMiddle
= _rMiddle
.getConstArray();
89 const T
* pRight
= _rRight
.getConstArray();
91 sal_Int32
nReturnLen(nLeft
+ nMiddle
+ nRight
);
92 staruno::Sequence
<T
> aReturn(nReturnLen
);
93 T
* pReturn
= aReturn
.getArray();
95 internal::implCopySequence(pLeft
, pReturn
, nLeft
);
96 internal::implCopySequence(pMiddle
, pReturn
, nMiddle
);
97 internal::implCopySequence(pRight
, pReturn
, nRight
);
102 //-------------------------------------------------------------------------
103 /// remove a specified element from a sequences
105 void removeElementAt(staruno::Sequence
<T
>& _rSeq
, sal_Int32 _nPos
)
107 sal_uInt32 nLength
= _rSeq
.getLength();
109 OSL_ENSURE(0 <= _nPos
&& (sal_uInt32
)_nPos
< nLength
, "invalid index");
111 for (sal_uInt32 i
= (sal_uInt32
)_nPos
+ 1; i
< nLength
; ++i
)
113 _rSeq
[i
-1] = _rSeq
[i
];
116 _rSeq
.realloc(nLength
-1);
119 //=====================================================================
120 //= iterating through sequences
121 //=====================================================================
122 /** a helper class for iterating through a sequence
124 template <class TYPE
>
125 class OSequenceIterator
127 const TYPE
* m_pElements
;
129 const TYPE
* m_pCurrent
;
132 /** contrcuct a sequence iterator from a sequence
134 OSequenceIterator(const ::com::sun::star::uno::Sequence
< TYPE
>& _rSeq
);
135 /** contrcuct a sequence iterator from a Any containing a sequence
137 OSequenceIterator(const ::com::sun::star::uno::Any
& _rSequenceAny
);
139 sal_Bool
hasMoreElements() const;
140 ::com::sun::star::uno::Any
nextElement();
143 void construct(const ::com::sun::star::uno::Sequence
< TYPE
>& _rSeq
);
146 //---------------------------------------------------------------------
147 template <class TYPE
>
148 OSequenceIterator
<TYPE
>::OSequenceIterator(const ::com::sun::star::uno::Sequence
< TYPE
>& _rSeq
)
156 //---------------------------------------------------------------------
157 template <class TYPE
>
158 OSequenceIterator
<TYPE
>::OSequenceIterator(const ::com::sun::star::uno::Any
& _rSequenceAny
)
163 ::com::sun::star::uno::Sequence
< TYPE
> aContainer
;
164 sal_Bool bSuccess
= _rSequenceAny
>>= aContainer
;
165 OSL_ENSURE(bSuccess
, "OSequenceIterator::OSequenceIterator: invalid Any!");
167 construct(aContainer
);
170 //---------------------------------------------------------------------
171 template <class TYPE
>
172 void OSequenceIterator
<TYPE
>::construct(const ::com::sun::star::uno::Sequence
< TYPE
>& _rSeq
)
174 m_pElements
= _rSeq
.getConstArray();
175 m_nLen
= _rSeq
.getLength();
176 m_pCurrent
= m_pElements
;
179 //---------------------------------------------------------------------
180 template <class TYPE
>
181 sal_Bool OSequenceIterator
<TYPE
>::hasMoreElements() const
183 return m_pCurrent
- m_pElements
< m_nLen
;
186 //---------------------------------------------------------------------
187 template <class TYPE
>
188 ::com::sun::star::uno::Any OSequenceIterator
<TYPE
>::nextElement()
190 return ::com::sun::star::uno::makeAny(*m_pCurrent
++);
193 //-------------------------------------------------------------------------
194 /** Copy from a plain C/C++ array into a Sequence.
197 Array element type. Must be assignable to DstType
200 Sequence element type. Must be assignable from SrcType
203 Valid pointer to at least num elements of type SrcType
206 Number of array elements to copy
208 @return the resulting Sequence
210 @attention when copying from e.g. a double array to a
211 Sequence<int>, no proper rounding will be performed, but the
212 values will be truncated. There's currently no measure to
213 prevent or detect precision loss, overflow or truncation.
215 template < typename DstType
, typename SrcType
>
216 ::com::sun::star::uno::Sequence
< DstType
> arrayToSequence( const SrcType
* i_pArray
, sal_Int32 nNum
)
218 ::com::sun::star::uno::Sequence
< DstType
> result( nNum
);
219 ::std::copy( i_pArray
, i_pArray
+nNum
, result
.getArray() );
223 //-------------------------------------------------------------------------
224 /** Copy from a Sequence into a plain C/C++ array
227 Sequence element type. Must be assignable to DstType
230 Array element type. Must be assignable from SrcType
233 Valid pointer to at least i_Sequence.getLength() elements of
237 Reference to a Sequence of SrcType elements
239 @return a pointer to the array
241 @attention when copying from e.g. a Sequence<double> to an int
242 array, no proper rounding will be performed, but the values
243 will be truncated. There's currently no measure to prevent or
244 detect precision loss, overflow or truncation.
246 template < typename DstType
, typename SrcType
>
247 DstType
* sequenceToArray( DstType
* io_pArray
, const ::com::sun::star::uno::Sequence
< SrcType
>& i_Sequence
)
249 ::std::copy( i_Sequence
.getConstArray(), i_Sequence
.getConstArray()+i_Sequence
.getLength(), io_pArray
);
253 //-------------------------------------------------------------------------
254 /** Copy from a container into a Sequence
257 Container type. This type must fulfill the STL container
258 concept, in particular, the size(), begin() and end() methods
259 must be available and have the usual semantics.
262 Sequence element type. Must be assignable from SrcType's
266 Reference to the input contain with elements of type SrcType
268 @return the generated Sequence
270 @attention this function always performs a copy. Furthermore,
271 when copying from e.g. a vector<double> to a Sequence<int>, no
272 proper rounding will be performed, but the values will be
273 truncated. There's currently no measure to prevent or detect
274 precision loss, overflow or truncation.
276 template < typename DstType
, typename SrcType
>
277 ::com::sun::star::uno::Sequence
< DstType
> containerToSequence( const SrcType
& i_Container
)
279 ::com::sun::star::uno::Sequence
< DstType
> result( i_Container
.size() );
280 ::std::copy( i_Container
.begin(), i_Container
.end(), result
.getArray() );
284 template <typename T
>
285 inline ::com::sun::star::uno::Sequence
<T
> containerToSequence(
286 ::std::vector
<T
> const& v
)
288 return ::com::sun::star::uno::Sequence
<T
>(
289 v
.empty() ? 0 : &v
[0], static_cast<sal_Int32
>(v
.size()) );
292 //-------------------------------------------------------------------------
293 /** Copy from a Sequence into a container
296 Sequence element type. Must be assignable to SrcType's
300 Container type. This type must fulfill the STL container and
301 sequence concepts, in particular, the begin(), end() and the
302 unary constructor DstType(int) methods must be available and
303 have the usual semantics.
306 Reference to a Sequence of SrcType elements
308 @return the generated container
310 @attention this function always performs a copy. Furthermore,
311 when copying from e.g. a Sequence<double> to a vector<int>, no
312 proper rounding will be performed, but the values will be
313 truncated. There's currently no measure to prevent or detect
314 precision loss, overflow or truncation.
316 template < typename DstType
, typename SrcType
>
317 DstType
sequenceToContainer( const ::com::sun::star::uno::Sequence
< SrcType
>& i_Sequence
)
319 DstType
result( i_Sequence
.getLength() );
320 ::std::copy( i_Sequence
.getConstArray(), i_Sequence
.getConstArray()+i_Sequence
.getLength(), result
.begin() );
323 //-------------------------------------------------------------------------
324 /** Copy from a Sequence into an existing container
326 This potentially saves a needless extra copy operation over
327 the whole container, as it passes the target object by
331 Sequence element type. Must be assignable to SrcType's
335 Container type. This type must fulfill the STL container and
336 sequence concepts, in particular, the begin(), end() and
337 resize(int) methods must be available and have the usual
341 Reference to the target container
344 Reference to a Sequence of SrcType elements
346 @return a non-const reference to the given container
348 @attention this function always performs a copy. Furthermore,
349 when copying from e.g. a Sequence<double> to a vector<int>, no
350 proper rounding will be performed, but the values will be
351 truncated. There's currently no measure to prevent or detect
352 precision loss, overflow or truncation.
354 template < typename DstType
, typename SrcType
>
355 DstType
& sequenceToContainer( DstType
& o_Output
, const ::com::sun::star::uno::Sequence
< SrcType
>& i_Sequence
)
357 o_Output
.resize( i_Sequence
.getLength() );
358 ::std::copy( i_Sequence
.getConstArray(), i_Sequence
.getConstArray()+i_Sequence
.getLength(), o_Output
.begin() );
362 //.........................................................................
363 } // namespace comphelper
364 //.........................................................................
367 #endif // _COMPHELPER_SEQUENCE_HXX_
369 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */