8 * Copyright 2010 ZXing authors All rights reserved.
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
23 //#define DEBUG_COUNTING
24 //using namespace std;
34 /* base class for reference-counted objects */
42 cout << "instantiating " << typeid(*this).name() << " " << this <<
43 " @ " << count_ << "\n";
48 virtual Counted *retain() {
50 cout << "retaining " << typeid(*this).name() << " " << this <<
55 cout << "->" << count_ << "\n";
59 virtual void release() {
61 cout << "releasing " << typeid(*this).name() << " " << this <<
64 if (count_ == 0 || count_ == 54321) {
66 cout << "\nOverreleasing already-deleted object " << this << "!!!\n";
72 cout << "->" << count_ << "\n";
76 cout << "deleting " << typeid(*this).name() << " " << this << "\n";
84 /* return the current count for denugging purposes or similar */
90 /* counting reference to reference-counted objects */
91 template<typename T> class Ref {
95 explicit Ref(T *o = 0) :
98 cout << "instantiating Ref " << this << " from pointer" << o << "\n";
103 explicit Ref(const T &o) :
105 #ifdef DEBUG_COUNTING
106 cout << "instantiating Ref " << this << " from reference\n";
108 reset(const_cast<T *>(&o));
111 Ref(const Ref &other) :
113 #ifdef DEBUG_COUNTING
114 cout << "instantiating Ref " << this << " from Ref " << &other << "\n";
116 reset(other.object_);
120 Ref(const Ref<Y> &other) :
122 #ifdef DEBUG_COUNTING
123 cout << "instantiating Ref " << this << " from reference\n";
125 reset(other.object_);
129 #ifdef DEBUG_COUNTING
130 cout << "destroying Ref " << this << " with " <<
131 (object_ ? typeid(*object_).name() : "NULL") << " " << object_ << "\n";
139 #ifdef DEBUG_COUNTING
140 cout << "resetting Ref " << this << " from " <<
141 (object_ ? typeid(*object_).name() : "NULL") << " " << object_ <<
142 " to " << (o ? typeid(*o).name() : "NULL") << " " << o << "\n";
152 Ref& operator=(const Ref &other) {
153 reset(other.object_);
157 Ref& operator=(const Ref<Y> &other) {
158 reset(other.object_);
161 Ref& operator=(T* o) {
166 Ref& operator=(Y* o) {
174 T* operator->() const {
177 operator T*() const {
181 bool operator==(const int x) {
182 return x == 0 ? object_ == 0 : false;
184 bool operator==(const Ref &other) const {
185 return object_ == other.object_ || *object_ == *(other.object_);
188 bool operator==(const Ref<Y> &other) const {
189 return object_ == other.object_ || *object_ == *(other.object_);
192 bool operator!=(const int x) {
193 return x == 0 ? object_ != 0 : true;
197 friend std::ostream& operator<<(std::ostream &out, Ref<Y>& ref);
201 #endif // __COUNTED_H__