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 CLASS_DWA200216_HPP
6 # define CLASS_DWA200216_HPP
8 # include <boost/python/detail/prefix.hpp>
10 # include <boost/noncopyable.hpp>
12 # include <boost/python/class_fwd.hpp>
13 # include <boost/python/object/class.hpp>
15 # include <boost/python/object.hpp>
16 # include <boost/python/type_id.hpp>
17 # include <boost/python/data_members.hpp>
18 # include <boost/python/make_function.hpp>
19 # include <boost/python/signature.hpp>
20 # include <boost/python/init.hpp>
21 # include <boost/python/args_fwd.hpp>
23 # include <boost/python/object/class_metadata.hpp>
24 # include <boost/python/object/pickle_support.hpp>
25 # include <boost/python/object/add_to_namespace.hpp>
27 # include <boost/python/detail/overloads_fwd.hpp>
28 # include <boost/python/detail/operator_id.hpp>
29 # include <boost/python/detail/def_helper.hpp>
30 # include <boost/python/detail/force_instantiate.hpp>
31 # include <boost/python/detail/unwrap_type_id.hpp>
32 # include <boost/python/detail/unwrap_wrapper.hpp>
34 # include <boost/type_traits/is_same.hpp>
35 # include <boost/type_traits/is_member_function_pointer.hpp>
36 # include <boost/type_traits/is_polymorphic.hpp>
38 # include <boost/mpl/size.hpp>
39 # include <boost/mpl/for_each.hpp>
40 # include <boost/mpl/bool.hpp>
41 # include <boost/mpl/not.hpp>
43 # include <boost/detail/workaround.hpp>
45 # if BOOST_WORKAROUND(__MWERKS__, <= 0x3004) \
46 /* pro9 reintroduced the bug */ \
47 || (BOOST_WORKAROUND(__MWERKS__, > 0x3100) \
48 && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3201)))
50 # define BOOST_PYTHON_NO_MEMBER_POINTER_ORDERING 1
54 # ifdef BOOST_PYTHON_NO_MEMBER_POINTER_ORDERING
55 # include <boost/mpl/and.hpp>
56 # include <boost/type_traits/is_member_pointer.hpp>
59 namespace boost { namespace python {
61 template <class DerivedVisitor> class def_visitor;
63 enum no_init_t { no_init };
67 // This function object is used with mpl::for_each to write the id
68 // of the type a pointer to which is passed as its 2nd compile-time
69 // argument. into the iterator pointed to by its runtime argument
72 write_type_id(type_info**p) : p(p) {}
74 // Here's the runtime behavior
76 void operator()(T*) const
78 *(*p)++ = type_id<T>();
85 struct is_data_member_pointer
88 , mpl::not_<is_member_function_pointer<T> >
92 # ifdef BOOST_PYTHON_NO_MEMBER_POINTER_ORDERING
93 # define BOOST_PYTHON_DATA_MEMBER_HELPER(D) , detail::is_data_member_pointer<D>()
94 # define BOOST_PYTHON_YES_DATA_MEMBER , mpl::true_
95 # define BOOST_PYTHON_NO_DATA_MEMBER , mpl::false_
96 # elif defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
97 # define BOOST_PYTHON_DATA_MEMBER_HELPER(D) , 0
98 # define BOOST_PYTHON_YES_DATA_MEMBER , int
99 # define BOOST_PYTHON_NO_DATA_MEMBER , ...
101 # define BOOST_PYTHON_DATA_MEMBER_HELPER(D)
102 # define BOOST_PYTHON_YES_DATA_MEMBER
103 # define BOOST_PYTHON_NO_DATA_MEMBER
109 // A meta-assertion mechanism which prints nice error messages and
110 // backtraces on lots of compilers. Usage:
112 // assertion<C>::failed
114 // where C is an MPL metafunction class
117 template <class C> struct assertion_failed { };
118 template <class C> struct assertion_ok { typedef C failed; };
122 : mpl::if_<C, assertion_ok<C>, assertion_failed<C> >::type
126 // Checks for validity of arguments used to define virtual
127 // functions with default implementations.
130 template <class Default>
131 void not_a_derived_class_member(Default) {}
133 template <class T, class Fn>
134 struct virtual_function_default
136 template <class Default>
138 must_be_derived_class_member(Default const&)
140 // https://svn.boost.org/trac/boost/ticket/5803
141 //typedef typename assertion<mpl::not_<is_same<Default,Fn> > >::failed test0;
142 # if !BOOST_WORKAROUND(__MWERKS__, <= 0x2407)
143 typedef typename assertion<is_polymorphic<T> >::failed test1 BOOST_ATTRIBUTE_UNUSED;
145 typedef typename assertion<is_member_function_pointer<Fn> >::failed test2 BOOST_ATTRIBUTE_UNUSED;
146 not_a_derived_class_member<Default>(Fn());
152 // This is the primary mechanism through which users will expose
153 // C++ classes to Python.
155 class W // class being wrapped
156 , class X1 // = detail::not_specified
157 , class X2 // = detail::not_specified
158 , class X3 // = detail::not_specified
160 class class_ : public objects::class_base
163 typedef objects::class_base base;
164 typedef class_<W,X1,X2,X3> self;
165 typedef typename objects::class_metadata<W,X1,X2,X3> metadata;
166 typedef W wrapped_type;
170 // A helper class which will contain an array of id objects to be
171 // passed to the base class constructor
174 typedef typename metadata::bases bases;
178 // Stick the derived class id into the first element of the array
179 ids[0] = detail::unwrap_type_id((W*)0, (W*)0);
181 // Write the rest of the elements into succeeding positions.
182 type_info* p = ids + 1;
183 mpl::for_each(detail::write_type_id(&p), (bases*)0, (add_pointer<mpl::_>*)0);
186 BOOST_STATIC_CONSTANT(
187 std::size_t, size = mpl::size<bases>::value + 1);
190 friend struct id_vector;
192 public: // constructors
194 // Construct with the class name, with or without docstring, and default __init__() function
195 class_(char const* name, char const* doc = 0);
197 // Construct with class name, no docstring, and an uncallable __init__ function
198 class_(char const* name, no_init_t);
200 // Construct with class name, docstring, and an uncallable __init__ function
201 class_(char const* name, char const* doc, no_init_t);
203 // Construct with class name and init<> function
204 template <class DerivedT>
205 inline class_(char const* name, init_base<DerivedT> const& i)
206 : base(name, id_vector::size, id_vector().ids)
211 // Construct with class name, docstring and init<> function
212 template <class DerivedT>
213 inline class_(char const* name, char const* doc, init_base<DerivedT> const& i)
214 : base(name, id_vector::size, id_vector().ids, doc)
219 public: // member functions
221 // Generic visitation
222 template <class Derived>
223 self& def(def_visitor<Derived> const& visitor)
225 visitor.visit(*this);
229 // Wrap a member function or a non-member function which can take
230 // a T, T cv&, or T cv* as its first parameter, a callable
231 // python object, or a generic visitor.
233 self& def(char const* name, F f)
236 detail::unwrap_wrapper((W*)0)
237 , name, f, detail::def_helper<char const*>(0), &f);
241 template <class A1, class A2>
242 self& def(char const* name, A1 a1, A2 const& a2)
244 this->def_maybe_overloads(name, a1, a2, &a2);
248 template <class Fn, class A1, class A2>
249 self& def(char const* name, Fn fn, A1 const& a1, A2 const& a2)
251 // The arguments are definitely:
252 // def(name, function, policy, doc_string)
253 // def(name, function, doc_string, policy)
256 detail::unwrap_wrapper((W*)0)
258 , detail::def_helper<A1,A2>(a1,a2)
264 template <class Fn, class A1, class A2, class A3>
265 self& def(char const* name, Fn fn, A1 const& a1, A2 const& a2, A3 const& a3)
268 detail::unwrap_wrapper((W*)0)
270 , detail::def_helper<A1,A2,A3>(a1,a2,a3)
277 // Data member access
280 self& def_readonly(char const* name, D const& d, char const* doc=0)
282 return this->def_readonly_impl(name, d, doc BOOST_PYTHON_DATA_MEMBER_HELPER(D));
286 self& def_readwrite(char const* name, D const& d, char const* doc=0)
288 return this->def_readwrite_impl(name, d, doc BOOST_PYTHON_DATA_MEMBER_HELPER(D));
292 self& def_readonly(char const* name, D& d, char const* doc=0)
294 return this->def_readonly_impl(name, d, doc BOOST_PYTHON_DATA_MEMBER_HELPER(D));
298 self& def_readwrite(char const* name, D& d, char const* doc=0)
300 return this->def_readwrite_impl(name, d, doc BOOST_PYTHON_DATA_MEMBER_HELPER(D));
305 self& add_property(char const* name, Get fget, char const* docstr = 0)
307 base::add_property(name, this->make_getter(fget), docstr);
311 template <class Get, class Set>
312 self& add_property(char const* name, Get fget, Set fset, char const* docstr = 0)
315 name, this->make_getter(fget), this->make_setter(fset), docstr);
320 self& add_static_property(char const* name, Get fget)
322 base::add_static_property(name, object(fget));
326 template <class Get, class Set>
327 self& add_static_property(char const* name, Get fget, Set fset)
329 base::add_static_property(name, object(fget), object(fset));
334 self& setattr(char const* name, U const& x)
336 this->base::setattr(name, object(x));
341 template <typename PickleSuiteType>
342 self& def_pickle(PickleSuiteType const& x)
344 error_messages::must_be_derived_from_pickle_suite(x);
345 detail::pickle_suite_finalize<PickleSuiteType>::register_(
347 &PickleSuiteType::getinitargs,
348 &PickleSuiteType::getstate,
349 &PickleSuiteType::setstate,
350 PickleSuiteType::getstate_manages_dict());
354 self& enable_pickling()
356 this->base::enable_pickling_(false);
360 self& staticmethod(char const* name)
362 this->make_method_static(name);
365 private: // helper functions
367 // Builds a method for this class around the given [member]
368 // function pointer or object, appropriately adjusting the type of
369 // the first signature argument so that if f is a member of a
370 // (possibly not wrapped) base class of T, an lvalue argument of
371 // type T will be required.
373 // @group PropertyHelpers {
375 object make_getter(F f)
377 typedef typename api::is_object_operators<F>::type is_obj_or_proxy;
379 return this->make_fn_impl(
380 detail::unwrap_wrapper((W*)0)
381 , f, is_obj_or_proxy(), (char*)0, detail::is_data_member_pointer<F>()
386 object make_setter(F f)
388 typedef typename api::is_object_operators<F>::type is_obj_or_proxy;
390 return this->make_fn_impl(
391 detail::unwrap_wrapper((W*)0)
392 , f, is_obj_or_proxy(), (int*)0, detail::is_data_member_pointer<F>()
396 template <class T, class F>
397 object make_fn_impl(T*, F const& f, mpl::false_, void*, mpl::false_)
399 return python::make_function(f, default_call_policies(), detail::get_signature(f, (T*)0));
402 template <class T, class D, class B>
403 object make_fn_impl(T*, D B::*pm_, mpl::false_, char*, mpl::true_)
406 return python::make_getter(pm);
409 template <class T, class D, class B>
410 object make_fn_impl(T*, D B::*pm_, mpl::false_, int*, mpl::true_)
413 return python::make_setter(pm);
416 template <class T, class F>
417 object make_fn_impl(T*, F const& x, mpl::true_, void*, mpl::false_)
423 template <class D, class B>
424 self& def_readonly_impl(
425 char const* name, D B::*pm_, char const* doc BOOST_PYTHON_YES_DATA_MEMBER)
427 return this->add_property(name, pm_, doc);
430 template <class D, class B>
431 self& def_readwrite_impl(
432 char const* name, D B::*pm_, char const* doc BOOST_PYTHON_YES_DATA_MEMBER)
434 return this->add_property(name, pm_, pm_, doc);
438 self& def_readonly_impl(
439 char const* name, D& d, char const* BOOST_PYTHON_NO_DATA_MEMBER)
441 return this->add_static_property(name, python::make_getter(d));
445 self& def_readwrite_impl(
446 char const* name, D& d, char const* BOOST_PYTHON_NO_DATA_MEMBER)
448 return this->add_static_property(name, python::make_getter(d), python::make_setter(d));
451 template <class DefVisitor>
452 inline void initialize(DefVisitor const& i)
454 metadata::register_(); // set up runtime metadata/conversions
456 typedef typename metadata::holder holder;
457 this->set_instance_size( objects::additional_instance_size<holder>::value );
462 inline void initialize(no_init_t)
464 metadata::register_(); // set up runtime metadata/conversions
469 // These two overloads discriminate between def() as applied to a
470 // generic visitor and everything else.
473 template <class T, class Helper, class LeafVisitor, class Visitor>
474 inline void def_impl(
478 , Helper const& helper
479 , def_visitor<Visitor> const* v
482 v->visit(*this, name, helper);
485 template <class T, class Fn, class Helper>
486 inline void def_impl(
490 , Helper const& helper
494 objects::add_to_namespace(
501 , detail::get_signature(fn, (T*)0)
506 this->def_default(name, fn, helper, mpl::bool_<Helper::has_default_implementation>());
511 // These two overloads handle the definition of default
512 // implementation overloads for virtual functions. The second one
513 // handles the case where no default implementation was specified.
515 // @group def_default {
516 template <class Fn, class Helper>
517 inline void def_default(
520 , Helper const& helper
523 detail::error::virtual_function_default<W,Fn>::must_be_derived_class_member(
524 helper.default_implementation());
526 objects::add_to_namespace(
529 helper.default_implementation(), helper.policies(), helper.keywords())
533 template <class Fn, class Helper>
534 inline void def_default(char const*, Fn, Helper const&, mpl::bool_<false>)
539 // These two overloads discriminate between def() as applied to
540 // regular functions and def() as applied to the result of
541 // BOOST_PYTHON_FUNCTION_OVERLOADS(). The final argument is used to
544 // @group def_maybe_overloads {
545 template <class OverloadsT, class SigT>
546 void def_maybe_overloads(
549 , OverloadsT const& overloads
550 , detail::overloads_base const*)
553 // convert sig to a type_list (see detail::get_signature in signature.hpp)
554 // before calling detail::define_with_defaults.
555 detail::define_with_defaults(
556 name, overloads, *this, detail::get_signature(sig));
559 template <class Fn, class A1>
560 void def_maybe_overloads(
567 detail::unwrap_wrapper((W*)0)
570 , detail::def_helper<A1>(a1)
583 template <class W, class X1, class X2, class X3>
584 inline class_<W,X1,X2,X3>::class_(char const* name, char const* doc)
585 : base(name, id_vector::size, id_vector().ids, doc)
587 this->initialize(init<>());
588 // select_holder::assert_default_constructible();
591 template <class W, class X1, class X2, class X3>
592 inline class_<W,X1,X2,X3>::class_(char const* name, no_init_t)
593 : base(name, id_vector::size, id_vector().ids)
595 this->initialize(no_init);
598 template <class W, class X1, class X2, class X3>
599 inline class_<W,X1,X2,X3>::class_(char const* name, char const* doc, no_init_t)
600 : base(name, id_vector::size, id_vector().ids, doc)
602 this->initialize(no_init);
605 }} // namespace boost::python
607 # undef BOOST_PYTHON_DATA_MEMBER_HELPER
608 # undef BOOST_PYTHON_YES_DATA_MEMBER
609 # undef BOOST_PYTHON_NO_DATA_MEMBER
610 # undef BOOST_PYTHON_NO_MEMBER_POINTER_ORDERING
612 #endif // CLASS_DWA200216_HPP