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