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.
26 * \brief Smart pointer with automatic deletion.
27 * \param T The type of the referenced object.
29 * This smart pointer calls the delete operator when the destructor
30 * is called. This has the effect that the object the pointer points to
31 * will be deleted when the pointer goes out of scope, or a new value gets
32 * assigned. The smart pointer provides a release() method to get a normal
33 * pointer to the object and set the smart pointer to NULL.
44 /** \brief The referenced type. */
48 * \brief Construction by assignment of a normal pointer.
49 * \param p The pointer to the object
51 explicit Auto_ptr(T *p = 0) throw() : _p(p) {}
54 * \brief Copy construction, releases the original pointer.
55 * \param o The smart pointer, which shall be copied and released.
57 Auto_ptr(Auto_ptr const &o) throw()
58 : _p(const_cast<Auto_ptr<T>&>(o).release())
62 * \brief Assignment from another smart pointer.
63 * \param o The source for the assignment (will be released).
65 Auto_ptr &operator = (Auto_ptr const &o) throw()
70 _p = const_cast<Auto_ptr<T>&>(o).release();
75 /** \brief Destruction, shall delete the object. */
77 { if (_p) delete _p; }
79 /** \brief Dereference the pointer. */
80 T &operator * () const throw() { return *_p; }
82 /** \brief Member access for the object. */
83 T *operator -> () const throw() { return _p; }
86 * \brief Get the normal pointer.
87 * \attention This function will not release the object, the
88 * object will be deleted by the smart pointer.
90 T *get() const throw() { return _p; }
93 * \brief Release the object and get the normal pointer back.
95 * After calling this function the smart pointer will point to NULL
96 * and the object will not be deleted by the pointer anymore.
98 T *release() throw() { T *t = _p; _p = 0; return t; }
101 * \brief Delete the object and reset the smart pointer to NULL.
103 void reset(T *p = 0) throw()
109 /** \brief Operator for <c>if (!ptr) ...<\c>.*/
110 operator Priv_type * () const throw()
111 { return reinterpret_cast<Priv_type*>(_p); }