class.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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 this->make_fn_impl(
  315. detail::unwrap_wrapper((W*)0)
  316. , f, is_obj_or_proxy(), (char*)0, detail::is_data_member_pointer<F>()
  317. );
  318. }
  319. template <class F>
  320. object make_setter(F f)
  321. {
  322. typedef typename api::is_object_operators<F>::type is_obj_or_proxy;
  323. return this->make_fn_impl(
  324. detail::unwrap_wrapper((W*)0)
  325. , f, is_obj_or_proxy(), (int*)0, detail::is_data_member_pointer<F>()
  326. );
  327. }
  328. template <class T, class F>
  329. object make_fn_impl(T*, F const& f, mpl::false_, void*, mpl::false_)
  330. {
  331. return python::make_function(f, default_call_policies(), detail::get_signature(f, (T*)0));
  332. }
  333. template <class T, class D, class B>
  334. object make_fn_impl(T*, D B::*pm_, mpl::false_, char*, mpl::true_)
  335. {
  336. D T::*pm = pm_;
  337. return python::make_getter(pm);
  338. }
  339. template <class T, class D, class B>
  340. object make_fn_impl(T*, D B::*pm_, mpl::false_, int*, mpl::true_)
  341. {
  342. D T::*pm = pm_;
  343. return python::make_setter(pm);
  344. }
  345. template <class T, class F>
  346. object make_fn_impl(T*, F const& x, mpl::true_, void*, mpl::false_)
  347. {
  348. return x;
  349. }
  350. // }
  351. template <class D, class B>
  352. self& def_readonly_impl(
  353. char const* name, D B::*pm_, char const* doc BOOST_PYTHON_YES_DATA_MEMBER)
  354. {
  355. return this->add_property(name, pm_, doc);
  356. }
  357. template <class D, class B>
  358. self& def_readwrite_impl(
  359. char const* name, D B::*pm_, char const* doc BOOST_PYTHON_YES_DATA_MEMBER)
  360. {
  361. return this->add_property(name, pm_, pm_, doc);
  362. }
  363. template <class D>
  364. self& def_readonly_impl(
  365. char const* name, D& d, char const* BOOST_PYTHON_NO_DATA_MEMBER)
  366. {
  367. return this->add_static_property(name, python::make_getter(d));
  368. }
  369. template <class D>
  370. self& def_readwrite_impl(
  371. char const* name, D& d, char const* BOOST_PYTHON_NO_DATA_MEMBER)
  372. {
  373. return this->add_static_property(name, python::make_getter(d), python::make_setter(d));
  374. }
  375. template <class DefVisitor>
  376. inline void initialize(DefVisitor const& i)
  377. {
  378. metadata::register_(); // set up runtime metadata/conversions
  379. typedef typename metadata::holder holder;
  380. this->set_instance_size( objects::additional_instance_size<holder>::value );
  381. this->def(i);
  382. }
  383. inline void initialize(no_init_t)
  384. {
  385. metadata::register_(); // set up runtime metadata/conversions
  386. this->def_no_init();
  387. }
  388. //
  389. // These two overloads discriminate between def() as applied to a
  390. // generic visitor and everything else.
  391. //
  392. // @group def_impl {
  393. template <class T, class Helper, class LeafVisitor, class Visitor>
  394. inline void def_impl(
  395. T*
  396. , char const* name
  397. , LeafVisitor
  398. , Helper const& helper
  399. , def_visitor<Visitor> const* v
  400. )
  401. {
  402. v->visit(*this, name, helper);
  403. }
  404. template <class T, class Fn, class Helper>
  405. inline void def_impl(
  406. T*
  407. , char const* name
  408. , Fn fn
  409. , Helper const& helper
  410. , ...
  411. )
  412. {
  413. objects::add_to_namespace(
  414. *this
  415. , name
  416. , make_function(
  417. fn
  418. , helper.policies()
  419. , helper.keywords()
  420. , detail::get_signature(fn, (T*)0)
  421. )
  422. , helper.doc()
  423. );
  424. this->def_default(name, fn, helper, mpl::bool_<Helper::has_default_implementation>());
  425. }
  426. // }
  427. //
  428. // These two overloads handle the definition of default
  429. // implementation overloads for virtual functions. The second one
  430. // handles the case where no default implementation was specified.
  431. //
  432. // @group def_default {
  433. template <class Fn, class Helper>
  434. inline void def_default(
  435. char const* name
  436. , Fn
  437. , Helper const& helper
  438. , mpl::bool_<true>)
  439. {
  440. detail::error::virtual_function_default<W,Fn>::must_be_derived_class_member(
  441. helper.default_implementation());
  442. objects::add_to_namespace(
  443. *this, name,
  444. make_function(
  445. helper.default_implementation(), helper.policies(), helper.keywords())
  446. );
  447. }
  448. template <class Fn, class Helper>
  449. inline void def_default(char const*, Fn, Helper const&, mpl::bool_<false>)
  450. { }
  451. // }
  452. //
  453. // These two overloads discriminate between def() as applied to
  454. // regular functions and def() as applied to the result of
  455. // BOOST_PYTHON_FUNCTION_OVERLOADS(). The final argument is used to
  456. // discriminate.
  457. //
  458. // @group def_maybe_overloads {
  459. template <class OverloadsT, class SigT>
  460. void def_maybe_overloads(
  461. char const* name
  462. , SigT sig
  463. , OverloadsT const& overloads
  464. , detail::overloads_base const*)
  465. {
  466. // convert sig to a type_list (see detail::get_signature in signature.hpp)
  467. // before calling detail::define_with_defaults.
  468. detail::define_with_defaults(
  469. name, overloads, *this, detail::get_signature(sig));
  470. }
  471. template <class Fn, class A1>
  472. void def_maybe_overloads(
  473. char const* name
  474. , Fn fn
  475. , A1 const& a1
  476. , ...)
  477. {
  478. this->def_impl(
  479. detail::unwrap_wrapper((W*)0)
  480. , name
  481. , fn
  482. , detail::def_helper<A1>(a1)
  483. , &fn
  484. );
  485. }
  486. // }
  487. };
  488. //
  489. // implementations
  490. //
  491. template <class W, class X1, class X2, class X3>
  492. inline class_<W,X1,X2,X3>::class_(char const* name, char const* doc)
  493. : base(name, id_vector::size, id_vector().ids, doc)
  494. {
  495. this->initialize(init<>());
  496. // select_holder::assert_default_constructible();
  497. }
  498. template <class W, class X1, class X2, class X3>
  499. inline class_<W,X1,X2,X3>::class_(char const* name, no_init_t)
  500. : base(name, id_vector::size, id_vector().ids)
  501. {
  502. this->initialize(no_init);
  503. }
  504. template <class W, class X1, class X2, class X3>
  505. inline class_<W,X1,X2,X3>::class_(char const* name, char const* doc, no_init_t)
  506. : base(name, id_vector::size, id_vector().ids, doc)
  507. {
  508. this->initialize(no_init);
  509. }
  510. }} // namespace boost::python
  511. # undef BOOST_PYTHON_DATA_MEMBER_HELPER
  512. # undef BOOST_PYTHON_YES_DATA_MEMBER
  513. # undef BOOST_PYTHON_NO_DATA_MEMBER
  514. # undef BOOST_PYTHON_NO_MEMBER_POINTER_ORDERING
  515. #endif // CLASS_DWA200216_HPP