aimstil  5.0.5
SmartObject.h
Go to the documentation of this file.
1 #ifndef TIL_SMARTOBJECT_H
2 #define TIL_SMARTOBJECT_H
3 
4 #include <iostream>
5 
6 #include "til/til_common.h"
7 
8 
9 // SmartObject is a base class used for garbage collection.
10 // A class should derive from SmartObject if garbage collection is needed
11 // for this class.
12 // SmartObject contains nothing but a number that counts the number of
13 // pointers pointing to this object. It provided also method for the pointers
14 // to increase and decrease this number.
15 
16 // Ptr and ConstPtr classes implements this functionality.
17 
18 
19 // Namespace
20 
21 namespace til {
22 
26 {
27 
28 public: // Constructors and destructor
29 
33  {
34 #ifdef SMARTOBJ_DEBUG
35  std::cout << "SmartObj constr" << std::endl;
36 #endif
37  m_refCount = 0;
38  m_lock = false;
39  }
40 
43  virtual ~SmartObject()
44  {
45 #ifdef SMARTOBJ_DEBUG
46  std::cout << "SmartObj destr" << std::endl;
47 #endif
48  }
49 
50 public: // functions
51 
55  void subscribe(void)
56  {
57  ++m_refCount;
58 #ifdef SMARTOBJ_DEBUG
59  std::cout << "Increase to " << m_refCount << std::endl;
60 #endif
61  }
62 
66  void unsubscribe(void)
67  {
68 #ifdef SMARTOBJ_DEBUG
69  std::cout << "Decrease to " << m_refCount - 1 << std::endl;
70 #endif
71  if ((--m_refCount == 0) && (!m_lock)) delete this;
72  }
73 
76  {
77  return m_refCount;
78  }
79 
83  void lock() { m_lock = true; }
84 
88  void unlock()
89  {
90  m_lock = false;
91  if (m_refCount==0) delete this;
92  }
93 
94 private: // data
95 
96  int m_refCount;
97  bool m_lock;
98 };
99 
100 
101 } // namespace
102 
103 
104 #endif
105 
int getReferenceCount(void)
Get the number of pointers that have registered to this object.
Definition: SmartObject.h:75
void lock()
Disable garbage collection.
Definition: SmartObject.h:83
void unsubscribe(void)
Unregister to this object.
Definition: SmartObject.h:66
Belongs to package Box Do not include directly, include til/Box.h instead.
Definition: Accumulator.h:10
General macros, definitions and functions.
#define TIL_API
Definition: til_common.h:42
void unlock()
Re-enable garbage collection.
Definition: SmartObject.h:88
void subscribe(void)
Register to this object.
Definition: SmartObject.h:55
Base class for all classes needing reference counting based garbage collection.
Definition: SmartObject.h:25
virtual ~SmartObject()
Destructor Do nothing special.
Definition: SmartObject.h:43
SmartObject(void)
Default constructor.
Definition: SmartObject.h:32