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 // $Id: Refcount.h,v 1.1 2011/05/26 09:46:53 mseidel Exp $
3 //
4 // File: Refcount.h
5 // Purpose: A base class for a simple reference-counted object.
6 // Created: Aug 2000, sss, from the version that used to be in d0om.
7 //
8 // To make a reference counted type, derive from class d0_util::Refcount.
9 // When the object is created, its refcount is initially set to 0;
10 // the first action taken by the creator should be to call incref()
11 // to bump the refcount to 1. Thereafter, the refcount may be incremented
12 // by incref() and decremented by decref(). If the reference count reaches
13 // 0, the object calls delete on itself.
14 //
15 // If the object is deleted explicitly, the reference count must be 0 or 1.
16 // Otherwise, an assertion violation will be reported.
17 //
18 // CMSSW File : interface/Refcount.h
19 // Original Author : Scott Stuart Snyder <snyder@bnl.gov> for D0
20 // Imported to CMSSW by Haryo Sumowidagdo <Suharyo.Sumowidagdo@cern.ch>
21 //
22 
23 
46 #ifndef D0_UTIL_REFCOUNT_H
47 #define D0_UTIL_REFCOUNT_H
48 
49 #include<stdexcept>
50 #include<sstream>
51 #include<cassert>
52 namespace hitfit {
53 
54 
68 class Refcount
69 //
70 // Purpose: Simple reference-counted object.
71 //
72 {
73 public:
74  // Constructor, destructor.
75 
79  Refcount ();
80 
85  virtual ~Refcount ();
86 
87  // Increment and decrement reference count.
91  void incref () const;
92 
96  void decref () const;
97 
98  // True if calling decref() will delete the object.
103  bool decref_will_delete () const;
104 
105  // True if incref() has never been called, or if the object is being
106  // deleted.
111  bool unowned () const;
112 
113 
114 protected:
115  // Reset the refcount to zero.
116  // This should only be used in the context of a dtor of a derived
117  // class that wants to throw an exception.
118  // It may also be used to implement a `release' function.
124  void nuke_refcount ();
125 
126 
127 private:
128  // The reference count itself.
132  mutable unsigned _refcount;
133 };
134 
135 
136 } // namespace hitfit
137 
138 
139 #include "TopQuarkAnalysis/TopHitFit/interface/Refcount.i"
140 
141 
142 #endif // not D0_UTIL_REFCOUNT_H
void incref() const
Increment the reference count.
Simple reference-counted object.
Definition: Refcount.h:68
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:42
unsigned _refcount
Definition: Refcount.h:132
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.