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 
29 namespace 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.begin());
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  std::copy_n(left.getConstArray(), n1, ret.getArray());
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;
75  }
76  }
77  if (!found) {
78  ret[n2++] = right[i];
79  }
80  }
81  ret.realloc(n2);
82  return ret;
83  }
84 
86  template<class T>
87  inline void removeElementAt(css::uno::Sequence<T>& _rSeq, sal_Int32 _nPos)
88  {
89  sal_Int32 nLength = _rSeq.getLength();
90 
91  OSL_ENSURE(0 <= _nPos && _nPos < nLength, "invalid index");
92 
93  T* pPos = _rSeq.getArray() + _nPos;
94  std::move(pPos + 1, pPos + nLength - _nPos, pPos);
95 
96  _rSeq.realloc(nLength-1);
97  }
98 
120  template < typename DstType, typename SrcType >
121  inline css::uno::Sequence< DstType > arrayToSequence( const SrcType* i_pArray, sal_Int32 nNum )
122  {
123  css::uno::Sequence< DstType > result( nNum );
124  ::std::copy( i_pArray, i_pArray+nNum, result.getArray() );
125  return result;
126  }
127 
128 
151  template < typename DstType, typename SrcType >
152  inline DstType* sequenceToArray( DstType* io_pArray, const css::uno::Sequence< SrcType >& i_Sequence )
153  {
154  ::std::copy( i_Sequence.begin(), i_Sequence.end(), io_pArray );
155  return io_pArray;
156  }
157 
158 
181  template < typename DstElementType, typename SrcType >
182  inline css::uno::Sequence< DstElementType > containerToSequence( const SrcType& i_Container )
183  {
184  css::uno::Sequence< DstElementType > result( i_Container.size() );
185  ::std::copy( i_Container.begin(), i_Container.end(), result.getArray() );
186  return result;
187  }
188 
189  // this one does better type deduction, but does not allow us to copy into a different element type
190  template < typename SrcType >
191  inline css::uno::Sequence< typename SrcType::value_type > containerToSequence( const SrcType& i_Container )
192  {
193  css::uno::Sequence< typename SrcType::value_type > result( i_Container.size() );
194  ::std::copy( i_Container.begin(), i_Container.end(), result.getArray() );
195  return result;
196  }
197 
198  // handle arrays
199  template<typename ElementType, std::size_t SrcSize>
200  inline css::uno::Sequence< ElementType > containerToSequence( ElementType const (&i_Array)[ SrcSize ] )
201  {
202  return css::uno::Sequence< ElementType >( i_Array, SrcSize );
203  }
204 
205  template <typename T>
206  inline css::uno::Sequence<T> containerToSequence(
207  ::std::vector<T> const& v )
208  {
209  return css::uno::Sequence<T>(
210  v.data(), static_cast<sal_Int32>(v.size()) );
211  }
212 
213 
235  template < typename DstType, typename SrcType >
236  inline DstType sequenceToContainer( const css::uno::Sequence< SrcType >& i_Sequence )
237  {
238  return DstType(i_Sequence.begin(), i_Sequence.end());
239  }
240 
241  // this one does better type deduction, but does not allow us to copy into a different element type
242  template < typename DstType >
243  inline DstType sequenceToContainer( const css::uno::Sequence< typename DstType::value_type >& i_Sequence )
244  {
245  return DstType(i_Sequence.begin(), i_Sequence.end());
246  }
247 
278  template < typename DstType, typename SrcType >
279  inline DstType& sequenceToContainer( DstType& o_Output, const css::uno::Sequence< SrcType >& i_Sequence )
280  {
281  o_Output.resize( i_Sequence.getLength() );
282  ::std::copy( i_Sequence.begin(), i_Sequence.end(), o_Output.begin() );
283  return o_Output;
284  }
285 
292  template < typename M >
293  inline css::uno::Sequence< typename M::key_type > mapKeysToSequence( M const& map )
294  {
295  css::uno::Sequence< typename M::key_type > ret( static_cast<sal_Int32>(map.size()) );
296  typename M::key_type* pArray = ret.getArray();
297  for (const auto& i : map)
298  *pArray++ = i.first;
299  return ret;
300  }
301 
302  template < typename M >
303  inline css::uno::Sequence< typename M::mapped_type > mapValuesToSequence( M const& map )
304  {
305  css::uno::Sequence< typename M::mapped_type > ret( static_cast<sal_Int32>(map.size()) );
306  typename M::mapped_type* pArray = ret.getArray();
307  for (const auto& i : map)
308  *pArray++ = i.second;
309  return ret;
310  }
311 
312 } // namespace comphelper
313 
314 
315 #endif // INCLUDED_COMPHELPER_SEQUENCE_HXX
316 
317 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
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< DstType > arrayToSequence(const SrcType *i_pArray, sal_Int32 nNum)
Copy from a plain C/C++ array into a Sequence.
Definition: sequence.hxx:121
DstType sequenceToContainer(const css::uno::Sequence< SrcType > &i_Sequence)
Copy from a Sequence into a container.
Definition: sequence.hxx:236
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
MetadataImporterPluginType * result
int i
ElementType
css::uno::Sequence< T > concatSequences(const css::uno::Sequence< T > &rS1, const Ss &...rSn)
concat several sequences
Definition: sequence.hxx:49
void removeElementAt(css::uno::Sequence< T > &_rSeq, sal_Int32 _nPos)
remove a specified element from a sequences
Definition: sequence.hxx:87
css::uno::Sequence< typename M::mapped_type > mapValuesToSequence(M const &map)
Definition: sequence.hxx:303
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
Copy from a container into a Sequence.
Definition: sequence.hxx:182
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:152
sal_Int32 const nLength
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:293