1 #ifndef MSP_CORE_REFPTR_H_
2 #define MSP_CORE_REFPTR_H_
10 KEEP = 1U<<(sizeof(unsigned)*8-1)
15 RefCounts(): count(0) { }
20 A reference counting smart pointer. When the last RefPtr for the data gets
21 destroyed, the data is deleted as well.
26 template<typename U> friend class RefPtr;
33 RefPtr(): data(0), counts(0) { }
34 RefPtr(T *d): data(d), counts(data ? new RefCounts : 0) { incref(); }
36 RefPtr(T *d, RefCounts *c): data(d), counts(d ? c : 0) { incref(); }
39 /* Must have this or the compiler will generate a default copy-c'tor despite
40 the template version */
41 RefPtr(const RefPtr &p): data(p.data), counts(p.counts) { incref(); }
44 RefPtr(const RefPtr<U> &p): data(p.data), counts(p.counts) { incref(); }
46 ~RefPtr() { decref(); }
48 RefPtr &operator=(T *d)
52 counts = (d ? new RefCounts : 0);
57 // Likewise for the assignment operator
58 RefPtr &operator=(const RefPtr &p) { return assign(p); }
61 RefPtr &operator=(const RefPtr<U> &p) { return assign(p); }
65 RefPtr &assign(const RefPtr<U> &p)
75 /** Makes the RefPtr release its reference of the data without deleting it.
76 Note that if there are other RefPtrs left with the same data, it might
77 still get deleted automatically. */
87 /** Marks the data to not be deleted. This affects all RefPtrs with the
92 counts->count |= RefCounts::KEEP;
95 T *get() const { return data; }
96 T &operator*() const { return *data; }
97 T *operator->() const { return data; }
98 operator bool() const { return data!=0; }
100 unsigned refcount() const { return (data ? counts->count : 0); }
103 static RefPtr<T> cast_dynamic(const RefPtr<U> &p)
104 { return RefPtr<T>(dynamic_cast<T *>(p.data), p.counts); }
124 else if(counts->count==RefCounts::KEEP)