cool#10630 doc sign: fix Impress sign line, to be able to finish signing again
[LibreOffice.git] / include / comphelper / sequence.hxx
blob4a9d132c271e585e2306924267012f747a527d4a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 INCLUDED_COMPHELPER_SEQUENCE_HXX
21 #define INCLUDED_COMPHELPER_SEQUENCE_HXX
23 #include <com/sun/star/uno/Sequence.hxx>
24 #include <osl/diagnose.h>
26 #include <vector>
28 namespace comphelper
30 /** Search the given value within the given sequence, return the position of the first occurrence.
31 Returns -1 if nothing found.
33 template <class T1, class T2>
34 inline sal_Int32 findValue(const css::uno::Sequence<T1>& _rList, const T2& _rValue)
36 // at which position do I find the value?
37 for (sal_Int32 i = 0; i < _rList.getLength(); ++i)
39 if (_rList[i] == _rValue)
40 return i;
43 return -1;
46 /// concat several sequences
47 template <class T, class... Ss>
48 inline css::uno::Sequence<T> concatSequences(const css::uno::Sequence<T>& rS1, const Ss&... rSn)
50 // unary fold to disallow empty parameter pack: at least have one sequence in rSn
51 css::uno::Sequence<T> aReturn(std::size(rS1) + (... + std::size(rSn)));
52 T* pReturn = std::copy(std::begin(rS1), std::end(rS1), aReturn.getArray());
53 (..., (pReturn = std::copy(std::begin(rSn), std::end(rSn), pReturn)));
54 return aReturn;
57 /// concat additional elements from right sequence to left sequence
58 ///
59 /// be aware that this takes time O(|left| * |right|)
60 template<typename T> inline css::uno::Sequence<T> combineSequences(
61 css::uno::Sequence<T> const & left, css::uno::Sequence<T> const & right)
63 sal_Int32 n1 = left.getLength();
64 css::uno::Sequence<T> ret(n1 + right.getLength());
65 //TODO: check for overflow
66 auto pRet = ret.getArray();
67 std::copy_n(left.getConstArray(), n1, pRet);
68 sal_Int32 n2 = n1;
69 for (sal_Int32 i = 0; i != right.getLength(); ++i) {
70 bool found = false;
71 for (sal_Int32 j = 0; j != n1; ++j) {
72 if (right[i] == left[j]) {
73 found = true;
74 break;
77 if (!found) {
78 pRet[n2++] = right[i];
81 ret.realloc(n2);
82 return ret;
85 /// remove a specified element from a sequences
86 template<class T>
87 inline void removeElementAt(css::uno::Sequence<T>& _rSeq, sal_Int32 _nPos)
89 sal_Int32 nLength = _rSeq.getLength();
91 OSL_ENSURE(0 <= _nPos && _nPos < nLength, "invalid index");
93 T* pPos = _rSeq.getArray() + _nPos;
94 std::move(pPos + 1, pPos + nLength - _nPos, pPos);
96 _rSeq.realloc(nLength-1);
99 /** Copy from a plain C/C++ array into a Sequence.
101 @tpl SrcType
102 Array element type. Must be assignable to DstType
104 @tpl DstType
105 Sequence element type. Must be assignable from SrcType
107 @param i_pArray
108 Valid pointer to at least num elements of type SrcType
110 @param nNum
111 Number of array elements to copy
113 @return the resulting Sequence
115 @attention when copying from e.g. a double array to a
116 Sequence<int>, no proper rounding will be performed, but the
117 values will be truncated. There's currently no measure to
118 prevent or detect precision loss, overflow or truncation.
120 template < typename DstType, typename SrcType >
121 inline css::uno::Sequence< DstType > arrayToSequence( const SrcType* i_pArray, sal_Int32 nNum )
123 if constexpr (std::is_same_v< DstType, SrcType >)
125 return css::uno::Sequence< DstType >( i_pArray, nNum );
127 else
129 css::uno::Sequence< DstType > result( nNum );
130 ::std::copy( i_pArray, i_pArray+nNum, result.getArray() );
131 return result;
136 /** Copy from a Sequence into a plain C/C++ array
138 @tpl SrcType
139 Sequence element type. Must be assignable to DstType
141 @tpl DstType
142 Array element type. Must be assignable from SrcType
144 @param io_pArray
145 Valid pointer to at least i_Sequence.getLength() elements of
146 type DstType
148 @param i_Sequence
149 Reference to a Sequence of SrcType elements
151 @return a pointer to the array
153 @attention when copying from e.g. a Sequence<double> to an int
154 array, no proper rounding will be performed, but the values
155 will be truncated. There's currently no measure to prevent or
156 detect precision loss, overflow or truncation.
158 template < typename DstType, typename SrcType >
159 inline DstType* sequenceToArray( DstType* io_pArray, const css::uno::Sequence< SrcType >& i_Sequence )
161 ::std::copy( i_Sequence.begin(), i_Sequence.end(), io_pArray );
162 return io_pArray;
166 /** Copy from a container into a Sequence
168 @tpl SrcType
169 Container type. This type must fulfill the STL container
170 concept, in particular, the size(), begin() and end() methods
171 must be available and have the usual semantics.
173 @tpl DstType
174 Sequence element type. Must be assignable from SrcType's
175 elements
177 @param i_Container
178 Reference to the input contain with elements of type SrcType
180 @return the generated Sequence
182 @attention this function always performs a copy. Furthermore,
183 when copying from e.g. a vector<double> to a Sequence<int>, no
184 proper rounding will be performed, but the values will be
185 truncated. There's currently no measure to prevent or detect
186 precision loss, overflow or truncation.
188 template < typename DstElementType, typename SrcType >
189 inline css::uno::Sequence< DstElementType > containerToSequence( const SrcType& i_Container )
191 using ::std::size, ::std::begin, ::std::end;
192 css::uno::Sequence< DstElementType > result( size(i_Container) );
193 ::std::copy( begin(i_Container), end(i_Container), result.getArray() );
194 return result;
197 // this one does better type deduction, but does not allow us to copy into a different element type
198 template < typename SrcType >
199 inline css::uno::Sequence< typename SrcType::value_type > containerToSequence( const SrcType& i_Container )
201 return containerToSequence<typename SrcType::value_type, SrcType>(i_Container);
204 // handle arrays
205 template<typename ElementType, std::size_t SrcSize>
206 inline css::uno::Sequence< ElementType > containerToSequence( ElementType const (&i_Array)[ SrcSize ] )
208 return css::uno::Sequence< ElementType >( i_Array, SrcSize );
211 template <typename T>
212 inline css::uno::Sequence<T> containerToSequence(
213 ::std::vector<T> const& v )
215 return css::uno::Sequence<T>(
216 v.data(), static_cast<sal_Int32>(v.size()) );
220 /** Copy from a Sequence into a container
222 @tpl SrcType
223 Sequence element type. Must be assignable to SrcType's
224 elements
226 @tpl DstType
227 Container type. This type must have a constructor taking a pair
228 of iterators defining a range to copy from
230 @param i_Sequence
231 Reference to a Sequence of SrcType elements
233 @return the generated container. C++17 copy elision rules apply
235 @attention this function always performs a copy. Furthermore,
236 when copying from e.g. a Sequence<double> to a vector<int>, no
237 proper rounding will be performed, but the values will be
238 truncated. There's currently no measure to prevent or detect
239 precision loss, overflow or truncation.
241 template < typename DstType, typename SrcType >
242 inline DstType sequenceToContainer( const css::uno::Sequence< SrcType >& i_Sequence )
244 return DstType(i_Sequence.begin(), i_Sequence.end());
247 // this one does better type deduction, but does not allow us to copy into a different element type
248 template < typename DstType >
249 inline DstType sequenceToContainer( const css::uno::Sequence< typename DstType::value_type >& i_Sequence )
251 return DstType(i_Sequence.begin(), i_Sequence.end());
254 /** Copy from a Sequence into an existing container
256 This potentially saves a needless extra copy operation over
257 the whole container, as it passes the target object by
258 reference.
260 @tpl SrcType
261 Sequence element type. Must be assignable to SrcType's
262 elements
264 @tpl DstType
265 Container type. This type must fulfill the STL container and
266 sequence concepts, in particular, the begin(), end() and
267 resize(int) methods must be available and have the usual
268 semantics.
270 @param o_Output
271 Reference to the target container
273 @param i_Sequence
274 Reference to a Sequence of SrcType elements
276 @return a non-const reference to the given container
278 @attention this function always performs a copy. Furthermore,
279 when copying from e.g. a Sequence<double> to a vector<int>, no
280 proper rounding will be performed, but the values will be
281 truncated. There's currently no measure to prevent or detect
282 precision loss, overflow or truncation.
284 template < typename DstType, typename SrcType >
285 inline DstType& sequenceToContainer( DstType& o_Output, const css::uno::Sequence< SrcType >& i_Sequence )
287 o_Output.resize( i_Sequence.getLength() );
288 ::std::copy( i_Sequence.begin(), i_Sequence.end(), o_Output.begin() );
289 return o_Output;
292 /** Copy (keys or values) from an associate container into a Sequence
294 @tpl M map container type eg. std::map/std::unordered_map
296 @return the generated Sequence
298 template < typename M >
299 inline css::uno::Sequence< typename M::key_type > mapKeysToSequence( M const& map )
301 css::uno::Sequence< typename M::key_type > ret( static_cast<sal_Int32>(map.size()) );
302 std::transform(map.begin(), map.end(), ret.getArray(),
303 [](const auto& i) -> const typename M::key_type& { return i.first; });
304 return ret;
307 template < typename M >
308 inline css::uno::Sequence< typename M::mapped_type > mapValuesToSequence( M const& map )
310 css::uno::Sequence< typename M::mapped_type > ret( static_cast<sal_Int32>(map.size()) );
311 std::transform(map.begin(), map.end(), ret.getArray(),
312 [](const auto& i) -> const typename M::mapped_type& { return i.second; });
313 return ret;
316 } // namespace comphelper
319 #endif // INCLUDED_COMPHELPER_SEQUENCE_HXX
321 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */