Chris@16: // Copyright David Abrahams 2001. 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 MAKE_CONSTRUCTOR_DWA20011221_HPP Chris@16: # define MAKE_CONSTRUCTOR_DWA20011221_HPP Chris@16: Chris@16: # include Chris@16: Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: Chris@16: # include Chris@16: # include Chris@16: 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: namespace detail Chris@16: { Chris@16: template Chris@16: struct install_holder : converter::context_result_converter Chris@16: { Chris@16: install_holder(PyObject* args_) Chris@16: : m_self(PyTuple_GetItem(args_, 0)) {} Chris@16: Chris@16: PyObject* operator()(T x) const Chris@16: { Chris@16: dispatch(x, is_pointer()); Chris@16: return none(); Chris@16: } Chris@16: Chris@16: private: Chris@16: template Chris@16: void dispatch(U* x, mpl::true_) const Chris@16: { Chris@16: std::auto_ptr owner(x); Chris@16: dispatch(owner, mpl::false_()); Chris@16: } Chris@16: Chris@16: template Chris@16: void dispatch(Ptr x, mpl::false_) const Chris@16: { Chris@16: typedef typename pointee::type value_type; Chris@16: typedef objects::pointer_holder holder; Chris@16: typedef objects::instance instance_t; Chris@16: Chris@16: void* memory = holder::allocate(this->m_self, offsetof(instance_t, storage), sizeof(holder)); Chris@16: try { Chris@16: (new (memory) holder(x))->install(this->m_self); Chris@16: } Chris@16: catch(...) { Chris@16: holder::deallocate(this->m_self, memory); Chris@16: throw; Chris@16: } Chris@16: } Chris@16: Chris@16: PyObject* m_self; Chris@16: }; Chris@16: Chris@16: struct constructor_result_converter Chris@16: { Chris@16: template Chris@16: struct apply Chris@16: { Chris@16: typedef install_holder type; Chris@16: }; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct offset_args Chris@16: { Chris@16: offset_args(BaseArgs base_) : base(base_) {} Chris@16: BaseArgs base; Chris@16: }; Chris@16: Chris@16: template Chris@16: inline PyObject* get(mpl::int_, offset_args const& args_) Chris@16: { Chris@16: return get(mpl::int_<(N+Offset::value)>(), args_.base); Chris@16: } Chris@16: Chris@16: template Chris@16: inline unsigned arity(offset_args const& args_) Chris@16: { Chris@16: return arity(args_.base) - Offset::value; Chris@16: } Chris@16: Chris@16: template Chris@16: struct constructor_policy : BasePolicy_ Chris@16: { Chris@16: constructor_policy(BasePolicy_ base) : BasePolicy_(base) {} Chris@16: Chris@16: // If the BasePolicy_ supplied a result converter it would be Chris@16: // ignored; issue an error if it's not the default. Chris@16: #if defined _MSC_VER && _MSC_VER < 1300 Chris@16: typedef is_same< Chris@16: typename BasePolicy_::result_converter Chris@16: , default_result_converter Chris@16: > same_result_converter; Chris@16: //see above for explanation Chris@16: BOOST_STATIC_ASSERT(same_result_converter::value) ; Chris@16: #else Chris@16: BOOST_MPL_ASSERT_MSG( Chris@16: (is_same< Chris@16: typename BasePolicy_::result_converter Chris@16: , default_result_converter Chris@16: >::value) Chris@16: , MAKE_CONSTRUCTOR_SUPPLIES_ITS_OWN_RESULT_CONVERTER_THAT_WOULD_OVERRIDE_YOURS Chris@16: , (typename BasePolicy_::result_converter) Chris@16: ); Chris@16: #endif Chris@16: typedef constructor_result_converter result_converter; Chris@16: typedef offset_args > argument_package; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct outer_constructor_signature Chris@16: { Chris@16: typedef typename mpl::pop_front::type inner_args; Chris@16: typedef typename mpl::push_front::type outer_args; Chris@16: typedef typename mpl::push_front::type type; Chris@16: }; Chris@16: Chris@16: // ETI workaround Chris@16: template <> Chris@16: struct outer_constructor_signature Chris@16: { Chris@16: typedef int type; Chris@16: }; Chris@16: Chris@16: // Chris@16: // These helper functions for make_constructor (below) do the raw work Chris@16: // of constructing a Python object from some invokable entity. See Chris@16: // for more information about how Chris@16: // the Sig arguments is used. Chris@16: // Chris@16: // @group make_constructor_aux { Chris@16: template Chris@16: object make_constructor_aux( Chris@16: F f // An object that can be invoked by detail::invoke() Chris@16: , CallPolicies const& p // CallPolicies to use in the invocation Chris@16: , Sig const& // An MPL sequence of argument types expected by F Chris@16: ) Chris@16: { Chris@16: typedef typename outer_constructor_signature::type outer_signature; Chris@16: Chris@16: typedef constructor_policy inner_policy; Chris@16: Chris@16: return objects::function_object( Chris@16: objects::py_function( Chris@16: detail::caller(f, inner_policy(p)) Chris@16: , outer_signature() Chris@16: ) Chris@16: ); Chris@16: } Chris@16: Chris@16: // As above, except that it accepts argument keywords. NumKeywords Chris@16: // is used only for a compile-time assertion to make sure the user Chris@16: // doesn't pass more keywords than the function can accept. To Chris@16: // disable all checking, pass mpl::int_<0> for NumKeywords. Chris@16: template Chris@16: object make_constructor_aux( Chris@16: F f Chris@16: , CallPolicies const& p Chris@16: , Sig const& Chris@16: , detail::keyword_range const& kw // a [begin,end) pair of iterators over keyword names Chris@16: , NumKeywords // An MPL integral type wrapper: the size of kw Chris@16: ) Chris@16: { Chris@16: enum { arity = mpl::size::value - 1 }; Chris@16: Chris@16: typedef typename detail::error::more_keywords_than_function_arguments< Chris@16: NumKeywords::value, arity Chris@16: >::too_many_keywords assertion; Chris@16: Chris@16: typedef typename outer_constructor_signature::type outer_signature; Chris@16: Chris@16: typedef constructor_policy inner_policy; Chris@16: Chris@16: return objects::function_object( Chris@16: objects::py_function( Chris@16: detail::caller(f, inner_policy(p)) Chris@16: , outer_signature() Chris@16: ) Chris@16: , kw Chris@16: ); Chris@16: } Chris@16: // } Chris@16: Chris@16: // Chris@16: // These dispatch functions are used to discriminate between the Chris@16: // cases when the 3rd argument is keywords or when it is a Chris@16: // signature. Chris@16: // Chris@16: // @group Helpers for make_constructor when called with 3 arguments. { Chris@16: // Chris@16: template Chris@16: object make_constructor_dispatch(F f, CallPolicies const& policies, Keywords const& kw, mpl::true_) Chris@16: { Chris@16: return detail::make_constructor_aux( Chris@16: f Chris@16: , policies Chris@16: , detail::get_signature(f) Chris@16: , kw.range() Chris@16: , mpl::int_() Chris@16: ); Chris@16: } Chris@16: Chris@16: template Chris@16: object make_constructor_dispatch(F f, CallPolicies const& policies, Signature const& sig, mpl::false_) Chris@16: { Chris@16: return detail::make_constructor_aux( Chris@16: f Chris@16: , policies Chris@16: , sig Chris@16: ); Chris@16: } Chris@16: // } Chris@16: } Chris@16: Chris@16: // These overloaded functions wrap a function or member function Chris@16: // pointer as a Python object, using optional CallPolicies, Chris@16: // Keywords, and/or Signature. @group { Chris@16: // Chris@16: template Chris@16: object make_constructor(F f) Chris@16: { Chris@16: return detail::make_constructor_aux( Chris@16: f,default_call_policies(), detail::get_signature(f)); Chris@16: } Chris@16: Chris@16: template Chris@16: object make_constructor(F f, CallPolicies const& policies) Chris@16: { Chris@16: return detail::make_constructor_aux( Chris@16: f, policies, detail::get_signature(f)); Chris@16: } Chris@16: Chris@16: template Chris@16: object make_constructor( Chris@16: F f Chris@16: , CallPolicies const& policies Chris@16: , KeywordsOrSignature const& keywords_or_signature) Chris@16: { Chris@16: typedef typename Chris@16: detail::is_reference_to_keywords::type Chris@16: is_kw; Chris@16: Chris@16: return detail::make_constructor_dispatch( Chris@16: f Chris@16: , policies Chris@16: , keywords_or_signature Chris@16: , is_kw() Chris@16: ); Chris@16: } Chris@16: Chris@16: template Chris@16: object make_constructor( Chris@16: F f Chris@16: , CallPolicies const& policies Chris@16: , Keywords const& kw Chris@16: , Signature const& sig Chris@16: ) Chris@16: { Chris@16: return detail::make_constructor_aux( Chris@16: f Chris@16: , policies Chris@16: , sig Chris@16: , kw.range() Chris@16: , mpl::int_() Chris@16: ); Chris@16: } Chris@16: // } Chris@16: Chris@16: }} Chris@16: Chris@16: Chris@16: #endif // MAKE_CONSTRUCTOR_DWA20011221_HPP