modify.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. http://spirit.sourceforge.net/
  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. #ifndef BOOST_SPIRIT_MODIFY_OCTOBER_25_2008_0142PM
  8. #define BOOST_SPIRIT_MODIFY_OCTOBER_25_2008_0142PM
  9. #if defined(_MSC_VER)
  10. #pragma once
  11. #endif
  12. #include <boost/type_traits/is_base_of.hpp>
  13. #include <boost/spirit/home/support/unused.hpp>
  14. namespace boost { namespace spirit
  15. {
  16. template <typename Domain, typename T, typename Enable = void>
  17. struct is_modifier_directive;
  18. // Testing if a modifier set includes a modifier T involves
  19. // checking for inheritance (i.e. Modifiers is derived from T)
  20. template <typename Modifiers, typename T>
  21. struct has_modifier
  22. : is_base_of<T, Modifiers> {};
  23. // Adding modifiers is done using multi-inheritance
  24. template <typename Current, typename New, typename Enable = void>
  25. struct compound_modifier : Current, New
  26. {
  27. compound_modifier()
  28. : Current(), New() {}
  29. compound_modifier(Current const& current, New const& new_)
  30. : Current(current), New(new_) {}
  31. };
  32. // Don't add if New is already in Current
  33. template <typename Current, typename New>
  34. struct compound_modifier<
  35. Current, New, typename enable_if<has_modifier<Current, New> >::type>
  36. : Current
  37. {
  38. compound_modifier()
  39. : Current() {}
  40. compound_modifier(Current const& current, New const&)
  41. : Current(current) {}
  42. };
  43. // Special case if Current is unused_type
  44. template <typename New, typename Enable>
  45. struct compound_modifier<unused_type, New, Enable> : New
  46. {
  47. compound_modifier()
  48. : New() {}
  49. compound_modifier(unused_type, New const& new_)
  50. : New(new_) {}
  51. };
  52. // Domains may specialize this modify metafunction to allow
  53. // directives to add information to the Modifier template
  54. // parameter that is passed to the make_component metafunction.
  55. // By default, we return the modifiers untouched
  56. template <typename Domain, typename Enable = void>
  57. struct modify
  58. {
  59. typedef void proto_is_callable_;
  60. template <typename Sig>
  61. struct result;
  62. template <typename This, typename Tag, typename Modifiers>
  63. struct result<This(Tag, Modifiers)>
  64. {
  65. typedef typename remove_const<
  66. typename remove_reference<Tag>::type>::type
  67. tag_type;
  68. typedef typename remove_const<
  69. typename remove_reference<Modifiers>::type>::type
  70. modifiers_type;
  71. typedef typename mpl::if_<
  72. is_modifier_directive<Domain, tag_type>
  73. , compound_modifier<modifiers_type, tag_type>
  74. , Modifiers>::type
  75. type;
  76. };
  77. template <typename Tag, typename Modifiers>
  78. typename result<modify(Tag, Modifiers)>::type
  79. operator()(Tag tag, Modifiers modifiers) const
  80. {
  81. return op(tag, modifiers, is_modifier_directive<Domain, Tag>());
  82. }
  83. template <typename Tag, typename Modifiers>
  84. Modifiers
  85. op(Tag /*tag*/, Modifiers modifiers, mpl::false_) const
  86. {
  87. return modifiers;
  88. }
  89. template <typename Tag, typename Modifiers>
  90. compound_modifier<Modifiers, Tag>
  91. op(Tag tag, Modifiers modifiers, mpl::true_) const
  92. {
  93. return compound_modifier<Modifiers, Tag>(modifiers, tag);
  94. }
  95. };
  96. }}
  97. #endif