binary.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Hartmut Kaiser
  3. Copyright (c) 2001-2011 Joel de Guzman
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #if !defined(BOOST_SPIRIT_X3_BINARY_MAY_08_2007_0808AM)
  8. #define BOOST_SPIRIT_X3_BINARY_MAY_08_2007_0808AM
  9. #include <boost/spirit/home/x3/core/parser.hpp>
  10. #include <boost/spirit/home/x3/core/skip_over.hpp>
  11. #include <boost/spirit/home/x3/support/traits/move_to.hpp>
  12. #include <cstdint>
  13. #include <boost/endian/conversion.hpp>
  14. #include <boost/endian/arithmetic.hpp>
  15. #include <boost/mpl/or.hpp>
  16. #include <boost/type_traits/is_integral.hpp>
  17. #include <boost/type_traits/is_enum.hpp>
  18. #include <boost/type_traits/is_floating_point.hpp>
  19. #include <boost/config.hpp>
  20. namespace boost { namespace spirit { namespace x3
  21. {
  22. template <typename V, typename T
  23. , boost::endian::order endian, std::size_t bits>
  24. struct binary_lit_parser
  25. : parser<binary_lit_parser<V, T, endian, bits> >
  26. {
  27. static bool const has_attribute = false;
  28. typedef unused_type attribute_type;
  29. binary_lit_parser(V n_)
  30. : n(n_) {}
  31. template <typename Iterator, typename Context, typename Attribute>
  32. bool parse(Iterator& first, Iterator const& last
  33. , Context const& context, unused_type, Attribute& attr_param) const
  34. {
  35. x3::skip_over(first, last, context);
  36. auto bytes = reinterpret_cast<const unsigned char*>(&n);
  37. Iterator it = first;
  38. for (unsigned int i = 0; i < sizeof(n); ++i)
  39. {
  40. if (it == last || *bytes++ != static_cast<unsigned char>(*it++))
  41. return false;
  42. }
  43. first = it;
  44. x3::traits::move_to(n, attr_param);
  45. return true;
  46. }
  47. boost::endian::endian_arithmetic<endian, T, bits> n;
  48. };
  49. ///////////////////////////////////////////////////////////////////////////
  50. template <typename T, boost::endian::order endian, std::size_t bits>
  51. struct any_binary_parser : parser<any_binary_parser<T, endian, bits > >
  52. {
  53. typedef T attribute_type;
  54. static bool const has_attribute =
  55. !is_same<unused_type, attribute_type>::value;
  56. template <typename Iterator, typename Context, typename Attribute>
  57. bool parse(Iterator& first, Iterator const& last
  58. , Context const& context, unused_type, Attribute& attr_param) const
  59. {
  60. x3::skip_over(first, last, context);
  61. attribute_type attr_;
  62. auto bytes = reinterpret_cast<unsigned char*>(&attr_);
  63. Iterator it = first;
  64. for (unsigned int i = 0; i < sizeof(attr_); ++i)
  65. {
  66. if (it == last)
  67. return false;
  68. *bytes++ = *it++;
  69. }
  70. first = it;
  71. x3::traits::move_to(
  72. endian::conditional_reverse<endian, endian::order::native>(attr_)
  73. , attr_param );
  74. return true;
  75. }
  76. template <typename V>
  77. binary_lit_parser< V, T, endian, bits> operator()(V n) const
  78. {
  79. return {n};
  80. }
  81. };
  82. #define BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(name, endiantype, attrtype, bits) \
  83. typedef any_binary_parser< attrtype, boost::endian::order::endiantype, bits > name##type; \
  84. name##type const name = name##type();
  85. BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(byte_, native, uint_least8_t, 8)
  86. BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(word, native, uint_least16_t, 16)
  87. BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(big_word, big, uint_least16_t, 16)
  88. BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(little_word, little, uint_least16_t, 16)
  89. BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(dword, native, uint_least32_t, 32)
  90. BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(big_dword, big, uint_least32_t, 32)
  91. BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(little_dword, little, uint_least32_t, 32)
  92. #ifdef BOOST_HAS_LONG_LONG
  93. BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(qword, native, uint_least64_t, 64)
  94. BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(big_qword, big, uint_least64_t, 64)
  95. BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(little_qword, little, uint_least64_t, 64)
  96. #endif
  97. // Use a pseudo configuration macro to make clear that endian libray support
  98. // for floating point types is required. Must be removed as soon as the endian library
  99. // properly supports floating point types.
  100. #ifdef BOOST_ENDIAN_HAS_FLOATING_POINT
  101. BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(bin_float, native, float, 32)
  102. BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(big_bin_float, big, float, 32)
  103. BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(little_bin_float, little, float, 32)
  104. BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(bin_double, native, double, 64)
  105. BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(big_bin_double, big, double, 64)
  106. BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(little_bin_double, little, double, 64)
  107. #endif
  108. #undef BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE
  109. ///////////////////////////////////////////////////////////////////////////
  110. template <typename T, std::size_t bits>
  111. struct get_info<any_binary_parser<T, endian::order::little, bits>>
  112. {
  113. typedef std::string result_type;
  114. std::string operator()(any_binary_parser<T, endian::order::little, bits> const& p) const
  115. {
  116. return "little-endian binary";
  117. }
  118. };
  119. template <typename T, std::size_t bits>
  120. struct get_info<any_binary_parser<T, endian::order::big, bits>>
  121. {
  122. typedef std::string result_type;
  123. std::string operator()(any_binary_parser<T, endian::order::big, bits> const& p) const
  124. {
  125. return "big-endian binary";
  126. }
  127. };
  128. template <typename V, typename T, std::size_t bits>
  129. struct get_info<binary_lit_parser<V, T, endian::order::little, bits>>
  130. {
  131. typedef std::string result_type;
  132. std::string operator()(binary_lit_parser<V, T, endian::order::little, bits> const& p) const
  133. {
  134. return "little-endian binary";
  135. }
  136. };
  137. template <typename V, typename T, std::size_t bits>
  138. struct get_info<binary_lit_parser<V, T, endian::order::big, bits>>
  139. {
  140. typedef std::string result_type;
  141. std::string operator()(binary_lit_parser<V, T, endian::order::big, bits> const& p) const
  142. {
  143. return "big-endian binary";
  144. }
  145. };
  146. }}}
  147. #endif