buffers.hpp 21 KB

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