]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/stl_tempbuf.h
algo.h: Use std not __STD.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / stl_tempbuf.h
1 /*
2 *
3 * Copyright (c) 1994
4 * Hewlett-Packard Company
5 *
6 * Permission to use, copy, modify, distribute and sell this software
7 * and its documentation for any purpose is hereby granted without fee,
8 * provided that the above copyright notice appear in all copies and
9 * that both that copyright notice and this permission notice appear
10 * in supporting documentation. Hewlett-Packard Company makes no
11 * representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 *
15 * Copyright (c) 1996,1997
16 * Silicon Graphics Computer Systems, Inc.
17 *
18 * Permission to use, copy, modify, distribute and sell this software
19 * and its documentation for any purpose is hereby granted without fee,
20 * provided that the above copyright notice appear in all copies and
21 * that both that copyright notice and this permission notice appear
22 * in supporting documentation. Silicon Graphics makes no
23 * representations about the suitability of this software for any
24 * purpose. It is provided "as is" without express or implied warranty.
25 */
26
27 /* NOTE: This is an internal header file, included by other STL headers.
28 * You should not attempt to use it directly.
29 */
30
31 #ifndef __SGI_STL_INTERNAL_TEMPBUF_H
32 #define __SGI_STL_INTERNAL_TEMPBUF_H
33
34 namespace std
35 {
36
37 template <class _Tp>
38 pair<_Tp*, ptrdiff_t>
39 __get_temporary_buffer(ptrdiff_t __len, _Tp*)
40 {
41 if (__len > ptrdiff_t(INT_MAX / sizeof(_Tp)))
42 __len = INT_MAX / sizeof(_Tp);
43
44 while (__len > 0) {
45 _Tp* __tmp = (_Tp*) malloc((size_t)__len * sizeof(_Tp));
46 if (__tmp != 0)
47 return pair<_Tp*, ptrdiff_t>(__tmp, __len);
48 __len /= 2;
49 }
50
51 return pair<_Tp*, ptrdiff_t>((_Tp*)0, 0);
52 }
53
54 template <class _Tp>
55 inline pair<_Tp*, ptrdiff_t> get_temporary_buffer(ptrdiff_t __len) {
56 return __get_temporary_buffer(__len, (_Tp*) 0);
57 }
58
59 // This overload is not required by the standard; it is an extension.
60 // It is supported for backward compatibility with the HP STL, and
61 // because not all compilers support the language feature (explicit
62 // function template arguments) that is required for the standard
63 // version of get_temporary_buffer.
64 template <class _Tp>
65 inline pair<_Tp*, ptrdiff_t> get_temporary_buffer(ptrdiff_t __len, _Tp*) {
66 return __get_temporary_buffer(__len, (_Tp*) 0);
67 }
68
69 template <class _Tp>
70 void return_temporary_buffer(_Tp* __p) {
71 free(__p);
72 }
73
74 template <class _ForwardIterator, class _Tp>
75 class _Temporary_buffer {
76 private:
77 ptrdiff_t _M_original_len;
78 ptrdiff_t _M_len;
79 _Tp* _M_buffer;
80
81 void _M_allocate_buffer() {
82 _M_original_len = _M_len;
83 _M_buffer = 0;
84
85 if (_M_len > (ptrdiff_t)(INT_MAX / sizeof(_Tp)))
86 _M_len = INT_MAX / sizeof(_Tp);
87
88 while (_M_len > 0) {
89 _M_buffer = (_Tp*) malloc(_M_len * sizeof(_Tp));
90 if (_M_buffer)
91 break;
92 _M_len /= 2;
93 }
94 }
95
96 void _M_initialize_buffer(const _Tp&, __true_type) {}
97 void _M_initialize_buffer(const _Tp& val, __false_type) {
98 uninitialized_fill_n(_M_buffer, _M_len, val);
99 }
100
101 public:
102 ptrdiff_t size() const { return _M_len; }
103 ptrdiff_t requested_size() const { return _M_original_len; }
104 _Tp* begin() { return _M_buffer; }
105 _Tp* end() { return _M_buffer + _M_len; }
106
107 _Temporary_buffer(_ForwardIterator __first, _ForwardIterator __last) {
108 // Workaround for a __type_traits bug in the pre-7.3 compiler.
109 typedef typename __type_traits<_Tp>::has_trivial_default_constructor
110 _Trivial;
111
112 __STL_TRY {
113 _M_len = 0;
114 distance(__first, __last, _M_len);
115 _M_allocate_buffer();
116 if (_M_len > 0)
117 _M_initialize_buffer(*__first, _Trivial());
118 }
119 __STL_UNWIND(free(_M_buffer); _M_buffer = 0; _M_len = 0);
120 }
121
122 ~_Temporary_buffer() {
123 destroy(_M_buffer, _M_buffer + _M_len);
124 free(_M_buffer);
125 }
126
127 private:
128 // Disable copy constructor and assignment operator.
129 _Temporary_buffer(const _Temporary_buffer&) {}
130 void operator=(const _Temporary_buffer&) {}
131 };
132
133 // Class temporary_buffer is not part of the standard. It is an extension.
134
135 template <class _ForwardIterator,
136 class _Tp
137 = typename iterator_traits<_ForwardIterator>::value_type
138 >
139 struct temporary_buffer : public _Temporary_buffer<_ForwardIterator, _Tp>
140 {
141 temporary_buffer(_ForwardIterator __first, _ForwardIterator __last)
142 : _Temporary_buffer<_ForwardIterator, _Tp>(__first, __last) {}
143 ~temporary_buffer() {}
144 };
145
146 } // namespace std
147
148 #endif /* __SGI_STL_INTERNAL_TEMPBUF_H */
149
150 // Local Variables:
151 // mode:C++
152 // End: