Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / endian / buffers.hpp
1 //  boost/endian/buffers.hpp  ----------------------------------------------------------//
2
3 //  (C) Copyright Darin Adler 2000
4 //  (C) Copyright Beman Dawes 2006, 2009, 2014
5 //  (C) Copyright Peter Dimov 2019
6
7 //  Distributed under the Boost Software License, Version 1.0.
8 //  See http://www.boost.org/LICENSE_1_0.txt
9
10 //  See library home page at http://www.boost.org/libs/endian
11
12 //--------------------------------------------------------------------------------------//
13
14 //  Original design developed by Darin Adler based on classes developed by Mark
15 //  Borgerding. Four original class templates were combined into a single endian
16 //  class template by Beman Dawes, who also added the unrolled_byte_loops sign
17 //  partial specialization to correctly extend the sign when cover integer size
18 //  differs from endian representation size.
19
20 // TODO: When a compiler supporting constexpr becomes available, try possible uses.
21
22 #ifndef BOOST_ENDIAN_BUFFERS_HPP
23 #define BOOST_ENDIAN_BUFFERS_HPP
24
25 #if defined(_MSC_VER)
26 # pragma warning(push)
27 # pragma warning(disable: 4127)  // conditional expression is constant
28 #endif
29
30 #include <boost/endian/detail/endian_store.hpp>
31 #include <boost/endian/detail/endian_load.hpp>
32 #include <boost/core/scoped_enum.hpp>
33 #include <boost/predef/other/endian.h>
34 #include <boost/static_assert.hpp>
35 #include <boost/cstdint.hpp>
36 #include <boost/config.hpp>
37 #include <boost/config/workaround.hpp>
38 #include <iosfwd>
39 #include <climits>
40 #include <cstring>
41
42 #if defined(__BORLANDC__) || defined( __CODEGEARC__)
43 # pragma pack(push, 1)
44 #endif
45
46 # if CHAR_BIT != 8
47 #   error Platforms with CHAR_BIT != 8 are not supported
48 # endif
49
50 # ifdef BOOST_NO_CXX11_DEFAULTED_FUNCTIONS
51 #   define BOOST_ENDIAN_DEFAULT_CONSTRUCT {}          // C++03
52 # else
53 #   define BOOST_ENDIAN_DEFAULT_CONSTRUCT = default;  // C++0x
54 # endif
55
56 // g++ pre-4.6 does not support unrestricted unions, but we have no Config macro for that
57 # if (defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) || BOOST_WORKAROUND(BOOST_GCC, < 40600)) && defined(BOOST_ENDIAN_FORCE_PODNESS)
58 #   define BOOST_ENDIAN_NO_CTORS
59 # endif
60
61 //----------------------------------  synopsis  ----------------------------------------//
62
63 namespace boost
64 {
65 namespace endian
66 {
67
68   BOOST_SCOPED_ENUM_START(align)
69   {no, yes
70 #   ifdef BOOST_ENDIAN_DEPRECATED_NAMES
71       , unaligned = no, aligned = yes
72 #   endif
73   }; BOOST_SCOPED_ENUM_END
74
75   template <BOOST_SCOPED_ENUM(order) Order, class T, std::size_t n_bits,
76     BOOST_SCOPED_ENUM(align) A = align::no>
77       class endian_buffer;
78
79   // aligned big endian signed integer buffers
80   typedef endian_buffer<order::big, int8_t, 8, align::yes>       big_int8_buf_at;
81   typedef endian_buffer<order::big, int16_t, 16, align::yes>     big_int16_buf_at;
82   typedef endian_buffer<order::big, int32_t, 32, align::yes>     big_int32_buf_at;
83   typedef endian_buffer<order::big, int64_t, 64, align::yes>     big_int64_buf_at;
84
85   // aligned big endian unsigned integer buffers
86   typedef endian_buffer<order::big, uint8_t, 8, align::yes>      big_uint8_buf_at;
87   typedef endian_buffer<order::big, uint16_t, 16, align::yes>    big_uint16_buf_at;
88   typedef endian_buffer<order::big, uint32_t, 32, align::yes>    big_uint32_buf_at;
89   typedef endian_buffer<order::big, uint64_t, 64, align::yes>    big_uint64_buf_at;
90
91   // aligned little endian signed integer buffers
92   typedef endian_buffer<order::little, int8_t, 8, align::yes>    little_int8_buf_at;
93   typedef endian_buffer<order::little, int16_t, 16, align::yes>  little_int16_buf_at;
94   typedef endian_buffer<order::little, int32_t, 32, align::yes>  little_int32_buf_at;
95   typedef endian_buffer<order::little, int64_t, 64, align::yes>  little_int64_buf_at;
96
97   // aligned little endian unsigned integer buffers
98   typedef endian_buffer<order::little, uint8_t, 8, align::yes>   little_uint8_buf_at;
99   typedef endian_buffer<order::little, uint16_t, 16, align::yes> little_uint16_buf_at;
100   typedef endian_buffer<order::little, uint32_t, 32, align::yes> little_uint32_buf_at;
101   typedef endian_buffer<order::little, uint64_t, 64, align::yes> little_uint64_buf_at;
102
103   // aligned floating point buffers
104   typedef endian_buffer<order::big, float, 32, align::yes>       big_float32_buf_at;
105   typedef endian_buffer<order::big, double, 64, align::yes>      big_float64_buf_at;
106   typedef endian_buffer<order::little, float, 32, align::yes>    little_float32_buf_at;
107   typedef endian_buffer<order::little, double, 64, align::yes>   little_float64_buf_at;
108
109   // aligned native endian typedefs are not provided because
110   // <cstdint> types are superior for this use case
111
112   // unaligned big endian signed integer buffers
113   typedef endian_buffer<order::big, int_least8_t, 8>        big_int8_buf_t;
114   typedef endian_buffer<order::big, int_least16_t, 16>      big_int16_buf_t;
115   typedef endian_buffer<order::big, int_least32_t, 24>      big_int24_buf_t;
116   typedef endian_buffer<order::big, int_least32_t, 32>      big_int32_buf_t;
117   typedef endian_buffer<order::big, int_least64_t, 40>      big_int40_buf_t;
118   typedef endian_buffer<order::big, int_least64_t, 48>      big_int48_buf_t;
119   typedef endian_buffer<order::big, int_least64_t, 56>      big_int56_buf_t;
120   typedef endian_buffer<order::big, int_least64_t, 64>      big_int64_buf_t;
121
122   // unaligned big endian unsigned integer buffers
123   typedef endian_buffer<order::big, uint_least8_t, 8>       big_uint8_buf_t;
124   typedef endian_buffer<order::big, uint_least16_t, 16>     big_uint16_buf_t;
125   typedef endian_buffer<order::big, uint_least32_t, 24>     big_uint24_buf_t;
126   typedef endian_buffer<order::big, uint_least32_t, 32>     big_uint32_buf_t;
127   typedef endian_buffer<order::big, uint_least64_t, 40>     big_uint40_buf_t;
128   typedef endian_buffer<order::big, uint_least64_t, 48>     big_uint48_buf_t;
129   typedef endian_buffer<order::big, uint_least64_t, 56>     big_uint56_buf_t;
130   typedef endian_buffer<order::big, uint_least64_t, 64>     big_uint64_buf_t;
131
132   // unaligned little endian signed integer buffers
133   typedef endian_buffer<order::little, int_least8_t, 8>     little_int8_buf_t;
134   typedef endian_buffer<order::little, int_least16_t, 16>   little_int16_buf_t;
135   typedef endian_buffer<order::little, int_least32_t, 24>   little_int24_buf_t;
136   typedef endian_buffer<order::little, int_least32_t, 32>   little_int32_buf_t;
137   typedef endian_buffer<order::little, int_least64_t, 40>   little_int40_buf_t;
138   typedef endian_buffer<order::little, int_least64_t, 48>   little_int48_buf_t;
139   typedef endian_buffer<order::little, int_least64_t, 56>   little_int56_buf_t;
140   typedef endian_buffer<order::little, int_least64_t, 64>   little_int64_buf_t;
141
142   // unaligned little endian unsigned integer buffers
143   typedef endian_buffer<order::little, uint_least8_t, 8>    little_uint8_buf_t;
144   typedef endian_buffer<order::little, uint_least16_t, 16>  little_uint16_buf_t;
145   typedef endian_buffer<order::little, uint_least32_t, 24>  little_uint24_buf_t;
146   typedef endian_buffer<order::little, uint_least32_t, 32>  little_uint32_buf_t;
147   typedef endian_buffer<order::little, uint_least64_t, 40>  little_uint40_buf_t;
148   typedef endian_buffer<order::little, uint_least64_t, 48>  little_uint48_buf_t;
149   typedef endian_buffer<order::little, uint_least64_t, 56>  little_uint56_buf_t;
150   typedef endian_buffer<order::little, uint_least64_t, 64>  little_uint64_buf_t;
151
152 # if BOOST_ENDIAN_BIG_BYTE
153   // unaligned native endian signed integer buffers
154   typedef big_int8_buf_t   native_int8_buf_t;
155   typedef big_int16_buf_t  native_int16_buf_t;
156   typedef big_int24_buf_t  native_int24_buf_t;
157   typedef big_int32_buf_t  native_int32_buf_t;
158   typedef big_int40_buf_t  native_int40_buf_t;
159   typedef big_int48_buf_t  native_int48_buf_t;
160   typedef big_int56_buf_t  native_int56_buf_t;
161   typedef big_int64_buf_t  native_int64_buf_t;
162
163   // unaligned native endian unsigned integer buffers
164   typedef big_uint8_buf_t   native_uint8_buf_t;
165   typedef big_uint16_buf_t  native_uint16_buf_t;
166   typedef big_uint24_buf_t  native_uint24_buf_t;
167   typedef big_uint32_buf_t  native_uint32_buf_t;
168   typedef big_uint40_buf_t  native_uint40_buf_t;
169   typedef big_uint48_buf_t  native_uint48_buf_t;
170   typedef big_uint56_buf_t  native_uint56_buf_t;
171   typedef big_uint64_buf_t  native_uint64_buf_t;
172 # else
173   // unaligned native endian signed integer buffers
174   typedef little_int8_buf_t   native_int8_buf_t;
175   typedef little_int16_buf_t  native_int16_buf_t;
176   typedef little_int24_buf_t  native_int24_buf_t;
177   typedef little_int32_buf_t  native_int32_buf_t;
178   typedef little_int40_buf_t  native_int40_buf_t;
179   typedef little_int48_buf_t  native_int48_buf_t;
180   typedef little_int56_buf_t  native_int56_buf_t;
181   typedef little_int64_buf_t  native_int64_buf_t;
182
183   // unaligned native endian unsigned integer buffers
184   typedef little_uint8_buf_t   native_uint8_buf_t;
185   typedef little_uint16_buf_t  native_uint16_buf_t;
186   typedef little_uint24_buf_t  native_uint24_buf_t;
187   typedef little_uint32_buf_t  native_uint32_buf_t;
188   typedef little_uint40_buf_t  native_uint40_buf_t;
189   typedef little_uint48_buf_t  native_uint48_buf_t;
190   typedef little_uint56_buf_t  native_uint56_buf_t;
191   typedef little_uint64_buf_t  native_uint64_buf_t;
192 # endif
193
194   // unaligned floating point buffers
195   typedef endian_buffer<order::big, float, 32, align::no>       big_float32_buf_t;
196   typedef endian_buffer<order::big, double, 64, align::no>      big_float64_buf_t;
197   typedef endian_buffer<order::little, float, 32, align::no>    little_float32_buf_t;
198   typedef endian_buffer<order::little, double, 64, align::no>   little_float64_buf_t;
199   typedef endian_buffer<order::native, float, 32, align::no>    native_float32_buf_t;
200   typedef endian_buffer<order::native, double, 64, align::no>   native_float64_buf_t;
201
202   // Stream inserter
203   template <class charT, class traits, BOOST_SCOPED_ENUM(order) Order, class T,
204     std::size_t n_bits, BOOST_SCOPED_ENUM(align) A>
205   std::basic_ostream<charT, traits>&
206     operator<<(std::basic_ostream<charT, traits>& os,
207       const endian_buffer<Order, T, n_bits, A>& x)
208   {
209     return os << x.value();
210   }
211
212   // Stream extractor
213   template <class charT, class traits, BOOST_SCOPED_ENUM(order) Order, class T,
214     std::size_t n_bits, BOOST_SCOPED_ENUM(align) A>
215   std::basic_istream<charT, traits>&
216     operator>>(std::basic_istream<charT, traits>& is,
217       endian_buffer<Order, T, n_bits, A>& x)
218   {
219     T i;
220     if (is >> i)
221       x = i;
222     return is;
223   }
224
225 //----------------------------------  end synopsis  ------------------------------------//
226
227 //  endian_buffer class template specializations  --------------------------------------//
228
229 //  Specializations that represent unaligned bytes.
230 //  Taking an integer type as a parameter provides a nice way to pass both
231 //  the size and signedness of the desired integer and get the appropriate
232 //  corresponding integer type for the interface.
233
234 // Q: Should endian_buffer supply "value_type operator value_type() const noexcept"?
235 // A: No. The rationale for endian_buffers is to prevent high-cost hidden
236 //    conversions. If an implicit conversion operator is supplied, hidden conversions
237 //    can occur.
238
239 //  unaligned endian_buffer specialization
240
241 template< BOOST_SCOPED_ENUM(order) Order, class T, std::size_t n_bits >
242 class endian_buffer<Order, T, n_bits, align::no>
243 {
244 private:
245
246     BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits );
247
248     unsigned char value_[ n_bits / 8 ];
249
250 public:
251
252     typedef T value_type;
253
254 #ifndef BOOST_ENDIAN_NO_CTORS
255
256     endian_buffer() BOOST_ENDIAN_DEFAULT_CONSTRUCT
257
258     explicit endian_buffer( T val ) BOOST_NOEXCEPT
259     {
260         boost::endian::endian_store<T, n_bits / 8, Order>( value_, val );
261     }
262
263 #endif
264
265     endian_buffer& operator=( T val ) BOOST_NOEXCEPT
266     {
267         boost::endian::endian_store<T, n_bits / 8, Order>( value_, val );
268         return *this;
269     }
270
271     value_type value() const BOOST_NOEXCEPT
272     {
273         return boost::endian::endian_load<T, n_bits / 8, Order>( value_ );
274     }
275
276     unsigned char const * data() const BOOST_NOEXCEPT
277     {
278         return value_;
279     }
280
281     unsigned char * data() BOOST_NOEXCEPT
282     {
283         return value_;
284     }
285 };
286
287 // aligned specializations; only n_bits == 16/32/64 supported
288
289 // aligned endian_buffer specialization
290
291 template< BOOST_SCOPED_ENUM(order) Order, class T, std::size_t n_bits >
292 class endian_buffer<Order, T, n_bits, align::yes>
293 {
294 private:
295
296     BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits );
297     BOOST_STATIC_ASSERT( sizeof(T) == n_bits/8 );
298
299     union
300     {
301         unsigned char value_[ n_bits / 8 ];
302         T align_;
303     };
304
305 public:
306
307     typedef T value_type;
308
309 #ifndef BOOST_ENDIAN_NO_CTORS
310
311     endian_buffer() BOOST_ENDIAN_DEFAULT_CONSTRUCT
312
313     explicit endian_buffer( T val ) BOOST_NOEXCEPT
314     {
315         boost::endian::endian_store<T, n_bits / 8, Order>( value_, val );
316     }
317
318 #endif
319
320     endian_buffer& operator=( T val ) BOOST_NOEXCEPT
321     {
322         boost::endian::endian_store<T, n_bits / 8, Order>( value_, val );
323         return *this;
324     }
325
326     value_type value() const BOOST_NOEXCEPT
327     {
328         return boost::endian::endian_load<T, n_bits / 8, Order>( value_ );
329     }
330
331     unsigned char const * data() const BOOST_NOEXCEPT
332     {
333         return value_;
334     }
335
336     unsigned char * data() BOOST_NOEXCEPT
337     {
338         return value_;
339     }
340 };
341
342 // aligned native endian_buffer specialization
343
344 template< class T, std::size_t n_bits >
345 class endian_buffer<order::native, T, n_bits, align::yes>
346 {
347 private:
348
349     BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits );
350     BOOST_STATIC_ASSERT( sizeof(T) == n_bits/8 );
351
352     T value_;
353
354 public:
355
356     typedef T value_type;
357
358 #ifndef BOOST_ENDIAN_NO_CTORS
359
360     endian_buffer() BOOST_ENDIAN_DEFAULT_CONSTRUCT
361
362     explicit endian_buffer( T val ) BOOST_NOEXCEPT: value_( val )
363     {
364     }
365
366 #endif
367
368     endian_buffer& operator=( T val ) BOOST_NOEXCEPT
369     {
370         value_ = val;
371         return *this;
372     }
373
374     value_type value() const BOOST_NOEXCEPT
375     {
376         return value_;
377     }
378
379     unsigned char const * data() const BOOST_NOEXCEPT
380     {
381         return reinterpret_cast< unsigned char const* >( &value_ );
382     }
383
384     unsigned char * data() BOOST_NOEXCEPT
385     {
386         return reinterpret_cast< unsigned char* >( &value_ );
387     }
388 };
389
390 } // namespace endian
391 } // namespace boost
392
393 #if defined(__BORLANDC__) || defined( __CODEGEARC__)
394 # pragma pack(pop)
395 #endif
396
397 #if defined(_MSC_VER)
398 # pragma warning(pop)
399 #endif
400
401 #endif // BOOST_ENDIAN_BUFFERS_HPP