Chris@16: // Copyright David Abrahams 2002. Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: #ifndef DEF_HELPER_DWA200287_HPP Chris@16: # define DEF_HELPER_DWA200287_HPP Chris@16: Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: Chris@16: namespace boost { namespace python { Chris@16: Chris@16: struct default_call_policies; Chris@16: Chris@16: namespace detail Chris@16: { Chris@16: // tuple_extract::extract(t) returns the first Chris@16: // element of a Tuple whose type E satisfies the given Predicate Chris@16: // applied to add_reference. The Predicate must be an MPL Chris@16: // metafunction class. Chris@16: template Chris@16: struct tuple_extract; Chris@16: Chris@16: // Implementation class for when the tuple's head type does not Chris@16: // satisfy the Predicate Chris@16: template Chris@16: struct tuple_extract_impl Chris@16: { Chris@16: template Chris@16: struct apply Chris@16: { Chris@16: typedef typename Tuple::head_type result_type; Chris@16: Chris@16: static typename Tuple::head_type extract(Tuple const& x) Chris@16: { Chris@16: return x.get_head(); Chris@16: } Chris@16: }; Chris@16: }; Chris@16: Chris@16: // Implementation specialization for when the tuple's head type Chris@16: // satisfies the predicate Chris@16: template <> Chris@16: struct tuple_extract_impl Chris@16: { Chris@16: template Chris@16: struct apply Chris@16: { Chris@16: // recursive application of tuple_extract on the tail of the tuple Chris@16: typedef tuple_extract next; Chris@16: typedef typename next::result_type result_type; Chris@16: Chris@16: static result_type extract(Tuple const& x) Chris@16: { Chris@16: return next::extract(x.get_tail()); Chris@16: } Chris@16: }; Chris@16: }; Chris@16: Chris@16: // A metafunction which selects a version of tuple_extract_impl to Chris@16: // use for the implementation of tuple_extract Chris@16: template Chris@16: struct tuple_extract_base_select Chris@16: { Chris@16: typedef typename Tuple::head_type head_type; Chris@16: typedef typename mpl::apply1::type>::type match_t; Chris@16: BOOST_STATIC_CONSTANT(bool, match = match_t::value); Chris@16: typedef typename tuple_extract_impl::template apply type; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct tuple_extract Chris@16: : tuple_extract_base_select< Chris@16: Tuple Chris@16: , typename mpl::lambda::type Chris@16: >::type Chris@16: { Chris@16: }; Chris@16: Chris@16: Chris@16: // Chris@16: // Specialized extractors for the docstring, keywords, CallPolicies, Chris@16: // and default implementation of virtual functions Chris@16: // Chris@16: Chris@16: template Chris@16: struct doc_extract Chris@16: : tuple_extract< Chris@16: Tuple Chris@16: , mpl::not_< Chris@16: mpl::or_< Chris@16: indirect_traits::is_reference_to_class Chris@16: , indirect_traits::is_reference_to_member_function_pointer Chris@16: > Chris@16: > Chris@16: > Chris@16: { Chris@16: }; Chris@16: Chris@16: template Chris@16: struct keyword_extract Chris@16: : tuple_extract > Chris@16: { Chris@16: }; Chris@16: Chris@16: template Chris@16: struct policy_extract Chris@16: : tuple_extract< Chris@16: Tuple Chris@16: , mpl::and_< Chris@16: mpl::not_ > Chris@16: , indirect_traits::is_reference_to_class Chris@16: , mpl::not_ > Chris@16: > Chris@16: > Chris@16: { Chris@16: }; Chris@16: Chris@16: template Chris@16: struct default_implementation_extract Chris@16: : tuple_extract< Chris@16: Tuple Chris@16: , indirect_traits::is_reference_to_member_function_pointer Chris@16: > Chris@16: { Chris@16: }; Chris@16: Chris@16: // Chris@16: // A helper class for decoding the optional arguments to def() Chris@16: // invocations, which can be supplied in any order and are Chris@16: // discriminated by their type properties. The template parameters Chris@16: // are expected to be the types of the actual (optional) arguments Chris@16: // passed to def(). Chris@16: // Chris@16: template Chris@16: struct def_helper Chris@16: { Chris@16: // A tuple type which begins with references to the supplied Chris@16: // arguments and ends with actual representatives of the default Chris@16: // types. Chris@16: typedef boost::tuples::tuple< Chris@16: T1 const& Chris@16: , T2 const& Chris@16: , T3 const& Chris@16: , T4 const& Chris@16: , default_call_policies Chris@16: , detail::keywords<0> Chris@16: , char const* Chris@16: , void(not_specified::*)() // A function pointer type which is never an Chris@16: // appropriate default implementation Chris@16: > all_t; Chris@16: Chris@16: // Constructors; these initialize an member of the tuple type Chris@16: // shown above. Chris@16: def_helper(T1 const& a1) : m_all(a1,m_nil,m_nil,m_nil) {} Chris@16: def_helper(T1 const& a1, T2 const& a2) : m_all(a1,a2,m_nil,m_nil) {} Chris@16: def_helper(T1 const& a1, T2 const& a2, T3 const& a3) : m_all(a1,a2,a3,m_nil) {} Chris@16: def_helper(T1 const& a1, T2 const& a2, T3 const& a3, T4 const& a4) : m_all(a1,a2,a3,a4) {} Chris@16: Chris@16: private: // types Chris@16: typedef typename default_implementation_extract::result_type default_implementation_t; Chris@16: Chris@16: public: // Constants which can be used for static assertions. Chris@16: Chris@16: // Users must not supply a default implementation for non-class Chris@16: // methods. Chris@16: BOOST_STATIC_CONSTANT( Chris@16: bool, has_default_implementation = ( Chris@16: !is_same::value)); Chris@16: Chris@16: public: // Extractor functions which pull the appropriate value out Chris@16: // of the tuple Chris@16: char const* doc() const Chris@16: { Chris@16: return doc_extract::extract(m_all); Chris@16: } Chris@16: Chris@16: typename keyword_extract::result_type keywords() const Chris@16: { Chris@16: return keyword_extract::extract(m_all); Chris@16: } Chris@16: Chris@16: typename policy_extract::result_type policies() const Chris@16: { Chris@16: return policy_extract::extract(m_all); Chris@16: } Chris@16: Chris@16: default_implementation_t default_implementation() const Chris@16: { Chris@16: return default_implementation_extract::extract(m_all); Chris@16: } Chris@16: Chris@16: private: // data members Chris@16: all_t m_all; Chris@16: not_specified m_nil; // for filling in not_specified slots Chris@16: }; Chris@16: } Chris@16: Chris@16: }} // namespace boost::python::detail Chris@16: Chris@16: #endif // DEF_HELPER_DWA200287_HPP