transform_attribute.hpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*=============================================================================
  2. Copyright (c) 2001-2014 Joel de Guzman
  3. Copyright (c) 2001-2011 Hartmut Kaiser
  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(SPIRIT_X3_DETAIL_ATTRIBUTES_APR_18_2010_0458PM)
  8. #define SPIRIT_X3_DETAIL_ATTRIBUTES_APR_18_2010_0458PM
  9. #include <boost/spirit/home/x3/support/traits/transform_attribute.hpp>
  10. #include <boost/spirit/home/x3/support/traits/move_to.hpp>
  11. #include <type_traits>
  12. #include <utility>
  13. ///////////////////////////////////////////////////////////////////////////////
  14. namespace boost { namespace spirit { namespace x3
  15. {
  16. struct parser_id;
  17. template <typename Exposed, typename Transformed>
  18. struct default_transform_attribute
  19. {
  20. typedef Transformed type;
  21. static Transformed pre(Exposed&) { return Transformed(); }
  22. static void post(Exposed& val, Transformed&& attr)
  23. {
  24. traits::move_to(std::forward<Transformed>(attr), val);
  25. }
  26. };
  27. // handle case where no transformation is required as the types are the same
  28. template <typename Attribute>
  29. struct default_transform_attribute<Attribute, Attribute>
  30. {
  31. typedef Attribute& type;
  32. static Attribute& pre(Attribute& val) { return val; }
  33. static void post(Attribute&, Attribute const&) {}
  34. };
  35. // main specialization for x3
  36. template <typename Exposed, typename Transformed, typename Enable = void>
  37. struct transform_attribute
  38. : default_transform_attribute<Exposed, Transformed> {};
  39. // unused_type needs some special handling as well
  40. template <>
  41. struct transform_attribute<unused_type, unused_type>
  42. {
  43. typedef unused_type type;
  44. static unused_type pre(unused_type) { return unused; }
  45. static void post(unused_type, unused_type) {}
  46. };
  47. template <>
  48. struct transform_attribute<unused_type const, unused_type>
  49. : transform_attribute<unused_type, unused_type> {};
  50. template <typename Attribute>
  51. struct transform_attribute<unused_type, Attribute>
  52. : transform_attribute<unused_type, unused_type> {};
  53. template <typename Attribute>
  54. struct transform_attribute<unused_type const, Attribute>
  55. : transform_attribute<unused_type, unused_type> {};
  56. template <typename Attribute>
  57. struct transform_attribute<Attribute, unused_type>
  58. : transform_attribute<unused_type, unused_type> {};
  59. template <typename Attribute>
  60. struct transform_attribute<Attribute const, unused_type>
  61. : transform_attribute<unused_type, unused_type> {};
  62. }}}
  63. ///////////////////////////////////////////////////////////////////////////////
  64. namespace boost { namespace spirit { namespace x3 { namespace traits
  65. {
  66. template <typename Exposed, typename Transformed>
  67. struct transform_attribute<Exposed, Transformed, x3::parser_id>
  68. : x3::transform_attribute<Exposed, Transformed>
  69. {
  70. static_assert(!std::is_reference<Exposed>::value,
  71. "Exposed cannot be a reference type");
  72. static_assert(!std::is_reference<Transformed>::value,
  73. "Transformed cannot be a reference type");
  74. };
  75. }}}}
  76. #endif