attr.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Hartmut Kaiser
  3. Copyright (c) 2001-2014 Joel de Guzman
  4. Copyright (c) 2013 Agustin Berge
  5. Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. ==============================================================================*/
  8. #ifndef BOOST_SPIRIT_X3_ATTR_JUL_23_2008_0956AM
  9. #define BOOST_SPIRIT_X3_ATTR_JUL_23_2008_0956AM
  10. #include <boost/spirit/home/x3/core/parser.hpp>
  11. #include <boost/spirit/home/x3/support/unused.hpp>
  12. #include <boost/spirit/home/x3/support/traits/container_traits.hpp>
  13. #include <boost/spirit/home/x3/support/traits/move_to.hpp>
  14. #include <boost/type_traits/is_same.hpp>
  15. #include <boost/type_traits/remove_cv.hpp>
  16. #include <boost/type_traits/remove_reference.hpp>
  17. #include <cstddef>
  18. #include <string>
  19. #include <utility>
  20. namespace boost { namespace spirit { namespace x3
  21. {
  22. namespace detail
  23. {
  24. template <typename Value, std::size_t N
  25. , typename = std::make_index_sequence<N>>
  26. struct array_helper;
  27. template <typename Value, std::size_t N, std::size_t... Is>
  28. struct array_helper<Value, N, std::index_sequence<Is...>>
  29. {
  30. constexpr array_helper(Value const (&value)[N])
  31. : value_{ value[Is]... } {}
  32. constexpr array_helper(Value (&&value)[N])
  33. : value_{ static_cast<Value&&>(value[Is])... } {}
  34. Value value_[N];
  35. };
  36. }
  37. template <typename Value>
  38. struct attr_parser : parser<attr_parser<Value>>
  39. {
  40. typedef Value attribute_type;
  41. static bool const has_attribute =
  42. !is_same<unused_type, attribute_type>::value;
  43. static bool const handles_container =
  44. traits::is_container<attribute_type>::value;
  45. constexpr attr_parser(Value const& value)
  46. : value_(value) {}
  47. constexpr attr_parser(Value&& value)
  48. : value_(std::move(value)) {}
  49. template <typename Iterator, typename Context
  50. , typename RuleContext, typename Attribute>
  51. bool parse(Iterator& /* first */, Iterator const& /* last */
  52. , Context const& /* context */, RuleContext&, Attribute& attr_) const
  53. {
  54. // $$$ Change to copy_to once we have it $$$
  55. traits::move_to(value_, attr_);
  56. return true;
  57. }
  58. Value value_;
  59. };
  60. template <typename Value, std::size_t N>
  61. struct attr_parser<Value[N]> : parser<attr_parser<Value[N]>>
  62. , detail::array_helper<Value, N>
  63. {
  64. using detail::array_helper<Value, N>::array_helper;
  65. typedef Value attribute_type[N];
  66. static bool const has_attribute =
  67. !is_same<unused_type, attribute_type>::value;
  68. static bool const handles_container = true;
  69. template <typename Iterator, typename Context
  70. , typename RuleContext, typename Attribute>
  71. bool parse(Iterator& /* first */, Iterator const& /* last */
  72. , Context const& /* context */, RuleContext&, Attribute& attr_) const
  73. {
  74. // $$$ Change to copy_to once we have it $$$
  75. traits::move_to(this->value_ + 0, this->value_ + N, attr_);
  76. return true;
  77. }
  78. };
  79. template <typename Value>
  80. struct get_info<attr_parser<Value>>
  81. {
  82. typedef std::string result_type;
  83. std::string operator()(attr_parser<Value> const& /*p*/) const
  84. {
  85. return "attr";
  86. }
  87. };
  88. struct attr_gen
  89. {
  90. template <typename Value>
  91. constexpr attr_parser<typename remove_cv<
  92. typename remove_reference<Value>::type>::type>
  93. operator()(Value&& value) const
  94. {
  95. return { std::forward<Value>(value) };
  96. }
  97. };
  98. constexpr auto attr = attr_gen{};
  99. }}}
  100. #endif