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/type_traits.hpp>
32 # include <boost/python/detail/unwrap_type_id.hpp>
33 # include <boost/python/detail/unwrap_wrapper.hpp>
34
35 # include <boost/mpl/size.hpp>
36 # include <boost/mpl/for_each.hpp>
37 # include <boost/mpl/bool.hpp>
38 # include <boost/mpl/not.hpp>
39
40 # include <boost/detail/workaround.hpp>
41
42 # if BOOST_WORKAROUND(__MWERKS__, <= 0x3004) \
43 /* pro9 reintroduced the bug */ \
44 || (BOOST_WORKAROUND(__MWERKS__, > 0x3100) \
45 && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3201)))
46
47 # define BOOST_PYTHON_NO_MEMBER_POINTER_ORDERING 1
48
49 # endif
50
51 # ifdef BOOST_PYTHON_NO_MEMBER_POINTER_ORDERING
52 # include <boost/mpl/and.hpp>
53 # endif
54
55 namespace boost { namespace python {
56
57 template <class DerivedVisitor> class def_visitor;
58
59 enum no_init_t { no_init };
60
61 namespace detail
62 {
63 // This function object is used with mpl::for_each to write the id
64 // of the type a pointer to which is passed as its 2nd compile-time
65 // argument. into the iterator pointed to by its runtime argument
66 struct write_type_id
67 {
write_type_idboost::python::detail::write_type_id68 write_type_id(type_info**p) : p(p) {}
69
70 // Here's the runtime behavior
71 template <class T>
operator ()boost::python::detail::write_type_id72 void operator()(T*) const
73 {
74 *(*p)++ = type_id<T>();
75 }
76
77 type_info** p;
78 };
79
80 template <class T>
81 struct is_data_member_pointer
82 : mpl::and_<
83 detail::is_member_pointer<T>
84 , mpl::not_<detail::is_member_function_pointer<T> >
85 >
86 {};
87
88 # ifdef BOOST_PYTHON_NO_MEMBER_POINTER_ORDERING
89 # define BOOST_PYTHON_DATA_MEMBER_HELPER(D) , detail::is_data_member_pointer<D>()
90 # define BOOST_PYTHON_YES_DATA_MEMBER , mpl::true_
91 # define BOOST_PYTHON_NO_DATA_MEMBER , mpl::false_
92 # elif defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
93 # define BOOST_PYTHON_DATA_MEMBER_HELPER(D) , 0
94 # define BOOST_PYTHON_YES_DATA_MEMBER , int
95 # define BOOST_PYTHON_NO_DATA_MEMBER , ...
96 # else
97 # define BOOST_PYTHON_DATA_MEMBER_HELPER(D)
98 # define BOOST_PYTHON_YES_DATA_MEMBER
99 # define BOOST_PYTHON_NO_DATA_MEMBER
100 # endif
101
102 namespace error
103 {
104 //
105 // A meta-assertion mechanism which prints nice error messages and
106 // backtraces on lots of compilers. Usage:
107 //
108 // assertion<C>::failed
109 //
110 // where C is an MPL metafunction class
111 //
112
113 template <class C> struct assertion_failed { };
114 template <class C> struct assertion_ok { typedef C failed; };
115
116 template <class C>
117 struct assertion
118 : mpl::if_<C, assertion_ok<C>, assertion_failed<C> >::type
119 {};
120
121 //
122 // Checks for validity of arguments used to define virtual
123 // functions with default implementations.
124 //
125
126 template <class Default>
not_a_derived_class_member(Default)127 void not_a_derived_class_member(Default) {}
128
129 template <class T, class Fn>
130 struct virtual_function_default
131 {
132 template <class Default>
133 static void
must_be_derived_class_memberboost::python::detail::error::virtual_function_default134 must_be_derived_class_member(Default const&)
135 {
136 // https://svn.boost.org/trac/boost/ticket/5803
137 //typedef typename assertion<mpl::not_<detail::is_same<Default,Fn> > >::failed test0;
138 # if !BOOST_WORKAROUND(__MWERKS__, <= 0x2407)
139 typedef typename assertion<detail::is_polymorphic<T> >::failed test1 BOOST_ATTRIBUTE_UNUSED;
140 # endif
141 typedef typename assertion<detail::is_member_function_pointer<Fn> >::failed test2 BOOST_ATTRIBUTE_UNUSED;
142 not_a_derived_class_member<Default>(Fn());
143 }
144 };
145 }
146 }
147
148 // This is the primary mechanism through which users will expose
149 // C++ classes to Python.
150 template <
151 class W // class being wrapped
152 , class X1 // = detail::not_specified
153 , class X2 // = detail::not_specified
154 , class X3 // = detail::not_specified
155 >
156 class class_ : public objects::class_base
157 {
158 public: // types
159 typedef objects::class_base base;
160 typedef class_<W,X1,X2,X3> self;
161 typedef typename objects::class_metadata<W,X1,X2,X3> metadata;
162 typedef W wrapped_type;
163
164 private: // types
165
166 // A helper class which will contain an array of id objects to be
167 // passed to the base class constructor
168 struct id_vector
169 {
170 typedef typename metadata::bases bases;
171
id_vectorboost::python::class_::id_vector172 id_vector()
173 {
174 // Stick the derived class id into the first element of the array
175 ids[0] = detail::unwrap_type_id((W*)0, (W*)0);
176
177 // Write the rest of the elements into succeeding positions.
178 type_info* p = ids + 1;
179 mpl::for_each(detail::write_type_id(&p), (bases*)0, (add_pointer<mpl::_>*)0);
180 }
181
182 BOOST_STATIC_CONSTANT(
183 std::size_t, size = mpl::size<bases>::value + 1);
184 type_info ids[size];
185 };
186 friend struct id_vector;
187
188 public: // constructors
189
190 // Construct with the class name, with or without docstring, and default __init__() function
191 class_(char const* name, char const* doc = 0);
192
193 // Construct with class name, no docstring, and an uncallable __init__ function
194 class_(char const* name, no_init_t);
195
196 // Construct with class name, docstring, and an uncallable __init__ function
197 class_(char const* name, char const* doc, no_init_t);
198
199 // Construct with class name and init<> function
200 template <class DerivedT>
class_(char const * name,init_base<DerivedT> const & i)201 inline class_(char const* name, init_base<DerivedT> const& i)
202 : base(name, id_vector::size, id_vector().ids)
203 {
204 this->initialize(i);
205 }
206
207 // Construct with class name, docstring and init<> function
208 template <class DerivedT>
class_(char const * name,char const * doc,init_base<DerivedT> const & i)209 inline class_(char const* name, char const* doc, init_base<DerivedT> const& i)
210 : base(name, id_vector::size, id_vector().ids, doc)
211 {
212 this->initialize(i);
213 }
214
215 public: // member functions
216
217 // Generic visitation
218 template <class Derived>
def(def_visitor<Derived> const & visitor)219 self& def(def_visitor<Derived> const& visitor)
220 {
221 visitor.visit(*this);
222 return *this;
223 }
224
225 // Wrap a member function or a non-member function which can take
226 // a T, T cv&, or T cv* as its first parameter, a callable
227 // python object, or a generic visitor.
228 template <class F>
def(char const * name,F f)229 self& def(char const* name, F f)
230 {
231 this->def_impl(
232 detail::unwrap_wrapper((W*)0)
233 , name, f, detail::def_helper<char const*>(0), &f);
234 return *this;
235 }
236
237 template <class A1, class A2>
def(char const * name,A1 a1,A2 const & a2)238 self& def(char const* name, A1 a1, A2 const& a2)
239 {
240 this->def_maybe_overloads(name, a1, a2, &a2);
241 return *this;
242 }
243
244 template <class Fn, class A1, class A2>
def(char const * name,Fn fn,A1 const & a1,A2 const & a2)245 self& def(char const* name, Fn fn, A1 const& a1, A2 const& a2)
246 {
247 // The arguments are definitely:
248 // def(name, function, policy, doc_string)
249 // def(name, function, doc_string, policy)
250
251 this->def_impl(
252 detail::unwrap_wrapper((W*)0)
253 , name, fn
254 , detail::def_helper<A1,A2>(a1,a2)
255 , &fn);
256
257 return *this;
258 }
259
260 template <class Fn, class A1, class A2, class A3>
def(char const * name,Fn fn,A1 const & a1,A2 const & a2,A3 const & a3)261 self& def(char const* name, Fn fn, A1 const& a1, A2 const& a2, A3 const& a3)
262 {
263 this->def_impl(
264 detail::unwrap_wrapper((W*)0)
265 , name, fn
266 , detail::def_helper<A1,A2,A3>(a1,a2,a3)
267 , &fn);
268
269 return *this;
270 }
271
272 //
273 // Data member access
274 //
275 template <class D>
def_readonly(char const * name,D const & d,char const * doc=0)276 self& def_readonly(char const* name, D const& d, char const* doc=0)
277 {
278 return this->def_readonly_impl(name, d, doc BOOST_PYTHON_DATA_MEMBER_HELPER(D));
279 }
280
281 template <class D>
def_readwrite(char const * name,D const & d,char const * doc=0)282 self& def_readwrite(char const* name, D const& d, char const* doc=0)
283 {
284 return this->def_readwrite_impl(name, d, doc BOOST_PYTHON_DATA_MEMBER_HELPER(D));
285 }
286
287 template <class D>
def_readonly(char const * name,D & d,char const * doc=0)288 self& def_readonly(char const* name, D& d, char const* doc=0)
289 {
290 return this->def_readonly_impl(name, d, doc BOOST_PYTHON_DATA_MEMBER_HELPER(D));
291 }
292
293 template <class D>
def_readwrite(char const * name,D & d,char const * doc=0)294 self& def_readwrite(char const* name, D& d, char const* doc=0)
295 {
296 return this->def_readwrite_impl(name, d, doc BOOST_PYTHON_DATA_MEMBER_HELPER(D));
297 }
298
299 // Property creation
300 template <class Get>
add_property(char const * name,Get fget,char const * docstr=0)301 self& add_property(char const* name, Get fget, char const* docstr = 0)
302 {
303 base::add_property(name, this->make_getter(fget), docstr);
304 return *this;
305 }
306
307 template <class Get, class Set>
add_property(char const * name,Get fget,Set fset,char const * docstr=0)308 self& add_property(char const* name, Get fget, Set fset, char const* docstr = 0)
309 {
310 base::add_property(
311 name, this->make_getter(fget), this->make_setter(fset), docstr);
312 return *this;
313 }
314
315 template <class Get>
add_static_property(char const * name,Get fget)316 self& add_static_property(char const* name, Get fget)
317 {
318 base::add_static_property(name, object(fget));
319 return *this;
320 }
321
322 template <class Get, class Set>
add_static_property(char const * name,Get fget,Set fset)323 self& add_static_property(char const* name, Get fget, Set fset)
324 {
325 base::add_static_property(name, object(fget), object(fset));
326 return *this;
327 }
328
329 template <class U>
setattr(char const * name,U const & x)330 self& setattr(char const* name, U const& x)
331 {
332 this->base::setattr(name, object(x));
333 return *this;
334 }
335
336 // Pickle support
337 template <typename PickleSuiteType>
def_pickle(PickleSuiteType const & x)338 self& def_pickle(PickleSuiteType const& x)
339 {
340 error_messages::must_be_derived_from_pickle_suite(x);
341 detail::pickle_suite_finalize<PickleSuiteType>::register_(
342 *this,
343 &PickleSuiteType::getinitargs,
344 &PickleSuiteType::getstate,
345 &PickleSuiteType::setstate,
346 PickleSuiteType::getstate_manages_dict());
347 return *this;
348 }
349
enable_pickling()350 self& enable_pickling()
351 {
352 this->base::enable_pickling_(false);
353 return *this;
354 }
355
staticmethod(char const * name)356 self& staticmethod(char const* name)
357 {
358 this->make_method_static(name);
359 return *this;
360 }
361 private: // helper functions
362
363 // Builds a method for this class around the given [member]
364 // function pointer or object, appropriately adjusting the type of
365 // the first signature argument so that if f is a member of a
366 // (possibly not wrapped) base class of T, an lvalue argument of
367 // type T will be required.
368 //
369 // @group PropertyHelpers {
370 template <class F>
make_getter(F f)371 object make_getter(F f)
372 {
373 typedef typename api::is_object_operators<F>::type is_obj_or_proxy;
374
375 return this->make_fn_impl(
376 detail::unwrap_wrapper((W*)0)
377 , f, is_obj_or_proxy(), (char*)0, detail::is_data_member_pointer<F>()
378 );
379 }
380
381 template <class F>
make_setter(F f)382 object make_setter(F f)
383 {
384 typedef typename api::is_object_operators<F>::type is_obj_or_proxy;
385
386 return this->make_fn_impl(
387 detail::unwrap_wrapper((W*)0)
388 , f, is_obj_or_proxy(), (int*)0, detail::is_data_member_pointer<F>()
389 );
390 }
391
392 template <class T, class F>
make_fn_impl(T *,F const & f,mpl::false_,void *,mpl::false_)393 object make_fn_impl(T*, F const& f, mpl::false_, void*, mpl::false_)
394 {
395 return python::make_function(f, default_call_policies(), detail::get_signature(f, (T*)0));
396 }
397
398 template <class T, class D, class B>
make_fn_impl(T *,D B::* pm_,mpl::false_,char *,mpl::true_)399 object make_fn_impl(T*, D B::*pm_, mpl::false_, char*, mpl::true_)
400 {
401 D T::*pm = pm_;
402 return python::make_getter(pm);
403 }
404
405 template <class T, class D, class B>
make_fn_impl(T *,D B::* pm_,mpl::false_,int *,mpl::true_)406 object make_fn_impl(T*, D B::*pm_, mpl::false_, int*, mpl::true_)
407 {
408 D T::*pm = pm_;
409 return python::make_setter(pm);
410 }
411
412 template <class T, class F>
make_fn_impl(T *,F const & x,mpl::true_,void *,mpl::false_)413 object make_fn_impl(T*, F const& x, mpl::true_, void*, mpl::false_)
414 {
415 return x;
416 }
417 // }
418
419 template <class D, class B>
def_readonly_impl(char const * name,D B::* pm_,char const * doc BOOST_PYTHON_YES_DATA_MEMBER)420 self& def_readonly_impl(
421 char const* name, D B::*pm_, char const* doc BOOST_PYTHON_YES_DATA_MEMBER)
422 {
423 return this->add_property(name, pm_, doc);
424 }
425
426 template <class D, class B>
def_readwrite_impl(char const * name,D B::* pm_,char const * doc BOOST_PYTHON_YES_DATA_MEMBER)427 self& def_readwrite_impl(
428 char const* name, D B::*pm_, char const* doc BOOST_PYTHON_YES_DATA_MEMBER)
429 {
430 return this->add_property(name, pm_, pm_, doc);
431 }
432
433 template <class D>
def_readonly_impl(char const * name,D & d,char const * BOOST_PYTHON_NO_DATA_MEMBER)434 self& def_readonly_impl(
435 char const* name, D& d, char const* BOOST_PYTHON_NO_DATA_MEMBER)
436 {
437 return this->add_static_property(name, python::make_getter(d));
438 }
439
440 template <class D>
def_readwrite_impl(char const * name,D & d,char const * BOOST_PYTHON_NO_DATA_MEMBER)441 self& def_readwrite_impl(
442 char const* name, D& d, char const* BOOST_PYTHON_NO_DATA_MEMBER)
443 {
444 return this->add_static_property(name, python::make_getter(d), python::make_setter(d));
445 }
446
447 template <class DefVisitor>
initialize(DefVisitor const & i)448 inline void initialize(DefVisitor const& i)
449 {
450 metadata::register_(); // set up runtime metadata/conversions
451
452 typedef typename metadata::holder holder;
453 this->set_instance_size( objects::additional_instance_size<holder>::value );
454
455 this->def(i);
456 }
457
initialize(no_init_t)458 inline void initialize(no_init_t)
459 {
460 metadata::register_(); // set up runtime metadata/conversions
461 this->def_no_init();
462 }
463
464 //
465 // These two overloads discriminate between def() as applied to a
466 // generic visitor and everything else.
467 //
468 // @group def_impl {
469 template <class T, class Helper, class LeafVisitor, class Visitor>
def_impl(T *,char const * name,LeafVisitor,Helper const & helper,def_visitor<Visitor> const * v)470 inline void def_impl(
471 T*
472 , char const* name
473 , LeafVisitor
474 , Helper const& helper
475 , def_visitor<Visitor> const* v
476 )
477 {
478 v->visit(*this, name, helper);
479 }
480
481 template <class T, class Fn, class Helper>
def_impl(T *,char const * name,Fn fn,Helper const & helper,...)482 inline void def_impl(
483 T*
484 , char const* name
485 , Fn fn
486 , Helper const& helper
487 , ...
488 )
489 {
490 objects::add_to_namespace(
491 *this
492 , name
493 , make_function(
494 fn
495 , helper.policies()
496 , helper.keywords()
497 , detail::get_signature(fn, (T*)0)
498 )
499 , helper.doc()
500 );
501
502 this->def_default(name, fn, helper, mpl::bool_<Helper::has_default_implementation>());
503 }
504 // }
505
506 //
507 // These two overloads handle the definition of default
508 // implementation overloads for virtual functions. The second one
509 // handles the case where no default implementation was specified.
510 //
511 // @group def_default {
512 template <class Fn, class Helper>
def_default(char const * name,Fn,Helper const & helper,mpl::bool_<true>)513 inline void def_default(
514 char const* name
515 , Fn
516 , Helper const& helper
517 , mpl::bool_<true>)
518 {
519 detail::error::virtual_function_default<W,Fn>::must_be_derived_class_member(
520 helper.default_implementation());
521
522 objects::add_to_namespace(
523 *this, name,
524 make_function(
525 helper.default_implementation(), helper.policies(), helper.keywords())
526 );
527 }
528
529 template <class Fn, class Helper>
def_default(char const *,Fn,Helper const &,mpl::bool_<false>)530 inline void def_default(char const*, Fn, Helper const&, mpl::bool_<false>)
531 { }
532 // }
533
534 //
535 // These two overloads discriminate between def() as applied to
536 // regular functions and def() as applied to the result of
537 // BOOST_PYTHON_FUNCTION_OVERLOADS(). The final argument is used to
538 // discriminate.
539 //
540 // @group def_maybe_overloads {
541 template <class OverloadsT, class SigT>
def_maybe_overloads(char const * name,SigT sig,OverloadsT const & overloads,detail::overloads_base const *)542 void def_maybe_overloads(
543 char const* name
544 , SigT sig
545 , OverloadsT const& overloads
546 , detail::overloads_base const*)
547
548 {
549 // convert sig to a type_list (see detail::get_signature in signature.hpp)
550 // before calling detail::define_with_defaults.
551 detail::define_with_defaults(
552 name, overloads, *this, detail::get_signature(sig));
553 }
554
555 template <class Fn, class A1>
def_maybe_overloads(char const * name,Fn fn,A1 const & a1,...)556 void def_maybe_overloads(
557 char const* name
558 , Fn fn
559 , A1 const& a1
560 , ...)
561 {
562 this->def_impl(
563 detail::unwrap_wrapper((W*)0)
564 , name
565 , fn
566 , detail::def_helper<A1>(a1)
567 , &fn
568 );
569
570 }
571 // }
572 };
573
574
575 //
576 // implementations
577 //
578
579 template <class W, class X1, class X2, class X3>
class_(char const * name,char const * doc)580 inline class_<W,X1,X2,X3>::class_(char const* name, char const* doc)
581 : base(name, id_vector::size, id_vector().ids, doc)
582 {
583 this->initialize(init<>());
584 // select_holder::assert_default_constructible();
585 }
586
587 template <class W, class X1, class X2, class X3>
class_(char const * name,no_init_t)588 inline class_<W,X1,X2,X3>::class_(char const* name, no_init_t)
589 : base(name, id_vector::size, id_vector().ids)
590 {
591 this->initialize(no_init);
592 }
593
594 template <class W, class X1, class X2, class X3>
class_(char const * name,char const * doc,no_init_t)595 inline class_<W,X1,X2,X3>::class_(char const* name, char const* doc, no_init_t)
596 : base(name, id_vector::size, id_vector().ids, doc)
597 {
598 this->initialize(no_init);
599 }
600
601 }} // namespace boost::python
602
603 # undef BOOST_PYTHON_DATA_MEMBER_HELPER
604 # undef BOOST_PYTHON_YES_DATA_MEMBER
605 # undef BOOST_PYTHON_NO_DATA_MEMBER
606 # undef BOOST_PYTHON_NO_MEMBER_POINTER_ORDERING
607
608 #endif // CLASS_DWA200216_HPP
609