OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
RCObject.h
Go to the documentation of this file.
1#ifndef CLASSIC_RCObject_HH
2#define CLASSIC_RCObject_HH
3
4// ------------------------------------------------------------------------
5// $RCSfile: RCObject.h,v $
6// ------------------------------------------------------------------------
7// $Revision: 1.1.1.1 $
8// ------------------------------------------------------------------------
9// Copyright: see Copyright.readme
10// ------------------------------------------------------------------------
11//
12// Class: RCObject
13//
14// ------------------------------------------------------------------------
15// Class category: MemoryManagement
16// ------------------------------------------------------------------------
17//
18// $Date: 2000/03/27 09:32:37 $
19// $Author: fci $
20//
21// ------------------------------------------------------------------------
22
23
24// Class RCObject
25// ------------------------------------------------------------------------
27// It collaborates with the templace class Pointer<Object>.
28// It is modelled after the class RCObject described in:
29// {center}
30// Scott Meyers, More Effective C++, Addison Wesley, 1996, pg. 195.
31// {/center}
32// The idiom [tt]delete this[/tt] has been avoided by moving the deletion
33// to class Pointer<Object>.
34// All derived classes must implement a clone() method, to allow the
35// Pointer class to copy the object pointed at.
36// All constructors, the destructor, and the assignment operators are
37// protected, since a stand-alone RCObject makes no sense.
38
39
40class RCObject {
41
42public:
43
45 // Return the new value of the reference count.
46 int addReference() const;
47
49 // Return the new value of the reference count.
50 int removeReference() const;
51
53 // Return true, if the pointee has more than one reference.
54 bool isShared() const;
55
56
57protected:
58
60 // By default a new object is sharable.
61 RCObject();
62
64 // The copy inherits the sharable state.
65 RCObject(const RCObject &);
66
67 // Destructor is pure, but implemented, to make class abstract.
68 virtual ~RCObject() = 0;
69
70 RCObject &operator=(const RCObject &right);
71
72private:
73
74 // The object's reference count.
75 // The value is mutable, since it is not really part of the objects state.
76 mutable int refCount;
77};
78
79
80// Inline functions for class RCObject
81// ------------------------------------------------------------------------
82
83inline int RCObject::addReference() const {
84 return ++refCount;
85}
86
87
88inline int RCObject::removeReference() const {
89 return --refCount;
90}
91
92
93inline bool RCObject::isShared() const {
94 return refCount > 1;
95}
96
97
99 refCount(0)
100{}
101
102
103inline RCObject::RCObject(const RCObject &/*rhs*/):
104 refCount(0)
105{}
106
107
109{}
110
111
112inline RCObject &RCObject::operator=(const RCObject &/*rhs*/) {
113 return *this;
114}
115
116#endif // CLASSIC_RCObject_HH
Abstract base class for reference counted objects.
Definition: RCObject.h:40
int removeReference() const
Decrement the reference count.
Definition: RCObject.h:88
int addReference() const
Increment reference count.
Definition: RCObject.h:83
int refCount
Definition: RCObject.h:76
RCObject()
Default constructor.
Definition: RCObject.h:98
RCObject & operator=(const RCObject &right)
Definition: RCObject.h:112
virtual ~RCObject()=0
Definition: RCObject.h:108
bool isShared() const
Test for sharing.
Definition: RCObject.h:93