LCOV - code coverage report
Current view: top level - include/oox/helper - refmap.hxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 33 33 100.0 %
Date: 2015-06-13 12:38:46 Functions: 117 136 86.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_OOX_HELPER_REFMAP_HXX
      21             : #define INCLUDED_OOX_HELPER_REFMAP_HXX
      22             : 
      23             : #include <sal/types.h>
      24             : #include <algorithm>
      25             : #include <map>
      26             : #include <memory>
      27             : #include <boost/bind.hpp>
      28             : 
      29             : namespace oox {
      30             : 
      31             : 
      32             : 
      33             : /** Template for a map of ref-counted objects with additional accessor functions.
      34             : 
      35             :     An instance of the class RefMap< Type > stores elements of the type
      36             :     std::shared_ptr< Type >. The new accessor functions has() and get()
      37             :     work correctly for nonexisting keys, there is no need to check the passed
      38             :     key before.
      39             :  */
      40             : template< typename KeyType, typename ObjType, typename CompType = std::less< KeyType > >
      41       75082 : class RefMap : public std::map< KeyType, std::shared_ptr< ObjType >, CompType >
      42             : {
      43             : public:
      44             :     typedef std::map< KeyType, std::shared_ptr< ObjType >, CompType > container_type;
      45             :     typedef typename container_type::key_type                               key_type;
      46             :     typedef typename container_type::mapped_type                            mapped_type;
      47             :     typedef typename container_type::value_type                             value_type;
      48             :     typedef typename container_type::key_compare                            key_compare;
      49             : 
      50             : public:
      51             :     /** Returns true, if the object associated to the passed key exists.
      52             :         Returns false, if the key exists but points to an empty reference. */
      53         236 :     bool                has( key_type nKey ) const
      54             :                         {
      55         236 :                             const mapped_type* pxRef = getRef( nKey );
      56         236 :                             return pxRef && pxRef->get();
      57             :                         }
      58             : 
      59             :     /** Returns a reference to the object associated to the passed key, or an
      60             :         empty reference on error. */
      61       15040 :     mapped_type         get( key_type nKey ) const
      62             :                         {
      63       15040 :                             if( const mapped_type* pxRef = getRef( nKey ) ) return *pxRef;
      64        1081 :                             return mapped_type();
      65             :                         }
      66             : 
      67             :     /** Calls the passed functor for every contained object, automatically
      68             :         skips all elements that are empty references. */
      69             :     template< typename FunctorType >
      70        3554 :     void                forEach( const FunctorType& rFunctor ) const
      71             :                         {
      72        3554 :                             std::for_each( this->begin(), this->end(), ForEachFunctor< FunctorType >( rFunctor ) );
      73        3554 :                         }
      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        3345 :     void                forEachMem( FuncType pFunc ) const
      79             :                         {
      80        3345 :                             forEach( ::boost::bind( pFunc, _1 ) );
      81        3345 :                         }
      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             :     void                forEachMem( FuncType pFunc, ParamType aParam ) const
      87             :                         {
      88             :                             forEach( ::boost::bind( pFunc, _1, aParam ) );
      89             :                         }
      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          32 :     void                forEachMem( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2 ) const
      95             :                         {
      96          32 :                             forEach( ::boost::bind( pFunc, _1, aParam1, aParam2 ) );
      97          32 :                         }
      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          32 :     void                forEachMem( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2, ParamType3 aParam3 ) const
     103             :                         {
     104          32 :                             forEach( ::boost::bind( pFunc, _1, aParam1, aParam2, aParam3 ) );
     105          32 :                         }
     106             : 
     107             :     /** Calls the passed member function of ObjType on every contained object,
     108             :         automatically skips all elements that are empty references. */
     109             :     template< typename FuncType, typename ParamType1, typename ParamType2, typename ParamType3, typename ParamType4 >
     110             :     void                forEachMem( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2, ParamType3 aParam3, ParamType4 aParam4 ) const
     111             :                         {
     112             :                             forEach( ::boost::bind( pFunc, _1, aParam1, aParam2, aParam3, aParam4 ) );
     113             :                         }
     114             : 
     115             : 
     116             :     /** Calls the passed functor for every contained object. Passes the key as
     117             :         first argument and the object reference as second argument to rFunctor. */
     118             :     template< typename FunctorType >
     119         145 :     void                forEachWithKey( const FunctorType& rFunctor ) const
     120             :                         {
     121         145 :                             std::for_each( this->begin(), this->end(), ForEachFunctorWithKey< FunctorType >( rFunctor ) );
     122         145 :                         }
     123             : 
     124             :     /** Calls the passed member function of ObjType on every contained object.
     125             :         Passes the object key as argument to the member function. */
     126             :     template< typename FuncType >
     127         145 :     void                forEachMemWithKey( FuncType pFunc ) const
     128             :                         {
     129         145 :                             forEachWithKey( ::boost::bind( pFunc, _2, _1 ) );
     130         145 :                         }
     131             : 
     132             :     /** Calls the passed member function of ObjType on every contained object.
     133             :         Passes the object key as first argument to the member function. */
     134             :     template< typename FuncType, typename ParamType >
     135             :     void                forEachMemWithKey( FuncType pFunc, ParamType aParam ) const
     136             :                         {
     137             :                             forEachWithKey( ::boost::bind( pFunc, _2, _1, aParam ) );
     138             :                         }
     139             : 
     140             :     /** Calls the passed member function of ObjType on every contained object.
     141             :         Passes the object key as first argument to the member function. */
     142             :     template< typename FuncType, typename ParamType1, typename ParamType2 >
     143             :     void                forEachMemWithKey( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2 ) const
     144             :                         {
     145             :                             forEachWithKey( ::boost::bind( pFunc, _2, _1, aParam1, aParam2 ) );
     146             :                         }
     147             : 
     148             :     /** Calls the passed member function of ObjType on every contained object.
     149             :         Passes the object key as first argument to the member function. */
     150             :     template< typename FuncType, typename ParamType1, typename ParamType2, typename ParamType3 >
     151             :     void                forEachMemWithKey( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2, ParamType3 aParam3 ) const
     152             :                         {
     153             :                             forEachWithKey( ::boost::bind( pFunc, _2, _1, aParam1, aParam2, aParam3 ) );
     154             :                         }
     155             : 
     156             : private:
     157             :     template< typename FunctorType >
     158         435 :     struct ForEachFunctor
     159             :     {
     160             :         FunctorType         maFunctor;
     161        3554 :         explicit     ForEachFunctor( const FunctorType& rFunctor ) : maFunctor( rFunctor ) {}
     162       14364 :         void         operator()( const value_type& rValue ) { if( rValue.second.get() ) maFunctor( *rValue.second ); }
     163             :     };
     164             : 
     165             :     template< typename FunctorType >
     166             :     struct ForEachFunctorWithKey
     167             :     {
     168             :         FunctorType         maFunctor;
     169         145 :         explicit     ForEachFunctorWithKey( const FunctorType& rFunctor ) : maFunctor( rFunctor ) {}
     170        1032 :         void         operator()( const value_type& rValue ) { if( rValue.second.get() ) maFunctor( rValue.first, *rValue.second ); }
     171             :     };
     172             : 
     173       15276 :     const mapped_type* getRef( key_type nKey ) const
     174             :     {
     175       15276 :         typename container_type::const_iterator aIt = this->find( nKey );
     176       15276 :         return (aIt == this->end()) ? 0 : &aIt->second;
     177             :     }
     178             : };
     179             : 
     180             : 
     181             : 
     182             : } // namespace oox
     183             : 
     184             : #endif
     185             : 
     186             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11