def_helper.hpp 6.4 KB

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