]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // boost/endian/buffers.hpp ----------------------------------------------------------// |
2 | ||
3 | // (C) Copyright Darin Adler 2000 | |
4 | // (C) Copyright Beman Dawes 2006, 2009, 2014 | |
5 | ||
6 | // Distributed under the Boost Software License, Version 1.0. | |
7 | // See http://www.boost.org/LICENSE_1_0.txt | |
8 | ||
9 | // See library home page at http://www.boost.org/libs/endian | |
10 | ||
11 | //--------------------------------------------------------------------------------------// | |
12 | ||
13 | // Original design developed by Darin Adler based on classes developed by Mark | |
14 | // Borgerding. Four original class templates were combined into a single endian | |
15 | // class template by Beman Dawes, who also added the unrolled_byte_loops sign | |
16 | // partial specialization to correctly extend the sign when cover integer size | |
17 | // differs from endian representation size. | |
18 | ||
19 | // TODO: When a compiler supporting constexpr becomes available, try possible uses. | |
20 | ||
21 | #ifndef BOOST_ENDIAN_BUFFERS_HPP | |
22 | #define BOOST_ENDIAN_BUFFERS_HPP | |
23 | ||
11fdf7f2 TL |
24 | #if defined(_MSC_VER) |
25 | # pragma warning(push) | |
7c673cae FG |
26 | # pragma warning(disable:4365) // conversion ... signed/unsigned mismatch |
27 | #endif | |
28 | ||
29 | #ifdef BOOST_ENDIAN_LOG | |
30 | # include <iostream> | |
31 | #endif | |
32 | ||
33 | #if defined(__BORLANDC__) || defined( __CODEGEARC__) | |
34 | # pragma pack(push, 1) | |
35 | #endif | |
36 | ||
37 | #include <boost/config.hpp> | |
38 | #include <boost/predef/detail/endian_compat.h> | |
39 | #include <boost/endian/conversion.hpp> | |
40 | #include <boost/type_traits/is_signed.hpp> | |
11fdf7f2 TL |
41 | #include <boost/type_traits/make_unsigned.hpp> |
42 | #include <boost/type_traits/conditional.hpp> | |
43 | #include <boost/type_traits/is_integral.hpp> | |
44 | #include <boost/type_traits/type_identity.hpp> | |
7c673cae FG |
45 | #include <boost/cstdint.hpp> |
46 | #include <boost/static_assert.hpp> | |
47 | #include <boost/core/scoped_enum.hpp> | |
48 | #include <iosfwd> | |
49 | #include <climits> | |
11fdf7f2 | 50 | #include <cstring> |
7c673cae FG |
51 | |
52 | # if CHAR_BIT != 8 | |
53 | # error Platforms with CHAR_BIT != 8 are not supported | |
54 | # endif | |
55 | ||
56 | # ifdef BOOST_NO_CXX11_DEFAULTED_FUNCTIONS | |
57 | # define BOOST_ENDIAN_DEFAULT_CONSTRUCT {} // C++03 | |
58 | # else | |
59 | # define BOOST_ENDIAN_DEFAULT_CONSTRUCT = default; // C++0x | |
60 | # endif | |
61 | ||
62 | # if defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) && defined(BOOST_ENDIAN_FORCE_PODNESS) | |
63 | # define BOOST_ENDIAN_NO_CTORS | |
64 | # endif | |
65 | ||
66 | //---------------------------------- synopsis ----------------------------------------// | |
67 | ||
68 | namespace boost | |
69 | { | |
70 | namespace endian | |
71 | { | |
72 | ||
73 | BOOST_SCOPED_ENUM_START(align) | |
74 | {no, yes | |
75 | # ifdef BOOST_ENDIAN_DEPRECATED_NAMES | |
76 | , unaligned = no, aligned = yes | |
77 | # endif | |
78 | }; BOOST_SCOPED_ENUM_END | |
79 | ||
80 | template <BOOST_SCOPED_ENUM(order) Order, class T, std::size_t n_bits, | |
81 | BOOST_SCOPED_ENUM(align) A = align::no> | |
82 | class endian_buffer; | |
83 | ||
84 | // aligned big endian signed integer buffers | |
85 | typedef endian_buffer<order::big, int8_t, 8, align::yes> big_int8_buf_at; | |
86 | typedef endian_buffer<order::big, int16_t, 16, align::yes> big_int16_buf_at; | |
87 | typedef endian_buffer<order::big, int32_t, 32, align::yes> big_int32_buf_at; | |
88 | typedef endian_buffer<order::big, int64_t, 64, align::yes> big_int64_buf_at; | |
89 | ||
90 | // aligned big endian unsigned integer buffers | |
91 | typedef endian_buffer<order::big, uint8_t, 8, align::yes> big_uint8_buf_at; | |
92 | typedef endian_buffer<order::big, uint16_t, 16, align::yes> big_uint16_buf_at; | |
93 | typedef endian_buffer<order::big, uint32_t, 32, align::yes> big_uint32_buf_at; | |
94 | typedef endian_buffer<order::big, uint64_t, 64, align::yes> big_uint64_buf_at; | |
95 | ||
96 | // aligned little endian signed integer buffers | |
97 | typedef endian_buffer<order::little, int8_t, 8, align::yes> little_int8_buf_at; | |
98 | typedef endian_buffer<order::little, int16_t, 16, align::yes> little_int16_buf_at; | |
99 | typedef endian_buffer<order::little, int32_t, 32, align::yes> little_int32_buf_at; | |
100 | typedef endian_buffer<order::little, int64_t, 64, align::yes> little_int64_buf_at; | |
101 | ||
102 | // aligned little endian unsigned integer buffers | |
103 | typedef endian_buffer<order::little, uint8_t, 8, align::yes> little_uint8_buf_at; | |
104 | typedef endian_buffer<order::little, uint16_t, 16, align::yes> little_uint16_buf_at; | |
105 | typedef endian_buffer<order::little, uint32_t, 32, align::yes> little_uint32_buf_at; | |
106 | typedef endian_buffer<order::little, uint64_t, 64, align::yes> little_uint64_buf_at; | |
107 | ||
108 | // aligned native endian typedefs are not provided because | |
109 | // <cstdint> types are superior for this use case | |
110 | ||
111 | // unaligned big endian signed integer buffers | |
112 | typedef endian_buffer<order::big, int_least8_t, 8> big_int8_buf_t; | |
113 | typedef endian_buffer<order::big, int_least16_t, 16> big_int16_buf_t; | |
114 | typedef endian_buffer<order::big, int_least32_t, 24> big_int24_buf_t; | |
115 | typedef endian_buffer<order::big, int_least32_t, 32> big_int32_buf_t; | |
116 | typedef endian_buffer<order::big, int_least64_t, 40> big_int40_buf_t; | |
117 | typedef endian_buffer<order::big, int_least64_t, 48> big_int48_buf_t; | |
118 | typedef endian_buffer<order::big, int_least64_t, 56> big_int56_buf_t; | |
119 | typedef endian_buffer<order::big, int_least64_t, 64> big_int64_buf_t; | |
120 | ||
121 | // unaligned big endian unsigned integer buffers | |
122 | typedef endian_buffer<order::big, uint_least8_t, 8> big_uint8_buf_t; | |
123 | typedef endian_buffer<order::big, uint_least16_t, 16> big_uint16_buf_t; | |
124 | typedef endian_buffer<order::big, uint_least32_t, 24> big_uint24_buf_t; | |
125 | typedef endian_buffer<order::big, uint_least32_t, 32> big_uint32_buf_t; | |
126 | typedef endian_buffer<order::big, uint_least64_t, 40> big_uint40_buf_t; | |
127 | typedef endian_buffer<order::big, uint_least64_t, 48> big_uint48_buf_t; | |
128 | typedef endian_buffer<order::big, uint_least64_t, 56> big_uint56_buf_t; | |
129 | typedef endian_buffer<order::big, uint_least64_t, 64> big_uint64_buf_t; | |
130 | ||
131 | // unaligned little endian signed integer buffers | |
132 | typedef endian_buffer<order::little, int_least8_t, 8> little_int8_buf_t; | |
133 | typedef endian_buffer<order::little, int_least16_t, 16> little_int16_buf_t; | |
134 | typedef endian_buffer<order::little, int_least32_t, 24> little_int24_buf_t; | |
135 | typedef endian_buffer<order::little, int_least32_t, 32> little_int32_buf_t; | |
136 | typedef endian_buffer<order::little, int_least64_t, 40> little_int40_buf_t; | |
137 | typedef endian_buffer<order::little, int_least64_t, 48> little_int48_buf_t; | |
138 | typedef endian_buffer<order::little, int_least64_t, 56> little_int56_buf_t; | |
139 | typedef endian_buffer<order::little, int_least64_t, 64> little_int64_buf_t; | |
140 | ||
141 | // unaligned little endian unsigned integer buffers | |
142 | typedef endian_buffer<order::little, uint_least8_t, 8> little_uint8_buf_t; | |
143 | typedef endian_buffer<order::little, uint_least16_t, 16> little_uint16_buf_t; | |
144 | typedef endian_buffer<order::little, uint_least32_t, 24> little_uint24_buf_t; | |
145 | typedef endian_buffer<order::little, uint_least32_t, 32> little_uint32_buf_t; | |
146 | typedef endian_buffer<order::little, uint_least64_t, 40> little_uint40_buf_t; | |
147 | typedef endian_buffer<order::little, uint_least64_t, 48> little_uint48_buf_t; | |
148 | typedef endian_buffer<order::little, uint_least64_t, 56> little_uint56_buf_t; | |
149 | typedef endian_buffer<order::little, uint_least64_t, 64> little_uint64_buf_t; | |
150 | ||
151 | # ifdef BOOST_BIG_ENDIAN | |
152 | // unaligned native endian signed integer buffers | |
153 | typedef big_int8_buf_t native_int8_buf_t; | |
154 | typedef big_int16_buf_t native_int16_buf_t; | |
155 | typedef big_int24_buf_t native_int24_buf_t; | |
156 | typedef big_int32_buf_t native_int32_buf_t; | |
157 | typedef big_int40_buf_t native_int40_buf_t; | |
158 | typedef big_int48_buf_t native_int48_buf_t; | |
159 | typedef big_int56_buf_t native_int56_buf_t; | |
160 | typedef big_int64_buf_t native_int64_buf_t; | |
161 | ||
162 | // unaligned native endian unsigned integer buffers | |
163 | typedef big_uint8_buf_t native_uint8_buf_t; | |
164 | typedef big_uint16_buf_t native_uint16_buf_t; | |
165 | typedef big_uint24_buf_t native_uint24_buf_t; | |
166 | typedef big_uint32_buf_t native_uint32_buf_t; | |
167 | typedef big_uint40_buf_t native_uint40_buf_t; | |
168 | typedef big_uint48_buf_t native_uint48_buf_t; | |
169 | typedef big_uint56_buf_t native_uint56_buf_t; | |
170 | typedef big_uint64_buf_t native_uint64_buf_t; | |
171 | # else | |
172 | // unaligned native endian signed integer buffers | |
173 | typedef little_int8_buf_t native_int8_buf_t; | |
174 | typedef little_int16_buf_t native_int16_buf_t; | |
175 | typedef little_int24_buf_t native_int24_buf_t; | |
176 | typedef little_int32_buf_t native_int32_buf_t; | |
177 | typedef little_int40_buf_t native_int40_buf_t; | |
178 | typedef little_int48_buf_t native_int48_buf_t; | |
179 | typedef little_int56_buf_t native_int56_buf_t; | |
180 | typedef little_int64_buf_t native_int64_buf_t; | |
181 | ||
182 | // unaligned native endian unsigned integer buffers | |
183 | typedef little_uint8_buf_t native_uint8_buf_t; | |
184 | typedef little_uint16_buf_t native_uint16_buf_t; | |
185 | typedef little_uint24_buf_t native_uint24_buf_t; | |
186 | typedef little_uint32_buf_t native_uint32_buf_t; | |
187 | typedef little_uint40_buf_t native_uint40_buf_t; | |
188 | typedef little_uint48_buf_t native_uint48_buf_t; | |
189 | typedef little_uint56_buf_t native_uint56_buf_t; | |
190 | typedef little_uint64_buf_t native_uint64_buf_t; | |
191 | # endif | |
192 | ||
193 | // Stream inserter | |
194 | template <class charT, class traits, BOOST_SCOPED_ENUM(order) Order, class T, | |
195 | std::size_t n_bits, BOOST_SCOPED_ENUM(align) A> | |
196 | std::basic_ostream<charT, traits>& | |
197 | operator<<(std::basic_ostream<charT, traits>& os, | |
198 | const endian_buffer<Order, T, n_bits, A>& x) | |
199 | { | |
200 | return os << x.value(); | |
201 | } | |
202 | ||
11fdf7f2 | 203 | // Stream extractor |
7c673cae FG |
204 | template <class charT, class traits, BOOST_SCOPED_ENUM(order) Order, class T, |
205 | std::size_t n_bits, BOOST_SCOPED_ENUM(align) A> | |
206 | std::basic_istream<charT, traits>& | |
207 | operator>>(std::basic_istream<charT, traits>& is, | |
208 | endian_buffer<Order, T, n_bits, A>& x) | |
209 | { | |
210 | T i; | |
211 | if (is >> i) | |
212 | x = i; | |
213 | return is; | |
214 | } | |
215 | ||
216 | //---------------------------------- end synopsis ------------------------------------// | |
217 | ||
218 | namespace detail | |
219 | { | |
220 | ||
221 | // Unrolled loops for loading and storing streams of bytes. | |
222 | ||
223 | template <typename T, std::size_t n_bytes, | |
224 | bool sign=boost::is_signed<T>::value > | |
225 | struct unrolled_byte_loops | |
226 | { | |
227 | typedef unrolled_byte_loops<T, n_bytes - 1, sign> next; | |
228 | ||
11fdf7f2 TL |
229 | // shifting a negative number is flagged by -fsanitize=undefined |
230 | // so use the corresponding unsigned type for the shifts | |
231 | ||
232 | typedef typename boost::conditional< | |
233 | boost::is_integral<T>::value, | |
234 | boost::make_unsigned<T>, boost::type_identity<T> >::type::type U; | |
235 | ||
7c673cae | 236 | static T load_big(const unsigned char* bytes) BOOST_NOEXCEPT |
11fdf7f2 | 237 | { return static_cast<T>(*(bytes - 1) | (static_cast<U>(next::load_big(bytes - 1)) << 8)); } |
7c673cae | 238 | static T load_little(const unsigned char* bytes) BOOST_NOEXCEPT |
11fdf7f2 | 239 | { return static_cast<T>(*bytes | (static_cast<U>(next::load_little(bytes + 1)) << 8)); } |
7c673cae FG |
240 | |
241 | static void store_big(char* bytes, T value) BOOST_NOEXCEPT | |
242 | { | |
243 | *(bytes - 1) = static_cast<char>(value); | |
11fdf7f2 | 244 | next::store_big(bytes - 1, static_cast<T>(static_cast<U>(value) >> 8)); |
7c673cae FG |
245 | } |
246 | static void store_little(char* bytes, T value) BOOST_NOEXCEPT | |
247 | { | |
248 | *bytes = static_cast<char>(value); | |
11fdf7f2 | 249 | next::store_little(bytes + 1, static_cast<T>(static_cast<U>(value) >> 8)); |
7c673cae FG |
250 | } |
251 | }; | |
252 | ||
253 | template <typename T> | |
254 | struct unrolled_byte_loops<T, 1, false> | |
255 | { | |
256 | static T load_big(const unsigned char* bytes) BOOST_NOEXCEPT | |
257 | { return *(bytes - 1); } | |
258 | static T load_little(const unsigned char* bytes) BOOST_NOEXCEPT | |
259 | { return *bytes; } | |
260 | static void store_big(char* bytes, T value) BOOST_NOEXCEPT | |
261 | { *(bytes - 1) = static_cast<char>(value); } | |
262 | static void store_little(char* bytes, T value) BOOST_NOEXCEPT | |
263 | { *bytes = static_cast<char>(value); } | |
264 | ||
265 | }; | |
266 | ||
267 | template <typename T> | |
268 | struct unrolled_byte_loops<T, 1, true> | |
269 | { | |
270 | static T load_big(const unsigned char* bytes) BOOST_NOEXCEPT | |
271 | { return *reinterpret_cast<const signed char*>(bytes - 1); } | |
272 | static T load_little(const unsigned char* bytes) BOOST_NOEXCEPT | |
273 | { return *reinterpret_cast<const signed char*>(bytes); } | |
274 | static void store_big(char* bytes, T value) BOOST_NOEXCEPT | |
275 | { *(bytes - 1) = static_cast<char>(value); } | |
276 | static void store_little(char* bytes, T value) BOOST_NOEXCEPT | |
277 | { *bytes = static_cast<char>(value); } | |
278 | }; | |
279 | ||
280 | template <typename T, std::size_t n_bytes> | |
281 | inline | |
282 | T load_big_endian(const void* bytes) BOOST_NOEXCEPT | |
283 | { | |
284 | return unrolled_byte_loops<T, n_bytes>::load_big | |
285 | (static_cast<const unsigned char*>(bytes) + n_bytes); | |
286 | } | |
287 | ||
288 | template <typename T, std::size_t n_bytes> | |
289 | inline | |
290 | T load_little_endian(const void* bytes) BOOST_NOEXCEPT | |
291 | { | |
292 | # if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || defined(_M_IX86) | |
293 | // On x86 (which is little endian), unaligned loads are permitted | |
294 | if (sizeof(T) == n_bytes) // GCC 4.9, VC++ 14.0, and probably others, elide this | |
295 | // test and generate code only for the applicable return | |
296 | // case since sizeof(T) and n_bytes are known at compile | |
297 | // time. | |
298 | { | |
11fdf7f2 TL |
299 | // Avoids -fsanitize=undefined violations due to unaligned loads |
300 | // All major x86 compilers optimize a short-sized memcpy into a single instruction | |
301 | ||
302 | T t; | |
303 | std::memcpy( &t, bytes, sizeof(T) ); | |
304 | return t; | |
7c673cae FG |
305 | } |
306 | # endif | |
307 | return unrolled_byte_loops<T, n_bytes>::load_little | |
308 | (static_cast<const unsigned char*>(bytes)); | |
309 | } | |
310 | ||
311 | template <typename T, std::size_t n_bytes> | |
312 | inline | |
313 | void store_big_endian(void* bytes, T value) BOOST_NOEXCEPT | |
314 | { | |
315 | unrolled_byte_loops<T, n_bytes>::store_big | |
316 | (static_cast<char*>(bytes) + n_bytes, value); | |
317 | } | |
318 | ||
319 | template <typename T, std::size_t n_bytes> | |
320 | inline | |
321 | void store_little_endian(void* bytes, T value) BOOST_NOEXCEPT | |
322 | { | |
323 | # if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || defined(_M_IX86) | |
324 | // On x86 (which is little endian), unaligned stores are permitted | |
325 | if (sizeof(T) == n_bytes) // GCC 4.9, VC++ 14.0, and probably others, elide this | |
326 | // test and generate code only for the applicable return | |
327 | // case since sizeof(T) and n_bytes are known at compile | |
328 | // time. | |
329 | { | |
11fdf7f2 TL |
330 | // Avoids -fsanitize=undefined violations due to unaligned stores |
331 | // All major x86 compilers optimize a short-sized memcpy into a single instruction | |
332 | ||
333 | std::memcpy( bytes, &value, sizeof(T) ); | |
7c673cae FG |
334 | return; |
335 | } | |
336 | # endif | |
337 | unrolled_byte_loops<T, n_bytes>::store_little | |
338 | (static_cast<char*>(bytes), value); | |
339 | } | |
340 | ||
341 | } // namespace detail | |
342 | ||
343 | # ifdef BOOST_ENDIAN_LOG | |
344 | bool endian_log(true); | |
345 | # endif | |
346 | ||
347 | // endian_buffer class template specializations --------------------------------------// | |
348 | ||
349 | // Specializations that represent unaligned bytes. | |
350 | // Taking an integer type as a parameter provides a nice way to pass both | |
351 | // the size and signedness of the desired integer and get the appropriate | |
352 | // corresponding integer type for the interface. | |
353 | ||
354 | // Q: Should endian_buffer supply "value_type operator value_type() const noexcept"? | |
355 | // A: No. The rationale for endian_buffers is to prevent high-cost hidden | |
356 | // conversions. If an implicit conversion operator is supplied, hidden conversions | |
357 | // can occur. | |
358 | ||
359 | // unaligned big endian_buffer specialization | |
360 | template <typename T, std::size_t n_bits> | |
361 | class endian_buffer< order::big, T, n_bits, align::no > | |
362 | { | |
363 | BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits ); | |
364 | public: | |
365 | typedef T value_type; | |
366 | # ifndef BOOST_ENDIAN_NO_CTORS | |
367 | endian_buffer() BOOST_ENDIAN_DEFAULT_CONSTRUCT | |
368 | explicit endian_buffer(T val) BOOST_NOEXCEPT | |
11fdf7f2 | 369 | { |
7c673cae FG |
370 | # ifdef BOOST_ENDIAN_LOG |
371 | if ( endian_log ) | |
372 | std::cout << "big, unaligned, " | |
373 | << n_bits << "-bits, construct(" << val << ")\n"; | |
374 | # endif | |
375 | detail::store_big_endian<T, n_bits/8>(m_value, val); | |
376 | } | |
377 | # endif | |
378 | endian_buffer & operator=(T val) BOOST_NOEXCEPT | |
379 | { | |
380 | # ifdef BOOST_ENDIAN_LOG | |
381 | if (endian_log) | |
382 | std::cout << "big, unaligned, " << n_bits << "-bits, assign(" << val << ")\n"; | |
383 | # endif | |
384 | detail::store_big_endian<T, n_bits/8>(m_value, val); | |
385 | return *this; | |
386 | } | |
387 | value_type value() const BOOST_NOEXCEPT | |
11fdf7f2 | 388 | { |
7c673cae FG |
389 | # ifdef BOOST_ENDIAN_LOG |
390 | if ( endian_log ) | |
391 | std::cout << "big, unaligned, " << n_bits << "-bits, convert(" | |
392 | << detail::load_big_endian<T, n_bits/8>(m_value) << ")\n"; | |
393 | # endif | |
394 | return detail::load_big_endian<T, n_bits/8>(m_value); | |
395 | } | |
396 | const char* data() const BOOST_NOEXCEPT { return m_value; } | |
397 | protected: | |
398 | char m_value[n_bits/8]; | |
399 | }; | |
400 | ||
401 | // unaligned little endian_buffer specialization | |
402 | template <typename T, std::size_t n_bits> | |
403 | class endian_buffer< order::little, T, n_bits, align::no > | |
404 | { | |
405 | BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits ); | |
406 | public: | |
407 | typedef T value_type; | |
408 | # ifndef BOOST_ENDIAN_NO_CTORS | |
409 | endian_buffer() BOOST_ENDIAN_DEFAULT_CONSTRUCT | |
410 | explicit endian_buffer(T val) BOOST_NOEXCEPT | |
11fdf7f2 | 411 | { |
7c673cae FG |
412 | # ifdef BOOST_ENDIAN_LOG |
413 | if ( endian_log ) | |
414 | std::cout << "little, unaligned, " << n_bits << "-bits, construct(" | |
415 | << val << ")\n"; | |
416 | # endif | |
417 | detail::store_little_endian<T, n_bits/8>(m_value, val); | |
418 | } | |
419 | # endif | |
420 | endian_buffer & operator=(T val) BOOST_NOEXCEPT | |
421 | { detail::store_little_endian<T, n_bits/8>(m_value, val); return *this; } | |
422 | value_type value() const BOOST_NOEXCEPT | |
11fdf7f2 | 423 | { |
7c673cae FG |
424 | # ifdef BOOST_ENDIAN_LOG |
425 | if ( endian_log ) | |
426 | std::cout << "little, unaligned, " << n_bits << "-bits, convert(" | |
427 | << detail::load_little_endian<T, n_bits/8>(m_value) << ")\n"; | |
428 | # endif | |
429 | return detail::load_little_endian<T, n_bits/8>(m_value); | |
430 | } | |
431 | const char* data() const BOOST_NOEXCEPT { return m_value; } | |
432 | protected: | |
433 | char m_value[n_bits/8]; | |
434 | }; | |
435 | ||
436 | // align::yes specializations; only n_bits == 16/32/64 supported | |
437 | ||
438 | // aligned big endian_buffer specialization | |
439 | template <typename T, std::size_t n_bits> | |
440 | class endian_buffer<order::big, T, n_bits, align::yes> | |
441 | { | |
442 | BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits ); | |
443 | BOOST_STATIC_ASSERT( sizeof(T) == n_bits/8 ); | |
444 | public: | |
445 | typedef T value_type; | |
446 | # ifndef BOOST_ENDIAN_NO_CTORS | |
447 | endian_buffer() BOOST_ENDIAN_DEFAULT_CONSTRUCT | |
448 | explicit endian_buffer(T val) BOOST_NOEXCEPT | |
449 | { | |
450 | # ifdef BOOST_ENDIAN_LOG | |
451 | if ( endian_log ) | |
452 | std::cout << "big, aligned, " << n_bits | |
453 | << "-bits, construct(" << val << ")\n"; | |
454 | # endif | |
455 | m_value = ::boost::endian::native_to_big(val); | |
456 | } | |
457 | ||
11fdf7f2 | 458 | # endif |
7c673cae FG |
459 | endian_buffer& operator=(T val) BOOST_NOEXCEPT |
460 | { | |
461 | m_value = ::boost::endian::native_to_big(val); | |
462 | return *this; | |
463 | } | |
464 | //operator value_type() const BOOST_NOEXCEPT | |
11fdf7f2 | 465 | //{ |
7c673cae FG |
466 | // return ::boost::endian::big_to_native(m_value); |
467 | //} | |
468 | value_type value() const BOOST_NOEXCEPT | |
469 | { | |
470 | # ifdef BOOST_ENDIAN_LOG | |
471 | if ( endian_log ) | |
472 | std::cout << "big, aligned, " << n_bits << "-bits, convert(" | |
473 | << ::boost::endian::big_to_native(m_value) << ")\n"; | |
474 | # endif | |
475 | return ::boost::endian::big_to_native(m_value); | |
476 | } | |
477 | const char* data() const BOOST_NOEXCEPT | |
478 | {return reinterpret_cast<const char*>(&m_value);} | |
479 | protected: | |
480 | T m_value; | |
481 | }; | |
482 | ||
483 | // aligned little endian_buffer specialization | |
484 | template <typename T, std::size_t n_bits> | |
485 | class endian_buffer<order::little, T, n_bits, align::yes> | |
486 | { | |
487 | BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits ); | |
488 | BOOST_STATIC_ASSERT( sizeof(T) == n_bits/8 ); | |
489 | public: | |
490 | typedef T value_type; | |
491 | # ifndef BOOST_ENDIAN_NO_CTORS | |
492 | endian_buffer() BOOST_ENDIAN_DEFAULT_CONSTRUCT | |
493 | explicit endian_buffer(T val) BOOST_NOEXCEPT | |
494 | { | |
495 | # ifdef BOOST_ENDIAN_LOG | |
496 | if ( endian_log ) | |
497 | std::cout << "little, aligned, " << n_bits | |
498 | << "-bits, construct(" << val << ")\n"; | |
499 | # endif | |
500 | m_value = ::boost::endian::native_to_little(val); | |
501 | } | |
502 | ||
11fdf7f2 | 503 | # endif |
7c673cae FG |
504 | endian_buffer& operator=(T val) BOOST_NOEXCEPT |
505 | { | |
506 | m_value = ::boost::endian::native_to_little(val); | |
507 | return *this; | |
508 | } | |
509 | value_type value() const BOOST_NOEXCEPT | |
510 | { | |
511 | # ifdef BOOST_ENDIAN_LOG | |
512 | if ( endian_log ) | |
513 | std::cout << "little, aligned, " << n_bits << "-bits, convert(" | |
514 | << ::boost::endian::little_to_native(m_value) << ")\n"; | |
515 | # endif | |
516 | return ::boost::endian::little_to_native(m_value); | |
517 | } | |
518 | const char* data() const BOOST_NOEXCEPT | |
519 | {return reinterpret_cast<const char*>(&m_value);} | |
520 | protected: | |
521 | T m_value; | |
522 | }; | |
523 | ||
524 | } // namespace endian | |
525 | } // namespace boost | |
526 | ||
527 | #if defined(__BORLANDC__) || defined( __CODEGEARC__) | |
528 | # pragma pack(pop) | |
529 | #endif | |
530 | ||
11fdf7f2 TL |
531 | #if defined(_MSC_VER) |
532 | # pragma warning(pop) | |
533 | #endif | |
7c673cae FG |
534 | |
535 | #endif // BOOST_ENDIAN_BUFFERS_HPP |