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