class.hpp 18 KB

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