python.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. // Copyright Daniel Wallin 2006. Use, modification and distribution is
  2. // subject to the Boost Software License, Version 1.0. (See accompanying
  3. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #ifndef BOOST_PARAMETER_PYTHON_060209_HPP
  5. # define BOOST_PARAMETER_PYTHON_060209_HPP
  6. # include <boost/mpl/vector.hpp>
  7. # include <boost/mpl/fold.hpp>
  8. # include <boost/mpl/prior.hpp>
  9. # include <boost/mpl/shift_right.hpp>
  10. # include <boost/mpl/shift_left.hpp>
  11. # include <boost/mpl/bitand.hpp>
  12. # include <boost/mpl/pair.hpp>
  13. # include <boost/mpl/size.hpp>
  14. # include <boost/mpl/push_back.hpp>
  15. # include <boost/mpl/or.hpp>
  16. # include <boost/mpl/count_if.hpp>
  17. # include <boost/mpl/transform.hpp>
  18. # include <boost/mpl/front.hpp>
  19. # include <boost/mpl/iterator_range.hpp>
  20. # include <boost/mpl/next.hpp>
  21. # include <boost/mpl/begin_end.hpp>
  22. # include <boost/mpl/not.hpp>
  23. # include <boost/mpl/empty.hpp>
  24. # include <boost/python/def.hpp>
  25. # include <boost/python/make_constructor.hpp>
  26. # include <boost/python/init.hpp>
  27. # include <boost/python/to_python_converter.hpp>
  28. # include <boost/parameter/aux_/maybe.hpp>
  29. # include <boost/parameter/aux_/python/invoker.hpp>
  30. namespace boost { namespace parameter { namespace python
  31. {
  32. namespace python_ = boost::python;
  33. }}}
  34. namespace boost { namespace parameter { namespace python { namespace aux
  35. {
  36. inline PyObject* unspecified_type()
  37. {
  38. static PyTypeObject unspecified = {
  39. PyVarObject_HEAD_INIT(NULL,0)
  40. "Boost.Parameter.Unspecified", /* tp_name */
  41. PyType_Type.tp_basicsize, /* tp_basicsize */
  42. 0, /* tp_itemsize */
  43. 0, /* tp_dealloc */
  44. 0, /* tp_print */
  45. 0, /* tp_getattr */
  46. 0, /* tp_setattr */
  47. 0, /* tp_compare */
  48. 0, /* tp_repr */
  49. 0, /* tp_as_number */
  50. 0, /* tp_as_sequence */
  51. 0, /* tp_as_mapping */
  52. 0, /* tp_hash */
  53. 0, /* tp_call */
  54. 0, /* tp_str */
  55. 0, /* tp_getattro */
  56. 0, /* tp_setattro */
  57. 0, /* tp_as_buffer */
  58. Py_TPFLAGS_DEFAULT, /* tp_flags */
  59. 0, /* tp_doc */
  60. };
  61. if (Py_TYPE(&unspecified) == 0)
  62. {
  63. Py_TYPE(&unspecified) = &PyType_Type;
  64. PyType_Ready(&unspecified);
  65. }
  66. return (PyObject*)&unspecified;
  67. }
  68. struct empty_tag {};
  69. struct empty_tag_to_python
  70. {
  71. static PyObject* convert(empty_tag)
  72. {
  73. return python_::xincref(unspecified_type());
  74. }
  75. };
  76. }}}} // namespace boost::parameter::python::aux
  77. namespace boost { namespace python
  78. {
  79. // Converts a Python value to a maybe<T>
  80. template <class T>
  81. struct arg_from_python<parameter::aux::maybe<T> >
  82. : arg_from_python<T>
  83. {
  84. arg_from_python(PyObject* p)
  85. : arg_from_python<T>(p)
  86. , empty(parameter::python::aux::unspecified_type() == p)
  87. {}
  88. bool convertible() const
  89. {
  90. return empty || arg_from_python<T>::convertible();
  91. }
  92. parameter::aux::maybe<T> operator()()
  93. {
  94. if (empty)
  95. {
  96. return parameter::aux::maybe<T>();
  97. }
  98. else
  99. {
  100. return parameter::aux::maybe<T>(
  101. arg_from_python<T>::operator()()
  102. );
  103. }
  104. }
  105. bool empty;
  106. };
  107. }} // namespace boost::python
  108. namespace boost { namespace parameter { namespace python {
  109. namespace aux
  110. {
  111. template <class K>
  112. struct is_optional
  113. : mpl::not_<
  114. mpl::or_<typename K::required, typename K::optimized_default>
  115. >
  116. {};
  117. template <class K, class Required, class Optimized, class T>
  118. struct arg_spec
  119. {
  120. typedef K keyword;
  121. typedef Required required;
  122. typedef T type;
  123. typedef Optimized optimized_default;
  124. };
  125. template <class K, class T, class Optimized = mpl::false_>
  126. struct make_arg_spec_impl
  127. {
  128. typedef arg_spec<
  129. typename K::first, typename K::second, Optimized, T
  130. > type;
  131. };
  132. template <class K, class T>
  133. struct make_arg_spec_impl<K, T, typename K::third>
  134. {
  135. typedef arg_spec<
  136. typename K::first, typename K::second, typename K::third, T
  137. > type;
  138. };
  139. template <class K, class T>
  140. struct make_arg_spec
  141. : make_arg_spec_impl<K, T>
  142. {
  143. };
  144. template <class Spec, class State>
  145. struct combinations_op
  146. {
  147. typedef typename State::second bits;
  148. typedef typename State::first result0;
  149. typedef typename mpl::if_<
  150. mpl::or_<
  151. typename Spec::required
  152. , typename Spec::optimized_default
  153. , mpl::bitand_<bits, mpl::long_<1> >
  154. >
  155. , typename mpl::push_back<result0, Spec>::type
  156. , result0
  157. >::type result;
  158. typedef typename mpl::if_<
  159. mpl::or_<
  160. typename Spec::required
  161. , typename Spec::optimized_default
  162. >
  163. , bits
  164. , typename mpl::shift_right<bits, mpl::long_<1> >::type
  165. >::type next_bits;
  166. typedef mpl::pair<
  167. result
  168. , next_bits
  169. > type;
  170. };
  171. // Used as start value in the recursive arg() composition below.
  172. struct no_keywords
  173. {
  174. template <class T>
  175. T const& operator,(T const& x) const
  176. {
  177. return x;
  178. }
  179. };
  180. template <class Def, class F, class Iter, class End, class Keywords>
  181. void def_combination_aux0(
  182. Def def, F f, Iter, End, Keywords const& keywords, mpl::false_)
  183. {
  184. typedef typename mpl::deref<Iter>::type spec;
  185. typedef typename spec::keyword kw;
  186. def_combination_aux(
  187. def, f, typename mpl::next<Iter>::type(), End()
  188. , (
  189. keywords, boost::python::arg(kw::keyword_name())
  190. )
  191. );
  192. }
  193. template <class Def, class F, class Iter, class End, class Keywords>
  194. void def_combination_aux0(
  195. Def def, F f, Iter, End, Keywords const& keywords, mpl::true_)
  196. {
  197. typedef typename mpl::deref<Iter>::type spec;
  198. typedef typename spec::keyword kw;
  199. def_combination_aux(
  200. def, f, typename mpl::next<Iter>::type(), End()
  201. , (
  202. keywords, boost::python::arg(kw::keyword_name()) = empty_tag()
  203. )
  204. );
  205. }
  206. inline void initialize_converter()
  207. {
  208. static python_::to_python_converter<empty_tag, empty_tag_to_python> x;
  209. }
  210. template <class Def, class F, class Iter, class End, class Keywords>
  211. void def_combination_aux(
  212. Def def, F f, Iter, End, Keywords const& keywords)
  213. {
  214. typedef typename mpl::deref<Iter>::type spec;
  215. typedef typename mpl::and_<
  216. typename spec::optimized_default
  217. , mpl::not_<typename spec::required>
  218. >::type optimized_default;
  219. def_combination_aux0(
  220. def, f, Iter(), End(), keywords, optimized_default()
  221. );
  222. }
  223. template <class Def, class F, class End, class Keywords>
  224. void def_combination_aux(
  225. Def def, F f, End, End, Keywords const& keywords)
  226. {
  227. def(f, keywords);
  228. }
  229. template <class Def, class F, class End>
  230. void def_combination_aux(
  231. Def def, F f, End, End, no_keywords const&)
  232. {
  233. def(f);
  234. }
  235. template <
  236. class Def, class Specs, class Bits, class Invoker
  237. >
  238. void def_combination(
  239. Def def, Specs*, Bits, Invoker*)
  240. {
  241. typedef typename mpl::fold<
  242. Specs
  243. , mpl::pair<mpl::vector0<>, Bits>
  244. , combinations_op<mpl::_2, mpl::_1>
  245. >::type combination0;
  246. typedef typename combination0::first combination;
  247. typedef typename mpl::apply_wrap1<
  248. Invoker, combination
  249. >::type invoker;
  250. def_combination_aux(
  251. def
  252. , &invoker::execute
  253. , typename mpl::begin<combination>::type()
  254. , typename mpl::end<combination>::type()
  255. , no_keywords()
  256. );
  257. }
  258. template <
  259. class Def, class Specs, class Bits, class End, class Invoker
  260. >
  261. void def_combinations(
  262. Def def, Specs*, Bits, End, Invoker*)
  263. {
  264. initialize_converter();
  265. def_combination(def, (Specs*)0, Bits(), (Invoker*)0);
  266. def_combinations(
  267. def
  268. , (Specs*)0
  269. , mpl::long_<Bits::value + 1>()
  270. , End()
  271. , (Invoker*)0
  272. );
  273. }
  274. template <
  275. class Def, class Specs, class End, class Invoker
  276. >
  277. void def_combinations(
  278. Def, Specs*, End, End, Invoker*)
  279. {}
  280. struct not_specified {};
  281. template <class CallPolicies>
  282. struct call_policies_as_options
  283. {
  284. call_policies_as_options(CallPolicies const& call_policies)
  285. : call_policies(call_policies)
  286. {}
  287. CallPolicies const& policies() const
  288. {
  289. return call_policies;
  290. }
  291. char const* doc() const
  292. {
  293. return 0;
  294. }
  295. CallPolicies call_policies;
  296. };
  297. template <class Class, class Options = not_specified>
  298. struct def_class
  299. {
  300. def_class(Class& cl, char const* name, Options options = Options())
  301. : cl(cl)
  302. , name(name)
  303. , options(options)
  304. {}
  305. template <class F>
  306. void def(F f, not_specified const*) const
  307. {
  308. cl.def(name, f);
  309. }
  310. template <class F>
  311. void def(F f, void const*) const
  312. {
  313. cl.def(name, f, options.doc(), options.policies());
  314. }
  315. template <class F>
  316. void operator()(F f) const
  317. {
  318. this->def(f, &options);
  319. }
  320. template <class F, class Keywords>
  321. void def(F f, Keywords const& keywords, not_specified const*) const
  322. {
  323. cl.def(name, f, keywords);
  324. }
  325. template <class F, class Keywords>
  326. void def(F f, Keywords const& keywords, void const*) const
  327. {
  328. cl.def(name, f, keywords, options.doc(), options.policies());
  329. }
  330. template <class F, class Keywords>
  331. void operator()(F f, Keywords const& keywords) const
  332. {
  333. this->def(f, keywords, &options);
  334. }
  335. Class& cl;
  336. char const* name;
  337. Options options;
  338. };
  339. template <class Class, class CallPolicies = boost::python::default_call_policies>
  340. struct def_init
  341. {
  342. def_init(Class& cl, CallPolicies call_policies = CallPolicies())
  343. : cl(cl)
  344. , call_policies(call_policies)
  345. {}
  346. template <class F>
  347. void operator()(F f) const
  348. {
  349. cl.def(
  350. "__init__"
  351. , boost::python::make_constructor(f, call_policies)
  352. );
  353. }
  354. template <class F, class Keywords>
  355. void operator()(F f, Keywords const& keywords) const
  356. {
  357. cl.def(
  358. "__init__"
  359. , boost::python::make_constructor(f, call_policies, keywords)
  360. );
  361. }
  362. Class& cl;
  363. CallPolicies call_policies;
  364. };
  365. struct def_function
  366. {
  367. def_function(char const* name)
  368. : name(name)
  369. {}
  370. template <class F>
  371. void operator()(F f) const
  372. {
  373. boost::python::def(name, f);
  374. }
  375. template <class F, class Keywords>
  376. void operator()(F f, Keywords const& keywords) const
  377. {
  378. boost::python::def(name, f, keywords);
  379. }
  380. char const* name;
  381. };
  382. } // namespace aux
  383. template <class M, class Signature>
  384. void def(char const* name, Signature)
  385. {
  386. typedef mpl::iterator_range<
  387. typename mpl::next<
  388. typename mpl::begin<Signature>::type
  389. >::type
  390. , typename mpl::end<Signature>::type
  391. > arg_types;
  392. typedef typename mpl::transform<
  393. typename M::keywords
  394. , arg_types
  395. , aux::make_arg_spec<mpl::_1, mpl::_2>
  396. , mpl::back_inserter<mpl::vector0<> >
  397. >::type arg_specs;
  398. typedef typename mpl::count_if<
  399. arg_specs
  400. , aux::is_optional<mpl::_1>
  401. >::type optional_arity;
  402. typedef typename mpl::front<Signature>::type result_type;
  403. typedef typename mpl::shift_left<mpl::long_<1>, optional_arity>::type upper;
  404. aux::def_combinations(
  405. aux::def_function(name)
  406. , (arg_specs*)0
  407. , mpl::long_<0>()
  408. , mpl::long_<upper::value>()
  409. , (aux::make_invoker<M, result_type>*)0
  410. );
  411. }
  412. template <class M, class Class, class Signature>
  413. void def(Class& cl, char const* name, Signature)
  414. {
  415. typedef mpl::iterator_range<
  416. typename mpl::next<
  417. typename mpl::begin<Signature>::type
  418. >::type
  419. , typename mpl::end<Signature>::type
  420. > arg_types;
  421. typedef typename mpl::transform<
  422. typename M::keywords
  423. , arg_types
  424. , aux::make_arg_spec<mpl::_1, mpl::_2>
  425. , mpl::back_inserter<mpl::vector0<> >
  426. >::type arg_specs;
  427. typedef typename mpl::count_if<
  428. arg_specs
  429. , aux::is_optional<mpl::_1>
  430. >::type optional_arity;
  431. typedef typename mpl::front<Signature>::type result_type;
  432. typedef typename mpl::shift_left<mpl::long_<1>, optional_arity>::type upper;
  433. aux::def_combinations(
  434. aux::def_class<Class>(cl, name)
  435. , (arg_specs*)0
  436. , mpl::long_<0>()
  437. , mpl::long_<upper::value>()
  438. , (aux::make_invoker<M, result_type>*)0
  439. );
  440. }
  441. namespace aux
  442. {
  443. template <class K>
  444. struct keyword
  445. {
  446. typedef K type;
  447. };
  448. template <class K>
  449. struct keyword<K*>
  450. {
  451. typedef K type;
  452. };
  453. template <class K>
  454. struct keyword<K**>
  455. {
  456. typedef K type;
  457. };
  458. template <class K>
  459. struct required
  460. {
  461. typedef mpl::true_ type;
  462. };
  463. template <class K>
  464. struct required<K*>
  465. {
  466. typedef mpl::false_ type;
  467. };
  468. template <class K>
  469. struct optimized
  470. {
  471. typedef mpl::true_ type;
  472. };
  473. template <class K>
  474. struct optimized<K**>
  475. {
  476. typedef mpl::false_ type;
  477. };
  478. template <class T>
  479. struct make_kw_spec;
  480. template <class K, class T>
  481. struct make_kw_spec<K(T)>
  482. {
  483. typedef arg_spec<
  484. typename keyword<K>::type
  485. , typename required<K>::type
  486. , typename optimized<K>::type
  487. , T
  488. > type;
  489. };
  490. } // namespace aux
  491. template <class ParameterSpecs, class CallPolicies = boost::python::default_call_policies>
  492. struct init
  493. : boost::python::def_visitor<init<ParameterSpecs, CallPolicies> >
  494. {
  495. init(CallPolicies call_policies = CallPolicies())
  496. : call_policies(call_policies)
  497. {}
  498. template <class CallPolicies1>
  499. init<ParameterSpecs, CallPolicies1>
  500. operator[](CallPolicies1 const& call_policies) const
  501. {
  502. return init<ParameterSpecs, CallPolicies1>(call_policies);
  503. }
  504. template <class Class>
  505. void visit_aux(Class& cl, mpl::true_) const
  506. {
  507. cl.def(boost::python::init<>()[call_policies]);
  508. }
  509. template <class Class>
  510. void visit_aux(Class& cl, mpl::false_) const
  511. {
  512. typedef typename mpl::transform<
  513. ParameterSpecs
  514. , aux::make_kw_spec<mpl::_>
  515. , mpl::back_inserter<mpl::vector0<> >
  516. >::type arg_specs;
  517. typedef typename mpl::count_if<
  518. arg_specs
  519. , aux::is_optional<mpl::_>
  520. >::type optional_arity;
  521. typedef typename mpl::shift_left<mpl::long_<1>, optional_arity>::type upper;
  522. aux::def_combinations(
  523. aux::def_init<Class, CallPolicies>(cl, call_policies)
  524. , (arg_specs*)0
  525. , mpl::long_<0>()
  526. , mpl::long_<upper::value>()
  527. , (aux::make_init_invoker<typename Class::wrapped_type>*)0
  528. );
  529. }
  530. template <class Class>
  531. void visit(Class& cl) const
  532. {
  533. visit_aux(cl, mpl::empty<ParameterSpecs>());
  534. }
  535. CallPolicies call_policies;
  536. };
  537. template <class ParameterSpecs, class CallPolicies = boost::python::default_call_policies>
  538. struct call
  539. : boost::python::def_visitor<call<ParameterSpecs, CallPolicies> >
  540. {
  541. call(CallPolicies const& call_policies = CallPolicies())
  542. : call_policies(call_policies)
  543. {}
  544. template <class CallPolicies1>
  545. call<ParameterSpecs, CallPolicies1>
  546. operator[](CallPolicies1 const& call_policies) const
  547. {
  548. return call<ParameterSpecs, CallPolicies1>(call_policies);
  549. }
  550. template <class Class>
  551. void visit(Class& cl) const
  552. {
  553. typedef mpl::iterator_range<
  554. typename mpl::next<
  555. typename mpl::begin<ParameterSpecs>::type
  556. >::type
  557. , typename mpl::end<ParameterSpecs>::type
  558. > arg_types;
  559. typedef typename mpl::front<ParameterSpecs>::type result_type;
  560. typedef typename mpl::transform<
  561. arg_types
  562. , aux::make_kw_spec<mpl::_>
  563. , mpl::back_inserter<mpl::vector0<> >
  564. >::type arg_specs;
  565. typedef typename mpl::count_if<
  566. arg_specs
  567. , aux::is_optional<mpl::_>
  568. >::type optional_arity;
  569. typedef typename mpl::shift_left<mpl::long_<1>, optional_arity>::type upper;
  570. typedef aux::call_policies_as_options<CallPolicies> options;
  571. aux::def_combinations(
  572. aux::def_class<Class, options>(cl, "__call__", options(call_policies))
  573. , (arg_specs*)0
  574. , mpl::long_<0>()
  575. , mpl::long_<upper::value>()
  576. , (aux::make_call_invoker<typename Class::wrapped_type, result_type>*)0
  577. );
  578. }
  579. CallPolicies call_policies;
  580. };
  581. template <class Fwd, class ParameterSpecs>
  582. struct function
  583. : boost::python::def_visitor<function<Fwd, ParameterSpecs> >
  584. {
  585. template <class Class, class Options>
  586. void visit(Class& cl, char const* name, Options const& options) const
  587. {
  588. typedef mpl::iterator_range<
  589. typename mpl::next<
  590. typename mpl::begin<ParameterSpecs>::type
  591. >::type
  592. , typename mpl::end<ParameterSpecs>::type
  593. > arg_types;
  594. typedef typename mpl::front<ParameterSpecs>::type result_type;
  595. typedef typename mpl::transform<
  596. arg_types
  597. , aux::make_kw_spec<mpl::_>
  598. , mpl::back_inserter<mpl::vector0<> >
  599. >::type arg_specs;
  600. typedef typename mpl::count_if<
  601. arg_specs
  602. , aux::is_optional<mpl::_>
  603. >::type optional_arity;
  604. typedef typename mpl::shift_left<mpl::long_<1>, optional_arity>::type upper;
  605. aux::def_combinations(
  606. aux::def_class<Class, Options>(cl, name, options)
  607. , (arg_specs*)0
  608. , mpl::long_<0>()
  609. , mpl::long_<upper::value>()
  610. , (aux::make_member_invoker<
  611. Fwd, result_type, typename Class::wrapped_type
  612. >*)0
  613. );
  614. }
  615. };
  616. }}} // namespace boost::parameter::python
  617. #endif // BOOST_PARAMETER_PYTHON_060209_HPP