1 _tempbuf.h |
1 /* |
|
2 * |
|
3 * Copyright (c) 1994 |
|
4 * Hewlett-Packard Company |
|
5 * |
|
6 * Copyright (c) 1996,1997 |
|
7 * Silicon Graphics Computer Systems, Inc. |
|
8 * |
|
9 * Copyright (c) 1997 |
|
10 * Moscow Center for SPARC Technology |
|
11 * |
|
12 * Copyright (c) 1999 |
|
13 * Boris Fomitchev |
|
14 * |
|
15 * This material is provided "as is", with absolutely no warranty expressed |
|
16 * or implied. Any use is at your own risk. |
|
17 * |
|
18 * Permission to use or copy this software for any purpose is hereby granted |
|
19 * without fee, provided the above notices are retained on all copies. |
|
20 * Permission to modify the code and to distribute modified code is granted, |
|
21 * provided the above notices are retained, and a notice that the code was |
|
22 * modified is included with the above copyright notice. |
|
23 * |
|
24 */ |
|
25 |
|
26 /* NOTE: This is an internal header file, included by other STL headers. |
|
27 * You should not attempt to use it directly. |
|
28 */ |
|
29 |
|
30 #ifndef _STLP_INTERNAL_TEMPBUF_H |
|
31 #define _STLP_INTERNAL_TEMPBUF_H |
|
32 |
|
33 # ifndef _STLP_CLIMITS |
|
34 # include <climits> |
|
35 # endif |
|
36 # ifndef _STLP_CSTDLIB |
|
37 # include <cstdlib> |
|
38 # endif |
|
39 # ifndef _STLP_INTERNAL_UNINITIALIZED_H |
|
40 # include <stl/_uninitialized.h> |
|
41 # endif |
|
42 |
|
43 _STLP_BEGIN_NAMESPACE |
|
44 |
|
45 template <class _Tp> |
|
46 pair<_Tp*, ptrdiff_t> _STLP_CALL |
|
47 __get_temporary_buffer(ptrdiff_t __len, _Tp*); |
|
48 |
|
49 #ifndef _STLP_NO_EXPLICIT_FUNCTION_TMPL_ARGS |
|
50 |
|
51 template <class _Tp> |
|
52 inline pair<_Tp*, ptrdiff_t> _STLP_CALL get_temporary_buffer(ptrdiff_t __len) { |
|
53 return __get_temporary_buffer(__len, (_Tp*) 0); |
|
54 } |
|
55 |
|
56 # if ! defined(_STLP_NO_EXTENSIONS) |
|
57 // This overload is not required by the standard; it is an extension. |
|
58 // It is supported for backward compatibility with the HP STL, and |
|
59 // because not all compilers support the language feature (explicit |
|
60 // function template arguments) that is required for the standard |
|
61 // version of get_temporary_buffer. |
|
62 template <class _Tp> |
|
63 inline pair<_Tp*, ptrdiff_t> _STLP_CALL |
|
64 get_temporary_buffer(ptrdiff_t __len, _Tp*) { |
|
65 return __get_temporary_buffer(__len, (_Tp*) 0); |
|
66 } |
|
67 # endif |
|
68 #endif |
|
69 |
|
70 template <class _Tp> |
|
71 inline void _STLP_CALL return_temporary_buffer(_Tp* __p) { |
|
72 // SunPro brain damage |
|
73 free((char*)__p); |
|
74 } |
|
75 |
|
76 template <class _ForwardIterator, class _Tp> |
|
77 class _Temporary_buffer { |
|
78 private: |
|
79 ptrdiff_t _M_original_len; |
|
80 ptrdiff_t _M_len; |
|
81 _Tp* _M_buffer; |
|
82 |
|
83 void _M_allocate_buffer() { |
|
84 _M_original_len = _M_len; |
|
85 _M_buffer = 0; |
|
86 |
|
87 if (_M_len > (ptrdiff_t)(INT_MAX / sizeof(_Tp))) |
|
88 _M_len = INT_MAX / sizeof(_Tp); |
|
89 |
|
90 while (_M_len > 0) { |
|
91 _M_buffer = (_Tp*) malloc(_M_len * sizeof(_Tp)); |
|
92 if (_M_buffer) |
|
93 break; |
|
94 _M_len /= 2; |
|
95 } |
|
96 } |
|
97 |
|
98 void _M_initialize_buffer(const _Tp&, const __true_type&) {} |
|
99 void _M_initialize_buffer(const _Tp& val, const __false_type&) { |
|
100 uninitialized_fill_n(_M_buffer, _M_len, val); |
|
101 } |
|
102 |
|
103 public: |
|
104 ptrdiff_t size() const { return _M_len; } |
|
105 ptrdiff_t requested_size() const { return _M_original_len; } |
|
106 _Tp* begin() { return _M_buffer; } |
|
107 _Tp* end() { return _M_buffer + _M_len; } |
|
108 |
|
109 _Temporary_buffer(_ForwardIterator __first, _ForwardIterator __last) { |
|
110 // Workaround for a __type_traits bug in the pre-7.3 compiler. |
|
111 # if defined(__sgi) && !defined(__GNUC__) && _COMPILER_VERSION < 730 |
|
112 typedef typename __type_traits<_Tp>::is_POD_type _Trivial; |
|
113 # else |
|
114 typedef typename __type_traits<_Tp>::has_trivial_default_constructor _Trivial; |
|
115 # endif |
|
116 _STLP_TRY { |
|
117 _M_len = distance(__first, __last); |
|
118 _M_allocate_buffer(); |
|
119 if (_M_len > 0) |
|
120 _M_initialize_buffer(*__first, _Trivial()); |
|
121 } |
|
122 _STLP_UNWIND(free(_M_buffer); _M_buffer = 0; _M_len = 0); |
|
123 } |
|
124 |
|
125 ~_Temporary_buffer() { |
|
126 _STLP_STD::_Destroy(_M_buffer, _M_buffer + _M_len); |
|
127 free(_M_buffer); |
|
128 } |
|
129 |
|
130 private: |
|
131 // Disable copy constructor and assignment operator. |
|
132 _Temporary_buffer(const _Temporary_buffer<_ForwardIterator, _Tp>&) {} |
|
133 void operator=(const _Temporary_buffer<_ForwardIterator, _Tp>&) {} |
|
134 }; |
|
135 |
|
136 # ifndef _STLP_NO_EXTENSIONS |
|
137 |
|
138 // Class temporary_buffer is not part of the standard. It is an extension. |
|
139 |
|
140 template <class _ForwardIterator, |
|
141 class _Tp |
|
142 #ifdef _STLP_CLASS_PARTIAL_SPECIALIZATION |
|
143 = typename iterator_traits<_ForwardIterator>::value_type |
|
144 #endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */ |
|
145 > |
|
146 struct temporary_buffer : public _Temporary_buffer<_ForwardIterator, _Tp> |
|
147 { |
|
148 temporary_buffer(_ForwardIterator __first, _ForwardIterator __last) |
|
149 : _Temporary_buffer<_ForwardIterator, _Tp>(__first, __last) {} |
|
150 ~temporary_buffer() {} |
|
151 }; |
|
152 |
|
153 # endif /* _STLP_NO_EXTENSIONS */ |
|
154 |
|
155 _STLP_END_NAMESPACE |
|
156 |
|
157 # ifndef _STLP_LINK_TIME_INSTANTIATION |
|
158 # include <stl/_tempbuf.c> |
|
159 # endif |
|
160 |
|
161 #endif /* _STLP_INTERNAL_TEMPBUF_H */ |
|
162 |
|
163 // Local Variables: |
|
164 // mode:C++ |
|
165 // End: |