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