OSDN Git Service

2001-06-18 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / std_memory.h
1 /*
2  * Copyright (c) 1997-1999
3  * Silicon Graphics Computer Systems, Inc.
4  *
5  * Permission to use, copy, modify, distribute and sell this software
6  * and its documentation for any purpose is hereby granted without fee,
7  * provided that the above copyright notice appear in all copies and
8  * that both that copyright notice and this permission notice appear
9  * in supporting documentation.  Silicon Graphics makes no
10  * representations about the suitability of this software for any
11  * purpose.  It is provided "as is" without express or implied warranty.
12  *
13  */
14
15 #ifndef _CPP_MEMORY
16 #define _CPP_MEMORY 1
17
18 #pragma GCC system_header
19
20 #include <bits/stl_algobase.h>
21 #include <bits/stl_alloc.h>
22 #include <bits/stl_construct.h>
23 #include <bits/stl_iterator_base_types.h> //for iterator_traits
24 #include <bits/stl_tempbuf.h>
25 #include <bits/stl_uninitialized.h>
26 #include <bits/stl_raw_storage_iter.h>
27
28 namespace std
29 {
30
31  template<class _Tp1> struct auto_ptr_ref {
32    _Tp1* _M_ptr;
33    auto_ptr_ref(_Tp1* __p) : _M_ptr(__p) {}
34 };
35
36 template <class _Tp> class auto_ptr {
37 private:
38   _Tp* _M_ptr;
39
40 public:
41   typedef _Tp element_type;
42
43   explicit auto_ptr(_Tp* __p = 0) __STL_NOTHROW : _M_ptr(__p) {}
44   auto_ptr(auto_ptr& __a) __STL_NOTHROW : _M_ptr(__a.release()) {}
45
46   template <class _Tp1> auto_ptr(auto_ptr<_Tp1>& __a) __STL_NOTHROW
47     : _M_ptr(__a.release()) {}
48
49   auto_ptr& operator=(auto_ptr& __a) __STL_NOTHROW {
50     reset(__a.release());
51     return *this;
52   }
53
54   template <class _Tp1>
55   auto_ptr& operator=(auto_ptr<_Tp1>& __a) __STL_NOTHROW {
56     reset(__a.release());
57     return *this;
58   }
59   
60   // Note: The C++ standard says there is supposed to be an empty throw
61   // specification here, but omitting it is standard conforming.  Its 
62   // presence can be detected only if _Tp::~_Tp() throws, but (17.4.3.6/2)
63   // this is prohibited.
64   ~auto_ptr() { delete _M_ptr; }
65  
66   _Tp& operator*() const __STL_NOTHROW {
67     return *_M_ptr;
68   }
69   _Tp* operator->() const __STL_NOTHROW {
70     return _M_ptr;
71   }
72   _Tp* get() const __STL_NOTHROW {
73     return _M_ptr;
74   }
75   _Tp* release() __STL_NOTHROW {
76     _Tp* __tmp = _M_ptr;
77     _M_ptr = 0;
78     return __tmp;
79   }
80   void reset(_Tp* __p = 0) __STL_NOTHROW {
81     if (__p != _M_ptr) {
82       delete _M_ptr;
83       _M_ptr = __p;
84     }    
85   }
86
87   // According to the C++ standard, these conversions are required.  Most
88   // present-day compilers, however, do not enforce that requirement---and, 
89   // in fact, most present-day compilers do not support the language 
90   // features that these conversions rely on.
91 public:
92   auto_ptr(auto_ptr_ref<_Tp> __ref) __STL_NOTHROW
93     : _M_ptr(__ref._M_ptr) {}
94
95   auto_ptr& operator=(auto_ptr_ref<_Tp> __ref) __STL_NOTHROW {
96     if (__ref._M_ptr != this->get()) {
97       delete _M_ptr;
98       _M_ptr = __ref._M_ptr;
99     }
100     return *this;
101   }
102
103   template <class _Tp1> operator auto_ptr_ref<_Tp1>() __STL_NOTHROW 
104     { return auto_ptr_ref<_Tp>(this->release()); }
105   template <class _Tp1> operator auto_ptr<_Tp1>() __STL_NOTHROW
106     { return auto_ptr<_Tp1>(this->release()); }
107 };
108
109 } // namespace std
110
111 #endif /* _CPP_MEMORY */
112
113
114 // Local Variables:
115 // mode:C++
116 // End: