L4Re - L4 Runtime Environment
auto_ptr
1 // vim:set ft=cpp: -*- Mode: C++ -*-
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 
35 template< typename T>
36 class Auto_ptr
37 {
38 private:
39  T *_p;
40 
41  struct Priv_type;
42 
43 public:
45  typedef T Ref_type;
46 
51  explicit Auto_ptr(T *p = 0) throw() : _p(p) {}
52 
57  Auto_ptr(Auto_ptr const &o) throw()
58  : _p(const_cast<Auto_ptr<T>&>(o).release())
59  {}
60 
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 
76  ~Auto_ptr() throw()
77  { if (_p) delete _p; }
78 
80  T &operator * () const throw() { return *_p; }
81 
83  T *operator -> () const throw() { return _p; }
84 
90  T *get() const throw() { return _p; }
91 
98  T *release() throw() { T *t = _p; _p = 0; return t; }
99 
103  void reset(T *p = 0) throw()
104  {
105  if (_p) delete _p;
106  _p = p;
107  }
108 
110  operator Priv_type * () const throw()
111  { return reinterpret_cast<Priv_type*>(_p); }
112 };
113 }
Our C++ library.
Definition: arith:22
T & operator*() const
Dereference the pointer.
Definition: auto_ptr:80
Auto_ptr(T *p=0)
Construction by assignment of a normal pointer.
Definition: auto_ptr:51
Auto_ptr(Auto_ptr const &o)
Copy construction, releases the original pointer.
Definition: auto_ptr:57
Smart pointer with automatic deletion.
Definition: auto_ptr:36
Auto_ptr & operator=(Auto_ptr const &o)
Assignment from another smart pointer.
Definition: auto_ptr:65
~Auto_ptr()
Destruction, shall delete the object.
Definition: auto_ptr:76
T * operator->() const
Member access for the object.
Definition: auto_ptr:83
T * release()
Release the object and get the normal pointer back.
Definition: auto_ptr:98
void reset(T *p=0)
Delete the object and reset the smart pointer to NULL.
Definition: auto_ptr:103
T Ref_type
The referenced type.
Definition: auto_ptr:41