OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
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 //#include <Ippl.h>
40 
41 
42 class RCObject {
43 
44 public:
45 
47  // Return the new value of the reference count.
48  int addReference() const;
49 
51  // Return the new value of the reference count.
52  int removeReference() const;
53 
55  // Return true, if the pointee has more than one reference.
56  bool isShared() const;
57 
58 
59 protected:
60 
62  // By default a new object is sharable.
63  RCObject();
64 
66  // The copy inherits the sharable state.
67  RCObject(const RCObject &);
68 
69  // Destructor is pure, but implemented, to make class abstract.
70  virtual ~RCObject() = 0;
71 
72  RCObject &operator=(const RCObject &right);
73 
74 private:
75 
76  // The object's reference count.
77  // The value is mutable, since it is not really part of the objects state.
78  mutable int refCount;
79 };
80 
81 
82 // Inline functions for class RCObject
83 // ------------------------------------------------------------------------
84 
85 inline int RCObject::addReference() const {
86  return ++refCount;
87 }
88 
89 
90 inline int RCObject::removeReference() const {
91  return --refCount;
92 }
93 
94 
95 inline bool RCObject::isShared() const {
96  return refCount > 1;
97 }
98 
99 
101  refCount(0)
102 {}
103 
104 
105 inline RCObject::RCObject(const RCObject &/*rhs*/):
106  refCount(0)
107 {}
108 
109 
111 {}
112 
113 
114 inline RCObject &RCObject::operator=(const RCObject &/*rhs*/) {
115  return *this;
116 }
117 
118 #endif // CLASSIC_RCObject_HH
int removeReference() const
Decrement the reference count.
Definition: RCObject.h:90
RCObject & operator=(const RCObject &right)
Definition: RCObject.h:114
int addReference() const
Increment reference count.
Definition: RCObject.h:85
Abstract base class for reference counted objects.
Definition: RCObject.h:42
bool isShared() const
Test for sharing.
Definition: RCObject.h:95
int refCount
Definition: RCObject.h:78
RCObject()
Default constructor.
Definition: RCObject.h:100
virtual ~RCObject()=0
Definition: RCObject.h:110