LCOV - code coverage report
Current view: top level - include/comphelper - sequence.hxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 72 0.0 %
Date: 2014-04-14 Functions: 0 86 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10