3 * (c) 2008-2009 Alexander Warg <warg@os.inf.tu-dresden.de>
4 * economic rights: Technische Universität Dresden (Germany)
6 * This file is part of TUD:OS and distributed under the terms of the
7 * GNU General Public License 2.
8 * Please see the COPYING-GPL-2 file for details.
10 * As a special exception, you may use this file as part of a free software
11 * library without restriction. Specifically, if other files instantiate
12 * templates or use macros or inline functions from this file, or you compile
13 * this file and link it with other files to produce an executable, this
14 * file does not by itself cause the resulting executable to be covered by
15 * the GNU General Public License. This exception does not however
16 * invalidate any other reasons why the executable file might be covered by
17 * the GNU General Public License.
24 template< typename T >
25 struct Default_ref_counter
27 void h_drop_ref(T *p) throw()
29 if (p->remove_ref() == 0)
33 void h_take_ref(T *p) throw()
45 template<typename T, template< typename X > class CNT = Default_ref_counter>
50 template< typename X > class CNT = Default_ref_counter
52 class Ref_ptr : public Ref_ptr_base, private CNT<T>
56 typedef Weak_ptr<T, CNT> Wp;
59 Ref_ptr() throw() : _p(0) {}
61 Ref_ptr(Ref_ptr_base::Default_value v) : _p((T*)v) {}
63 Ref_ptr(Wp const &o) throw() : _p(o.ptr())
66 explicit Ref_ptr(T *o) throw() : _p(o)
69 /** The couterpart to release (does not create a new reference).
71 Ref_ptr(T *o, bool) throw() : _p(o) {}
73 T *get() const throw()
78 T *ptr() const throw()
94 Ref_ptr(Ref_ptr<OT, CNT> const &o) throw()
100 Ref_ptr(Ref_ptr<T> const &o) throw()
106 template< typename OT >
107 void operator = (Ref_ptr<OT> const &o) throw()
114 void operator = (Ref_ptr<T> const &o) throw()
124 void operator = (T *o) throw()
131 T *operator -> () const throw ()
134 operator Null_type const * () const throw()
135 { return reinterpret_cast<Null_type const*>(_p); }
138 void __drop_ref() throw()
141 this->h_drop_ref(_p);
144 void __take_ref() throw()
147 this->h_take_ref(_p);
154 template<typename T, template< typename X > class CNT>
159 typedef Ref_ptr<T, CNT> Rp;
162 Weak_ptr() throw() {}
163 Weak_ptr(Rp const &o) throw() : _p(o.ptr()) {}
164 explicit Weak_ptr(T *o) throw() : _p(o) {}
166 T *get() const throw() { return _p; }
167 T *ptr() const throw() { return _p; }
169 T *operator -> () const throw () { return _p; }
170 operator Null_type const * () const throw()
171 { return reinterpret_cast<Null_type const*>(_p); }
177 template<typename OT, typename T> inline
178 Ref_ptr<OT> ref_ptr_static_cast(Ref_ptr<T> const &o)
179 { return ref_ptr(static_cast<OT*>(o.ptr())); }
181 template< typename T >
182 inline Ref_ptr<T> ref_ptr(T *t)
183 { return Ref_ptr<T>(t); }
185 template< typename T >
186 inline Weak_ptr<T> weak_ptr(T *t)
187 { return Weak_ptr<T>(t); }
193 mutable int _ref_cnt;
196 Ref_obj() : _ref_cnt(0) {}
197 void add_ref() const throw() { ++_ref_cnt; }
198 int remove_ref() const throw() { return --_ref_cnt; }