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-2008
5  * Oracle. All rights reserved.
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  * $Id$
20  */
21 
22 #ifndef _REFERENCECOUNTED_HPP
23 #define _REFERENCECOUNTED_HPP
24 
25 #include <xqilla/framework/XQillaExport.hpp>
27 
28 // for null RefCountPointer instances
29 #define NULLRCP ((void *)0)
30 
32 class XQILLA_API ReferenceCounted
33 {
34 public:
36  : _ref_count(0) {}
37  virtual ~ReferenceCounted() {}
38 
40  void incrementRefCount() const
41  {
42  ++const_cast<unsigned int&>(_ref_count);
43  }
44 
46  virtual void decrementRefCount() const
47  {
48  if(--const_cast<unsigned int&>(_ref_count) == 0) {
49  delete this;
50  }
51  }
52 
53 protected:
54  unsigned int _ref_count; // mutable
55 };
56 
58 template<class T> class RefCountPointer
59 {
60 public:
61  RefCountPointer(T *p = 0) : _p(p)
62  {
63  if(_p != 0) _p->incrementRefCount();
64  }
65 
66  template<class T2> RefCountPointer(const RefCountPointer<T2> &o) : _p((T*)(T2*)o)
67  {
68  if(_p != 0) _p->incrementRefCount();
69  }
70 
72  {
73  if(_p != 0) _p->incrementRefCount();
74  }
75 
77  {
78  if(_p != o._p) {
79  if(_p != 0) _p->decrementRefCount();
80  _p = o._p;
81  if(_p != 0) _p->incrementRefCount();
82  }
83  return *this;
84  }
85 
87  {
88  if(_p != 0) _p->decrementRefCount();
89  }
90 
91  T *operator->() const
92  {
93  return _p;
94  }
95 
96  operator T*() const
97  {
98  return _p;
99  }
100 
101  T *get() const
102  {
103  return _p;
104  }
105 
106  bool isNull() const
107  {
108  return (_p == 0);
109  }
110 
111  bool notNull() const
112  {
113  return (_p != 0);
114  }
115 
116 protected:
117  T *_p;
118 };
119 
120 template<class T1, class T2>
121 inline bool operator==(const RefCountPointer<T1> &a, const RefCountPointer<T2> &b)
122 {
123  return (void*)(T1*)a == (void*)(T2*)b;
124 }
125 
126 template<class T1, class T2>
127 inline bool operator!=(const RefCountPointer<T1> &a, const RefCountPointer<T2> &b)
128 {
129  return (void*)(T1*)a != (void*)(T2*)b;
130 }
131 
132 template<class T>
133 inline bool operator==(const RefCountPointer<T> &a, void *b)
134 {
135  return (T*)a == (T*)b;
136 }
137 
138 template<class T>
139 inline bool operator!=(const RefCountPointer<T> &a, void *b)
140 {
141  return (T*)a != (T*)b;
142 }
143 
144 #endif
Super class for reference counted classes.
Definition: ReferenceCounted.hpp:32
RefCountPointer & operator=(const RefCountPointer< T > &o)
Definition: ReferenceCounted.hpp:76
Super class of all the reference counted wrappers for Items.
Definition: ReferenceCounted.hpp:58
void incrementRefCount() const
Increment the reference count.
Definition: ReferenceCounted.hpp:40
T * _p
Definition: ReferenceCounted.hpp:117
bool operator==(const RefCountPointer< T1 > &a, const RefCountPointer< T2 > &b)
Definition: ReferenceCounted.hpp:121
T * operator->() const
Definition: ReferenceCounted.hpp:91
unsigned int _ref_count
Definition: ReferenceCounted.hpp:54
bool operator!=(const RefCountPointer< T1 > &a, const RefCountPointer< T2 > &b)
Definition: ReferenceCounted.hpp:127
RefCountPointer(const RefCountPointer< T2 > &o)
Definition: ReferenceCounted.hpp:66
virtual ~ReferenceCounted()
Definition: ReferenceCounted.hpp:37
bool isNull() const
Definition: ReferenceCounted.hpp:106
virtual void decrementRefCount() const
Decrement the reference count, deleting if it becomes zero.
Definition: ReferenceCounted.hpp:46
bool notNull() const
Definition: ReferenceCounted.hpp:111
RefCountPointer(T *p=0)
Definition: ReferenceCounted.hpp:61
ReferenceCounted()
Definition: ReferenceCounted.hpp:35
~RefCountPointer()
Definition: ReferenceCounted.hpp:86
RefCountPointer(const RefCountPointer< T > &o)
Definition: ReferenceCounted.hpp:71