~ubuntu-branches/ubuntu/quantal/flightgear/quantal

« back to all changes in this revision

Viewing changes to src/Include/auto_ptr.hxx

  • Committer: Bazaar Package Importer
  • Author(s): Ove Kaaven
  • Date: 2002-03-27 21:50:15 UTC
  • Revision ID: james.westby@ubuntu.com-20020327215015-0rvi3o8iml0a8s93
Tags: upstream-0.7.9
ImportĀ upstreamĀ versionĀ 0.7.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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: auto_ptr.hxx,v 1.2 2001/03/23 21:59:19 curt Exp $
 
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