From a8c1d6168797526b9d24bc8c86f2578f3be59fa8 Mon Sep 17 00:00:00 2001 From: neoraider Date: Fri, 14 Dec 2007 02:47:03 +0000 Subject: zoomedit: Verallgemeinerte Level-Objekte implementiert. --- SharedPtr.h | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 SharedPtr.h (limited to 'SharedPtr.h') diff --git a/SharedPtr.h b/SharedPtr.h new file mode 100644 index 0000000..1b5d2bc --- /dev/null +++ b/SharedPtr.h @@ -0,0 +1,46 @@ +#ifndef SHAREDPTR_H_ +#define SHAREDPTR_H_ + +template +class SharedPtr { + private: + class Counter { + public: + Counter(T *o) : object(o), refCount(1) {} + ~Counter() {delete object;} + + T *object; + unsigned int refCount; + }; + + Counter *counter; + + public: + SharedPtr(T *o) : counter(new Counter(o)) {} + + SharedPtr(const SharedPtr &c) : counter(c.counter) { + counter->refCount++; + } + + virtual ~SharedPtr() { + if(--counter->refCount == 0) + delete counter; + } + + SharedPtr& operator=(const SharedPtr& c) { + c.counter->refCount++; + if(--counter->refCount == 0) + delete counter; + + counter = c.counter; + return *this; + } + + T* operator->() {return counter->object;} + T& operator*() {return *counter->object;} + + const T* operator->() const {return counter->object;} + const T& operator*() const {return *counter->object;} +}; + +#endif /*SHAREDPTR_H_*/ -- cgit v1.2.3