LibreOffice Module comphelper (master) 1
sequence.hxx
Go to the documentation of this file.
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 <com/sun/star/uno/Sequence.hxx>
24#include <osl/diagnose.h>
25
26#include <algorithm>
27#include <vector>
28
29namespace comphelper
30{
34 template <class T1, class T2>
35 inline sal_Int32 findValue(const css::uno::Sequence<T1>& _rList, const T2& _rValue)
36 {
37 // at which position do I find the value?
38 for (sal_Int32 i = 0; i < _rList.getLength(); ++i)
39 {
40 if (_rList[i] == _rValue)
41 return i;
42 }
43
44 return -1;
45 }
46
48 template <class T, class... Ss>
49 inline css::uno::Sequence<T> concatSequences(const css::uno::Sequence<T>& rS1, const Ss&... rSn)
50 {
51 // unary fold to disallow empty parameter pack: at least have one sequence in rSn
52 css::uno::Sequence<T> aReturn(std::size(rS1) + (... + std::size(rSn)));
53 T* pReturn = std::copy(std::begin(rS1), std::end(rS1), aReturn.getArray());
54 (..., (pReturn = std::copy(std::begin(rSn), std::end(rSn), pReturn)));
55 return aReturn;
56 }
57
61 template<typename T> inline css::uno::Sequence<T> combineSequences(
62 css::uno::Sequence<T> const & left, css::uno::Sequence<T> const & right)
63 {
64 sal_Int32 n1 = left.getLength();
65 css::uno::Sequence<T> ret(n1 + right.getLength());
66 //TODO: check for overflow
67 auto pRet = ret.getArray();
68 std::copy_n(left.getConstArray(), n1, pRet);
69 sal_Int32 n2 = n1;
70 for (sal_Int32 i = 0; i != right.getLength(); ++i) {
71 bool found = false;
72 for (sal_Int32 j = 0; j != n1; ++j) {
73 if (right[i] == left[j]) {
74 found = true;
75 break;
76 }
77 }
78 if (!found) {
79 pRet[n2++] = right[i];
80 }
81 }
82 ret.realloc(n2);
83 return ret;
84 }
85
87 template<class T>
88 inline void removeElementAt(css::uno::Sequence<T>& _rSeq, sal_Int32 _nPos)
89 {
90 sal_Int32 nLength = _rSeq.getLength();
91
92 OSL_ENSURE(0 <= _nPos && _nPos < nLength, "invalid index");
93
94 T* pPos = _rSeq.getArray() + _nPos;
95 std::move(pPos + 1, pPos + nLength - _nPos, pPos);
96
97 _rSeq.realloc(nLength-1);
98 }
99
121 template < typename DstType, typename SrcType >
122 inline css::uno::Sequence< DstType > arrayToSequence( const SrcType* i_pArray, sal_Int32 nNum )
123 {
124 if constexpr (std::is_same_v< DstType, SrcType >)
125 {
126 return css::uno::Sequence< DstType >( i_pArray, nNum );
127 }
128 else
129 {
130 css::uno::Sequence< DstType > result( nNum );
131 ::std::copy( i_pArray, i_pArray+nNum, result.getArray() );
132 return result;
133 }
134 }
135
136
159 template < typename DstType, typename SrcType >
160 inline DstType* sequenceToArray( DstType* io_pArray, const css::uno::Sequence< SrcType >& i_Sequence )
161 {
162 ::std::copy( i_Sequence.begin(), i_Sequence.end(), io_pArray );
163 return io_pArray;
164 }
165
166
189 template < typename DstElementType, typename SrcType >
190 inline css::uno::Sequence< DstElementType > containerToSequence( const SrcType& i_Container )
191 {
192 using ::std::size, ::std::begin, ::std::end;
193 css::uno::Sequence< DstElementType > result( size(i_Container) );
194 ::std::copy( begin(i_Container), end(i_Container), result.getArray() );
195 return result;
196 }
197
198 // this one does better type deduction, but does not allow us to copy into a different element type
199 template < typename SrcType >
200 inline css::uno::Sequence< typename SrcType::value_type > containerToSequence( const SrcType& i_Container )
201 {
202 return containerToSequence<typename SrcType::value_type, SrcType>(i_Container);
203 }
204
205 // handle arrays
206 template<typename ElementType, std::size_t SrcSize>
207 inline css::uno::Sequence< ElementType > containerToSequence( ElementType const (&i_Array)[ SrcSize ] )
208 {
209 return css::uno::Sequence< ElementType >( i_Array, SrcSize );
210 }
211
212 template <typename T>
213 inline css::uno::Sequence<T> containerToSequence(
214 ::std::vector<T> const& v )
215 {
216 return css::uno::Sequence<T>(
217 v.data(), static_cast<sal_Int32>(v.size()) );
218 }
219
220
242 template < typename DstType, typename SrcType >
243 inline DstType sequenceToContainer( const css::uno::Sequence< SrcType >& i_Sequence )
244 {
245 return DstType(i_Sequence.begin(), i_Sequence.end());
246 }
247
248 // this one does better type deduction, but does not allow us to copy into a different element type
249 template < typename DstType >
250 inline DstType sequenceToContainer( const css::uno::Sequence< typename DstType::value_type >& i_Sequence )
251 {
252 return DstType(i_Sequence.begin(), i_Sequence.end());
253 }
254
285 template < typename DstType, typename SrcType >
286 inline DstType& sequenceToContainer( DstType& o_Output, const css::uno::Sequence< SrcType >& i_Sequence )
287 {
288 o_Output.resize( i_Sequence.getLength() );
289 ::std::copy( i_Sequence.begin(), i_Sequence.end(), o_Output.begin() );
290 return o_Output;
291 }
292
299 template < typename M >
300 inline css::uno::Sequence< typename M::key_type > mapKeysToSequence( M const& map )
301 {
302 css::uno::Sequence< typename M::key_type > ret( static_cast<sal_Int32>(map.size()) );
303 std::transform(map.begin(), map.end(), ret.getArray(),
304 [](const auto& i) { return i.first; });
305 return ret;
306 }
307
308 template < typename M >
309 inline css::uno::Sequence< typename M::mapped_type > mapValuesToSequence( M const& map )
310 {
311 css::uno::Sequence< typename M::mapped_type > ret( static_cast<sal_Int32>(map.size()) );
312 std::transform(map.begin(), map.end(), ret.getArray(),
313 [](const auto& i) { return i.second; });
314 return ret;
315 }
316
317} // namespace comphelper
318
319
320#endif // INCLUDED_COMPHELPER_SEQUENCE_HXX
321
322/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OString right
float v
int n2
int n1
size
sal_Int32 findValue(const css::uno::Sequence< T1 > &_rList, const T2 &_rValue)
Search the given value within the given sequence, return the position of the first occurrence.
Definition: sequence.hxx:35
css::uno::Sequence< T > concatSequences(const css::uno::Sequence< T > &rS1, const Ss &... rSn)
concat several sequences
Definition: sequence.hxx:49
DstType * sequenceToArray(DstType *io_pArray, const css::uno::Sequence< SrcType > &i_Sequence)
Copy from a Sequence into a plain C/C++ array.
Definition: sequence.hxx:160
DstType sequenceToContainer(const css::uno::Sequence< SrcType > &i_Sequence)
Copy from a Sequence into a container.
Definition: sequence.hxx:243
css::uno::Sequence< typename M::key_type > mapKeysToSequence(M const &map)
Copy (keys or values) from an associate container into a Sequence.
Definition: sequence.hxx:300
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
Copy from a container into a Sequence.
Definition: sequence.hxx:190
css::uno::Sequence< typename M::mapped_type > mapValuesToSequence(M const &map)
Definition: sequence.hxx:309
css::uno::Sequence< DstType > arrayToSequence(const SrcType *i_pArray, sal_Int32 nNum)
Copy from a plain C/C++ array into a Sequence.
Definition: sequence.hxx:122
void removeElementAt(css::uno::Sequence< T > &_rSeq, sal_Int32 _nPos)
remove a specified element from a sequences
Definition: sequence.hxx:88
css::uno::Sequence< T > combineSequences(css::uno::Sequence< T > const &left, css::uno::Sequence< T > const &right)
concat additional elements from right sequence to left sequence
Definition: sequence.hxx:61
ElementType
int i
enumrange< T >::Iterator begin(enumrange< T >)
end
std::map< OUString, rtl::Reference< Entity > > map
Any result
sal_uInt64 left
sal_Int32 _nPos
sal_Int32 nLength