OSDN Git Service

2000-04-21 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / 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 #include <bits/stl_algobase.h>
19 #include <bits/stl_alloc.h>
20 #include <bits/stl_construct.h>
21 #include <bits/stl_iterator_base.h> //for iterator_traits
22 #include <bits/stl_tempbuf.h>
23 #include <bits/stl_uninitialized.h>
24 #include <bits/stl_raw_storage_iter.h>
25
26 __STL_BEGIN_NAMESPACE
27
28 #if defined(__SGI_STL_USE_AUTO_PTR_CONVERSIONS) && \
29      defined(__STL_MEMBER_TEMPLATES)
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 #endif
37
38 template <class _Tp> class auto_ptr {
39 private:
40   _Tp* _M_ptr;
41
42 public:
43   typedef _Tp element_type;
44
45   explicit auto_ptr(_Tp* __p = 0) __STL_NOTHROW : _M_ptr(__p) {}
46   auto_ptr(auto_ptr& __a) __STL_NOTHROW : _M_ptr(__a.release()) {}
47
48 #ifdef __STL_MEMBER_TEMPLATES
49   template <class _Tp1> auto_ptr(auto_ptr<_Tp1>& __a) __STL_NOTHROW
50     : _M_ptr(__a.release()) {}
51 #endif /* __STL_MEMBER_TEMPLATES */
52
53   auto_ptr& operator=(auto_ptr& __a) __STL_NOTHROW {
54     reset(__a.release());
55     return *this;
56   }
57
58 #ifdef __STL_MEMBER_TEMPLATES
59   template <class _Tp1>
60   auto_ptr& operator=(auto_ptr<_Tp1>& __a) __STL_NOTHROW {
61     reset(__a.release());
62     return *this;
63   }
64 #endif /* __STL_MEMBER_TEMPLATES */
65
66   ~auto_ptr() __STL_NOTHROW { delete _M_ptr; }
67
68   _Tp& operator*() const __STL_NOTHROW {
69     return *_M_ptr;
70   }
71   _Tp* operator->() const __STL_NOTHROW {
72     return _M_ptr;
73   }
74   _Tp* get() const __STL_NOTHROW {
75     return _M_ptr;
76   }
77   _Tp* release() __STL_NOTHROW {
78     _Tp* __tmp = _M_ptr;
79     _M_ptr = 0;
80     return __tmp;
81   }
82   void reset(_Tp* __p = 0) __STL_NOTHROW {
83     if (__p != _M_ptr) {
84       delete _M_ptr;
85       _M_ptr = __p;
86     }    
87   }
88
89   // According to the C++ standard, these conversions are required.  Most
90   // present-day compilers, however, do not enforce that requirement---and, 
91   // in fact, most present-day compilers do not support the language 
92   // features that these conversions rely on.
93   
94 #if defined(__SGI_STL_USE_AUTO_PTR_CONVERSIONS) && \
95     defined(__STL_MEMBER_TEMPLATES)
96
97 public:
98   auto_ptr(auto_ptr_ref<_Tp> __ref) __STL_NOTHROW
99     : _M_ptr(__ref._M_ptr) {}
100
101   auto_ptr& operator=(auto_ptr_ref<_Tp> __ref) __STL_NOTHROW {
102     if (__ref._M_ptr != this->get()) {
103       delete _M_ptr;
104       _M_ptr = __ref._M_ptr;
105     }
106     return *this;
107   }
108
109   template <class _Tp1> operator auto_ptr_ref<_Tp1>() __STL_NOTHROW 
110     { return auto_ptr_ref<_Tp>(this->release()); }
111   template <class _Tp1> operator auto_ptr<_Tp1>() __STL_NOTHROW
112     { return auto_ptr<_Tp1>(this->release()); }
113
114 #endif /* auto ptr conversions && member templates */
115 };
116
117 __STL_END_NAMESPACE
118
119 #endif /* _CPP_MEMORY */
120
121
122 // Local Variables:
123 // mode:C++
124 // End: