OSDN Git Service

2010-10-28 Paolo Carlini <paolo.carlini@oracle.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / stl_tempbuf.h
1 // Temporary buffer implementation -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
20
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24 // <http://www.gnu.org/licenses/>.
25
26 /*
27  *
28  * Copyright (c) 1994
29  * Hewlett-Packard Company
30  *
31  * Permission to use, copy, modify, distribute and sell this software
32  * and its documentation for any purpose is hereby granted without fee,
33  * provided that the above copyright notice appear in all copies and
34  * that both that copyright notice and this permission notice appear
35  * in supporting documentation.  Hewlett-Packard Company makes no
36  * representations about the suitability of this software for any
37  * purpose.  It is provided "as is" without express or implied warranty.
38  *
39  *
40  * Copyright (c) 1996,1997
41  * Silicon Graphics Computer Systems, Inc.
42  *
43  * Permission to use, copy, modify, distribute and sell this software
44  * and its documentation for any purpose is hereby granted without fee,
45  * provided that the above copyright notice appear in all copies and
46  * that both that copyright notice and this permission notice appear
47  * in supporting documentation.  Silicon Graphics makes no
48  * representations about the suitability of this software for any
49  * purpose.  It is provided "as is" without express or implied warranty.
50  */
51
52 /** @file stl_tempbuf.h
53  *  This is an internal header file, included by other library headers.
54  *  You should not attempt to use it directly.
55  */
56
57 #ifndef _STL_TEMPBUF_H
58 #define _STL_TEMPBUF_H 1
59
60 #include <bits/stl_algobase.h>
61 #include <bits/stl_construct.h>
62
63 _GLIBCXX_BEGIN_NAMESPACE(std)
64
65   /**
66    *  @brief Allocates a temporary buffer.
67    *  @param  len  The number of objects of type Tp.
68    *  @return See full description.
69    *
70    *  Reinventing the wheel, but this time with prettier spokes!
71    *
72    *  This function tries to obtain storage for @c len adjacent Tp
73    *  objects.  The objects themselves are not constructed, of course.
74    *  A pair<> is returned containing <em>the buffer s address and
75    *  capacity (in the units of sizeof(Tp)), or a pair of 0 values if
76    *  no storage can be obtained.</em>  Note that the capacity obtained
77    *  may be less than that requested if the memory is unavailable;
78    *  you should compare len with the .second return value.
79    *
80    * Provides the nothrow exception guarantee.
81    */
82   template<typename _Tp>
83     pair<_Tp*, ptrdiff_t>
84     get_temporary_buffer(ptrdiff_t __len)
85     {
86       const ptrdiff_t __max =
87         __gnu_cxx::__numeric_traits<ptrdiff_t>::__max / sizeof(_Tp);
88       if (__len > __max)
89         __len = __max;
90       
91       while (__len > 0) 
92         {
93           _Tp* __tmp = static_cast<_Tp*>(::operator new(__len * sizeof(_Tp), 
94                                                         std::nothrow));
95           if (__tmp != 0)
96             return std::pair<_Tp*, ptrdiff_t>(__tmp, __len);
97           __len /= 2;
98         }
99       return std::pair<_Tp*, ptrdiff_t>(static_cast<_Tp*>(0), 0);
100     }
101
102   /**
103    *  @brief The companion to get_temporary_buffer().
104    *  @param  p  A buffer previously allocated by get_temporary_buffer.
105    *  @return   None.
106    *
107    *  Frees the memory pointed to by p.
108    */
109   template<typename _Tp>
110     inline void
111     return_temporary_buffer(_Tp* __p)
112     { ::operator delete(__p, std::nothrow); }
113
114
115   /**
116    *  This class is used in two places: stl_algo.h and ext/memory,
117    *  where it is wrapped as the temporary_buffer class.  See
118    *  temporary_buffer docs for more notes.
119    */
120   template<typename _ForwardIterator, typename _Tp>
121     class _Temporary_buffer
122     {
123       // concept requirements
124       __glibcxx_class_requires(_ForwardIterator, _ForwardIteratorConcept)
125
126     public:
127       typedef _Tp         value_type;
128       typedef value_type* pointer;
129       typedef pointer     iterator;
130       typedef ptrdiff_t   size_type;
131
132     protected:
133       size_type  _M_original_len;
134       size_type  _M_len;
135       pointer    _M_buffer;
136
137     public:
138       /// As per Table mumble.
139       size_type
140       size() const
141       { return _M_len; }
142
143       /// Returns the size requested by the constructor; may be >size().
144       size_type
145       requested_size() const
146       { return _M_original_len; }
147
148       /// As per Table mumble.
149       iterator
150       begin()
151       { return _M_buffer; }
152
153       /// As per Table mumble.
154       iterator
155       end()
156       { return _M_buffer + _M_len; }
157
158       /**
159        * Constructs a temporary buffer of a size somewhere between
160        * zero and the size of the given range.
161        */
162       _Temporary_buffer(_ForwardIterator __first, _ForwardIterator __last);
163
164       ~_Temporary_buffer()
165       {
166         std::_Destroy(_M_buffer, _M_buffer + _M_len);
167         std::return_temporary_buffer(_M_buffer);
168       }
169
170     private:
171       // Disable copy constructor and assignment operator.
172       _Temporary_buffer(const _Temporary_buffer&);
173
174       void
175       operator=(const _Temporary_buffer&);
176     };
177
178
179   template<bool>
180     struct __uninitialized_construct_buf_dispatch
181     {
182       template<typename _ForwardIterator, typename _Tp>
183         static void
184         __ucr(_ForwardIterator __first, _ForwardIterator __last,
185               _Tp& __value)
186         {
187           if(__first == __last)
188             return;
189
190           _ForwardIterator __cur = __first;
191           __try
192             {
193               std::_Construct(std::__addressof(*__first),
194                               _GLIBCXX_MOVE(__value));
195               _ForwardIterator __prev = __cur;
196               ++__cur;
197               for(; __cur != __last; ++__cur, ++__prev)
198                 std::_Construct(std::__addressof(*__cur),
199                                 _GLIBCXX_MOVE(*__prev));
200               __value = _GLIBCXX_MOVE(*__prev);
201             }
202           __catch(...)
203             {
204               std::_Destroy(__first, __cur);
205               __throw_exception_again;
206             }
207         }
208     };
209
210   template<>
211     struct __uninitialized_construct_buf_dispatch<true>
212     {
213       template<typename _ForwardIterator, typename _Tp>
214         static void
215         __ucr(_ForwardIterator, _ForwardIterator, _Tp&) { }
216     };
217
218   // Constructs objects in the range [first, last).
219   // Note that while these new objects will take valid values,
220   // their exact value is not defined. In particular they may
221   // be 'moved from'.
222   //
223   // While __value may altered during this algorithm, it will have
224   // the same value when the algorithm finishes, unless one of the
225   // constructions throws.
226   //
227   // Requirements: _ForwardIterator::value_type(_Tp&&) is valid.
228   template<typename _ForwardIterator, typename _Tp>
229     inline void
230     __uninitialized_construct_buf(_ForwardIterator __first,
231                                   _ForwardIterator __last,
232                                   _Tp& __value)
233     {
234       typedef typename std::iterator_traits<_ForwardIterator>::value_type
235         _ValueType;
236
237       std::__uninitialized_construct_buf_dispatch<
238         __has_trivial_constructor(_ValueType)>::
239           __ucr(__first, __last, __value);
240     }
241
242   template<typename _ForwardIterator, typename _Tp>
243     _Temporary_buffer<_ForwardIterator, _Tp>::
244     _Temporary_buffer(_ForwardIterator __first, _ForwardIterator __last)
245     : _M_original_len(std::distance(__first, __last)),
246       _M_len(0), _M_buffer(0)
247     {
248       __try
249         {
250           std::pair<pointer, size_type> __p(std::get_temporary_buffer<
251                                             value_type>(_M_original_len));
252           _M_buffer = __p.first;
253           _M_len = __p.second;
254           if(_M_buffer)
255             std::__uninitialized_construct_buf(_M_buffer, _M_buffer + _M_len,
256                                                *__first);
257         }
258       __catch(...)
259         {
260           std::return_temporary_buffer(_M_buffer);
261           _M_buffer = 0;
262           _M_len = 0;
263           __throw_exception_again;
264         }
265     }
266
267 _GLIBCXX_END_NAMESPACE
268
269 #endif /* _STL_TEMPBUF_H */
270