2005-12-18 Benjamin Kosnik <bkoz@redhat.com>
[official-gcc.git] / libstdc++-v3 / include / std / std_memory.h
blob6b1996b3b60b1fafed72fc9e44e8556c29c270cd
1 // <memory> -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2004, 2005 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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
45 * This is a Standard C++ Library header.
48 #ifndef _GLIBCXX_MEMORY
49 #define _GLIBCXX_MEMORY 1
51 #pragma GCC system_header
53 #include <bits/stl_algobase.h>
54 #include <bits/allocator.h>
55 #include <bits/stl_construct.h>
56 #include <bits/stl_iterator_base_types.h> //for iterator_traits
57 #include <bits/stl_uninitialized.h>
58 #include <bits/stl_raw_storage_iter.h>
59 #include <debug/debug.h>
60 #include <limits>
62 _GLIBCXX_BEGIN_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 const ptrdiff_t __max = numeric_limits<ptrdiff_t>::max() / sizeof(_Tp);
77 if (__len > __max)
78 __len = __max;
80 while (__len > 0)
82 _Tp* __tmp = static_cast<_Tp*>(::operator new(__len * sizeof(_Tp),
83 nothrow));
84 if (__tmp != 0)
85 return pair<_Tp*, ptrdiff_t>(__tmp, __len);
86 __len /= 2;
88 return pair<_Tp*, ptrdiff_t>(static_cast<_Tp*>(0), 0);
91 /**
92 * @brief Allocates a temporary buffer.
93 * @param len The number of objects of type Tp.
94 * @return See full description.
96 * Reinventing the wheel, but this time with prettier spokes!
98 * This function tries to obtain storage for @c len adjacent Tp
99 * objects. The objects themselves are not constructed, of course.
100 * A pair<> is returned containing "the buffer s address and
101 * capacity (in the units of sizeof(Tp)), or a pair of 0 values if
102 * no storage can be obtained." Note that the capacity obtained
103 * may be less than that requested if the memory is unavailable;
104 * you should compare len with the .second return value.
106 * Provides the nothrow exception guarantee.
108 template<typename _Tp>
109 inline pair<_Tp*, ptrdiff_t>
110 get_temporary_buffer(ptrdiff_t __len)
111 { return std::__get_temporary_buffer(__len, static_cast<_Tp*>(0)); }
114 * @brief The companion to get_temporary_buffer().
115 * @param p A buffer previously allocated by get_temporary_buffer.
116 * @return None.
118 * Frees the memory pointed to by p.
120 template<typename _Tp>
121 void
122 return_temporary_buffer(_Tp* __p)
123 { ::operator delete(__p, nothrow); }
126 * A wrapper class to provide auto_ptr with reference semantics.
127 * For example, an auto_ptr can be assigned (or constructed from)
128 * the result of a function which returns an auto_ptr by value.
130 * All the auto_ptr_ref stuff should happen behind the scenes.
132 template<typename _Tp1>
133 struct auto_ptr_ref
135 _Tp1* _M_ptr;
137 explicit
138 auto_ptr_ref(_Tp1* __p): _M_ptr(__p) { }
143 * @brief A simple smart pointer providing strict ownership semantics.
145 * The Standard says:
146 * <pre>
147 * An @c auto_ptr owns the object it holds a pointer to. Copying
148 * an @c auto_ptr copies the pointer and transfers ownership to the
149 * destination. If more than one @c auto_ptr owns the same object
150 * at the same time the behavior of the program is undefined.
152 * The uses of @c auto_ptr include providing temporary
153 * exception-safety for dynamically allocated memory, passing
154 * ownership of dynamically allocated memory to a function, and
155 * returning dynamically allocated memory from a function. @c
156 * auto_ptr does not meet the CopyConstructible and Assignable
157 * requirements for Standard Library <a
158 * href="tables.html#65">container</a> elements and thus
159 * instantiating a Standard Library container with an @c auto_ptr
160 * results in undefined behavior.
161 * </pre>
162 * Quoted from [20.4.5]/3.
164 * Good examples of what can and cannot be done with auto_ptr can
165 * be found in the libstdc++ testsuite.
167 * @if maint
168 * _GLIBCXX_RESOLVE_LIB_DEFECTS
169 * 127. auto_ptr<> conversion issues
170 * These resolutions have all been incorporated.
171 * @endif
173 template<typename _Tp>
174 class auto_ptr
176 private:
177 _Tp* _M_ptr;
179 public:
180 /// The pointed-to type.
181 typedef _Tp element_type;
184 * @brief An %auto_ptr is usually constructed from a raw pointer.
185 * @param p A pointer (defaults to NULL).
187 * This object now @e owns the object pointed to by @a p.
189 explicit
190 auto_ptr(element_type* __p = 0) throw() : _M_ptr(__p) { }
193 * @brief An %auto_ptr can be constructed from another %auto_ptr.
194 * @param a Another %auto_ptr of the same type.
196 * This object now @e owns the object previously owned by @a a,
197 * which has given up ownsership.
199 auto_ptr(auto_ptr& __a) throw() : _M_ptr(__a.release()) { }
202 * @brief An %auto_ptr can be constructed from another %auto_ptr.
203 * @param a Another %auto_ptr of a different but related type.
205 * A pointer-to-Tp1 must be convertible to a
206 * pointer-to-Tp/element_type.
208 * This object now @e owns the object previously owned by @a a,
209 * which has given up ownsership.
211 template<typename _Tp1>
212 auto_ptr(auto_ptr<_Tp1>& __a) throw() : _M_ptr(__a.release()) { }
215 * @brief %auto_ptr assignment operator.
216 * @param a Another %auto_ptr of the same type.
218 * This object now @e owns the object previously owned by @a a,
219 * which has given up ownsership. The object that this one @e
220 * used to own and track has been deleted.
222 auto_ptr&
223 operator=(auto_ptr& __a) throw()
225 reset(__a.release());
226 return *this;
230 * @brief %auto_ptr assignment operator.
231 * @param a Another %auto_ptr of a different but related type.
233 * A pointer-to-Tp1 must be convertible to a pointer-to-Tp/element_type.
235 * This object now @e owns the object previously owned by @a a,
236 * which has given up ownsership. The object that this one @e
237 * used to own and track has been deleted.
239 template<typename _Tp1>
240 auto_ptr&
241 operator=(auto_ptr<_Tp1>& __a) throw()
243 reset(__a.release());
244 return *this;
248 * When the %auto_ptr goes out of scope, the object it owns is
249 * deleted. If it no longer owns anything (i.e., @c get() is
250 * @c NULL), then this has no effect.
252 * @if maint
253 * The C++ standard says there is supposed to be an empty throw
254 * specification here, but omitting it is standard conforming. Its
255 * presence can be detected only if _Tp::~_Tp() throws, but this is
256 * prohibited. [17.4.3.6]/2
257 * @endif
259 ~auto_ptr() { delete _M_ptr; }
262 * @brief Smart pointer dereferencing.
264 * If this %auto_ptr no longer owns anything, then this
265 * operation will crash. (For a smart pointer, "no longer owns
266 * anything" is the same as being a null pointer, and you know
267 * what happens when you dereference one of those...)
269 element_type&
270 operator*() const throw()
272 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
273 return *_M_ptr;
277 * @brief Smart pointer dereferencing.
279 * This returns the pointer itself, which the language then will
280 * automatically cause to be dereferenced.
282 element_type*
283 operator->() const throw()
285 _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
286 return _M_ptr;
290 * @brief Bypassing the smart pointer.
291 * @return The raw pointer being managed.
293 * You can get a copy of the pointer that this object owns, for
294 * situations such as passing to a function which only accepts
295 * a raw pointer.
297 * @note This %auto_ptr still owns the memory.
299 element_type*
300 get() const throw() { return _M_ptr; }
303 * @brief Bypassing the smart pointer.
304 * @return The raw pointer being managed.
306 * You can get a copy of the pointer that this object owns, for
307 * situations such as passing to a function which only accepts
308 * a raw pointer.
310 * @note This %auto_ptr no longer owns the memory. When this object
311 * goes out of scope, nothing will happen.
313 element_type*
314 release() throw()
316 element_type* __tmp = _M_ptr;
317 _M_ptr = 0;
318 return __tmp;
322 * @brief Forcibly deletes the managed object.
323 * @param p A pointer (defaults to NULL).
325 * This object now @e owns the object pointed to by @a p. The
326 * previous object has been deleted.
328 void
329 reset(element_type* __p = 0) throw()
331 if (__p != _M_ptr)
333 delete _M_ptr;
334 _M_ptr = __p;
338 /**
339 * @brief Automatic conversions
341 * These operations convert an %auto_ptr into and from an auto_ptr_ref
342 * automatically as needed. This allows constructs such as
343 * @code
344 * auto_ptr<Derived> func_returning_auto_ptr(.....);
345 * ...
346 * auto_ptr<Base> ptr = func_returning_auto_ptr(.....);
347 * @endcode
349 auto_ptr(auto_ptr_ref<element_type> __ref) throw()
350 : _M_ptr(__ref._M_ptr) { }
352 auto_ptr&
353 operator=(auto_ptr_ref<element_type> __ref) throw()
355 if (__ref._M_ptr != this->get())
357 delete _M_ptr;
358 _M_ptr = __ref._M_ptr;
360 return *this;
363 template<typename _Tp1>
364 operator auto_ptr_ref<_Tp1>() throw()
365 { return auto_ptr_ref<_Tp1>(this->release()); }
367 template<typename _Tp1>
368 operator auto_ptr<_Tp1>() throw()
369 { return auto_ptr<_Tp1>(this->release()); }
372 _GLIBCXX_END_NAMESPACE
374 #endif /* _GLIBCXX_MEMORY */