]> gcc.gnu.org Git - gcc.git/blame - libstdc++-v3/include/std/std_memory.h
Move from CPP to CXX.
[gcc.git] / libstdc++-v3 / include / std / std_memory.h
CommitLineData
54c1bf78 1// <memory> -*- C++ -*-
de96ac46 2
6b20f9b5 3// Copyright (C) 2001, 2002 Free Software Foundation, Inc.
de96ac46
BK
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.
10
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.
15
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.
20
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.
29
54c1bf78
BK
30/*
31 * Copyright (c) 1997-1999
32 * Silicon Graphics Computer Systems, Inc.
33 *
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.
41 *
42 */
43
ffe94f83
PE
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.
2f9d51b8
PE
47 */
48
3d7c150e
BK
49#ifndef _MEMORY
50#define _MEMORY 1
54c1bf78
BK
51
52#pragma GCC system_header
53
54#include <bits/stl_algobase.h>
1ff9402d 55#include <bits/allocator.h>
54c1bf78
BK
56#include <bits/stl_construct.h>
57#include <bits/stl_iterator_base_types.h> //for iterator_traits
54c1bf78
BK
58#include <bits/stl_uninitialized.h>
59#include <bits/stl_raw_storage_iter.h>
60
61namespace std
62{
16ad69a1 63 /**
5acf59f8
BK
64 * @if maint
65 * This is a helper function. The unused second parameter exists to
66 * permit the real get_temporary_buffer to use template parameter deduction.
16ad69a1 67 *
5acf59f8
BK
68 * XXX This should perhaps use the pool.
69 * @endif
70 */
71 template<typename _Tp>
72 pair<_Tp*, ptrdiff_t>
73 __get_temporary_buffer(ptrdiff_t __len, _Tp*)
16ad69a1 74 {
5acf59f8
BK
75 if (__len > ptrdiff_t(INT_MAX / sizeof(_Tp)))
76 __len = INT_MAX / sizeof(_Tp);
77
78 while (__len > 0)
79 {
80 _Tp* __tmp = (_Tp*) std::malloc((std::size_t)__len * sizeof(_Tp));
81 if (__tmp != 0)
82 return pair<_Tp*, ptrdiff_t>(__tmp, __len);
83 __len /= 2;
84 }
85 return pair<_Tp*, ptrdiff_t>((_Tp*)0, 0);
16ad69a1
JR
86 }
87
88 /**
5acf59f8
BK
89 * @brief This is a mostly-useless wrapper around malloc().
90 * @param len The number of objects of type Tp.
91 * @return See full description.
16ad69a1 92 *
5acf59f8 93 * Reinventing the wheel, but this time with prettier spokes!
16ad69a1 94 *
5acf59f8
BK
95 * This function tries to obtain storage for @c len adjacent Tp objects.
96 * The objects themselves are not constructed, of course. A pair<> is
97 * returned containing "the buffer s address and capacity (in the units of
98 * sizeof(Tp)), or a pair of 0 values if no storage can be obtained."
99 * Note that the capacity obtained may be less than that requested if the
100 * memory is unavailable; you should compare len with the .second return
101 * value.
102 */
103 template<typename _Tp>
104 inline pair<_Tp*,ptrdiff_t>
105 get_temporary_buffer(ptrdiff_t __len)
106 { return __get_temporary_buffer(__len, (_Tp*) 0); }
16ad69a1
JR
107
108 /**
5acf59f8
BK
109 * @brief The companion to get_temporary_buffer().
110 * @param p A buffer previously allocated by get_temporary_buffer.
111 * @return None.
16ad69a1 112 *
5acf59f8
BK
113 * Frees the memory pointed to by p.
114 */
115 template<typename _Tp>
116 void
117 return_temporary_buffer(_Tp* __p)
118 { std::free(__p); }
54c1bf78 119
16ad69a1 120 /**
5acf59f8
BK
121 * A wrapper class to provide auto_ptr with reference semantics. For
122 * example, an auto_ptr can be assigned (or constructed from) the result of
123 * a function which returns an auto_ptr by value.
16ad69a1 124 *
5acf59f8
BK
125 * All the auto_ptr_ref stuff should happen behind the scenes.
126 */
127 template<typename _Tp1>
128 struct auto_ptr_ref
129 {
130 _Tp1* _M_ptr;
131
132 explicit
133 auto_ptr_ref(_Tp1* __p): _M_ptr(__p) { }
134 };
16ad69a1 135
16ad69a1
JR
136
137 /**
5acf59f8 138 * @brief A simple smart pointer providing strict ownership semantics.
16ad69a1 139 *
5acf59f8
BK
140 * The Standard says:
141 * <pre>
142 * An @c auto_ptr owns the object it holds a pointer to. Copying an
143 * @c auto_ptr copies the pointer and transfers ownership to the destination.
144 * If more than one @c auto_ptr owns the same object at the same time the
145 * behavior of the program is undefined.
16ad69a1 146 *
5acf59f8
BK
147 * The uses of @c auto_ptr include providing temporary exception-safety for
148 * dynamically allocated memory, passing ownership of dynamically allocated
149 * memory to a function, and returning dynamically allocated memory from a
150 * function. @c auto_ptr does not meet the CopyConstructible and Assignable
151 * requirements for Standard Library <a href="tables.html#65">container</a>
152 * elements and thus instantiating a Standard Library container with an
153 * @c auto_ptr results in undefined behavior.
154 * </pre>
155 * Quoted from [20.4.5]/3.
16ad69a1 156 *
5acf59f8
BK
157 * Good examples of what can and cannot be done with auto_ptr can be found
158 * in the libstdc++ testsuite.
16ad69a1 159 *
5acf59f8 160 * @if maint
3d7c150e 161 * _GLIBCXX_RESOLVE_LIB_DEFECTS
5acf59f8
BK
162 * 127. auto_ptr<> conversion issues
163 * These resolutions have all been incorporated.
164 * @endif
165 */
166 template<typename _Tp>
167 class auto_ptr
16ad69a1 168 {
5acf59f8
BK
169 private:
170 _Tp* _M_ptr;
171
172 public:
173 /// The pointed-to type.
174 typedef _Tp element_type;
175
176 /**
177 * @brief An %auto_ptr is usually constructed from a raw pointer.
178 * @param p A pointer (defaults to NULL).
179 *
180 * This object now @e owns the object pointed to by @a p.
181 */
182 explicit
183 auto_ptr(element_type* __p = 0) throw() : _M_ptr(__p) { }
184
185 /**
186 * @brief An %auto_ptr can be constructed from another %auto_ptr.
187 * @param a Another %auto_ptr of the same type.
188 *
189 * This object now @e owns the object previously owned by @a a,
190 * which has given up ownsership.
191 */
192 auto_ptr(auto_ptr& __a) throw() : _M_ptr(__a.release()) { }
193
194 /**
195 * @brief An %auto_ptr can be constructed from another %auto_ptr.
196 * @param a Another %auto_ptr of a different but related type.
197 *
198 * A pointer-to-Tp1 must be convertible to a pointer-to-Tp/element_type.
199 *
200 * This object now @e owns the object previously owned by @a a,
201 * which has given up ownsership.
202 */
203 template<typename _Tp1>
204 auto_ptr(auto_ptr<_Tp1>& __a) throw() : _M_ptr(__a.release()) { }
205
206 /**
207 * @brief %auto_ptr assignment operator.
208 * @param a Another %auto_ptr of the same type.
209 *
210 * This object now @e owns the object previously owned by @a a,
211 * which has given up ownsership. The object that this one @e
212 * used to own and track has been deleted.
213 */
214 auto_ptr&
215 operator=(auto_ptr& __a) throw()
216 {
217 reset(__a.release());
218 return *this;
219 }
220
221 /**
222 * @brief %auto_ptr assignment operator.
223 * @param a Another %auto_ptr of a different but related type.
224 *
225 * A pointer-to-Tp1 must be convertible to a pointer-to-Tp/element_type.
226 *
227 * This object now @e owns the object previously owned by @a a,
228 * which has given up ownsership. The object that this one @e
229 * used to own and track has been deleted.
230 */
231 template<typename _Tp1>
232 auto_ptr&
233 operator=(auto_ptr<_Tp1>& __a) throw()
16ad69a1 234 {
5acf59f8
BK
235 reset(__a.release());
236 return *this;
237 }
238
239 /**
240 * When the %auto_ptr goes out of scope, the object it owns is deleted.
241 * If it no longer owns anything (i.e., @c get() is @c NULL), then this
242 * has no effect.
243 *
244 * @if maint
245 * The C++ standard says there is supposed to be an empty throw
246 * specification here, but omitting it is standard conforming. Its
247 * presence can be detected only if _Tp::~_Tp() throws, but this is
248 * prohibited. [17.4.3.6]/2
249 * @end maint
250 */
251 ~auto_ptr() { delete _M_ptr; }
252
253 /**
254 * @brief Smart pointer dereferencing.
255 *
256 * If this %auto_ptr no longer owns anything, then this
257 * operation will crash. (For a smart pointer, "no longer owns
258 * anything" is the same as being a null pointer, and you know
259 * what happens when you dereference one of those...)
260 */
261 element_type&
262 operator*() const throw() { return *_M_ptr; }
263
264 /**
265 * @brief Smart pointer dereferencing.
266 *
267 * This returns the pointer itself, which the language then will
268 * automatically cause to be dereferenced.
269 */
270 element_type*
271 operator->() const throw() { return _M_ptr; }
272
273 /**
274 * @brief Bypassing the smart pointer.
275 * @return The raw pointer being managed.
276 *
277 * You can get a copy of the pointer that this object owns, for
278 * situations such as passing to a function which only accepts a raw
279 * pointer.
280 *
281 * @note This %auto_ptr still owns the memory.
282 */
283 element_type*
284 get() const throw() { return _M_ptr; }
285
286 /**
287 * @brief Bypassing the smart pointer.
288 * @return The raw pointer being managed.
289 *
290 * You can get a copy of the pointer that this object owns, for
291 * situations such as passing to a function which only accepts a raw
292 * pointer.
293 *
294 * @note This %auto_ptr no longer owns the memory. When this object
295 * goes out of scope, nothing will happen.
296 */
297 element_type*
298 release() throw()
299 {
300 element_type* __tmp = _M_ptr;
301 _M_ptr = 0;
302 return __tmp;
303 }
304
305 /**
306 * @brief Forcibly deletes the managed object.
307 * @param p A pointer (defaults to NULL).
308 *
309 * This object now @e owns the object pointed to by @a p. The previous
310 * object has been deleted.
311 */
312 void
313 reset(element_type* __p = 0) throw()
314 {
315 if (__p != _M_ptr)
316 {
317 delete _M_ptr;
318 _M_ptr = __p;
319 }
320 }
321
322 /** @{
323 * @brief Automatic conversions
324 *
325 * These operations convert an %auto_ptr into and from an auto_ptr_ref
326 * automatically as needed. This allows constructs such as
327 * @code
328 * auto_ptr<Derived> func_returning_auto_ptr(.....);
329 * ...
330 * auto_ptr<Base> ptr = func_returning_auto_ptr(.....);
331 * @endcode
332 */
333 auto_ptr(auto_ptr_ref<element_type> __ref) throw()
334 : _M_ptr(__ref._M_ptr) { }
335
336 auto_ptr&
337 operator=(auto_ptr_ref<element_type> __ref) throw()
338 {
339 if (__ref._M_ptr != this->get())
340 {
341 delete _M_ptr;
342 _M_ptr = __ref._M_ptr;
343 }
344 return *this;
345 }
346
347 template<typename _Tp1>
348 operator auto_ptr_ref<_Tp1>() throw()
349 { return auto_ptr_ref<_Tp1>(this->release()); }
350
351 template<typename _Tp1>
352 operator auto_ptr<_Tp1>() throw()
353 { return auto_ptr<_Tp1>(this->release()); }
354 /** @} */
355 };
54c1bf78
BK
356} // namespace std
357
5acf59f8 358#endif
This page took 0.229445 seconds and 5 git commands to generate.