summaryrefslogtreecommitdiffstats
path: root/SharedPtr.h
diff options
context:
space:
mode:
authorneoraider <devnull@localhost>2007-12-14 03:47:03 +0100
committerneoraider <devnull@localhost>2007-12-14 03:47:03 +0100
commita8c1d6168797526b9d24bc8c86f2578f3be59fa8 (patch)
tree8515976a634b788d99b2c894757a0b4ecbb6fa6a /SharedPtr.h
parentd82c597917d8ef5866c7a83d0c101f423a2ac05d (diff)
downloadzoomedit-a8c1d6168797526b9d24bc8c86f2578f3be59fa8.tar
zoomedit-a8c1d6168797526b9d24bc8c86f2578f3be59fa8.zip
zoomedit: Verallgemeinerte Level-Objekte implementiert.
Diffstat (limited to 'SharedPtr.h')
-rw-r--r--SharedPtr.h46
1 files changed, 46 insertions, 0 deletions
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 T>
+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<T> &c) : counter(c.counter) {
+ counter->refCount++;
+ }
+
+ virtual ~SharedPtr() {
+ if(--counter->refCount == 0)
+ delete counter;
+ }
+
+ SharedPtr<T>& operator=(const SharedPtr<T>& 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_*/