3 This file is part of libmspcore
4 Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
7 #ifndef MSP_CORE_REFPTR_H_
8 #define MSP_CORE_REFPTR_H_
13 A reference counting smart pointer. When the last RefPtr for the data gets
14 destroyed, the data is deleted as well.
20 RefPtr(): data(0), count(0) { }
21 RefPtr(T *d): data(d), count(0) { if(data) count=new unsigned(1); }
22 RefPtr(const RefPtr &p): data(p.data), count(p.count) { incref(); }
23 RefPtr &operator=(const RefPtr &p)
32 ~RefPtr() { decref(); }
35 Makes the RefPtr release its reference of the data. Note that if there are
36 other RefPtrs left with the same data, it might still get deleted
48 T *get() const { return data; }
49 T &operator*() const { return *data; }
50 T *operator->() const { return data; }
51 operator bool() const { return data!=0; }
54 static RefPtr<T> cast_dynamic(const RefPtr<U> &p) { return RefPtr<T>(dynamic_cast<T *>(p.data), p.count); }
55 template<typename U> friend class RefPtr;
60 RefPtr(T *d, unsigned *c): data(d), count(c) { incref(); }