SourceForge.net Logo
ReferenceCounted.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001, 2008,
3  * DecisionSoft Limited. All rights reserved.
4  * Copyright (c) 2004, 2018 Oracle and/or its affiliates. All rights reserved.
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #ifndef _REFERENCECOUNTED_HPP
21 #define _REFERENCECOUNTED_HPP
22 
23 #include <xqilla/framework/XQillaExport.hpp>
25 
26 // for null RefCountPointer instances
27 #define NULLRCP ((void *)0)
28 
30 class XQILLA_API ReferenceCounted
31 {
32 public:
34  : _ref_count(0) {}
35  virtual ~ReferenceCounted() {}
36 
38  void incrementRefCount() const
39  {
40  ++const_cast<unsigned int&>(_ref_count);
41  }
42 
44  virtual void decrementRefCount() const
45  {
46  if(--const_cast<unsigned int&>(_ref_count) == 0) {
47  delete this;
48  }
49  }
50 
51  unsigned int getRefCount() const
52  {
53  return _ref_count;
54  }
55 
56 protected:
57  unsigned int _ref_count; // mutable
58 };
59 
61 template<class T> class RefCountPointer
62 {
63 public:
64  RefCountPointer(T *p = 0) : _p(p)
65  {
66  if(_p != 0) _p->incrementRefCount();
67  }
68 
69  template<class T2> RefCountPointer(const RefCountPointer<T2> &o) : _p((T*)(T2*)o)
70  {
71  if(_p != 0) _p->incrementRefCount();
72  }
73 
75  {
76  if(_p != 0) _p->incrementRefCount();
77  }
78 
80  {
81  if(_p != o._p) {
82  if(_p != 0) _p->decrementRefCount();
83  _p = o._p;
84  if(_p != 0) _p->incrementRefCount();
85  }
86  return *this;
87  }
88 
90  {
91  if(_p != 0) _p->decrementRefCount();
92  }
93 
94  T *operator->() const
95  {
96  return _p;
97  }
98 
99  operator T*() const
100  {
101  return _p;
102  }
103 
104  T *get() const
105  {
106  return _p;
107  }
108 
109  bool isNull() const
110  {
111  return (_p == 0);
112  }
113 
114  bool notNull() const
115  {
116  return (_p != 0);
117  }
118 
119 protected:
120  T *_p;
121 };
122 
123 template<class T1, class T2>
124 inline bool operator==(const RefCountPointer<T1> &a, const RefCountPointer<T2> &b)
125 {
126  return (void*)(T1*)a == (void*)(T2*)b;
127 }
128 
129 template<class T1, class T2>
130 inline bool operator!=(const RefCountPointer<T1> &a, const RefCountPointer<T2> &b)
131 {
132  return (void*)(T1*)a != (void*)(T2*)b;
133 }
134 
135 template<class T>
136 inline bool operator==(const RefCountPointer<T> &a, void *b)
137 {
138  return (T*)a == (T*)b;
139 }
140 
141 template<class T>
142 inline bool operator!=(const RefCountPointer<T> &a, void *b)
143 {
144  return (T*)a != (T*)b;
145 }
146 
147 #endif
Super class for reference counted classes.
Definition: ReferenceCounted.hpp:30
RefCountPointer & operator=(const RefCountPointer< T > &o)
Definition: ReferenceCounted.hpp:79
Super class of all the reference counted wrappers for Items.
Definition: ReferenceCounted.hpp:61
void incrementRefCount() const
Increment the reference count.
Definition: ReferenceCounted.hpp:38
T * _p
Definition: ReferenceCounted.hpp:120
bool operator==(const RefCountPointer< T1 > &a, const RefCountPointer< T2 > &b)
Definition: ReferenceCounted.hpp:124
T * operator->() const
Definition: ReferenceCounted.hpp:94
unsigned int getRefCount() const
Definition: ReferenceCounted.hpp:51
unsigned int _ref_count
Definition: ReferenceCounted.hpp:57
bool operator!=(const RefCountPointer< T1 > &a, const RefCountPointer< T2 > &b)
Definition: ReferenceCounted.hpp:130
RefCountPointer(const RefCountPointer< T2 > &o)
Definition: ReferenceCounted.hpp:69
virtual ~ReferenceCounted()
Definition: ReferenceCounted.hpp:35
bool isNull() const
Definition: ReferenceCounted.hpp:109
virtual void decrementRefCount() const
Decrement the reference count, deleting if it becomes zero.
Definition: ReferenceCounted.hpp:44
bool notNull() const
Definition: ReferenceCounted.hpp:114
RefCountPointer(T *p=0)
Definition: ReferenceCounted.hpp:64
ReferenceCounted()
Definition: ReferenceCounted.hpp:33
~RefCountPointer()
Definition: ReferenceCounted.hpp:89
RefCountPointer(const RefCountPointer< T > &o)
Definition: ReferenceCounted.hpp:74