LCOV - code coverage report
Current view: top level - libreoffice/solver/unxlngi6.pro/inc/comphelper - sequenceasvector.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 43 44 97.7 %
Date: 2012-12-27 Functions: 56 105 53.3 %
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 _COMPHELPER_SEQUENCEASVECTOR_HXX_
      21             : #define _COMPHELPER_SEQUENCEASVECTOR_HXX_
      22             : 
      23             : #include <vector>
      24             : #include <algorithm>
      25             : #include <com/sun/star/uno/Sequence.hxx>
      26             : 
      27             : #include <com/sun/star/beans/IllegalTypeException.hpp>
      28             : 
      29             : 
      30             : namespace comphelper{
      31             : 
      32             : 
      33             : /** @short  Implements a stl vector on top of any
      34             :             uno sequence.
      35             : 
      36             :     @descr  That provides the possibility to modify
      37             :             sequences very easy ...
      38             :             Of course this can be usefull only, if
      39             :             count of modifications is high, so copying
      40             :             of the sequence make sense!
      41             :  */
      42             : template< class TElementType >
      43       11420 : class SequenceAsVector : public ::std::vector< TElementType >
      44             : {
      45             :     //-------------------------------------------
      46             :     // types
      47             : 
      48             :     public:
      49             : 
      50             :         //---------------------------------------
      51             :         /** @short  When inheriting from a template using typename is generally required when using
      52             :                     types from the base! */
      53             :         typedef typename ::std::vector< TElementType >::const_iterator const_iterator;
      54             : 
      55             :         //---------------------------------------
      56             :         /** @short  When inheriting from a template using typename is generally required when using
      57             :                     types from the base! */
      58             :         typedef typename ::std::vector< TElementType >::iterator iterator;
      59             : 
      60             :     //-------------------------------------------
      61             :     // interface
      62             :     public:
      63             : 
      64             :         //---------------------------------------
      65             :         /** @short  default ctor, to create an empty list.
      66             :          */
      67       99748 :         SequenceAsVector()
      68       99748 :         {}
      69             : 
      70             :         //---------------------------------------
      71             :         /** @short  default dtor
      72             :          */
      73      150470 :         ~SequenceAsVector()
      74      150470 :         {}
      75             : 
      76             :         //---------------------------------------
      77             :         /** @short  creates a new vector with the given length.
      78             : 
      79             :             @param  nLength
      80             :                     the number of elements for the new vector.
      81             :          */
      82           1 :         explicit SequenceAsVector(sal_Int32 nLength) :
      83           1 :             ::std::vector< TElementType >( static_cast< size_t >( nLength ) )
      84             :         {
      85           1 :         }
      86             : 
      87             :         //---------------------------------------
      88             :         /** @short  creates a new deque from the given uno sequence.
      89             : 
      90             :             @param  lSource
      91             :                     contains the new items for this deque.
      92             :          */
      93       43439 :         SequenceAsVector(const ::com::sun::star::uno::Sequence< TElementType >& lSource)
      94       43439 :         {
      95       43439 :             (*this) << lSource;
      96       43439 :         }
      97             : 
      98             :         //---------------------------------------
      99             :         /** @short      creates a new instance from the given Any, which
     100             :                         of course must contain a valid sequence using the
     101             :                         right element type for every item.
     102             : 
     103             :             @attention  If the given Any is an empty one
     104             :                         (if its set to VOID), no exception
     105             :                         is thrown. In such case this instance will
     106             :                         be created as an empty one too!
     107             : 
     108             :             @param      aSource
     109             :                         this any must contain a suitable sequence. :-)
     110             : 
     111             :             @throw      A <type scope="com::sun::star::beans">IllegalTypeException</type>
     112             :                         if an unsupported element inside this Any
     113             :                         is given. An empty Any reset this instance!
     114             :          */
     115        7411 :         SequenceAsVector(const ::com::sun::star::uno::Any& aSource)
     116        7411 :         {
     117        7411 :             (*this) << aSource;
     118        7411 :         }
     119             : 
     120             :         //---------------------------------------
     121             :         /** @short  fill this instance from the given uno sequence.
     122             : 
     123             :             @param  lSource
     124             :                     contains the new items for this deque.
     125             :          */
     126       51185 :         void operator<<(const ::com::sun::star::uno::Sequence< TElementType >& lSource)
     127             :         {
     128       51185 :             this->clear();
     129             : 
     130       51185 :                   sal_Int32     c       = lSource.getLength();
     131       51185 :             const TElementType* pSource = lSource.getConstArray();
     132             : 
     133      622371 :             for (sal_Int32 i=0; i<c; ++i)
     134      571186 :                 this->push_back(pSource[i]);
     135       51185 :         }
     136             : 
     137             :         //---------------------------------------
     138             :         /** @short      fill this instance from the given Any, which
     139             :                         of course must contain a valid sequence using the
     140             :                         right element type for every item.
     141             : 
     142             :             @attention  If the given Any is an empty one
     143             :                         (if its set to VOID), no exception
     144             :                         is thrown. In such case this instance will
     145             :                         be created as an empty one too!
     146             : 
     147             :             @param      aSource
     148             :                         this any must contain a suitable sequence. :-)
     149             : 
     150             :             @throw      A <type scope="com::sun::star::beans">IllegalTypeException</type>
     151             :                         if an unsupported element inside this Any
     152             :                         is given. An empty Any reset this instance!
     153             :          */
     154        7867 :         void operator<<(const ::com::sun::star::uno::Any& aSource)
     155             :         {
     156             :             // An empty Any reset this instance!
     157        7867 :             if (!aSource.hasValue())
     158             :             {
     159         710 :                 this->clear();
     160        7867 :                 return;
     161             :             }
     162             : 
     163        7157 :             ::com::sun::star::uno::Sequence< TElementType > lSource;
     164        7157 :             if (!(aSource >>= lSource))
     165             :                 throw ::com::sun::star::beans::IllegalTypeException(
     166             :                         ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("SequenceAsVector operator<<(Any) was called with an unsupported Any type.")),
     167           0 :                         ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >());
     168             : 
     169        7157 :             (*this) << lSource;
     170             :         }
     171             : 
     172             :         //---------------------------------------
     173             :         /** @short  converts this instance to an uno sequence.
     174             : 
     175             :             @param  lDestination
     176             :                     target sequence for converting.
     177             :          */
     178       35337 :         void operator>>(::com::sun::star::uno::Sequence< TElementType >& lDestination) const
     179             :         {
     180       35337 :             sal_Int32 c = (sal_Int32)this->size();
     181       35337 :             lDestination.realloc(c);
     182       35337 :             TElementType* pDestination = lDestination.getArray();
     183             : 
     184       35337 :             sal_Int32 i = 0;
     185      164640 :             for (typename std::vector<TElementType>::const_iterator pThis  = this->begin();
     186             :                                                                     pThis != this->end()  ;
     187             :                                                                     ++pThis           )
     188             :             {
     189      129303 :                 pDestination[i] = *pThis;
     190      129303 :                 ++i;
     191             :             }
     192       35337 :         }
     193             : 
     194             :         //---------------------------------------
     195             :         /** @short  converts this instance to an uno any
     196             :                     which contains a suitable sequence
     197             :                     of items of this stl struct.
     198             : 
     199             :             @param  aDestination
     200             :                     target any for converting.
     201             :          */
     202             :         void operator>>(::com::sun::star::uno::Any& aDestination) const
     203             :         {
     204             :             sal_Int32                                       c            = (sal_Int32)this->size();
     205             :             ::com::sun::star::uno::Sequence< TElementType > lDestination(c);
     206             :             TElementType*                                   pDestination = lDestination.getArray();
     207             : 
     208             :             sal_Int32 i = 0;
     209             :             for (typename std::vector<TElementType>::const_iterator pThis  = this->begin();
     210             :                                                                     pThis != this->end()  ;
     211             :                                                                     ++pThis           )
     212             :             {
     213             :                 pDestination[i] = *pThis;
     214             :                 ++i;
     215             :             }
     216             : 
     217             :             aDestination <<= lDestination;
     218             :         }
     219             : 
     220             :         //---------------------------------------
     221             :         /** @short      converts this deque to a suitable uno
     222             :                         sequence which contains all items.
     223             : 
     224             :             @attention  It return a const sequence to prevent
     225             :                         the outside code against using of this
     226             :                         return value as [in/]out parameter for
     227             :                         direct function calls!
     228             :                         Of course it can be casted to non const
     229             :                         ... but then its a problem of the outside
     230             :                         code :-)
     231             : 
     232             :             @return     A (const!) sequence, which contains all items of
     233             :                         this deque.
     234             :          */
     235       28214 :         const ::com::sun::star::uno::Sequence< TElementType > getAsConstList() const
     236             :         {
     237       28214 :             ::com::sun::star::uno::Sequence< TElementType > lDestination;
     238       28214 :             (*this) >> lDestination;
     239       28214 :             return lDestination;
     240             :         }
     241             : };
     242             : 
     243             : } // namespace comphelper
     244             : 
     245             : #endif // _COMPHELPER_SEQUENCEASVECTOR_HXX_
     246             : 
     247             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10