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))) \
49 || BOOST_WORKAROUND(__GNUC__, < 3)
51 # define BOOST_PYTHON_NO_MEMBER_POINTER_ORDERING 1
55 # ifdef BOOST_PYTHON_NO_MEMBER_POINTER_ORDERING
56 # include <boost/mpl/and.hpp>
57 # include <boost/type_traits/is_member_pointer.hpp>
60 namespace boost
{ namespace python
{
62 template <class DerivedVisitor
> class def_visitor
;
64 enum no_init_t
{ no_init
};
68 // This function object is used with mpl::for_each to write the id
69 // of the type a pointer to which is passed as its 2nd compile-time
70 // argument. into the iterator pointed to by its runtime argument
73 write_type_id(type_info
**p
) : p(p
) {}
75 // Here's the runtime behavior
77 void operator()(T
*) const
79 *(*p
)++ = type_id
<T
>();
86 struct is_data_member_pointer
89 , mpl::not_
<is_member_function_pointer
<T
> >
93 # ifdef BOOST_PYTHON_NO_MEMBER_POINTER_ORDERING
94 # define BOOST_PYTHON_DATA_MEMBER_HELPER(D) , detail::is_data_member_pointer<D>()
95 # define BOOST_PYTHON_YES_DATA_MEMBER , mpl::true_
96 # define BOOST_PYTHON_NO_DATA_MEMBER , mpl::false_
97 # elif defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
98 # define BOOST_PYTHON_DATA_MEMBER_HELPER(D) , 0
99 # define BOOST_PYTHON_YES_DATA_MEMBER , int
100 # define BOOST_PYTHON_NO_DATA_MEMBER , ...
102 # define BOOST_PYTHON_DATA_MEMBER_HELPER(D)
103 # define BOOST_PYTHON_YES_DATA_MEMBER
104 # define BOOST_PYTHON_NO_DATA_MEMBER
110 // A meta-assertion mechanism which prints nice error messages and
111 // backtraces on lots of compilers. Usage:
113 // assertion<C>::failed
115 // where C is an MPL metafunction class
118 template <class C
> struct assertion_failed
{ };
119 template <class C
> struct assertion_ok
{ typedef C failed
; };
123 : mpl::if_
<C
, assertion_ok
<C
>, assertion_failed
<C
> >::type
127 // Checks for validity of arguments used to define virtual
128 // functions with default implementations.
131 template <class Default
>
132 void not_a_derived_class_member(Default
) {}
134 template <class T
, class Fn
>
135 struct virtual_function_default
137 template <class Default
>
139 must_be_derived_class_member(Default
const&)
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
;
145 typedef typename assertion
<is_member_function_pointer
<Fn
> >::failed test2
;
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
));
304 # if !BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
306 self
& add_property(char const* name
, Get fget
, char const* docstr
= 0)
308 base::add_property(name
, this->make_getter(fget
), docstr
);
312 template <class Get
, class Set
>
313 self
& add_property(char const* name
, Get fget
, Set fset
, char const* docstr
= 0)
316 name
, this->make_getter(fget
), this->make_setter(fset
), docstr
);
322 self
& add_property_impl(char const* name
, Get fget
, char const* docstr
, int)
324 base::add_property(name
, this->make_getter(fget
), docstr
);
328 template <class Get
, class Set
>
329 self
& add_property_impl(char const* name
, Get fget
, Set fset
, ...)
332 name
, this->make_getter(fget
), this->make_setter(fset
), 0);
338 self
& add_property(char const* name
, Get fget
)
340 base::add_property(name
, this->make_getter(fget
), 0);
344 template <class Get
, class DocStrOrSet
>
345 self
& add_property(char const* name
, Get fget
, DocStrOrSet docstr_or_set
)
347 this->add_property_impl(name
, this->make_getter(fget
), docstr_or_set
, 0);
351 template <class Get
, class Set
>
353 add_property(char const* name
, Get fget
, Set fset
, char const* docstr
)
356 name
, this->make_getter(fget
), this->make_setter(fset
), docstr
);
362 self
& add_static_property(char const* name
, Get fget
)
364 base::add_static_property(name
, object(fget
));
368 template <class Get
, class Set
>
369 self
& add_static_property(char const* name
, Get fget
, Set fset
)
371 base::add_static_property(name
, object(fget
), object(fset
));
376 self
& setattr(char const* name
, U
const& x
)
378 this->base::setattr(name
, object(x
));
383 template <typename PickleSuiteType
>
384 self
& def_pickle(PickleSuiteType
const& x
)
386 error_messages::must_be_derived_from_pickle_suite(x
);
387 detail::pickle_suite_finalize
<PickleSuiteType
>::register_(
389 &PickleSuiteType::getinitargs
,
390 &PickleSuiteType::getstate
,
391 &PickleSuiteType::setstate
,
392 PickleSuiteType::getstate_manages_dict());
396 self
& enable_pickling()
398 this->base::enable_pickling_(false);
402 self
& staticmethod(char const* name
)
404 this->make_method_static(name
);
407 private: // helper functions
409 // Builds a method for this class around the given [member]
410 // function pointer or object, appropriately adjusting the type of
411 // the first signature argument so that if f is a member of a
412 // (possibly not wrapped) base class of T, an lvalue argument of
413 // type T will be required.
415 // @group PropertyHelpers {
417 object
make_getter(F f
)
419 typedef typename
api::is_object_operators
<F
>::type is_obj_or_proxy
;
421 return this->make_fn_impl(
422 detail::unwrap_wrapper((W
*)0)
423 , f
, is_obj_or_proxy(), (char*)0, detail::is_data_member_pointer
<F
>()
428 object
make_setter(F f
)
430 typedef typename
api::is_object_operators
<F
>::type is_obj_or_proxy
;
432 return this->make_fn_impl(
433 detail::unwrap_wrapper((W
*)0)
434 , f
, is_obj_or_proxy(), (int*)0, detail::is_data_member_pointer
<F
>()
438 template <class T
, class F
>
439 object
make_fn_impl(T
*, F
const& f
, mpl::false_
, void*, mpl::false_
)
441 return python::make_function(f
, default_call_policies(), detail::get_signature(f
, (T
*)0));
444 template <class T
, class D
, class B
>
445 object
make_fn_impl(T
*, D
B::*pm_
, mpl::false_
, char*, mpl::true_
)
448 return python::make_getter(pm
);
451 template <class T
, class D
, class B
>
452 object
make_fn_impl(T
*, D
B::*pm_
, mpl::false_
, int*, mpl::true_
)
455 return python::make_setter(pm
);
458 template <class T
, class F
>
459 object
make_fn_impl(T
*, F
const& x
, mpl::true_
, void*, mpl::false_
)
465 template <class D
, class B
>
466 self
& def_readonly_impl(
467 char const* name
, D
B::*pm_
, char const* doc BOOST_PYTHON_YES_DATA_MEMBER
)
469 return this->add_property(name
, pm_
, doc
);
472 template <class D
, class B
>
473 self
& def_readwrite_impl(
474 char const* name
, D
B::*pm_
, char const* doc BOOST_PYTHON_YES_DATA_MEMBER
)
476 return this->add_property(name
, pm_
, pm_
, doc
);
480 self
& def_readonly_impl(
481 char const* name
, D
& d
, char const* BOOST_PYTHON_NO_DATA_MEMBER
)
483 return this->add_static_property(name
, python::make_getter(d
));
487 self
& def_readwrite_impl(
488 char const* name
, D
& d
, char const* BOOST_PYTHON_NO_DATA_MEMBER
)
490 return this->add_static_property(name
, python::make_getter(d
), python::make_setter(d
));
493 template <class DefVisitor
>
494 inline void initialize(DefVisitor
const& i
)
496 metadata::register_(); // set up runtime metadata/conversions
498 typedef typename
metadata::holder holder
;
499 this->set_instance_size( objects::additional_instance_size
<holder
>::value
);
504 inline void initialize(no_init_t
)
506 metadata::register_(); // set up runtime metadata/conversions
511 // These two overloads discriminate between def() as applied to a
512 // generic visitor and everything else.
515 template <class T
, class Helper
, class LeafVisitor
, class Visitor
>
516 inline void def_impl(
520 , Helper
const& helper
521 , def_visitor
<Visitor
> const* v
524 v
->visit(*this, name
, helper
);
527 template <class T
, class Fn
, class Helper
>
528 inline void def_impl(
532 , Helper
const& helper
536 objects::add_to_namespace(
543 , detail::get_signature(fn
, (T
*)0)
548 this->def_default(name
, fn
, helper
, mpl::bool_
<Helper::has_default_implementation
>());
553 // These two overloads handle the definition of default
554 // implementation overloads for virtual functions. The second one
555 // handles the case where no default implementation was specified.
557 // @group def_default {
558 template <class Fn
, class Helper
>
559 inline void def_default(
562 , Helper
const& helper
565 detail::error::virtual_function_default
<W
,Fn
>::must_be_derived_class_member(
566 helper
.default_implementation());
568 objects::add_to_namespace(
571 helper
.default_implementation(), helper
.policies(), helper
.keywords())
575 template <class Fn
, class Helper
>
576 inline void def_default(char const*, Fn
, Helper
const&, mpl::bool_
<false>)
581 // These two overloads discriminate between def() as applied to
582 // regular functions and def() as applied to the result of
583 // BOOST_PYTHON_FUNCTION_OVERLOADS(). The final argument is used to
586 // @group def_maybe_overloads {
587 template <class OverloadsT
, class SigT
>
588 void def_maybe_overloads(
591 , OverloadsT
const& overloads
592 , detail::overloads_base
const*)
595 // convert sig to a type_list (see detail::get_signature in signature.hpp)
596 // before calling detail::define_with_defaults.
597 detail::define_with_defaults(
598 name
, overloads
, *this, detail::get_signature(sig
));
601 template <class Fn
, class A1
>
602 void def_maybe_overloads(
609 detail::unwrap_wrapper((W
*)0)
612 , detail::def_helper
<A1
>(a1
)
625 template <class W
, class X1
, class X2
, class X3
>
626 inline class_
<W
,X1
,X2
,X3
>::class_(char const* name
, char const* doc
)
627 : base(name
, id_vector::size
, id_vector().ids
, doc
)
629 this->initialize(init
<>());
630 // select_holder::assert_default_constructible();
633 template <class W
, class X1
, class X2
, class X3
>
634 inline class_
<W
,X1
,X2
,X3
>::class_(char const* name
, no_init_t
)
635 : base(name
, id_vector::size
, id_vector().ids
)
637 this->initialize(no_init
);
640 template <class W
, class X1
, class X2
, class X3
>
641 inline class_
<W
,X1
,X2
,X3
>::class_(char const* name
, char const* doc
, no_init_t
)
642 : base(name
, id_vector::size
, id_vector().ids
, doc
)
644 this->initialize(no_init
);
647 }} // namespace boost::python
649 # undef BOOST_PYTHON_DATA_MEMBER_HELPER
650 # undef BOOST_PYTHON_YES_DATA_MEMBER
651 # undef BOOST_PYTHON_NO_DATA_MEMBER
652 # undef BOOST_PYTHON_NO_MEMBER_POINTER_ORDERING
654 #endif // CLASS_DWA200216_HPP