Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
_autoptr.h
Go to the documentation of this file.
1// -*- C++ -*-
2
33#ifndef _RWSTD_RW_AUTOPTR_H_INCLUDED
34#define _RWSTD_RW_AUTOPTR_H_INCLUDED
35
36#ifndef _RWSTD_RW_DEFS_H_INCLUDED
37# include <rw/_defs.h>
38#endif // _RWSTD_RW_DEFS_H_INCLUDED
39
40
42
43
44template <class _TypeT>
45struct __rw_nonvoid_ref
46{
47 typedef _TypeT& _C_ref;
48};
49
50
52struct __rw_nonvoid_ref<void>
53{
54 // makes the declaration of auto_ptr<void>::operator*() well-formed
55 // (the function definition itself may still be ill-formed and must
56 // not be instantiated)
57 typedef void _C_ref;
58};
59
60#ifndef _RWSTD_NO_CV_VOID_SPECIALIZATIONS
61
63struct __rw_nonvoid_ref<const void>
64{
65 typedef void _C_ref;
66};
67
69struct __rw_nonvoid_ref<volatile void>
70{
71 typedef void _C_ref;
72};
73
75struct __rw_nonvoid_ref<const volatile void>
76{
77 typedef void _C_ref;
78};
79
80#endif // _RWSTD_NO_CV_VOID_SPECIALIZATIONS
81
82} // namespace __rw
83
84
86
87
88// 20.4.5 - Template class auto_ptr
89
90template <class _TypeT>
91class auto_ptr;
92
93
94// 20.4.5, p2 (defined outside of auto_ptr<> according to the proposed
95// resolution of lwg issue 127)
96template <class _TypeT>
97class auto_ptr_ref
98{
99 template <class _TypeU>
100 friend class auto_ptr;
101
102 // implicit conversion from auto_ptr<U> available only
103 // if U* is implicitly convertible to T* (via inheritance)
104 template <class _TypeU>
105 auto_ptr_ref (auto_ptr<_TypeU>& __rhs, const _TypeU* = 0) _THROWS (())
106 : _C_ptr (_RWSTD_REINTERPRET_CAST (auto_ptr<_TypeT>&, __rhs)) { }
107
108 // reference to the owning auto_ptr object
109 auto_ptr<_TypeT>& _C_ptr;
110};
111
112
113template<class _TypeT>
114class auto_ptr
115{
116public:
117 typedef _TypeT element_type;
118
119 // 20.4.5.1, p1
120 explicit auto_ptr (element_type* __p = 0) _THROWS (())
121 : _C_ptr (__p) { }
122
123 // 20.4.5.1, p2
124 auto_ptr (auto_ptr& __rhs) _THROWS (())
125 : _C_ptr (__rhs.release ()) { }
126
127 // 20.4.5.1, p7
128 auto_ptr& operator= (auto_ptr& __rhs) _THROWS (()) {
129 reset (__rhs.release ());
130 return *this;
131 }
132
133 // 20.4.5.1, p13
134 ~auto_ptr () _THROWS (()) {
135 delete _C_ptr;
136 }
137
138 // 20.4.5.1, p4
139 template <class _TypeU>
140 auto_ptr (auto_ptr<_TypeU>& __rhs) _THROWS (())
141 : _C_ptr (__rhs.release ()) { }
142
143 // 20.4.5.1, p10
144 template <class _TypeU>
145 auto_ptr& operator= (auto_ptr<_TypeU>& __rhs) _THROWS (()) {
146 reset (__rhs.release ());
147 return *this;
148 }
149
150 // 20.4.5.3, p3
151 template <class _TypeU>
152 operator auto_ptr_ref<_TypeU>() _THROWS (()) {
153 // convertible only if T* is implicitly convertible to U*
154 return auto_ptr_ref<_TypeU>(*this, _C_ptr);
155 }
156
157 // 20.4.5.3, p4
158 template <class _TypeU>
159 operator auto_ptr<_TypeU>() _THROWS (()) {
160 return auto_ptr<_TypeU>(release ());
161 }
162
163 // 20.4.5.2, p1
164 typename _RW::__rw_nonvoid_ref<element_type>::_C_ref
165 operator* () const _THROWS (()) {
166 _RWSTD_ASSERT (0 != get ());
167 return *get ();
168 }
169
170#ifndef _RWSTD_NO_NONCLASS_ARROW_RETURN
171
172 // 20.4.5.2, p3
173 element_type* operator->() const _THROWS (()) {
174 // avoid using the _RWSTD_OPERATOR_ARROW() helper macro
175 // (defined in terms of operator*()) in order to permit
176 // auto_ptr<void>::operator->() to be instantiated
177 return _C_ptr;
178 }
179
180#endif // _RWSTD_NO_NONCLASS_ARROW_RETURN
181
182 // 20.4.5.2, p4
183 element_type* get () const _THROWS (()) {
184 return _C_ptr;
185 }
186
187 // 20.4.5.2, p5
188 element_type* release () _THROWS (()) {
189 element_type* __tmp = _C_ptr;
190 _C_ptr = 0;
191 return __tmp;
192 }
193
194 // 20.4.5.2, p7
195 void reset (element_type* __p = 0) _THROWS (()) {
196 if (_C_ptr != __p) {
197 delete _C_ptr;
198 _C_ptr = __p;
199 }
200 }
201
202 // 20.4.5.3, p1
203 auto_ptr (auto_ptr_ref<element_type> __r) _THROWS (())
204 : _C_ptr (__r._C_ptr.release ()) { }
205
206 // 20.4.5.3, p? - follows lwg issue 127
207 auto_ptr& operator= (auto_ptr_ref<element_type> __rhs) _THROWS (()) {
208 reset (__rhs._C_ptr.release ());
209 return *this;
210 }
211
212private:
213 element_type* _C_ptr;
214};
215
216
217} // namespace std
218
219
220#endif // _RWSTD_RW_AUTOPTR_H_INCLUDED
const plus_asgn< T > & operator*(const plus_asgn< T > &lhs, const plus_asgn< T > &rhs)
#define _RWSTD_SPECIALIZED_CLASS
#define _THROWS()
#define _RWSTD_REINTERPRET_CAST(T, x)
Definition _defs.h:535
#define _RWSTD_NAMESPACE(name)
Definition _defs.h:567
#define _RWSTD_ASSERT(ignore)
Definition _defs.h:1108
Definition json.h:5363