LCOV - code coverage report
Current view: top level - include/tools - ref.hxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 82 88 93.2 %
Date: 2014-11-03 Functions: 408 461 88.5 %
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             : #ifndef INCLUDED_TOOLS_REF_HXX
      20             : #define INCLUDED_TOOLS_REF_HXX
      21             : 
      22             : #include <sal/config.h>
      23             : 
      24             : #include <cassert>
      25             : 
      26             : #include <tools/toolsdllapi.h>
      27             : #include <vector>
      28             : 
      29             : /**
      30             :    This implements similar functionality to boost::intrusive_ptr
      31             : */
      32             : 
      33             : namespace tools {
      34             : 
      35             : /** T must be a class that extends SvRefBase */
      36             : template<typename T> class SvRef {
      37             : public:
      38     1514531 :     SvRef(): pObj(0) {}
      39             : 
      40      415868 :     SvRef(SvRef const & rObj): pObj(rObj.pObj)
      41             :     {
      42      415868 :         if (pObj != 0) pObj->AddNextRef();
      43      415868 :     }
      44             : 
      45     1524360 :     SvRef(T * pObjP): pObj(pObjP)
      46             :     {
      47     1524360 :         if (pObj != 0) pObj->AddFirstRef();
      48     1524360 :     }
      49             : 
      50     3396199 :     ~SvRef()
      51             :     {
      52     3396199 :         if (pObj != 0) pObj->ReleaseRef();
      53     3396199 :     }
      54             : 
      55       18620 :     void Clear()
      56             :     {
      57       18620 :         if (pObj != 0) {
      58       11180 :             T * pRefObj = pObj;
      59       11180 :             pObj = 0;
      60       11180 :             pRefObj->ReleaseRef();
      61             :         }
      62       18620 :     }
      63             : 
      64      870468 :     SvRef & operator =(SvRef const & rObj)
      65             :     {
      66      870468 :         if (rObj.pObj != 0) {
      67      782558 :             rObj.pObj->AddNextRef();
      68             :         }
      69      870468 :         T * pRefObj = pObj;
      70      870468 :         pObj = rObj.pObj;
      71      870468 :         if (pRefObj != 0) {
      72       92181 :             pRefObj->ReleaseRef();
      73             :         }
      74      870468 :         return *this;
      75             :     }
      76             : 
      77     5789761 :     bool Is()         const { return pObj != 0; }
      78             : 
      79      661122 :     T * operator &()  const { return pObj; }
      80             : 
      81    37329743 :     T * operator ->() const { assert(pObj != 0); return pObj; }
      82             : 
      83     1040734 :     T & operator *()  const { assert(pObj != 0); return *pObj; }
      84             : 
      85    26805774 :     operator T *()    const { return pObj; }
      86             : 
      87             : protected:
      88             :     T * pObj;
      89             : };
      90             : 
      91             : }
      92             : 
      93             : template<typename T>
      94        3771 : class SvRefMemberList : private std::vector<T>
      95             : {
      96             : private:
      97             :     typedef typename std::vector<T> base_t;
      98             : 
      99             : public:
     100             :     using base_t::size;
     101             :     using base_t::front;
     102             :     using base_t::back;
     103             :     using base_t::operator[];
     104             :     using base_t::begin;
     105             :     using base_t::end;
     106             :     using typename base_t::iterator;
     107             :     using typename base_t::const_iterator;
     108             :     using base_t::rbegin;
     109             :     using base_t::rend;
     110             :     using typename base_t::reverse_iterator;
     111             :     using base_t::empty;
     112             : 
     113        3771 :     inline ~SvRefMemberList() { clear(); }
     114        3787 :     inline void clear()
     115             :     {
     116       36622 :         for( typename base_t::const_iterator it = base_t::begin(); it != base_t::end(); ++it )
     117             :         {
     118       32835 :               T p = *it;
     119       32835 :               if( p )
     120       32835 :                   p->ReleaseRef();
     121             :         }
     122        3787 :         base_t::clear();
     123        3787 :     }
     124             : 
     125       32843 :     inline void push_back( T p )
     126             :     {
     127       32843 :         base_t::push_back( p );
     128       32843 :         p->AddFirstRef();
     129       32843 :     }
     130             : 
     131             :     inline void insert(const SvRefMemberList& rOther)
     132             :     {
     133             :        for( typename base_t::const_iterator it = rOther.begin(); it != rOther.end(); ++it )
     134             :        {
     135             :            push_back(*it);
     136             :        }
     137             :     }
     138             : 
     139           8 :     inline T pop_back()
     140             :     {
     141           8 :         T p = base_t::back();
     142           8 :         base_t::pop_back();
     143           8 :         if( p )
     144           8 :             p->ReleaseRef();
     145           8 :         return p;
     146             :     }
     147             : };
     148             : 
     149             : 
     150             : /** Classes that want to be referenced-counted via SvRef<T>, should extend this base class */
     151             : class TOOLS_DLLPUBLIC SvRefBase
     152             : {
     153             :     // the only reason this is not bool is because MSVC cannot handle mixed type bitfields
     154             :     unsigned int bNoDelete : 1;
     155             :     unsigned int nRefCount : 31;
     156             : 
     157             : protected:
     158             :     virtual         ~SvRefBase();
     159             : 
     160             : public:
     161     1552297 :                     SvRefBase() : bNoDelete(1), nRefCount(0) {}
     162             : 
     163       31614 :                     SvRefBase( const SvRefBase & /* rObj */ )  : bNoDelete(1), nRefCount(0) {}
     164             : 
     165       20520 :     SvRefBase &     operator = ( const SvRefBase & )
     166       20520 :                     { return *this; }
     167             : 
     168           0 :     void            RestoreNoDelete()
     169           0 :                     { bNoDelete = 1; }
     170             : 
     171     1135961 :     void            AddNextRef()
     172             :                     {
     173             :                         assert( nRefCount < (1 << 30) && "Do not add refs to dead objects" );
     174     1135961 :                         ++nRefCount;
     175     1135961 :                     }
     176             : 
     177     1954558 :     void            AddFirstRef()
     178             :                     {
     179             :                         assert( nRefCount < (1 << 30) && "Do not add refs to dead objects" );
     180     1954558 :                         if( bNoDelete )
     181     1190302 :                             bNoDelete = 0;
     182     1954558 :                         ++nRefCount;
     183     1954558 :                     }
     184             : 
     185     2879709 :     void            ReleaseRef()
     186             :                     {
     187             :                         assert( nRefCount >= 1);
     188     2879709 :                         if( --nRefCount == 0 && !bNoDelete)
     189             :                         {
     190             :                             // I'm not sure about the original purpose of this line, but right now
     191             :                             // it serves the purpose that anything that attempts to do an AddRef()
     192             :                             // after an object is deleted will trip an assert.
     193      981021 :                             nRefCount = 1 << 30;
     194      981021 :                             delete this;
     195             :                         }
     196     2879709 :                     }
     197             : 
     198       84860 :     unsigned int    GetRefCount() const
     199       84860 :                     { return nRefCount; }
     200             : };
     201             : 
     202             : template<typename T>
     203             : class SvCompatWeakBase;
     204             : 
     205             : /** SvCompatWeakHdl acts as a intermediary between SvCompatWeakRef<T> and T.
     206             : */
     207             : template<typename T>
     208       11012 : class SvCompatWeakHdl : public SvRefBase
     209             : {
     210             :     friend class SvCompatWeakBase<T>;
     211             :     T* _pObj;
     212             : 
     213        5508 :     SvCompatWeakHdl( T* pObj ) : _pObj( pObj ) {}
     214             : 
     215             : public:
     216        5506 :     void  ResetWeakBase( ) { _pObj = 0; }
     217        5267 :     T*    GetObj()        { return _pObj; }
     218             : };
     219             : 
     220             : /** We only have one place that extends this, in include/sfx2/frame.hxx, class SfxFrame.
     221             :     Its function is to notify the SvCompatWeakHdl when an SfxFrame object is deleted.
     222             : */
     223             : template<typename T>
     224             : class SvCompatWeakBase
     225             : {
     226             :     tools::SvRef< SvCompatWeakHdl<T> > _xHdl;
     227             : 
     228             : public:
     229             :     /** Does not use initializer due to compiler warnings,
     230             :         because the lifetime of the _xHdl object can exceed the lifetime of this class.
     231             :      */
     232        5508 :     SvCompatWeakBase( T* pObj ) { _xHdl = new SvCompatWeakHdl<T>( pObj ); }
     233             : 
     234        5506 :     ~SvCompatWeakBase() { _xHdl->ResetWeakBase(); }
     235             : 
     236       10775 :     SvCompatWeakHdl<T>* GetHdl() { return _xHdl; }
     237             : };
     238             : 
     239             : /** We only have one weak reference in LO, in include/sfx2/frame.hxx, class SfxFrameWeak.
     240             : */
     241             : template<typename T>
     242       24562 : class SvCompatWeakRef
     243             : {
     244             :     tools::SvRef< SvCompatWeakHdl<T> > _xHdl;
     245             : public:
     246       19300 :     inline               SvCompatWeakRef( ) {}
     247        5267 :     inline               SvCompatWeakRef( T* pObj )
     248        5267 :                          {  if( pObj ) _xHdl = pObj->GetHdl(); }
     249        5508 :     inline SvCompatWeakRef& operator = ( T * pObj )
     250        5508 :                          {  _xHdl = pObj ? pObj->GetHdl() : 0; return *this; }
     251        5267 :     inline bool          Is() const
     252        5267 :                          { return _xHdl.Is() && _xHdl->GetObj(); }
     253           0 :     inline T*            operator -> () const
     254           0 :                          { return _xHdl.Is() ? _xHdl->GetObj() : 0; }
     255             :     inline T*            operator &  () const
     256             :                          { return _xHdl.Is() ? _xHdl->GetObj() : 0; }
     257           0 :     inline operator T* () const
     258           0 :                          { return _xHdl.Is() ? _xHdl->GetObj() : 0; }
     259             : };
     260             : 
     261             : #endif
     262             : 
     263             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10