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