LCOV - code coverage report
Current view: top level - pg/include/mcrl2/pg - RefCounted.h (source / functions) Hit Total Coverage
Test: mcrl2_coverage.info.cleaned Lines: 0 8 0.0 %
Date: 2024-04-19 03:43:27 Functions: 0 5 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2009-2013 University of Twente
       2             : // Copyright (c) 2009-2013 Michael Weber <michaelw@cs.utwente.nl>
       3             : // Copyright (c) 2009-2013 Maks Verver <maksverver@geocities.com>
       4             : // Copyright (c) 2009-2013 Eindhoven University of Technology
       5             : //
       6             : // Distributed under the Boost Software License, Version 1.0.
       7             : // (See accompanying file LICENSE_1_0.txt or copy at
       8             : // http://www.boost.org/LICENSE_1_0.txt)
       9             : 
      10             : #ifndef MCRL2_PG_REFCOUNTED_H
      11             : #define MCRL2_PG_REFCOUNTED_H
      12             : 
      13             : #include <cassert>
      14             : #include <cstdio>
      15             : 
      16             : /*! A simple reference counting base class.
      17             : 
      18             :     Instances of this class start with an initial reference count of 1 (by
      19             :     default), which can be increased or decreased by calling the ref() and
      20             :     deref() methods.  When the reference count becomes zero, the object
      21             :     is deleted and should not be used anymore.
      22             : 
      23             :     It is allowed to delete an object directly (without calling deref())
      24             :     provided the caller has the only reference to the object.  In effect, this
      25             :     is the same as calling deref(), but supports use cases like putting
      26             :     instances into std::auto_ptr wrappers.
      27             : */
      28             : class RefCounted
      29             : {
      30             : public:
      31             :     //! Construct and initialize the reference count to `init_refcount`.
      32           0 :     RefCounted(std::size_t init_refcount = 0)
      33           0 :         : refs_(init_refcount) { }
      34             : 
      35             :     //! Increment reference count.
      36           0 :     void ref() const { ++refs_; }
      37             : 
      38             :     //! Decrement reference count and delete the object if it becomes zero.
      39           0 :     void deref() const
      40             :     {
      41           0 :         assert(refs_ > 0);
      42           0 :         if (--refs_ == 0) delete this;
      43           0 :     }
      44             : 
      45             :     //! Return the current reference count. Mostly useful for debugging.
      46             :     std::size_t refcount() { return refs_; }
      47             : 
      48             : protected:
      49           0 :     virtual ~RefCounted() { assert(refs_ <= 1); }
      50             : 
      51             : protected:
      52             :     mutable std::size_t refs_;  //!< Number of references to this object
      53             : };
      54             : 
      55             : #endif /* ndef MCRL2_PG_REFCOUNTED_H */

Generated by: LCOV version 1.14