8 * Created by Christian Brunschen on 07/05/2008.
9 * Copyright 2008 Google UK. All rights reserved.
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
24 //#define DEBUG_COUNTING
25 //using namespace std;
35 /* base class for reference-counted objects */
43 cout << "instantiating " << typeid(*this).name() << " " << this <<
44 " @ " << count_ << "\n";
49 virtual Counted *retain() {
51 cout << "retaining " << typeid(*this).name() << " " << this <<
56 cout << "->" << count_ << "\n";
60 virtual void release() {
62 cout << "releasing " << typeid(*this).name() << " " << this <<
65 if (count_ == 0 || count_ == 54321) {
67 cout << "\nOverreleasing already-deleted object " << this << "!!!\n";
73 cout << "->" << count_ << "\n";
77 cout << "deleting " << typeid(*this).name() << " " << this << "\n";
85 /* return the current count for denugging purposes or similar */
91 /* counting reference to reference-counted objects */
92 template<typename T> class Ref {
96 explicit Ref(T *o = 0) :
99 cout << "instantiating Ref " << this << " from pointer" << o << "\n";
104 explicit Ref(const T &o) :
106 #ifdef DEBUG_COUNTING
107 cout << "instantiating Ref " << this << " from reference\n";
109 reset(const_cast<T *>(&o));
112 Ref(const Ref &other) :
114 #ifdef DEBUG_COUNTING
115 cout << "instantiating Ref " << this << " from Ref " << &other << "\n";
117 reset(other.object_);
121 Ref(const Ref<Y> &other) :
123 #ifdef DEBUG_COUNTING
124 cout << "instantiating Ref " << this << " from reference\n";
126 reset(other.object_);
130 #ifdef DEBUG_COUNTING
131 cout << "destroying Ref " << this << " with " <<
132 (object_ ? typeid(*object_).name() : "NULL") << " " << object_ << "\n";
140 #ifdef DEBUG_COUNTING
141 cout << "resetting Ref " << this << " from " <<
142 (object_ ? typeid(*object_).name() : "NULL") << " " << object_ <<
143 " to " << (o ? typeid(*o).name() : "NULL") << " " << o << "\n";
153 Ref& operator=(const Ref &other) {
154 reset(other.object_);
158 Ref& operator=(const Ref<Y> &other) {
159 reset(other.object_);
162 Ref& operator=(T* o) {
167 Ref& operator=(Y* o) {
175 T* operator->() const {
178 operator T*() const {
182 bool operator==(const int x) {
183 return x == 0 ? object_ == 0 : false;
185 bool operator==(const Ref &other) const {
186 return object_ == other.object_ || *object_ == *(other.object_);
189 bool operator==(const Ref<Y> &other) const {
190 return object_ == other.object_ || *object_ == *(other.object_);
193 bool operator!=(const int x) {
194 return x == 0 ? object_ != 0 : true;
198 friend std::ostream& operator<<(std::ostream &out, Ref<Y>& ref);
202 #endif // __COUNTED_H__