]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/cxx/lib/tl/include/auto_ptr
update
[l4.git] / l4 / pkg / cxx / lib / tl / include / auto_ptr
1 // vim:ft=cpp
2 /*
3  * (c) 2008-2009 Alexander Warg <warg@os.inf.tu-dresden.de>
4  *     economic rights: Technische Universität Dresden (Germany)
5  *
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.
9  *
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.
18  */
19
20 #pragma once
21
22 namespace cxx {
23
24 /**
25  * \ingroup cxx_api
26  * \brief Smart pointer with automatic deletion.
27  * \param T The type of the referenced object.
28  *
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.
34  */
35 template< typename T>
36 class Auto_ptr
37 {
38 private:
39   T *_p;
40
41   struct Priv_type;
42
43 public:
44   /** \brief The referenced type. */
45   typedef T Ref_type;
46
47   /**
48    * \brief Construction by assignment of a normal pointer.
49    * \param p The pointer to the object
50    */
51   explicit Auto_ptr(T *p = 0) throw() : _p(p) {}
52
53   /**
54    * \brief Copy construction, releases the original pointer.
55    * \param o The smart pointer, which shall be copied and released.
56    */
57   Auto_ptr(Auto_ptr const &o) throw()
58   : _p(const_cast<Auto_ptr<T>&>(o).release())
59   {}
60
61   /**
62    * \brief Assignment from another smart pointer.
63    * \param o The source for the assignment (will be released).
64    */
65   Auto_ptr &operator = (Auto_ptr const &o) throw()
66   {
67     if (&o != this)
68       {
69         if (_p) delete _p;
70         _p = const_cast<Auto_ptr<T>&>(o).release();
71       }
72     return *this;
73   }
74
75   /** \brief Destruction, shall delete the object. */
76   ~Auto_ptr() throw()
77   { if (_p) delete _p; }
78
79   /** \brief Dereference the pointer. */
80   T &operator * () const throw() { return *_p; }
81
82   /** \brief Member access for the object. */
83   T *operator -> () const throw() { return _p; }
84
85   /**
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.
89    */
90   T *get() const throw() { return _p; }
91
92   /**
93    * \brief Release the object and get the normal pointer back.
94    * 
95    * After calling this function the smart pointer will point to NULL
96    * and the object will not be deleted by the pointer anymore.
97    */
98   T *release() throw() { T *t = _p; _p = 0; return t; }
99
100   /**
101    * \brief Delete the object and reset the smart pointer to NULL.
102    */
103   void reset(T *p = 0) throw()
104   {
105     if (_p) delete _p;
106     _p = p;
107   }
108
109   /** \brief Operator for <c>if (!ptr) ...<\c>.*/
110   operator Priv_type * () const throw()
111   { return reinterpret_cast<Priv_type*>(_p); }
112 };
113 }