def_helper.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright David Abrahams 2002.
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef DEF_HELPER_DWA200287_HPP
  6. # define DEF_HELPER_DWA200287_HPP
  7. # include <boost/python/args.hpp>
  8. # include <boost/python/detail/indirect_traits.hpp>
  9. # include <boost/python/detail/type_traits.hpp>
  10. # include <boost/mpl/not.hpp>
  11. # include <boost/mpl/and.hpp>
  12. # include <boost/mpl/or.hpp>
  13. # include <boost/mpl/lambda.hpp>
  14. # include <boost/mpl/apply.hpp>
  15. # include <boost/tuple/tuple.hpp>
  16. # include <boost/python/detail/not_specified.hpp>
  17. # include <boost/python/detail/def_helper_fwd.hpp>
  18. namespace boost { namespace python {
  19. struct default_call_policies;
  20. namespace detail
  21. {
  22. // tuple_extract<Tuple,Predicate>::extract(t) returns the first
  23. // element of a Tuple whose type E satisfies the given Predicate
  24. // applied to add_reference<E>. The Predicate must be an MPL
  25. // metafunction class.
  26. template <class Tuple, class Predicate>
  27. struct tuple_extract;
  28. // Implementation class for when the tuple's head type does not
  29. // satisfy the Predicate
  30. template <bool matched>
  31. struct tuple_extract_impl
  32. {
  33. template <class Tuple, class Predicate>
  34. struct apply
  35. {
  36. typedef typename Tuple::head_type result_type;
  37. static typename Tuple::head_type extract(Tuple const& x)
  38. {
  39. return x.get_head();
  40. }
  41. };
  42. };
  43. // Implementation specialization for when the tuple's head type
  44. // satisfies the predicate
  45. template <>
  46. struct tuple_extract_impl<false>
  47. {
  48. template <class Tuple, class Predicate>
  49. struct apply
  50. {
  51. // recursive application of tuple_extract on the tail of the tuple
  52. typedef tuple_extract<typename Tuple::tail_type, Predicate> next;
  53. typedef typename next::result_type result_type;
  54. static result_type extract(Tuple const& x)
  55. {
  56. return next::extract(x.get_tail());
  57. }
  58. };
  59. };
  60. // A metafunction which selects a version of tuple_extract_impl to
  61. // use for the implementation of tuple_extract
  62. template <class Tuple, class Predicate>
  63. struct tuple_extract_base_select
  64. {
  65. typedef typename Tuple::head_type head_type;
  66. typedef typename mpl::apply1<Predicate,
  67. typename add_lvalue_reference<head_type>::type>::type match_t;
  68. BOOST_STATIC_CONSTANT(bool, match = match_t::value);
  69. typedef typename tuple_extract_impl<match>::template apply<Tuple,Predicate> type;
  70. };
  71. template <class Tuple, class Predicate>
  72. struct tuple_extract
  73. : tuple_extract_base_select<
  74. Tuple
  75. , typename mpl::lambda<Predicate>::type
  76. >::type
  77. {
  78. };
  79. //
  80. // Specialized extractors for the docstring, keywords, CallPolicies,
  81. // and default implementation of virtual functions
  82. //
  83. template <class Tuple>
  84. struct doc_extract
  85. : tuple_extract<
  86. Tuple
  87. , mpl::not_<
  88. mpl::or_<
  89. indirect_traits::is_reference_to_class<mpl::_1>
  90. , indirect_traits::is_reference_to_member_function_pointer<mpl::_1 >
  91. >
  92. >
  93. >
  94. {
  95. };
  96. template <class Tuple>
  97. struct keyword_extract
  98. : tuple_extract<Tuple, is_reference_to_keywords<mpl::_1 > >
  99. {
  100. };
  101. template <class Tuple>
  102. struct policy_extract
  103. : tuple_extract<
  104. Tuple
  105. , mpl::and_<
  106. mpl::not_<is_same<not_specified const&,mpl::_1> >
  107. , indirect_traits::is_reference_to_class<mpl::_1 >
  108. , mpl::not_<is_reference_to_keywords<mpl::_1 > >
  109. >
  110. >
  111. {
  112. };
  113. template <class Tuple>
  114. struct default_implementation_extract
  115. : tuple_extract<
  116. Tuple
  117. , indirect_traits::is_reference_to_member_function_pointer<mpl::_1 >
  118. >
  119. {
  120. };
  121. //
  122. // A helper class for decoding the optional arguments to def()
  123. // invocations, which can be supplied in any order and are
  124. // discriminated by their type properties. The template parameters
  125. // are expected to be the types of the actual (optional) arguments
  126. // passed to def().
  127. //
  128. template <class T1, class T2, class T3, class T4>
  129. struct def_helper
  130. {
  131. // A tuple type which begins with references to the supplied
  132. // arguments and ends with actual representatives of the default
  133. // types.
  134. typedef boost::tuples::tuple<
  135. T1 const&
  136. , T2 const&
  137. , T3 const&
  138. , T4 const&
  139. , default_call_policies
  140. , detail::keywords<0>
  141. , char const*
  142. , void(not_specified::*)() // A function pointer type which is never an
  143. // appropriate default implementation
  144. > all_t;
  145. // Constructors; these initialize an member of the tuple type
  146. // shown above.
  147. def_helper(T1 const& a1) : m_all(a1,m_nil,m_nil,m_nil) {}
  148. def_helper(T1 const& a1, T2 const& a2) : m_all(a1,a2,m_nil,m_nil) {}
  149. def_helper(T1 const& a1, T2 const& a2, T3 const& a3) : m_all(a1,a2,a3,m_nil) {}
  150. def_helper(T1 const& a1, T2 const& a2, T3 const& a3, T4 const& a4) : m_all(a1,a2,a3,a4) {}
  151. private: // types
  152. typedef typename default_implementation_extract<all_t>::result_type default_implementation_t;
  153. public: // Constants which can be used for static assertions.
  154. // Users must not supply a default implementation for non-class
  155. // methods.
  156. BOOST_STATIC_CONSTANT(
  157. bool, has_default_implementation = (
  158. !is_same<default_implementation_t, void(not_specified::*)()>::value));
  159. public: // Extractor functions which pull the appropriate value out
  160. // of the tuple
  161. char const* doc() const
  162. {
  163. return doc_extract<all_t>::extract(m_all);
  164. }
  165. typename keyword_extract<all_t>::result_type keywords() const
  166. {
  167. return keyword_extract<all_t>::extract(m_all);
  168. }
  169. typename policy_extract<all_t>::result_type policies() const
  170. {
  171. return policy_extract<all_t>::extract(m_all);
  172. }
  173. default_implementation_t default_implementation() const
  174. {
  175. return default_implementation_extract<all_t>::extract(m_all);
  176. }
  177. private: // data members
  178. all_t m_all;
  179. not_specified m_nil; // for filling in not_specified slots
  180. };
  181. }
  182. }} // namespace boost::python::detail
  183. #endif // DEF_HELPER_DWA200287_HPP