00001 /* 00002 * Copyright (C) 2006 Helmut Grohne 00003 * 00004 * This program is free software; you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation; either version 2 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 */ 00018 00019 #ifndef LIBMUTH_REFCOUNTER_DECLS_H 00020 #define LIBMUTH_REFCOUNTER_DECLS_H 00021 00022 #include <set> 00023 #include <stdexcept> 00024 #include "lock.h" 00025 #include "condition.h" 00026 00027 class WeakRefBase; 00028 template<class RC> class WeakReference; 00029 00033 class RefCounter { 00034 private: 00035 Lock datalock; 00036 Condition cond; 00037 int refcount; 00038 std::set<WeakRefBase*> notifier; 00039 void addWeakRef(WeakRefBase &wr); 00040 void removeWeakRef(WeakRefBase &wr); 00041 void notifyall(); 00042 friend class WeakRefBase; 00043 template<class RC> friend class WeakReference; 00044 protected: 00049 RefCounter(); 00050 public: 00054 void incref(); 00059 void decref(); 00060 virtual ~RefCounter(); 00061 }; 00062 00063 template<class RC> class WeakReference; 00064 00069 template<class RC> class Reference { 00070 private: 00071 RC *object; 00072 friend class WeakReference<RC>; 00073 public: 00079 Reference(RC *obj); 00085 Reference(const Reference &other); 00091 Reference<RC> &operator=(RC *obj); 00098 Reference<RC> &operator=(const Reference &other); 00104 bool operator==(const Reference &other) const; 00110 bool operator!=(const Reference &other) const; 00115 RC &operator*() const; 00120 RC *operator->() const; 00124 ~Reference(); 00125 }; 00126 00130 class WeakRefBase { 00131 friend class RefCounter; 00132 protected: 00136 mutable Lock datalock; 00140 virtual void notify() = 0; 00141 WeakRefBase(); 00142 virtual ~WeakRefBase() {} 00143 }; 00144 00149 class NullPointerException : public std::runtime_error { 00150 public: 00151 NullPointerException() : std::runtime_error("NullPointer") {} 00152 }; 00153 00158 template<class RC> class WeakReference : private WeakRefBase { 00159 private: 00160 RC *object; 00161 void notify(); 00162 friend class Reference<RC>; 00163 public: 00168 WeakReference(); 00174 WeakReference(const WeakReference &other); 00180 WeakReference(const Reference<RC> &other); 00187 WeakReference(RC *obj); 00193 WeakReference &operator=(const WeakReference &other); 00199 WeakReference &operator=(const Reference<RC> &other); 00205 bool operator==(const WeakReference &other) const; 00211 bool operator==(const Reference<RC> &other) const; 00217 bool operator!=(const WeakReference &other) const; 00223 bool operator!=(const Reference<RC> &other) const; 00229 Reference<RC> operator*() const; 00233 ~WeakReference(); 00234 }; 00235 00236 #endif