extract_param.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Copyright (c) 2001-2011 Hartmut Kaiser
  4. Copyright (c) 2009 Francois Barel
  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. #if !defined(BOOST_SPIRIT_EXTRACT_PARAM_AUGUST_08_2009_0848AM)
  9. #define BOOST_SPIRIT_EXTRACT_PARAM_AUGUST_08_2009_0848AM
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. #include <boost/spirit/home/support/meta_compiler.hpp>
  14. #include <boost/spirit/home/support/nonterminal/locals.hpp>
  15. #include <boost/spirit/home/support/unused.hpp>
  16. #include <boost/spirit/home/support/common_terminals.hpp>
  17. #include <boost/function_types/is_function.hpp>
  18. #include <boost/function_types/parameter_types.hpp>
  19. #include <boost/function_types/result_type.hpp>
  20. #include <boost/fusion/include/as_list.hpp>
  21. #include <boost/fusion/include/as_vector.hpp>
  22. #include <boost/mpl/deref.hpp>
  23. #include <boost/mpl/end.hpp>
  24. #include <boost/mpl/eval_if.hpp>
  25. #include <boost/mpl/find_if.hpp>
  26. #include <boost/mpl/identity.hpp>
  27. #include <boost/mpl/if.hpp>
  28. #include <boost/mpl/or.hpp>
  29. #include <boost/mpl/and.hpp>
  30. #include <boost/mpl/placeholders.hpp>
  31. #include <boost/type_traits/is_same.hpp>
  32. namespace boost { namespace spirit { namespace detail
  33. {
  34. ///////////////////////////////////////////////////////////////////////////
  35. // Helpers to extract params (locals, attributes, ...) from nonterminal
  36. // template arguments
  37. ///////////////////////////////////////////////////////////////////////////
  38. template <typename Types, typename Pred, typename Default>
  39. struct extract_param
  40. {
  41. typedef typename mpl::find_if<Types, Pred>::type pos;
  42. typedef typename
  43. mpl::eval_if<
  44. is_same<pos, typename mpl::end<Types>::type>
  45. , mpl::identity<Default>
  46. , mpl::deref<pos>
  47. >::type
  48. type;
  49. };
  50. ///////////////////////////////////////////////////////////////////////////
  51. template <typename Types>
  52. struct extract_locals
  53. : fusion::result_of::as_vector<
  54. typename extract_param<
  55. Types
  56. , is_locals<mpl::_>
  57. , locals<>
  58. >::type
  59. >
  60. {};
  61. ///////////////////////////////////////////////////////////////////////////
  62. template <typename Domain, typename Types>
  63. struct extract_component
  64. : spirit::result_of::compile<
  65. Domain
  66. , typename extract_param<
  67. Types
  68. , traits::matches<Domain, mpl::_>
  69. , unused_type
  70. >::type
  71. >
  72. {};
  73. ///////////////////////////////////////////////////////////////////////////
  74. template <typename T>
  75. struct make_function_type : mpl::identity<T()> {};
  76. ///////////////////////////////////////////////////////////////////////////
  77. template <typename Types, typename Encoding, typename Domain>
  78. struct extract_sig
  79. {
  80. typedef typename
  81. extract_param<
  82. Types
  83. , mpl::or_<
  84. function_types::is_function<mpl::_>
  85. , mpl::and_<
  86. mpl::not_<is_locals<mpl::_> >
  87. , mpl::not_<is_same<mpl::_, Encoding> >
  88. , mpl::not_<traits::matches<Domain, mpl::_> >
  89. , mpl::not_<is_same<mpl::_, unused_type> >
  90. >
  91. >
  92. , void()
  93. >::type
  94. attr_of_ftype;
  95. typedef typename
  96. mpl::eval_if<
  97. function_types::is_function<attr_of_ftype>
  98. , mpl::identity<attr_of_ftype>
  99. , make_function_type<attr_of_ftype>
  100. >::type
  101. type;
  102. };
  103. template <typename Sig>
  104. struct attr_from_sig
  105. {
  106. typedef typename function_types::result_type<Sig>::type attr;
  107. typedef typename
  108. mpl::if_<
  109. is_same<attr, void>
  110. , unused_type
  111. , attr
  112. >::type
  113. type;
  114. };
  115. template <typename Sig>
  116. struct params_from_sig
  117. {
  118. typedef typename function_types::parameter_types<Sig>::type params;
  119. typedef typename fusion::result_of::as_list<params>::type type;
  120. };
  121. ///////////////////////////////////////////////////////////////////////////
  122. template <typename Types>
  123. struct extract_encoding
  124. : extract_param<
  125. Types
  126. , is_char_encoding<mpl::_>
  127. , unused_type
  128. >
  129. {};
  130. }}}
  131. #endif