python_test.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #include <math.h>
  5. #include <boost/python.hpp>
  6. #include <boost/parameter/preprocessor.hpp>
  7. #include <boost/parameter/keyword.hpp>
  8. #include <boost/parameter/python.hpp>
  9. #include <boost/utility/enable_if.hpp>
  10. namespace test {
  11. BOOST_PARAMETER_KEYWORD(tags, x)
  12. BOOST_PARAMETER_KEYWORD(tags, y)
  13. BOOST_PARAMETER_KEYWORD(tags, z)
  14. struct Xbase
  15. {
  16. // We need the disable_if part for VC7.1/8.0.
  17. template <class Args>
  18. Xbase(
  19. Args const& args
  20. , typename boost::disable_if<
  21. boost::is_base_and_derived<Xbase, Args>
  22. >::type* = 0
  23. )
  24. : value(std::string(args[x | "foo"]) + args[y | "bar"])
  25. {}
  26. std::string value;
  27. };
  28. struct X : Xbase
  29. {
  30. BOOST_PARAMETER_CONSTRUCTOR(X, (Xbase), tags,
  31. (optional
  32. (x, *)
  33. (y, *)
  34. )
  35. )
  36. BOOST_PARAMETER_BASIC_MEMBER_FUNCTION((int), f, tags,
  37. (required
  38. (x, *)
  39. (y, *)
  40. )
  41. (optional
  42. (z, *)
  43. )
  44. )
  45. {
  46. return args[x] + args[y] + args[z | 0];
  47. }
  48. BOOST_PARAMETER_BASIC_MEMBER_FUNCTION((std::string), g, tags,
  49. (optional
  50. (x, *)
  51. (y, *)
  52. )
  53. )
  54. {
  55. return std::string(args[x | "foo"]) + args[y | "bar"];
  56. }
  57. BOOST_PARAMETER_MEMBER_FUNCTION((X&), h, tags,
  58. (optional (x, *, "") (y, *, ""))
  59. )
  60. {
  61. return *this;
  62. }
  63. template <class A0>
  64. X& operator()(A0 const& a0)
  65. {
  66. return *this;
  67. }
  68. };
  69. } // namespace test
  70. struct f_fwd
  71. {
  72. template <class R, class T, class A0, class A1, class A2>
  73. R operator()(boost::type<R>, T& self, A0 const& a0, A1 const& a1, A2 const& a2)
  74. {
  75. return self.f(a0,a1,a2);
  76. }
  77. };
  78. struct g_fwd
  79. {
  80. template <class R, class T, class A0, class A1>
  81. R operator()(boost::type<R>, T& self, A0 const& a0, A1 const& a1)
  82. {
  83. return self.g(a0,a1);
  84. }
  85. };
  86. struct h_fwd
  87. {
  88. template <class R, class T>
  89. R operator()(boost::type<R>, T& self)
  90. {
  91. return self.h();
  92. }
  93. template <class R, class T, class A0>
  94. R operator()(boost::type<R>, T& self, A0 const& a0)
  95. {
  96. return self.h(a0);
  97. }
  98. template <class R, class T, class A0, class A1>
  99. R operator()(boost::type<R>, T& self, A0 const& a0, A1 const& a1)
  100. {
  101. return self.h(a0,a1);
  102. }
  103. };
  104. BOOST_PYTHON_MODULE(python_test_ext)
  105. {
  106. namespace mpl = boost::mpl;
  107. using namespace test;
  108. using namespace boost::python;
  109. class_<X>("X")
  110. .def(
  111. boost::parameter::python::init<
  112. mpl::vector<
  113. tags::x*(std::string), tags::y*(std::string)
  114. >
  115. >()
  116. )
  117. .def(
  118. "f"
  119. , boost::parameter::python::function<
  120. f_fwd
  121. , mpl::vector<
  122. int, tags::x(int), tags::y(int), tags::z*(int)
  123. >
  124. >()
  125. )
  126. .def(
  127. "g"
  128. , boost::parameter::python::function<
  129. g_fwd
  130. , mpl::vector<
  131. std::string, tags::x*(std::string), tags::y*(std::string)
  132. >
  133. >()
  134. )
  135. .def(
  136. "h"
  137. , boost::parameter::python::function<
  138. h_fwd
  139. , mpl::vector<
  140. X&, tags::x**(std::string), tags::y**(std::string)
  141. >
  142. >()
  143. , return_arg<>()
  144. )
  145. .def(
  146. boost::parameter::python::call<
  147. mpl::vector<
  148. X&, tags::x(int)
  149. >
  150. >() [ return_arg<>() ]
  151. )
  152. .def_readonly("value", &X::value);
  153. }