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 |
7 | |
8 | # include <boost/python/detail/prefix.hpp> |
9 | |
10 | # include <boost/noncopyable.hpp> |
11 | |
12 | # include <boost/python/class_fwd.hpp> |
13 | # include <boost/python/object/class.hpp> |
14 | |
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> |
22 | |
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> |
26 | |
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> |
33 | |
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> |
37 | |
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> |
42 | |
43 | # include <boost/detail/workaround.hpp> |
44 | |
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 | |
50 | # define BOOST_PYTHON_NO_MEMBER_POINTER_ORDERING 1 |
51 | |
52 | # endif |
53 | |
54 | # ifdef BOOST_PYTHON_NO_MEMBER_POINTER_ORDERING |
55 | # include <boost/mpl/and.hpp> |
56 | # include <boost/type_traits/is_member_pointer.hpp> |
57 | # endif |
58 | |
59 | namespace boost { namespace python { |
60 | |
61 | template <class DerivedVisitor> class def_visitor; |
62 | |
63 | enum no_init_t { no_init }; |
64 | |
65 | namespace detail |
66 | { |
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 |
70 | struct write_type_id |
71 | { |
72 | write_type_id(type_info**p) : p(p) {} |
73 | |
74 | // Here's the runtime behavior |
75 | template <class T> |
76 | void operator()(T*) const |
77 | { |
78 | *(*p)++ = type_id<T>(); |
79 | } |
80 | |
81 | type_info** p; |
82 | }; |
83 | |
84 | template <class T> |
85 | struct is_data_member_pointer |
86 | : mpl::and_< |
87 | is_member_pointer<T> |
88 | , mpl::not_<is_member_function_pointer<T> > |
89 | > |
90 | {}; |
91 | |
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 , ... |
100 | # else |
101 | # define BOOST_PYTHON_DATA_MEMBER_HELPER(D) |
102 | # define BOOST_PYTHON_YES_DATA_MEMBER |
103 | # define BOOST_PYTHON_NO_DATA_MEMBER |
104 | # endif |
105 | |
106 | namespace error |
107 | { |
108 | // |
109 | // A meta-assertion mechanism which prints nice error messages and |
110 | // backtraces on lots of compilers. Usage: |
111 | // |
112 | // assertion<C>::failed |
113 | // |
114 | // where C is an MPL metafunction class |
115 | // |
116 | |
117 | template <class C> struct assertion_failed { }; |
118 | template <class C> struct assertion_ok { typedef C failed; }; |
119 | |
120 | template <class C> |
121 | struct assertion |
122 | : mpl::if_<C, assertion_ok<C>, assertion_failed<C> >::type |
123 | {}; |
124 | |
125 | // |
126 | // Checks for validity of arguments used to define virtual |
127 | // functions with default implementations. |
128 | // |
129 | |
130 | template <class Default> |
131 | void not_a_derived_class_member(Default) {} |
132 | |
133 | template <class T, class Fn> |
134 | struct virtual_function_default |
135 | { |
136 | template <class Default> |
137 | static void |
138 | must_be_derived_class_member(Default const&) |
139 | { |
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; |
144 | # endif |
145 | typedef typename assertion<is_member_function_pointer<Fn> >::failed test2; |
146 | not_a_derived_class_member<Default>(Fn()); |
147 | } |
148 | }; |
149 | } |
150 | } |
151 | |
152 | // This is the primary mechanism through which users will expose |
153 | // C++ classes to Python. |
154 | template < |
155 | class W // class being wrapped |
156 | , class X1 // = detail::not_specified |
157 | , class X2 // = detail::not_specified |
158 | , class X3 // = detail::not_specified |
159 | > |
160 | class class_ : public objects::class_base |
161 | { |
162 | public: // types |
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; |
167 | |
168 | private: // types |
169 | |
170 | // A helper class which will contain an array of id objects to be |
171 | // passed to the base class constructor |
172 | struct id_vector |
173 | { |
174 | typedef typename metadata::bases bases; |
175 | |
176 | id_vector() |
177 | { |
178 | // Stick the derived class id into the first element of the array |
179 | ids[0] = detail::unwrap_type_id((W*)0, (W*)0); |
180 | |
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); |
184 | } |
185 | |
186 | BOOST_STATIC_CONSTANT( |
187 | std::size_t, size = mpl::size<bases>::value + 1); |
188 | type_info ids[size]; |
189 | }; |
190 | friend struct id_vector; |
191 | |
192 | public: // constructors |
193 | |
194 | // Construct with the class name, with or without docstring, and default __init__() function |
195 | class_(char const* name, char const* doc = 0); |
196 | |
197 | // Construct with class name, no docstring, and an uncallable __init__ function |
198 | class_(char const* name, no_init_t); |
199 | |
200 | // Construct with class name, docstring, and an uncallable __init__ function |
201 | class_(char const* name, char const* doc, no_init_t); |
202 | |
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) |
207 | { |
208 | this->initialize(i); |
209 | } |
210 | |
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) |
215 | { |
216 | this->initialize(i); |
217 | } |
218 | |
219 | public: // member functions |
220 | |
221 | // Generic visitation |
222 | template <class Derived> |
223 | self& def(def_visitor<Derived> const& visitor) |
224 | { |
225 | visitor.visit(*this); |
226 | return *this; |
227 | } |
228 | |
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. |
232 | template <class F> |
233 | self& def(char const* name, F f) |
234 | { |
235 | this->def_impl( |
236 | detail::unwrap_wrapper((W*)0) |
237 | , name, f, detail::def_helper<char const*>(0), &f); |
238 | return *this; |
239 | } |
240 | |
241 | template <class A1, class A2> |
242 | self& def(char const* name, A1 a1, A2 const& a2) |
243 | { |
244 | this->def_maybe_overloads(name, a1, a2, &a2); |
245 | return *this; |
246 | } |
247 | |
248 | template <class Fn, class A1, class A2> |
249 | self& def(char const* name, Fn fn, A1 const& a1, A2 const& a2) |
250 | { |
251 | // The arguments are definitely: |
252 | // def(name, function, policy, doc_string) |
253 | // def(name, function, doc_string, policy) |
254 | |
255 | this->def_impl( |
256 | detail::unwrap_wrapper((W*)0) |
257 | , name, fn |
258 | , detail::def_helper<A1,A2>(a1,a2) |
259 | , &fn); |
260 | |
261 | return *this; |
262 | } |
263 | |
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) |
266 | { |
267 | this->def_impl( |
268 | detail::unwrap_wrapper((W*)0) |
269 | , name, fn |
270 | , detail::def_helper<A1,A2,A3>(a1,a2,a3) |
271 | , &fn); |
272 | |
273 | return *this; |
274 | } |
275 | |
276 | // |
277 | // Data member access |
278 | // |
279 | template <class D> |
280 | self& def_readonly(char const* name, D const& d, char const* doc=0) |
281 | { |
282 | return this->def_readonly_impl(name, d, doc BOOST_PYTHON_DATA_MEMBER_HELPER(D)); |
283 | } |
284 | |
285 | template <class D> |
286 | self& def_readwrite(char const* name, D const& d, char const* doc=0) |
287 | { |
288 | return this->def_readwrite_impl(name, d, doc BOOST_PYTHON_DATA_MEMBER_HELPER(D)); |
289 | } |
290 | |
291 | template <class D> |
292 | self& def_readonly(char const* name, D& d, char const* doc=0) |
293 | { |
294 | return this->def_readonly_impl(name, d, doc BOOST_PYTHON_DATA_MEMBER_HELPER(D)); |
295 | } |
296 | |
297 | template <class D> |
298 | self& def_readwrite(char const* name, D& d, char const* doc=0) |
299 | { |
300 | return this->def_readwrite_impl(name, d, doc BOOST_PYTHON_DATA_MEMBER_HELPER(D)); |
301 | } |
302 | |
303 | // Property creation |
304 | template <class Get> |
305 | self& add_property(char const* name, Get fget, char const* docstr = 0) |
306 | { |
307 | base::add_property(name, this->make_getter(fget), docstr); |
308 | return *this; |
309 | } |
310 | |
311 | template <class Get, class Set> |
312 | self& add_property(char const* name, Get fget, Set fset, char const* docstr = 0) |
313 | { |
314 | base::add_property( |
315 | name, this->make_getter(fget), this->make_setter(fset), docstr); |
316 | return *this; |
317 | } |
318 | |
319 | template <class Get> |
320 | self& add_static_property(char const* name, Get fget) |
321 | { |
322 | base::add_static_property(name, object(fget)); |
323 | return *this; |
324 | } |
325 | |
326 | template <class Get, class Set> |
327 | self& add_static_property(char const* name, Get fget, Set fset) |
328 | { |
329 | base::add_static_property(name, object(fget), object(fset)); |
330 | return *this; |
331 | } |
332 | |
333 | template <class U> |
334 | self& setattr(char const* name, U const& x) |
335 | { |
336 | this->base::setattr(name, object(x)); |
337 | return *this; |
338 | } |
339 | |
340 | // Pickle support |
341 | template <typename PickleSuiteType> |
342 | self& def_pickle(PickleSuiteType const& x) |
343 | { |
344 | error_messages::must_be_derived_from_pickle_suite(x); |
345 | detail::pickle_suite_finalize<PickleSuiteType>::register_( |
346 | *this, |
347 | &PickleSuiteType::getinitargs, |
348 | &PickleSuiteType::getstate, |
349 | &PickleSuiteType::setstate, |
350 | PickleSuiteType::getstate_manages_dict()); |
351 | return *this; |
352 | } |
353 | |
354 | self& enable_pickling() |
355 | { |
356 | this->base::enable_pickling_(false); |
357 | return *this; |
358 | } |
359 | |
360 | self& staticmethod(char const* name) |
361 | { |
362 | this->make_method_static(name); |
363 | return *this; |
364 | } |
365 | private: // helper functions |
366 | |
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. |
372 | // |
373 | // @group PropertyHelpers { |
374 | template <class F> |
375 | object make_getter(F f) |
376 | { |
377 | typedef typename api::is_object_operators<F>::type is_obj_or_proxy; |
378 | |
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>() |
382 | ); |
383 | } |
384 | |
385 | template <class F> |
386 | object make_setter(F f) |
387 | { |
388 | typedef typename api::is_object_operators<F>::type is_obj_or_proxy; |
389 | |
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>() |
393 | ); |
394 | } |
395 | |
396 | template <class T, class F> |
397 | object make_fn_impl(T*, F const& f, mpl::false_, void*, mpl::false_) |
398 | { |
399 | return python::make_function(f, default_call_policies(), detail::get_signature(f, (T*)0)); |
400 | } |
401 | |
402 | template <class T, class D, class B> |
403 | object make_fn_impl(T*, D B::*pm_, mpl::false_, char*, mpl::true_) |
404 | { |
405 | D T::*pm = pm_; |
406 | return python::make_getter(pm); |
407 | } |
408 | |
409 | template <class T, class D, class B> |
410 | object make_fn_impl(T*, D B::*pm_, mpl::false_, int*, mpl::true_) |
411 | { |
412 | D T::*pm = pm_; |
413 | return python::make_setter(pm); |
414 | } |
415 | |
416 | template <class T, class F> |
417 | object make_fn_impl(T*, F const& x, mpl::true_, void*, mpl::false_) |
418 | { |
419 | return x; |
420 | } |
421 | // } |
422 | |
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) |
426 | { |
427 | return this->add_property(name, pm_, doc); |
428 | } |
429 | |
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) |
433 | { |
434 | return this->add_property(name, pm_, pm_, doc); |
435 | } |
436 | |
437 | template <class D> |
438 | self& def_readonly_impl( |
439 | char const* name, D& d, char const* BOOST_PYTHON_NO_DATA_MEMBER) |
440 | { |
441 | return this->add_static_property(name, python::make_getter(d)); |
442 | } |
443 | |
444 | template <class D> |
445 | self& def_readwrite_impl( |
446 | char const* name, D& d, char const* BOOST_PYTHON_NO_DATA_MEMBER) |
447 | { |
448 | return this->add_static_property(name, python::make_getter(d), python::make_setter(d)); |
449 | } |
450 | |
451 | template <class DefVisitor> |
452 | inline void initialize(DefVisitor const& i) |
453 | { |
454 | metadata::register_(); // set up runtime metadata/conversions |
455 | |
456 | typedef typename metadata::holder holder; |
457 | this->set_instance_size( objects::additional_instance_size<holder>::value ); |
458 | |
459 | this->def(i); |
460 | } |
461 | |
462 | inline void initialize(no_init_t) |
463 | { |
464 | metadata::register_(); // set up runtime metadata/conversions |
465 | this->def_no_init(); |
466 | } |
467 | |
468 | // |
469 | // These two overloads discriminate between def() as applied to a |
470 | // generic visitor and everything else. |
471 | // |
472 | // @group def_impl { |
473 | template <class T, class Helper, class LeafVisitor, class Visitor> |
474 | inline void def_impl( |
475 | T* |
476 | , char const* name |
477 | , LeafVisitor |
478 | , Helper const& helper |
479 | , def_visitor<Visitor> const* v |
480 | ) |
481 | { |
482 | v->visit(*this, name, helper); |
483 | } |
484 | |
485 | template <class T, class Fn, class Helper> |
486 | inline void def_impl( |
487 | T* |
488 | , char const* name |
489 | , Fn fn |
490 | , Helper const& helper |
491 | , ... |
492 | ) |
493 | { |
494 | objects::add_to_namespace( |
495 | *this |
496 | , name |
497 | , make_function( |
498 | fn |
499 | , helper.policies() |
500 | , helper.keywords() |
501 | , detail::get_signature(fn, (T*)0) |
502 | ) |
503 | , helper.doc() |
504 | ); |
505 | |
506 | this->def_default(name, fn, helper, mpl::bool_<Helper::has_default_implementation>()); |
507 | } |
508 | // } |
509 | |
510 | // |
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. |
514 | // |
515 | // @group def_default { |
516 | template <class Fn, class Helper> |
517 | inline void def_default( |
518 | char const* name |
519 | , Fn |
520 | , Helper const& helper |
521 | , mpl::bool_<true>) |
522 | { |
523 | detail::error::virtual_function_default<W,Fn>::must_be_derived_class_member( |
524 | helper.default_implementation()); |
525 | |
526 | objects::add_to_namespace( |
527 | *this, name, |
528 | make_function( |
529 | helper.default_implementation(), helper.policies(), helper.keywords()) |
530 | ); |
531 | } |
532 | |
533 | template <class Fn, class Helper> |
534 | inline void def_default(char const*, Fn, Helper const&, mpl::bool_<false>) |
535 | { } |
536 | // } |
537 | |
538 | // |
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 |
542 | // discriminate. |
543 | // |
544 | // @group def_maybe_overloads { |
545 | template <class OverloadsT, class SigT> |
546 | void def_maybe_overloads( |
547 | char const* name |
548 | , SigT sig |
549 | , OverloadsT const& overloads |
550 | , detail::overloads_base const*) |
551 | |
552 | { |
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)); |
557 | } |
558 | |
559 | template <class Fn, class A1> |
560 | void def_maybe_overloads( |
561 | char const* name |
562 | , Fn fn |
563 | , A1 const& a1 |
564 | , ...) |
565 | { |
566 | this->def_impl( |
567 | detail::unwrap_wrapper((W*)0) |
568 | , name |
569 | , fn |
570 | , detail::def_helper<A1>(a1) |
571 | , &fn |
572 | ); |
573 | |
574 | } |
575 | // } |
576 | }; |
577 | |
578 | |
579 | // |
580 | // implementations |
581 | // |
582 | |
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) |
586 | { |
587 | this->initialize(init<>()); |
588 | // select_holder::assert_default_constructible(); |
589 | } |
590 | |
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) |
594 | { |
595 | this->initialize(no_init); |
596 | } |
597 | |
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) |
601 | { |
602 | this->initialize(no_init); |
603 | } |
604 | |
605 | }} // namespace boost::python |
606 | |
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 |
611 | |
612 | #endif // CLASS_DWA200216_HPP |
613 | |