]> git.mxchange.org Git - flightgear.git/blob - src/Include/auto_ptr.hxx
Small tweaks to initialization sequence and logic so we can default to
[flightgear.git] / src / Include / auto_ptr.hxx
1 /**************************************************************************
2  * auto_ptr.hxx -- A simple auto_ptr definition.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  *
18  * $Id$
19  **************************************************************************/
20
21 #ifndef _AUTO_PTR_HXX
22 #define _AUTO_PTR_HXX
23
24 #include "fg_stl_config.h"
25
26 //-----------------------------------------------------------------------------
27 //
28 // auto_ptr is initialised with a pointer obtained via new and deletes that
29 // object when it itself is destroyed (such as when leaving block scope).
30 // auto_ptr can be used in any way that a normal pointer can be.
31 //
32 // This class is only required when the STL doesn't supply one.
33 //
34 template <class X> class auto_ptr {
35 private:
36     X* ptr;
37     mutable bool owns;
38
39 public:
40     typedef X element_type;
41
42     explicit auto_ptr(X* p = 0) : ptr(p), owns(p) {}
43
44     auto_ptr(const auto_ptr& a) : ptr(a.ptr), owns(a.owns) {
45         a.owns = 0;
46     }
47
48 #ifdef _SG_MEMBER_TEMPLATES
49     template <class T> auto_ptr(const auto_ptr<T>& a)
50         : ptr(a.ptr), owns(a.owns) {
51         a.owns = 0;
52     }
53 #endif
54
55     auto_ptr& operator = (const auto_ptr& a) {
56         if (&a != this) {
57             if (owns)
58                 delete ptr;
59             owns = a.owns;
60             ptr = a.ptr;
61             a.owns = 0;
62         }
63     }
64
65 #ifdef _SG_MEMBER_TEMPLATES
66     template <class T> auto_ptr& operator = (const auto_ptr<T>& a) {
67         if (&a != this) {
68             if (owns)
69                 delete ptr;
70             owns = a.owns;
71             ptr = a.ptr;
72             a.owns = 0;
73         }
74     }
75 #endif
76
77     ~auto_ptr() {
78         if (owns)
79             delete ptr;
80     }
81
82     X& operator*() const { return *ptr; }
83     X* operator->() const { return ptr; }
84     X* get() const { return ptr; }
85     X* release() const { owns = false; return ptr; }
86 };
87
88 #endif /* _AUTO_PTR_HXX */
89