LCOV - code coverage report
Current view: top level - include/salhelper - simplereferenceobject.hxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 5 5 100.0 %
Date: 2014-11-03 Functions: 3 3 100.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_SALHELPER_SIMPLEREFERENCEOBJECT_HXX
      21             : #define INCLUDED_SALHELPER_SIMPLEREFERENCEOBJECT_HXX
      22             : 
      23             : #include <osl/interlck.h>
      24             : #include <sal/types.h>
      25             : #include <salhelper/salhelperdllapi.h>
      26             : 
      27             : #include <cstddef>
      28             : #include <new>
      29             : 
      30             : namespace salhelper {
      31             : 
      32             : /** A simple base implementation for reference-counted objects.
      33             : 
      34             :     Classes that want to implement a reference-counting mechanism based on the
      35             :     acquire()/release() interface should derive from this class.
      36             : 
      37             :     The reason to have class local operators new and delete here is technical.
      38             :     Imagine a class D derived from SimpleReferenceObject, but implemented in
      39             :     another shared library that happens to use different global operators new
      40             :     and delete from those used in this shared library (which, sadly, seems to
      41             :     be possible with shared libraries).  Now, without the class local
      42             :     operators new and delete here, a code sequence like "new D" would use the
      43             :     global operator new as found in the other shared library, while the code
      44             :     sequence "delete this" in release() would use the global operator delete
      45             :     as found in this shared library---and these two operators would not be
      46             :     guaranteed to match.
      47             : 
      48             :     There are no overloaded operators new and delete for placement new here,
      49             :     because it is felt that the concept of placement new does not work well
      50             :     with the concept of reference-counted objects; so it seems best to simply
      51             :     leave those operators out.
      52             : 
      53             :     The same problem as with operators new and delete would also be there with
      54             :     operators new[] and delete[].  But since arrays of reference-counted
      55             :     objects are of no use, anyway, it seems best to simply declare and not
      56             :     define (private) operators new[] and delete[].
      57             :  */
      58             : class SALHELPER_DLLPUBLIC SimpleReferenceObject
      59             : {
      60             : public:
      61    21167219 :     inline SimpleReferenceObject(): m_nCount(0) {}
      62             : 
      63             :     /** @attention
      64             :         The results are undefined if, for any individual instance of
      65             :         SimpleReferenceObject, the total number of calls to acquire() exceeds
      66             :         the total number of calls to release() by a platform dependent amount
      67             :         (which, hopefully, is quite large).
      68             :      */
      69   215415365 :     inline void acquire()
      70   215415365 :     { osl_atomic_increment(&m_nCount); }
      71             : 
      72   215272825 :     inline void release()
      73   215272825 :     { if (osl_atomic_decrement(&m_nCount) == 0) delete this; }
      74             : 
      75             :     /** see general class documentation
      76             :      */
      77             :     static void * operator new(std::size_t nSize);
      78             : 
      79             :     /** see general class documentation
      80             :      */
      81             :     static void * operator new(std::size_t nSize,
      82             :                                std::nothrow_t const & rNothrow);
      83             : 
      84             :     /** see general class documentation
      85             :      */
      86             :     static void operator delete(void * pPtr);
      87             : 
      88             :     /** see general class documentation
      89             :      */
      90             :     static void operator delete(void * pPtr, std::nothrow_t const & rNothrow);
      91             : 
      92             : protected:
      93             :     virtual ~SimpleReferenceObject();
      94             : 
      95             : private:
      96             :     oslInterlockedCount m_nCount;
      97             : 
      98             :     /** not implemented
      99             :      */
     100             :     SALHELPER_DLLPRIVATE SimpleReferenceObject(SimpleReferenceObject &);
     101             : 
     102             :     /** not implemented
     103             :      */
     104             :     SALHELPER_DLLPRIVATE void operator =(SimpleReferenceObject);
     105             : 
     106             :     /// @cond INTERNAL
     107             : 
     108             : #ifdef _MSC_VER
     109             : /* We can't now have these private with MSVC2008 at least, it leads to
     110             :    compilation errors in xmloff and other places.
     111             : */
     112             : protected:
     113             : #endif
     114             :     /** not implemented (see general class documentation)
     115             :      */
     116             :     static void * operator new[](std::size_t);
     117             : 
     118             :     /** not implemented (see general class documentation)
     119             :      */
     120             :     static void operator delete[](void * pPtr);
     121             : 
     122             :     /// @endcond
     123             : };
     124             : 
     125             : }
     126             : 
     127             : #endif // INCLUDED_SALHELPER_SIMPLEREFERENCEOBJECT_HXX
     128             : 
     129             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10