sl@0: // Copyright David Abrahams 2002. sl@0: // Distributed under the Boost Software License, Version 1.0. (See sl@0: // accompanying file LICENSE_1_0.txt or copy at sl@0: // http://www.boost.org/LICENSE_1_0.txt) sl@0: #ifndef CLASS_DWA200216_HPP sl@0: # define CLASS_DWA200216_HPP sl@0: sl@0: # include sl@0: sl@0: # include sl@0: sl@0: # include sl@0: # include sl@0: sl@0: # include sl@0: # include sl@0: # include sl@0: # include sl@0: # include sl@0: # include sl@0: # include sl@0: sl@0: # include sl@0: # include sl@0: # include sl@0: sl@0: # include sl@0: # include sl@0: # include sl@0: # include sl@0: # include sl@0: # include sl@0: sl@0: # include sl@0: # include sl@0: # include sl@0: sl@0: # include sl@0: # include sl@0: # include sl@0: # include sl@0: sl@0: # include sl@0: sl@0: # if BOOST_WORKAROUND(__MWERKS__, <= 0x3004) \ sl@0: /* pro9 reintroduced the bug */ \ sl@0: || (BOOST_WORKAROUND(__MWERKS__, > 0x3100) \ sl@0: && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3201))) \ sl@0: || BOOST_WORKAROUND(__GNUC__, < 3) sl@0: sl@0: # define BOOST_PYTHON_NO_MEMBER_POINTER_ORDERING 1 sl@0: sl@0: # endif sl@0: sl@0: # ifdef BOOST_PYTHON_NO_MEMBER_POINTER_ORDERING sl@0: # include sl@0: # include sl@0: # endif sl@0: sl@0: namespace boost { namespace python { sl@0: sl@0: template class def_visitor; sl@0: sl@0: enum no_init_t { no_init }; sl@0: sl@0: namespace detail sl@0: { sl@0: // This function object is used with mpl::for_each to write the id sl@0: // of the type a pointer to which is passed as its 2nd compile-time sl@0: // argument. into the iterator pointed to by its runtime argument sl@0: struct write_type_id sl@0: { sl@0: write_type_id(type_info**p) : p(p) {} sl@0: sl@0: // Here's the runtime behavior sl@0: template sl@0: void operator()(T*) const sl@0: { sl@0: *(*p)++ = type_id(); sl@0: } sl@0: sl@0: type_info** p; sl@0: }; sl@0: sl@0: template sl@0: struct is_data_member_pointer sl@0: : mpl::and_< sl@0: is_member_pointer sl@0: , mpl::not_ > sl@0: > sl@0: {}; sl@0: sl@0: # ifdef BOOST_PYTHON_NO_MEMBER_POINTER_ORDERING sl@0: # define BOOST_PYTHON_DATA_MEMBER_HELPER(D) , detail::is_data_member_pointer() sl@0: # define BOOST_PYTHON_YES_DATA_MEMBER , mpl::true_ sl@0: # define BOOST_PYTHON_NO_DATA_MEMBER , mpl::false_ sl@0: # elif defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) sl@0: # define BOOST_PYTHON_DATA_MEMBER_HELPER(D) , 0 sl@0: # define BOOST_PYTHON_YES_DATA_MEMBER , int sl@0: # define BOOST_PYTHON_NO_DATA_MEMBER , ... sl@0: # else sl@0: # define BOOST_PYTHON_DATA_MEMBER_HELPER(D) sl@0: # define BOOST_PYTHON_YES_DATA_MEMBER sl@0: # define BOOST_PYTHON_NO_DATA_MEMBER sl@0: # endif sl@0: sl@0: namespace error sl@0: { sl@0: // sl@0: // A meta-assertion mechanism which prints nice error messages and sl@0: // backtraces on lots of compilers. Usage: sl@0: // sl@0: // assertion::failed sl@0: // sl@0: // where C is an MPL metafunction class sl@0: // sl@0: sl@0: template struct assertion_failed { }; sl@0: template struct assertion_ok { typedef C failed; }; sl@0: sl@0: template sl@0: struct assertion sl@0: : mpl::if_, assertion_failed >::type sl@0: {}; sl@0: sl@0: // sl@0: // Checks for validity of arguments used to define virtual sl@0: // functions with default implementations. sl@0: // sl@0: sl@0: template sl@0: void not_a_derived_class_member(Default) {} sl@0: sl@0: template sl@0: struct virtual_function_default sl@0: { sl@0: template sl@0: static void sl@0: must_be_derived_class_member(Default const&) sl@0: { sl@0: typedef typename assertion > >::failed test0; sl@0: # if !BOOST_WORKAROUND(__MWERKS__, <= 0x2407) sl@0: typedef typename assertion >::failed test1; sl@0: # endif sl@0: typedef typename assertion >::failed test2; sl@0: not_a_derived_class_member(Fn()); sl@0: } sl@0: }; sl@0: } sl@0: } sl@0: sl@0: // This is the primary mechanism through which users will expose sl@0: // C++ classes to Python. sl@0: template < sl@0: class W // class being wrapped sl@0: , class X1 // = detail::not_specified sl@0: , class X2 // = detail::not_specified sl@0: , class X3 // = detail::not_specified sl@0: > sl@0: class class_ : public objects::class_base sl@0: { sl@0: public: // types sl@0: typedef objects::class_base base; sl@0: typedef class_ self; sl@0: typedef typename objects::class_metadata metadata; sl@0: typedef W wrapped_type; sl@0: sl@0: private: // types sl@0: sl@0: // A helper class which will contain an array of id objects to be sl@0: // passed to the base class constructor sl@0: struct id_vector sl@0: { sl@0: typedef typename metadata::bases bases; sl@0: sl@0: id_vector() sl@0: { sl@0: // Stick the derived class id into the first element of the array sl@0: ids[0] = detail::unwrap_type_id((W*)0, (W*)0); sl@0: sl@0: // Write the rest of the elements into succeeding positions. sl@0: type_info* p = ids + 1; sl@0: mpl::for_each(detail::write_type_id(&p), (bases*)0, (add_pointer*)0); sl@0: } sl@0: sl@0: BOOST_STATIC_CONSTANT( sl@0: std::size_t, size = mpl::size::value + 1); sl@0: type_info ids[size]; sl@0: }; sl@0: friend struct id_vector; sl@0: sl@0: public: // constructors sl@0: sl@0: // Construct with the class name, with or without docstring, and default __init__() function sl@0: class_(char const* name, char const* doc = 0); sl@0: sl@0: // Construct with class name, no docstring, and an uncallable __init__ function sl@0: class_(char const* name, no_init_t); sl@0: sl@0: // Construct with class name, docstring, and an uncallable __init__ function sl@0: class_(char const* name, char const* doc, no_init_t); sl@0: sl@0: // Construct with class name and init<> function sl@0: template sl@0: inline class_(char const* name, init_base const& i) sl@0: : base(name, id_vector::size, id_vector().ids) sl@0: { sl@0: this->initialize(i); sl@0: } sl@0: sl@0: // Construct with class name, docstring and init<> function sl@0: template sl@0: inline class_(char const* name, char const* doc, init_base const& i) sl@0: : base(name, id_vector::size, id_vector().ids, doc) sl@0: { sl@0: this->initialize(i); sl@0: } sl@0: sl@0: public: // member functions sl@0: sl@0: // Generic visitation sl@0: template sl@0: self& def(def_visitor const& visitor) sl@0: { sl@0: visitor.visit(*this); sl@0: return *this; sl@0: } sl@0: sl@0: // Wrap a member function or a non-member function which can take sl@0: // a T, T cv&, or T cv* as its first parameter, a callable sl@0: // python object, or a generic visitor. sl@0: template sl@0: self& def(char const* name, F f) sl@0: { sl@0: this->def_impl( sl@0: detail::unwrap_wrapper((W*)0) sl@0: , name, f, detail::def_helper(0), &f); sl@0: return *this; sl@0: } sl@0: sl@0: template sl@0: self& def(char const* name, A1 a1, A2 const& a2) sl@0: { sl@0: this->def_maybe_overloads(name, a1, a2, &a2); sl@0: return *this; sl@0: } sl@0: sl@0: template sl@0: self& def(char const* name, Fn fn, A1 const& a1, A2 const& a2) sl@0: { sl@0: // The arguments are definitely: sl@0: // def(name, function, policy, doc_string) sl@0: // def(name, function, doc_string, policy) sl@0: sl@0: this->def_impl( sl@0: detail::unwrap_wrapper((W*)0) sl@0: , name, fn sl@0: , detail::def_helper(a1,a2) sl@0: , &fn); sl@0: sl@0: return *this; sl@0: } sl@0: sl@0: template sl@0: self& def(char const* name, Fn fn, A1 const& a1, A2 const& a2, A3 const& a3) sl@0: { sl@0: this->def_impl( sl@0: detail::unwrap_wrapper((W*)0) sl@0: , name, fn sl@0: , detail::def_helper(a1,a2,a3) sl@0: , &fn); sl@0: sl@0: return *this; sl@0: } sl@0: sl@0: // sl@0: // Data member access sl@0: // sl@0: template sl@0: self& def_readonly(char const* name, D const& d, char const* doc=0) sl@0: { sl@0: return this->def_readonly_impl(name, d, doc BOOST_PYTHON_DATA_MEMBER_HELPER(D)); sl@0: } sl@0: sl@0: template sl@0: self& def_readwrite(char const* name, D const& d, char const* doc=0) sl@0: { sl@0: return this->def_readwrite_impl(name, d, doc BOOST_PYTHON_DATA_MEMBER_HELPER(D)); sl@0: } sl@0: sl@0: template sl@0: self& def_readonly(char const* name, D& d, char const* doc=0) sl@0: { sl@0: return this->def_readonly_impl(name, d, doc BOOST_PYTHON_DATA_MEMBER_HELPER(D)); sl@0: } sl@0: sl@0: template sl@0: self& def_readwrite(char const* name, D& d, char const* doc=0) sl@0: { sl@0: return this->def_readwrite_impl(name, d, doc BOOST_PYTHON_DATA_MEMBER_HELPER(D)); sl@0: } sl@0: sl@0: // Property creation sl@0: # if !BOOST_WORKAROUND(BOOST_MSVC, <= 1300) sl@0: template sl@0: self& add_property(char const* name, Get fget, char const* docstr = 0) sl@0: { sl@0: base::add_property(name, this->make_getter(fget), docstr); sl@0: return *this; sl@0: } sl@0: sl@0: template sl@0: self& add_property(char const* name, Get fget, Set fset, char const* docstr = 0) sl@0: { sl@0: base::add_property( sl@0: name, this->make_getter(fget), this->make_setter(fset), docstr); sl@0: return *this; sl@0: } sl@0: # else sl@0: private: sl@0: template sl@0: self& add_property_impl(char const* name, Get fget, char const* docstr, int) sl@0: { sl@0: base::add_property(name, this->make_getter(fget), docstr); sl@0: return *this; sl@0: } sl@0: sl@0: template sl@0: self& add_property_impl(char const* name, Get fget, Set fset, ...) sl@0: { sl@0: base::add_property( sl@0: name, this->make_getter(fget), this->make_setter(fset), 0); sl@0: return *this; sl@0: } sl@0: sl@0: public: sl@0: template sl@0: self& add_property(char const* name, Get fget) sl@0: { sl@0: base::add_property(name, this->make_getter(fget), 0); sl@0: return *this; sl@0: } sl@0: sl@0: template sl@0: self& add_property(char const* name, Get fget, DocStrOrSet docstr_or_set) sl@0: { sl@0: this->add_property_impl(name, this->make_getter(fget), docstr_or_set, 0); sl@0: return *this; sl@0: } sl@0: sl@0: template sl@0: self& sl@0: add_property(char const* name, Get fget, Set fset, char const* docstr) sl@0: { sl@0: base::add_property( sl@0: name, this->make_getter(fget), this->make_setter(fset), docstr); sl@0: return *this; sl@0: } sl@0: # endif sl@0: sl@0: template sl@0: self& add_static_property(char const* name, Get fget) sl@0: { sl@0: base::add_static_property(name, object(fget)); sl@0: return *this; sl@0: } sl@0: sl@0: template sl@0: self& add_static_property(char const* name, Get fget, Set fset) sl@0: { sl@0: base::add_static_property(name, object(fget), object(fset)); sl@0: return *this; sl@0: } sl@0: sl@0: template sl@0: self& setattr(char const* name, U const& x) sl@0: { sl@0: this->base::setattr(name, object(x)); sl@0: return *this; sl@0: } sl@0: sl@0: // Pickle support sl@0: template sl@0: self& def_pickle(PickleSuiteType const& x) sl@0: { sl@0: error_messages::must_be_derived_from_pickle_suite(x); sl@0: detail::pickle_suite_finalize::register_( sl@0: *this, sl@0: &PickleSuiteType::getinitargs, sl@0: &PickleSuiteType::getstate, sl@0: &PickleSuiteType::setstate, sl@0: PickleSuiteType::getstate_manages_dict()); sl@0: return *this; sl@0: } sl@0: sl@0: self& enable_pickling() sl@0: { sl@0: this->base::enable_pickling_(false); sl@0: return *this; sl@0: } sl@0: sl@0: self& staticmethod(char const* name) sl@0: { sl@0: this->make_method_static(name); sl@0: return *this; sl@0: } sl@0: private: // helper functions sl@0: sl@0: // Builds a method for this class around the given [member] sl@0: // function pointer or object, appropriately adjusting the type of sl@0: // the first signature argument so that if f is a member of a sl@0: // (possibly not wrapped) base class of T, an lvalue argument of sl@0: // type T will be required. sl@0: // sl@0: // @group PropertyHelpers { sl@0: template sl@0: object make_getter(F f) sl@0: { sl@0: typedef typename api::is_object_operators::type is_obj_or_proxy; sl@0: sl@0: return this->make_fn_impl( sl@0: detail::unwrap_wrapper((W*)0) sl@0: , f, is_obj_or_proxy(), (char*)0, detail::is_data_member_pointer() sl@0: ); sl@0: } sl@0: sl@0: template sl@0: object make_setter(F f) sl@0: { sl@0: typedef typename api::is_object_operators::type is_obj_or_proxy; sl@0: sl@0: return this->make_fn_impl( sl@0: detail::unwrap_wrapper((W*)0) sl@0: , f, is_obj_or_proxy(), (int*)0, detail::is_data_member_pointer() sl@0: ); sl@0: } sl@0: sl@0: template sl@0: object make_fn_impl(T*, F const& f, mpl::false_, void*, mpl::false_) sl@0: { sl@0: return python::make_function(f, default_call_policies(), detail::get_signature(f, (T*)0)); sl@0: } sl@0: sl@0: template sl@0: object make_fn_impl(T*, D B::*pm_, mpl::false_, char*, mpl::true_) sl@0: { sl@0: D T::*pm = pm_; sl@0: return python::make_getter(pm); sl@0: } sl@0: sl@0: template sl@0: object make_fn_impl(T*, D B::*pm_, mpl::false_, int*, mpl::true_) sl@0: { sl@0: D T::*pm = pm_; sl@0: return python::make_setter(pm); sl@0: } sl@0: sl@0: template sl@0: object make_fn_impl(T*, F const& x, mpl::true_, void*, mpl::false_) sl@0: { sl@0: return x; sl@0: } sl@0: // } sl@0: sl@0: template sl@0: self& def_readonly_impl( sl@0: char const* name, D B::*pm_, char const* doc BOOST_PYTHON_YES_DATA_MEMBER) sl@0: { sl@0: return this->add_property(name, pm_, doc); sl@0: } sl@0: sl@0: template sl@0: self& def_readwrite_impl( sl@0: char const* name, D B::*pm_, char const* doc BOOST_PYTHON_YES_DATA_MEMBER) sl@0: { sl@0: return this->add_property(name, pm_, pm_, doc); sl@0: } sl@0: sl@0: template sl@0: self& def_readonly_impl( sl@0: char const* name, D& d, char const* BOOST_PYTHON_NO_DATA_MEMBER) sl@0: { sl@0: return this->add_static_property(name, python::make_getter(d)); sl@0: } sl@0: sl@0: template sl@0: self& def_readwrite_impl( sl@0: char const* name, D& d, char const* BOOST_PYTHON_NO_DATA_MEMBER) sl@0: { sl@0: return this->add_static_property(name, python::make_getter(d), python::make_setter(d)); sl@0: } sl@0: sl@0: template sl@0: inline void initialize(DefVisitor const& i) sl@0: { sl@0: metadata::register_(); // set up runtime metadata/conversions sl@0: sl@0: typedef typename metadata::holder holder; sl@0: this->set_instance_size( objects::additional_instance_size::value ); sl@0: sl@0: this->def(i); sl@0: } sl@0: sl@0: inline void initialize(no_init_t) sl@0: { sl@0: metadata::register_(); // set up runtime metadata/conversions sl@0: this->def_no_init(); sl@0: } sl@0: sl@0: // sl@0: // These two overloads discriminate between def() as applied to a sl@0: // generic visitor and everything else. sl@0: // sl@0: // @group def_impl { sl@0: template sl@0: inline void def_impl( sl@0: T* sl@0: , char const* name sl@0: , LeafVisitor sl@0: , Helper const& helper sl@0: , def_visitor const* v sl@0: ) sl@0: { sl@0: v->visit(*this, name, helper); sl@0: } sl@0: sl@0: template sl@0: inline void def_impl( sl@0: T* sl@0: , char const* name sl@0: , Fn fn sl@0: , Helper const& helper sl@0: , ... sl@0: ) sl@0: { sl@0: objects::add_to_namespace( sl@0: *this sl@0: , name sl@0: , make_function( sl@0: fn sl@0: , helper.policies() sl@0: , helper.keywords() sl@0: , detail::get_signature(fn, (T*)0) sl@0: ) sl@0: , helper.doc() sl@0: ); sl@0: sl@0: this->def_default(name, fn, helper, mpl::bool_()); sl@0: } sl@0: // } sl@0: sl@0: // sl@0: // These two overloads handle the definition of default sl@0: // implementation overloads for virtual functions. The second one sl@0: // handles the case where no default implementation was specified. sl@0: // sl@0: // @group def_default { sl@0: template sl@0: inline void def_default( sl@0: char const* name sl@0: , Fn sl@0: , Helper const& helper sl@0: , mpl::bool_) sl@0: { sl@0: detail::error::virtual_function_default::must_be_derived_class_member( sl@0: helper.default_implementation()); sl@0: sl@0: objects::add_to_namespace( sl@0: *this, name, sl@0: make_function( sl@0: helper.default_implementation(), helper.policies(), helper.keywords()) sl@0: ); sl@0: } sl@0: sl@0: template sl@0: inline void def_default(char const*, Fn, Helper const&, mpl::bool_) sl@0: { } sl@0: // } sl@0: sl@0: // sl@0: // These two overloads discriminate between def() as applied to sl@0: // regular functions and def() as applied to the result of sl@0: // BOOST_PYTHON_FUNCTION_OVERLOADS(). The final argument is used to sl@0: // discriminate. sl@0: // sl@0: // @group def_maybe_overloads { sl@0: template sl@0: void def_maybe_overloads( sl@0: char const* name sl@0: , SigT sig sl@0: , OverloadsT const& overloads sl@0: , detail::overloads_base const*) sl@0: sl@0: { sl@0: // convert sig to a type_list (see detail::get_signature in signature.hpp) sl@0: // before calling detail::define_with_defaults. sl@0: detail::define_with_defaults( sl@0: name, overloads, *this, detail::get_signature(sig)); sl@0: } sl@0: sl@0: template sl@0: void def_maybe_overloads( sl@0: char const* name sl@0: , Fn fn sl@0: , A1 const& a1 sl@0: , ...) sl@0: { sl@0: this->def_impl( sl@0: detail::unwrap_wrapper((W*)0) sl@0: , name sl@0: , fn sl@0: , detail::def_helper(a1) sl@0: , &fn sl@0: ); sl@0: sl@0: } sl@0: // } sl@0: }; sl@0: sl@0: sl@0: // sl@0: // implementations sl@0: // sl@0: sl@0: template sl@0: inline class_::class_(char const* name, char const* doc) sl@0: : base(name, id_vector::size, id_vector().ids, doc) sl@0: { sl@0: this->initialize(init<>()); sl@0: // select_holder::assert_default_constructible(); sl@0: } sl@0: sl@0: template sl@0: inline class_::class_(char const* name, no_init_t) sl@0: : base(name, id_vector::size, id_vector().ids) sl@0: { sl@0: this->initialize(no_init); sl@0: } sl@0: sl@0: template sl@0: inline class_::class_(char const* name, char const* doc, no_init_t) sl@0: : base(name, id_vector::size, id_vector().ids, doc) sl@0: { sl@0: this->initialize(no_init); sl@0: } sl@0: sl@0: }} // namespace boost::python sl@0: sl@0: # undef BOOST_PYTHON_DATA_MEMBER_HELPER sl@0: # undef BOOST_PYTHON_YES_DATA_MEMBER sl@0: # undef BOOST_PYTHON_NO_DATA_MEMBER sl@0: # undef BOOST_PYTHON_NO_MEMBER_POINTER_ORDERING sl@0: sl@0: #endif // CLASS_DWA200216_HPP