OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
RefCounted.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /***************************************************************************
3  *
4  * The IPPL Framework
5  *
6  *
7  * Visit http://people.web.psi.ch/adelmann/ for more details
8  *
9  ***************************************************************************/
10 
11 #ifndef REF_COUNTED_H
12 #define REF_COUNTED_H
13 
15 
17 {
18 public:
19 
20  // The reference count.
21  int RefCount;
22 
23  // The constructor just initializes the reference count.
25  : RefCount(0)
26  {
27  }
28 
29 };
30 
32 
33 template < class T >
35 {
36 public:
37 
38  // Null ctor constructs with a null ptr.
40  : p(0)
41  {
42  }
43 
44  // Initialize from a pointer to the pointed class.
46  : p(pp)
47  {
48  if (pp)
49  ++(pp->RefCount);
50  }
51 
52  // Copy ctor.
54  : p(pp.p)
55  {
56  if(p)
57  ++(p->RefCount);
58  }
59 
60  // When you destroy one of these, decrement the RefCount.
62  {
63  if (p) {
64  if ( (--(p->RefCount)) == 0 )
65  delete p;
66  p = 0;
67  }
68  }
69 
70  // Assignment operator increments the refcount.
72  {
73  // First unlink from the one we're pointing to now.
74  if ( p && (--(p->RefCount)==0) )
75  delete p;
76  // Assign the new one.
77  if ((p = rhs.p))
78  ++(p->RefCount);
79  return *this;
80  }
81 
82  // Assignment operator increments the refcount.
84  {
85  // First unlink from the one we're pointing to now.
86  if ( p && (--(p->RefCount)==0) )
87  delete p;
88  // Assign the new one.
89  if ((p = pp))
90  ++(p->RefCount);
91  return *this;
92  }
93 
94  // Two ways to use the pointer.
95  T* operator->() const { return p; }
96  T& operator*() const { return *p; }
97 
98  // Just extract the pointer if you need it.
99  operator T*() { return p; }
100  operator const T*() const { return p; }
101 
102  void invalidate()
103  {
104  if ( p && (--(p->RefCount)==0) )
105  delete p;
106  p = 0;
107  }
108  bool valid()
109  {
110  return p!=0;
111  }
112 
113  // Utility function for implementing copy-on-write semantics.
115  {
116  // If more than one thing is referring to this one
117  if ( p && (p->RefCount > 1) )
118  {
119  // Unlink from the existing object.
120  --(p->RefCount);
121  // Copy it.
122  p = new T( *p );
123  // Inform it that we are watching.
124  p->RefCount = 1;
125  }
126  // Return yourself for further processing.
127  return *this;
128  }
129 
130 private:
131  // The pointer itself.
132  T *p;
133 };
134 
136 
137 #endif // REF_COUNTED_H
138 
139 /***************************************************************************
140  * $RCSfile: RefCounted.h,v $ $Author: adelmann $
141  * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:33 $
142  * IPPL_VERSION_ID: $Id: RefCounted.h,v 1.1.1.1 2003/01/23 07:40:33 adelmann Exp $
143  ***************************************************************************/
int RefCount
Definition: RefCounted.h:21
T * operator->() const
Definition: RefCounted.h:95
RefCountedP(const RefCountedP< T > &pp)
Definition: RefCounted.h:53
RefCountedP & operator=(T *pp)
Definition: RefCounted.h:83
RefCountedP(T *pp)
Definition: RefCounted.h:45
bool valid()
Definition: RefCounted.h:108
void invalidate()
Definition: RefCounted.h:102
RefCountedP< T > & CopyForWrite()
Definition: RefCounted.h:114
T & operator*() const
Definition: RefCounted.h:96
RefCountedP & operator=(const RefCountedP &rhs)
Definition: RefCounted.h:71