LCOV - code coverage report
Current view: top level - include/oox/helper - refvector.hxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 34 37 91.9 %
Date: 2014-04-11 Functions: 180 230 78.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 INCLUDED_OOX_HELPER_REFVECTOR_HXX
      21             : #define INCLUDED_OOX_HELPER_REFVECTOR_HXX
      22             : 
      23             : #include <vector>
      24             : #include <boost/bind.hpp>
      25             : #include <boost/shared_ptr.hpp>
      26             : #include <sal/types.h>
      27             : 
      28             : namespace oox {
      29             : 
      30             : 
      31             : 
      32             : /** Template for a vector of ref-counted objects with additional accessor functions.
      33             : 
      34             :     An instance of the class RefVector< Type > stores elements of the type
      35             :     ::boost::shared_ptr< Type >. The new accessor functions has() and get()
      36             :     work correctly for indexes out of the current range, there is no need to
      37             :     check the passed index before.
      38             :  */
      39             : template< typename ObjType >
      40       68462 : class RefVector : public ::std::vector< ::boost::shared_ptr< ObjType > >
      41             : {
      42             : public:
      43             :     typedef ::std::vector< ::boost::shared_ptr< ObjType > > container_type;
      44             :     typedef typename container_type::value_type             value_type;
      45             :     typedef typename container_type::size_type              size_type;
      46             : 
      47             : public:
      48             :     /** Returns true, if the object with the passed index exists. Returns
      49             :         false, if the vector element exists but is an empty reference. */
      50             :     bool                has( sal_Int32 nIndex ) const
      51             :                         {
      52             :                             const value_type* pxRef = getRef( nIndex );
      53             :                             return pxRef && pxRef->get();
      54             :                         }
      55             : 
      56             :     /** Returns a reference to the object with the passed index, or 0 on error. */
      57        5924 :     value_type          get( sal_Int32 nIndex ) const
      58             :                         {
      59        5924 :                             if( const value_type* pxRef = getRef( nIndex ) ) return *pxRef;
      60         106 :                             return value_type();
      61             :                         }
      62             : 
      63             :     /** Returns the index of the last element, or -1, if the vector is empty.
      64             :         Does *not* check whether the last element is an empty reference. */
      65             :     sal_Int32           getLastIndex() const { return static_cast< sal_Int32 >( this->size() ) - 1; }
      66             : 
      67             :     /** Calls the passed functor for every contained object, automatically
      68             :         skips all elements that are empty references. */
      69             :     template< typename FunctorType >
      70        1047 :     void                forEach( FunctorType aFunctor ) const
      71             :                         {
      72        1047 :                             ::std::for_each( this->begin(), this->end(), ForEachFunctor< FunctorType >( aFunctor ) );
      73        1047 :                         }
      74             : 
      75             :     /** Calls the passed member function of ObjType on every contained object,
      76             :         automatically skips all elements that are empty references. */
      77             :     template< typename FuncType >
      78         999 :     void                forEachMem( FuncType pFunc ) const
      79             :                         {
      80         999 :                             forEach( ::boost::bind( pFunc, _1 ) );
      81         999 :                         }
      82             : 
      83             :     /** Calls the passed member function of ObjType on every contained object,
      84             :         automatically skips all elements that are empty references. */
      85             :     template< typename FuncType, typename ParamType >
      86          44 :     void                forEachMem( FuncType pFunc, ParamType aParam ) const
      87             :                         {
      88          44 :                             forEach( ::boost::bind( pFunc, _1, aParam ) );
      89          44 :                         }
      90             : 
      91             :     /** Calls the passed member function of ObjType on every contained object,
      92             :         automatically skips all elements that are empty references. */
      93             :     template< typename FuncType, typename ParamType1, typename ParamType2 >
      94           0 :     void                forEachMem( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2 ) const
      95             :                         {
      96           0 :                             forEach( ::boost::bind( pFunc, _1, aParam1, aParam2 ) );
      97           0 :                         }
      98             : 
      99             :     /** Calls the passed member function of ObjType on every contained object,
     100             :         automatically skips all elements that are empty references. */
     101             :     template< typename FuncType, typename ParamType1, typename ParamType2, typename ParamType3 >
     102           2 :     void                forEachMem( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2, ParamType3 aParam3 ) const
     103             :                         {
     104           2 :                             forEach( ::boost::bind( pFunc, _1, aParam1, aParam2, aParam3 ) );
     105           2 :                         }
     106             : 
     107             :     /** Calls the passed functor for every contained object. Passes the index as
     108             :         first argument and the object reference as second argument to rFunctor. */
     109             :     template< typename FunctorType >
     110           2 :     void                forEachWithIndex( const FunctorType& rFunctor ) const
     111             :                         {
     112           2 :                             ::std::for_each( this->begin(), this->end(), ForEachFunctorWithIndex< FunctorType >( rFunctor ) );
     113           2 :                         }
     114             : 
     115             :     /** Calls the passed member function of ObjType on every contained object.
     116             :         Passes the vector index to the member function. */
     117             :     template< typename FuncType >
     118             :     void                forEachMemWithIndex( FuncType pFunc ) const
     119             :                         {
     120             :                             forEachWithIndex( ::boost::bind( pFunc, _2, _1 ) );
     121             :                         }
     122             : 
     123             :     /** Calls the passed member function of ObjType on every contained object.
     124             :         Passes the vector index as first argument to the member function. */
     125             :     template< typename FuncType, typename ParamType >
     126             :     void                forEachMemWithIndex( FuncType pFunc, ParamType aParam ) const
     127             :                         {
     128             :                             forEachWithIndex( ::boost::bind( pFunc, _2, _1, aParam ) );
     129             :                         }
     130             : 
     131             :     /** Calls the passed member function of ObjType on every contained object.
     132             :         Passes the vector index as first argument to the member function. */
     133             :     template< typename FuncType, typename ParamType1, typename ParamType2 >
     134           2 :     void                forEachMemWithIndex( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2 ) const
     135             :                         {
     136           2 :                             forEachWithIndex( ::boost::bind( pFunc, _2, _1, aParam1, aParam2 ) );
     137           2 :                         }
     138             : 
     139             :     /** Calls the passed member function of ObjType on every contained object.
     140             :         Passes the vector index as first argument to the member function. */
     141             :     template< typename FuncType, typename ParamType1, typename ParamType2, typename ParamType3 >
     142             :     void                forEachMemWithIndex( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2, ParamType3 aParam3 ) const
     143             :                         {
     144             :                             forEachWithIndex( ::boost::bind( pFunc, _2, _1, aParam1, aParam2, aParam3 ) );
     145             :                         }
     146             : 
     147             :     /** Searches for an element by using the passed functor that takes a
     148             :         constant reference of the object type (const ObjType&). */
     149             :     template< typename FunctorType >
     150           1 :     value_type          findIf( const FunctorType& rFunctor ) const
     151             :                         {
     152           1 :                             typename container_type::const_iterator aIt = ::std::find_if( this->begin(), this->end(), FindFunctor< FunctorType >( rFunctor ) );
     153           1 :                             return (aIt == this->end()) ? value_type() : *aIt;
     154             :                         }
     155             : 
     156             : private:
     157             :     template< typename FunctorType >
     158             :     struct ForEachFunctor
     159             :     {
     160             :         FunctorType         maFunctor;
     161        1047 :         explicit            ForEachFunctor( const FunctorType& rFunctor ) : maFunctor( rFunctor ) {}
     162        2078 :         void                operator()( const value_type& rxValue ) { if( rxValue.get() ) maFunctor( *rxValue ); }
     163             :     };
     164             : 
     165             :     template< typename FunctorType >
     166             :     struct ForEachFunctorWithIndex
     167             :     {
     168             :         FunctorType         maFunctor;
     169             :         sal_Int32           mnIndex;
     170           2 :         explicit            ForEachFunctorWithIndex( const FunctorType& rFunctor ) : maFunctor( rFunctor ), mnIndex( 0 ) {}
     171           6 :         void                operator()( const value_type& rxValue ) { if( rxValue.get() ) maFunctor( mnIndex, *rxValue ); ++mnIndex; }
     172             :     };
     173             : 
     174             :     template< typename FunctorType >
     175             :     struct FindFunctor
     176             :     {
     177             :         FunctorType         maFunctor;
     178           1 :         explicit            FindFunctor( const FunctorType& rFunctor ) : maFunctor( rFunctor ) {}
     179           1 :         bool                operator()( const value_type& rxValue ) { return rxValue.get() && maFunctor( *rxValue ); }
     180             :     };
     181             : 
     182        5924 :     const value_type*   getRef( sal_Int32 nIndex ) const
     183             :                         {
     184        5818 :                             return ((0 <= nIndex) && (static_cast< size_type >( nIndex ) < this->size())) ?
     185       11742 :                                 &(*this)[ static_cast< size_type >( nIndex ) ] : 0;
     186             :                         }
     187             : };
     188             : 
     189             : 
     190             : 
     191             : } // namespace oox
     192             : 
     193             : #endif
     194             : 
     195             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10