fixes for host gcc 4.6.1
[zpugcc/jano.git] / toolchain / gcc / libstdc++-v3 / include / std / std_memory.h
blob4e6641ef673998e3f5e612d8a9829d83594309bd
1 // <memory> -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2004 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
31 * Copyright (c) 1997-1999
32 * Silicon Graphics Computer Systems, Inc.
34 * Permission to use, copy, modify, distribute and sell this software
35 * and its documentation for any purpose is hereby granted without fee,
36 * provided that the above copyright notice appear in all copies and
37 * that both that copyright notice and this permission notice appear
38 * in supporting documentation. Silicon Graphics makes no
39 * representations about the suitability of this software for any
40 * purpose. It is provided "as is" without express or implied warranty.
44 /** @file memory
45 * This is a Standard C++ Library header. You should @c #include this header
46 * in your programs, rather than any of the "st[dl]_*.h" implementation files.
49 #ifndef _GLIBCXX_MEMORY
50 #define _GLIBCXX_MEMORY 1
52 #pragma GCC system_header
54 #include <bits/stl_algobase.h>
55 #include <bits/allocator.h>
56 #include <bits/stl_construct.h>
57 #include <bits/stl_iterator_base_types.h> //for iterator_traits
58 #include <bits/stl_uninitialized.h>
59 #include <bits/stl_raw_storage_iter.h>
60 #include <debug/debug.h>
62 namespace std
64 /**
65 * @if maint
66 * This is a helper function. The unused second parameter exists to
67 * permit the real get_temporary_buffer to use template parameter deduction.
69 * XXX This should perhaps use the pool.
70 * @endif
72 template<typename _Tp>
73 pair<_Tp*, ptrdiff_t>
74 __get_temporary_buffer(ptrdiff_t __len, _Tp*)
76 if (__len > ptrdiff_t(INT_MAX / sizeof(_Tp)))
77 __len = INT_MAX / sizeof(_Tp);
79 while (__len > 0)
81 _Tp* __tmp = static_cast<_Tp*>(::operator new(__len * sizeof(_Tp),
82 nothrow));
83 if (__tmp != 0)
84 return pair<_Tp*, ptrdiff_t>(__tmp, __len);
85 __len /= 2;
87 return pair<_Tp*, ptrdiff_t>(static_cast<_Tp*>(0), 0);
90 /**
91 * @brief Allocates a temporary buffer.
92 * @param len The number of objects of type Tp.
93 * @return See full description.
95 * Reinventing the wheel, but this time with prettier spokes!
97 * This function tries to obtain storage for @c len adjacent Tp
98 * objects. The objects themselves are not constructed, of course.
99 * A pair<> is returned containing "the buffer s address and
100 * capacity (in the units of sizeof(Tp)), or a pair of 0 values if
101 * no storage can be obtained." Note that the capacity obtained
102 * may be less than that requested if the memory is unavailable;
103 * you should compare len with the .second return value.
105 * Provides the nothrow exception guarantee.
107 template<typename _Tp>
108 inline pair<_Tp*,ptrdiff_t>
109 get_temporary_buffer(ptrdiff_t __len)
110 { return std::__get_temporary_buffer(__len, static_cast<_Tp*>(0)); }
113 * @brief The companion to get_temporary_buffer().
114 * @param p A buffer previously allocated by get_temporary_buffer.
115 * @return None.
117 * Frees the memory pointed to by p.
119 template<typename _Tp>
120 void
121 return_temporary_buffer(_Tp* __p)
122 { ::operator delete(__p, nothrow); }
125 * A wrapper class to provide auto_ptr with reference semantics.
126 * For example, an auto_ptr can be assigned (or constructed from)
127 * the result of a function which returns an auto_ptr by value.
129 * All the auto_ptr_ref stuff should happen behind the scenes.
131 template<typename _Tp1>
132 struct auto_ptr_ref
134 _Tp1* _M_ptr;
136 explicit
137 auto_ptr_ref(_Tp1* __p): _M_ptr(__p) { }
142 * @brief A simple smart pointer providing strict ownership semantics.
144 * The Standard says:
145 * <pre>
146 * An @c auto_ptr owns the object it holds a pointer to. Copying
147 * an @c auto_ptr copies the pointer and transfers ownership to the
148 * destination. If more than one @c auto_ptr owns the same object
149 * at the same time the behavior of the program is undefined.
151 * The uses of @c auto_ptr include providing temporary
152 * exception-safety for dynamically allocated memory, passing
153 * ownership of dynamically allocated memory to a function, and
154 * returning dynamically allocated memory from a function. @c
155 * auto_ptr does not meet the CopyConstructible and Assignable
156 * requirements for Standard Library <a
157 * href="tables.html#65">container</a> elements and thus
158 * instantiating a Standard Library container with an @c auto_ptr
159 * results in undefined behavior.
160 * </pre>
161 * Quoted from [20.4.5]/3.
163 * Good examples of what can and cannot be done with auto_ptr can
164 * be found in the libstdc++ testsuite.
166 * @if maint
167 * _GLIBCXX_RESOLVE_LIB_DEFECTS
168 * 127. auto_ptr<> conversion issues
169 * These resolutions have all been incorporated.
170 * @endif
172 template<typename _Tp>
173 class auto_ptr
175 private:
176 _Tp* _M_ptr;
178 public:
179 /// The pointed-to type.
180 typedef _Tp element_type;
183 * @brief An %auto_ptr is usually constructed from a raw pointer.
184 * @param p A pointer (defaults to NULL).
186 * This object now @e owns the object pointed to by @a p.
188 explicit
189 auto_ptr(element_type* __p = 0) throw() : _M_ptr(__p) { }
192 * @brief An %auto_ptr can be constructed from another %auto_ptr.
193 * @param a Another %auto_ptr of the same type.
195 * This object now @e owns the object previously owned by @a a,
196 * which has given up ownsership.
198 auto_ptr(auto_ptr& __a) throw() : _M_ptr(__a.release()) { }
201 * @brief An %auto_ptr can be constructed from another %auto_ptr.
202 * @param a Another %auto_ptr of a different but related type.
204 * A pointer-to-Tp1 must be convertible to a
205 * pointer-to-Tp/element_type.
207 * This object now @e owns the object previously owned by @a a,
208 * which has given up ownsership.
210 template<typename _Tp1>
211 auto_ptr(auto_ptr<_Tp1>& __a) throw() : _M_ptr(__a.release()) { }
214 * @brief %auto_ptr assignment operator.
215 * @param a Another %auto_ptr of the same type.
217 * This object now @e owns the object previously owned by @a a,
218 * which has given up ownsership. The object that this one @e
219 * used to own and track has been deleted.
221 auto_ptr&
222 operator=(auto_ptr& __a) throw()
224 reset(__a.release());
225 return *this;
229 * @brief %auto_ptr assignment operator.
230 * @param a Another %auto_ptr of a different but related type.
232 * A pointer-to-Tp1 must be convertible to a pointer-to-Tp/element_type.
234 * This object now @e owns the object previously owned by @a a,
235 * which has given up ownsership. The object that this one @e
236 * used to own and track has been deleted.
238 template<typename _Tp1>
239 auto_ptr&
240 operator=(auto_ptr<_Tp1>& __a) throw()
242 reset(__a.release());
243 return *this;
247 * When the %auto_ptr goes out of scope, the object it owns is
248 * deleted. If it no longer owns anything (i.e., @c get() is
249 * @c NULL), then this has no effect.
251 * @if maint
252 * The C++ standard says there is supposed to be an empty throw
253 * specification here, but omitting it is standard conforming. Its
254 * presence can be detected only if _Tp::~_Tp() throws, but this is
255 * prohibited. [17.4.3.6]/2
256 * @end maint
258 ~auto_ptr() { delete _M_ptr; }
261 * @brief Smart pointer dereferencing.
263 * If this %auto_ptr no longer owns anything, then this
264 * operation will crash. (For a smart pointer, "no longer owns
265 * anything" is the same as being a null pointer, and you know
266 * what happens when you dereference one of those...)
268 element_type&
269 operator*() const throw()
271 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
272 return *_M_ptr;
276 * @brief Smart pointer dereferencing.
278 * This returns the pointer itself, which the language then will
279 * automatically cause to be dereferenced.
281 element_type*
282 operator->() const throw()
284 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
285 return _M_ptr;
289 * @brief Bypassing the smart pointer.
290 * @return The raw pointer being managed.
292 * You can get a copy of the pointer that this object owns, for
293 * situations such as passing to a function which only accepts
294 * a raw pointer.
296 * @note This %auto_ptr still owns the memory.
298 element_type*
299 get() const throw() { return _M_ptr; }
302 * @brief Bypassing the smart pointer.
303 * @return The raw pointer being managed.
305 * You can get a copy of the pointer that this object owns, for
306 * situations such as passing to a function which only accepts
307 * a raw pointer.
309 * @note This %auto_ptr no longer owns the memory. When this object
310 * goes out of scope, nothing will happen.
312 element_type*
313 release() throw()
315 element_type* __tmp = _M_ptr;
316 _M_ptr = 0;
317 return __tmp;
321 * @brief Forcibly deletes the managed object.
322 * @param p A pointer (defaults to NULL).
324 * This object now @e owns the object pointed to by @a p. The
325 * previous object has been deleted.
327 void
328 reset(element_type* __p = 0) throw()
330 if (__p != _M_ptr)
332 delete _M_ptr;
333 _M_ptr = __p;
337 /** @{
338 * @brief Automatic conversions
340 * These operations convert an %auto_ptr into and from an auto_ptr_ref
341 * automatically as needed. This allows constructs such as
342 * @code
343 * auto_ptr<Derived> func_returning_auto_ptr(.....);
344 * ...
345 * auto_ptr<Base> ptr = func_returning_auto_ptr(.....);
346 * @endcode
348 auto_ptr(auto_ptr_ref<element_type> __ref) throw()
349 : _M_ptr(__ref._M_ptr) { }
351 auto_ptr&
352 operator=(auto_ptr_ref<element_type> __ref) throw()
354 if (__ref._M_ptr != this->get())
356 delete _M_ptr;
357 _M_ptr = __ref._M_ptr;
359 return *this;
362 template<typename _Tp1>
363 operator auto_ptr_ref<_Tp1>() throw()
364 { return auto_ptr_ref<_Tp1>(this->release()); }
366 template<typename _Tp1>
367 operator auto_ptr<_Tp1>() throw()
368 { return auto_ptr<_Tp1>(this->release()); }
369 /** @} */
371 } // namespace std
373 #endif /* _GLIBCXX_MEMORY */