test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Refcount.h
Go to the documentation of this file.
1 //
2 //
3 // File: Refcount.h
4 // Purpose: A base class for a simple reference-counted object.
5 // Created: Aug 2000, sss, from the version that used to be in d0om.
6 //
7 // To make a reference counted type, derive from class d0_util::Refcount.
8 // When the object is created, its refcount is initially set to 0;
9 // the first action taken by the creator should be to call incref()
10 // to bump the refcount to 1. Thereafter, the refcount may be incremented
11 // by incref() and decremented by decref(). If the reference count reaches
12 // 0, the object calls delete on itself.
13 //
14 // If the object is deleted explicitly, the reference count must be 0 or 1.
15 // Otherwise, an assertion violation will be reported.
16 //
17 // CMSSW File : interface/Refcount.h
18 // Original Author : Scott Stuart Snyder <snyder@bnl.gov> for D0
19 // Imported to CMSSW by Haryo Sumowidagdo <Suharyo.Sumowidagdo@cern.ch>
20 //
21 
22 
45 #ifndef D0_UTIL_REFCOUNT_H
46 #define D0_UTIL_REFCOUNT_H
47 
48 #include<stdexcept>
49 #include<sstream>
50 #include<cassert>
51 namespace hitfit {
52 
53 
67 class Refcount
68 //
69 // Purpose: Simple reference-counted object.
70 //
71 {
72 public:
73  // Constructor, destructor.
74 
78  Refcount ();
79 
84  virtual ~Refcount ();
85 
86  // Increment and decrement reference count.
90  void incref () const;
91 
95  void decref () const;
96 
97  // True if calling decref() will delete the object.
102  bool decref_will_delete () const;
103 
104  // True if incref() has never been called, or if the object is being
105  // deleted.
110  bool unowned () const;
111 
112 
113 protected:
114  // Reset the refcount to zero.
115  // This should only be used in the context of a dtor of a derived
116  // class that wants to throw an exception.
117  // It may also be used to implement a `release' function.
123  void nuke_refcount ();
124 
125 
126 private:
127  // The reference count itself.
131  mutable unsigned _refcount;
132 };
133 
134 
135 } // namespace hitfit
136 
137 
138 #include "TopQuarkAnalysis/TopHitFit/interface/Refcount.i"
139 
140 
141 #endif // not D0_UTIL_REFCOUNT_H
void incref() const
Increment the reference count.
Simple reference-counted object.
Definition: Refcount.h:67
Refcount()
Constructor, initialize the reference count to 0.
void nuke_refcount()
Reset the reference count to zero. This should only be used in the context of a destructor of a deriv...
Definition: Refcount.cc:41
unsigned _refcount
Definition: Refcount.h:131
bool unowned() const
Return true if incref() has never been called or if the object is being deleted. Otherwise return FAL...
virtual ~Refcount()
Destructor, it is an error to try to delete an object if the reference count is not 0...
void decref() const
Decrease the reference count.
bool decref_will_delete() const
Return true if calling decref() will delete the object. Otherwise return FALSE.