]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/stl_tempbuf.h
stl_list.h: Rename guard macro consistently with file name.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / stl_tempbuf.h
1 // Temporary buffer implementation -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
21
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 /*
32 *
33 * Copyright (c) 1994
34 * Hewlett-Packard Company
35 *
36 * Permission to use, copy, modify, distribute and sell this software
37 * and its documentation for any purpose is hereby granted without fee,
38 * provided that the above copyright notice appear in all copies and
39 * that both that copyright notice and this permission notice appear
40 * in supporting documentation. Hewlett-Packard Company makes no
41 * representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied warranty.
43 *
44 *
45 * Copyright (c) 1996,1997
46 * Silicon Graphics Computer Systems, Inc.
47 *
48 * Permission to use, copy, modify, distribute and sell this software
49 * and its documentation for any purpose is hereby granted without fee,
50 * provided that the above copyright notice appear in all copies and
51 * that both that copyright notice and this permission notice appear
52 * in supporting documentation. Silicon Graphics makes no
53 * representations about the suitability of this software for any
54 * purpose. It is provided "as is" without express or implied warranty.
55 */
56
57 /** @file stl_tempbuf.h
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
60 */
61
62 #ifndef _STL_TEMPBUF_H
63 #define _STL_TEMPBUF_H 1
64
65 #include <bits/stl_algobase.h>
66 #include <bits/stl_construct.h>
67 #include <bits/stl_uninitialized.h>
68
69 _GLIBCXX_BEGIN_NAMESPACE(std)
70
71 /**
72 * @if maint
73 * This is a helper function. The unused second parameter exists to
74 * permit the real get_temporary_buffer to use template parameter deduction.
75 * @endif
76 */
77 template<typename _Tp>
78 pair<_Tp*, ptrdiff_t>
79 __get_temporary_buffer(ptrdiff_t __len, _Tp*)
80 {
81 const ptrdiff_t __max =
82 __gnu_cxx::__numeric_traits<ptrdiff_t>::__max / sizeof(_Tp);
83 if (__len > __max)
84 __len = __max;
85
86 while (__len > 0)
87 {
88 _Tp* __tmp = static_cast<_Tp*>(::operator new(__len * sizeof(_Tp),
89 std::nothrow));
90 if (__tmp != 0)
91 return std::pair<_Tp*, ptrdiff_t>(__tmp, __len);
92 __len /= 2;
93 }
94 return std::pair<_Tp*, ptrdiff_t>(static_cast<_Tp*>(0), 0);
95 }
96
97 /**
98 * @brief Allocates a temporary buffer.
99 * @param len The number of objects of type Tp.
100 * @return See full description.
101 *
102 * Reinventing the wheel, but this time with prettier spokes!
103 *
104 * This function tries to obtain storage for @c len adjacent Tp
105 * objects. The objects themselves are not constructed, of course.
106 * A pair<> is returned containing "the buffer s address and
107 * capacity (in the units of sizeof(Tp)), or a pair of 0 values if
108 * no storage can be obtained." Note that the capacity obtained
109 * may be less than that requested if the memory is unavailable;
110 * you should compare len with the .second return value.
111 *
112 * Provides the nothrow exception guarantee.
113 */
114 template<typename _Tp>
115 inline pair<_Tp*, ptrdiff_t>
116 get_temporary_buffer(ptrdiff_t __len)
117 { return std::__get_temporary_buffer(__len, static_cast<_Tp*>(0)); }
118
119 /**
120 * @brief The companion to get_temporary_buffer().
121 * @param p A buffer previously allocated by get_temporary_buffer.
122 * @return None.
123 *
124 * Frees the memory pointed to by p.
125 */
126 template<typename _Tp>
127 inline void
128 return_temporary_buffer(_Tp* __p)
129 { ::operator delete(__p, std::nothrow); }
130
131
132 /**
133 * @if maint
134 * This class is used in two places: stl_algo.h and ext/memory,
135 * where it is wrapped as the temporary_buffer class. See
136 * temporary_buffer docs for more notes.
137 * @endif
138 */
139 template<typename _ForwardIterator, typename _Tp>
140 class _Temporary_buffer
141 {
142 // concept requirements
143 __glibcxx_class_requires(_ForwardIterator, _ForwardIteratorConcept)
144
145 public:
146 typedef _Tp value_type;
147 typedef value_type* pointer;
148 typedef pointer iterator;
149 typedef ptrdiff_t size_type;
150
151 protected:
152 size_type _M_original_len;
153 size_type _M_len;
154 pointer _M_buffer;
155
156 public:
157 /// As per Table mumble.
158 size_type
159 size() const
160 { return _M_len; }
161
162 /// Returns the size requested by the constructor; may be >size().
163 size_type
164 requested_size() const
165 { return _M_original_len; }
166
167 /// As per Table mumble.
168 iterator
169 begin()
170 { return _M_buffer; }
171
172 /// As per Table mumble.
173 iterator
174 end()
175 { return _M_buffer + _M_len; }
176
177 /**
178 * Constructs a temporary buffer of a size somewhere between
179 * zero and the size of the given range.
180 */
181 _Temporary_buffer(_ForwardIterator __first, _ForwardIterator __last);
182
183 ~_Temporary_buffer()
184 {
185 std::_Destroy(_M_buffer, _M_buffer + _M_len);
186 std::return_temporary_buffer(_M_buffer);
187 }
188
189 private:
190 // Disable copy constructor and assignment operator.
191 _Temporary_buffer(const _Temporary_buffer&);
192
193 void
194 operator=(const _Temporary_buffer&);
195 };
196
197 template<typename _ForwardIterator, typename _Tp>
198 _Temporary_buffer<_ForwardIterator, _Tp>::
199 _Temporary_buffer(_ForwardIterator __first, _ForwardIterator __last)
200 : _M_original_len(std::distance(__first, __last)),
201 _M_len(0), _M_buffer(0)
202 {
203 try
204 {
205 std::pair<pointer, size_type> __p(std::get_temporary_buffer<
206 value_type>(_M_original_len));
207 _M_buffer = __p.first;
208 _M_len = __p.second;
209 if (!__is_pod(_Tp) && _M_len > 0)
210 std::uninitialized_fill_n(_M_buffer, _M_len, *__first);
211 }
212 catch(...)
213 {
214 std::return_temporary_buffer(_M_buffer);
215 _M_buffer = 0;
216 _M_len = 0;
217 __throw_exception_again;
218 }
219 }
220
221 _GLIBCXX_END_NAMESPACE
222
223 #endif /* _STL_TEMPBUF_H */
224