class.hpp 19 KB

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