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