buffers.hpp 14 KB

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