class.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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. # define BOOST_PYTHON_NO_MEMBER_POINTER_ORDERING 1
  40. # endif
  41. # ifdef BOOST_PYTHON_NO_MEMBER_POINTER_ORDERING
  42. # include <boost/mpl/and.hpp>
  43. # include <boost/type_traits/is_member_pointer.hpp>
  44. # endif
  45. namespace boost { namespace python {
  46. template <class DerivedVisitor> class def_visitor;
  47. enum no_init_t { no_init };
  48. namespace detail
  49. {
  50. // This function object is used with mpl::for_each to write the id
  51. // of the type a pointer to which is passed as its 2nd compile-time
  52. // argument. into the iterator pointed to by its runtime argument
  53. struct write_type_id
  54. {
  55. write_type_id(type_info**p) : p(p) {}
  56. // Here's the runtime behavior
  57. template <class T>
  58. void operator()(T*) const
  59. {
  60. *(*p)++ = type_id<T>();
  61. }
  62. type_info** p;
  63. };
  64. template <class T>
  65. struct is_data_member_pointer
  66. : mpl::and_<
  67. is_member_pointer<T>
  68. , mpl::not_<is_member_function_pointer<T> >
  69. >
  70. {};
  71. # ifdef BOOST_PYTHON_NO_MEMBER_POINTER_ORDERING
  72. # define BOOST_PYTHON_DATA_MEMBER_HELPER(D) , detail::is_data_member_pointer<D>()
  73. # define BOOST_PYTHON_YES_DATA_MEMBER , mpl::true_
  74. # define BOOST_PYTHON_NO_DATA_MEMBER , mpl::false_
  75. # elif defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
  76. # define BOOST_PYTHON_DATA_MEMBER_HELPER(D) , 0
  77. # define BOOST_PYTHON_YES_DATA_MEMBER , int
  78. # define BOOST_PYTHON_NO_DATA_MEMBER , ...
  79. # else
  80. # define BOOST_PYTHON_DATA_MEMBER_HELPER(D)
  81. # define BOOST_PYTHON_YES_DATA_MEMBER
  82. # define BOOST_PYTHON_NO_DATA_MEMBER
  83. # endif
  84. namespace error
  85. {
  86. //
  87. // A meta-assertion mechanism which prints nice error messages and
  88. // backtraces on lots of compilers. Usage:
  89. //
  90. // assertion<C>::failed
  91. //
  92. // where C is an MPL metafunction class
  93. //
  94. template <class C> struct assertion_failed { };
  95. template <class C> struct assertion_ok { typedef C failed; };
  96. template <class C>
  97. struct assertion
  98. : mpl::if_<C, assertion_ok<C>, assertion_failed<C> >::type
  99. {};
  100. //
  101. // Checks for validity of arguments used to define virtual
  102. // functions with default implementations.
  103. //
  104. template <class Default>
  105. void not_a_derived_class_member(Default) {}
  106. template <class T, class Fn>
  107. struct virtual_function_default
  108. {
  109. template <class Default>
  110. static void
  111. must_be_derived_class_member(Default const&)
  112. {
  113. // https://svn.boost.org/trac/boost/ticket/5803
  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. template <class Get>
  252. self& add_property(char const* name, Get fget, char const* docstr = 0)
  253. {
  254. base::add_property(name, this->make_getter(fget), docstr);
  255. return *this;
  256. }
  257. template <class Get, class Set>
  258. self& add_property(char const* name, Get fget, Set fset, char const* docstr = 0)
  259. {
  260. base::add_property(
  261. name, this->make_getter(fget), this->make_setter(fset), docstr);
  262. return *this;
  263. }
  264. template <class Get>
  265. self& add_static_property(char const* name, Get fget)
  266. {
  267. base::add_static_property(name, object(fget));
  268. return *this;
  269. }
  270. template <class Get, class Set>
  271. self& add_static_property(char const* name, Get fget, Set fset)
  272. {
  273. base::add_static_property(name, object(fget), object(fset));
  274. return *this;
  275. }
  276. template <class U>
  277. self& setattr(char const* name, U const& x)
  278. {
  279. this->base::setattr(name, object(x));
  280. return *this;
  281. }
  282. // Pickle support
  283. template <typename PickleSuiteType>
  284. self& def_pickle(PickleSuiteType const& x)
  285. {
  286. error_messages::must_be_derived_from_pickle_suite(x);
  287. detail::pickle_suite_finalize<PickleSuiteType>::register_(
  288. *this,
  289. &PickleSuiteType::getinitargs,
  290. &PickleSuiteType::getstate,
  291. &PickleSuiteType::setstate,
  292. PickleSuiteType::getstate_manages_dict());
  293. return *this;
  294. }
  295. self& enable_pickling()
  296. {
  297. this->base::enable_pickling_(false);
  298. return *this;
  299. }
  300. self& staticmethod(char const* name)
  301. {
  302. this->make_method_static(name);
  303. return *this;
  304. }
  305. private: // helper functions
  306. // Builds a method for this class around the given [member]
  307. // function pointer or object, appropriately adjusting the type of
  308. // the first signature argument so that if f is a member of a
  309. // (possibly not wrapped) base class of T, an lvalue argument of
  310. // type T will be required.
  311. //
  312. // @group PropertyHelpers {
  313. template <class F>
  314. object make_getter(F f)
  315. {
  316. typedef typename api::is_object_operators<F>::type is_obj_or_proxy;
  317. return this->make_fn_impl(
  318. detail::unwrap_wrapper((W*)0)
  319. , f, is_obj_or_proxy(), (char*)0, detail::is_data_member_pointer<F>()
  320. );
  321. }
  322. template <class F>
  323. object make_setter(F f)
  324. {
  325. typedef typename api::is_object_operators<F>::type is_obj_or_proxy;
  326. return this->make_fn_impl(
  327. detail::unwrap_wrapper((W*)0)
  328. , f, is_obj_or_proxy(), (int*)0, detail::is_data_member_pointer<F>()
  329. );
  330. }
  331. template <class T, class F>
  332. object make_fn_impl(T*, F const& f, mpl::false_, void*, mpl::false_)
  333. {
  334. return python::make_function(f, default_call_policies(), detail::get_signature(f, (T*)0));
  335. }
  336. template <class T, class D, class B>
  337. object make_fn_impl(T*, D B::*pm_, mpl::false_, char*, mpl::true_)
  338. {
  339. D T::*pm = pm_;
  340. return python::make_getter(pm);
  341. }
  342. template <class T, class D, class B>
  343. object make_fn_impl(T*, D B::*pm_, mpl::false_, int*, mpl::true_)
  344. {
  345. D T::*pm = pm_;
  346. return python::make_setter(pm);
  347. }
  348. template <class T, class F>
  349. object make_fn_impl(T*, F const& x, mpl::true_, void*, mpl::false_)
  350. {
  351. return x;
  352. }
  353. // }
  354. template <class D, class B>
  355. self& def_readonly_impl(
  356. char const* name, D B::*pm_, char const* doc BOOST_PYTHON_YES_DATA_MEMBER)
  357. {
  358. return this->add_property(name, pm_, doc);
  359. }
  360. template <class D, class B>
  361. self& def_readwrite_impl(
  362. char const* name, D B::*pm_, char const* doc BOOST_PYTHON_YES_DATA_MEMBER)
  363. {
  364. return this->add_property(name, pm_, pm_, doc);
  365. }
  366. template <class D>
  367. self& def_readonly_impl(
  368. char const* name, D& d, char const* BOOST_PYTHON_NO_DATA_MEMBER)
  369. {
  370. return this->add_static_property(name, python::make_getter(d));
  371. }
  372. template <class D>
  373. self& def_readwrite_impl(
  374. char const* name, D& d, char const* BOOST_PYTHON_NO_DATA_MEMBER)
  375. {
  376. return this->add_static_property(name, python::make_getter(d), python::make_setter(d));
  377. }
  378. template <class DefVisitor>
  379. inline void initialize(DefVisitor const& i)
  380. {
  381. metadata::register_(); // set up runtime metadata/conversions
  382. typedef typename metadata::holder holder;
  383. this->set_instance_size( objects::additional_instance_size<holder>::value );
  384. this->def(i);
  385. }
  386. inline void initialize(no_init_t)
  387. {
  388. metadata::register_(); // set up runtime metadata/conversions
  389. this->def_no_init();
  390. }
  391. //
  392. // These two overloads discriminate between def() as applied to a
  393. // generic visitor and everything else.
  394. //
  395. // @group def_impl {
  396. template <class T, class Helper, class LeafVisitor, class Visitor>
  397. inline void def_impl(
  398. T*
  399. , char const* name
  400. , LeafVisitor
  401. , Helper const& helper
  402. , def_visitor<Visitor> const* v
  403. )
  404. {
  405. v->visit(*this, name, helper);
  406. }
  407. template <class T, class Fn, class Helper>
  408. inline void def_impl(
  409. T*
  410. , char const* name
  411. , Fn fn
  412. , Helper const& helper
  413. , ...
  414. )
  415. {
  416. objects::add_to_namespace(
  417. *this
  418. , name
  419. , make_function(
  420. fn
  421. , helper.policies()
  422. , helper.keywords()
  423. , detail::get_signature(fn, (T*)0)
  424. )
  425. , helper.doc()
  426. );
  427. this->def_default(name, fn, helper, mpl::bool_<Helper::has_default_implementation>());
  428. }
  429. // }
  430. //
  431. // These two overloads handle the definition of default
  432. // implementation overloads for virtual functions. The second one
  433. // handles the case where no default implementation was specified.
  434. //
  435. // @group def_default {
  436. template <class Fn, class Helper>
  437. inline void def_default(
  438. char const* name
  439. , Fn
  440. , Helper const& helper
  441. , mpl::bool_<true>)
  442. {
  443. detail::error::virtual_function_default<W,Fn>::must_be_derived_class_member(
  444. helper.default_implementation());
  445. objects::add_to_namespace(
  446. *this, name,
  447. make_function(
  448. helper.default_implementation(), helper.policies(), helper.keywords())
  449. );
  450. }
  451. template <class Fn, class Helper>
  452. inline void def_default(char const*, Fn, Helper const&, mpl::bool_<false>)
  453. { }
  454. // }
  455. //
  456. // These two overloads discriminate between def() as applied to
  457. // regular functions and def() as applied to the result of
  458. // BOOST_PYTHON_FUNCTION_OVERLOADS(). The final argument is used to
  459. // discriminate.
  460. //
  461. // @group def_maybe_overloads {
  462. template <class OverloadsT, class SigT>
  463. void def_maybe_overloads(
  464. char const* name
  465. , SigT sig
  466. , OverloadsT const& overloads
  467. , detail::overloads_base const*)
  468. {
  469. // convert sig to a type_list (see detail::get_signature in signature.hpp)
  470. // before calling detail::define_with_defaults.
  471. detail::define_with_defaults(
  472. name, overloads, *this, detail::get_signature(sig));
  473. }
  474. template <class Fn, class A1>
  475. void def_maybe_overloads(
  476. char const* name
  477. , Fn fn
  478. , A1 const& a1
  479. , ...)
  480. {
  481. this->def_impl(
  482. detail::unwrap_wrapper((W*)0)
  483. , name
  484. , fn
  485. , detail::def_helper<A1>(a1)
  486. , &fn
  487. );
  488. }
  489. // }
  490. };
  491. //
  492. // implementations
  493. //
  494. template <class W, class X1, class X2, class X3>
  495. inline class_<W,X1,X2,X3>::class_(char const* name, char const* doc)
  496. : base(name, id_vector::size, id_vector().ids, doc)
  497. {
  498. this->initialize(init<>());
  499. // select_holder::assert_default_constructible();
  500. }
  501. template <class W, class X1, class X2, class X3>
  502. inline class_<W,X1,X2,X3>::class_(char const* name, no_init_t)
  503. : base(name, id_vector::size, id_vector().ids)
  504. {
  505. this->initialize(no_init);
  506. }
  507. template <class W, class X1, class X2, class X3>
  508. inline class_<W,X1,X2,X3>::class_(char const* name, char const* doc, no_init_t)
  509. : base(name, id_vector::size, id_vector().ids, doc)
  510. {
  511. this->initialize(no_init);
  512. }
  513. }} // namespace boost::python
  514. # undef BOOST_PYTHON_DATA_MEMBER_HELPER
  515. # undef BOOST_PYTHON_YES_DATA_MEMBER
  516. # undef BOOST_PYTHON_NO_DATA_MEMBER
  517. # undef BOOST_PYTHON_NO_MEMBER_POINTER_ORDERING
  518. #endif // CLASS_DWA200216_HPP