• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
6 #include <boost/python/def.hpp>
7 #include <boost/python/module.hpp>
8 #include <boost/python/class.hpp>
9 #include <boost/python/tuple.hpp>
10 #include <boost/python/list.hpp>
11 #include <boost/python/overloads.hpp>
12 #include <boost/python/return_internal_reference.hpp>
13 
14 #if defined(_AIX) && defined(__EDG_VERSION__) && __EDG_VERSION__ < 245
15 # include <iostream> // works around a KCC intermediate code generation bug
16 #endif
17 
18 using namespace boost::python;
19 namespace bpl = boost::python;
20 
21 char const* const format = "int(%s); char(%s); string(%s); double(%s); ";
22 
23 ///////////////////////////////////////////////////////////////////////////////
24 //
25 //  Overloaded functions
26 //
27 ///////////////////////////////////////////////////////////////////////////////
28 object
bar(int a,char b,std::string c,double d)29 bar(int a, char b, std::string c, double d)
30 {
31     return format % bpl::make_tuple(a, b, c, d);
32 }
33 
34 object
bar(int a,char b,std::string c)35 bar(int a, char b, std::string c)
36 {
37     return format % bpl::make_tuple(a, b, c, 0.0);
38 }
39 
40 object
bar(int a,char b)41 bar(int a, char b)
42 {
43     return format % bpl::make_tuple(a, b, "default", 0.0);
44 }
45 
46 object
bar(int a)47 bar(int a)
48 {
49     return format % bpl::make_tuple(a, 'D', "default", 0.0);
50 }
51 
52 BOOST_PYTHON_FUNCTION_OVERLOADS(bar_stubs, bar, 1, 4)
53 
54 ///////////////////////////////////////////////////////////////////////////////
55 //
56 //  Functions with default arguments
57 //
58 ///////////////////////////////////////////////////////////////////////////////
59 object
foo(int a,char b='D',std::string c="default",double d=0.0)60 foo(int a, char b = 'D', std::string c = "default", double d = 0.0)
61 {
62     return format % bpl::make_tuple(a, b, c, d);
63 }
64 
65 BOOST_PYTHON_FUNCTION_OVERLOADS(foo_stubs, foo, 1, 4)
66 
67 ///////////////////////////////////////////////////////////////////////////////
68 //
69 //  Overloaded member functions with default arguments
70 //
71 ///////////////////////////////////////////////////////////////////////////////
72 struct Y {
73 
YY74     Y() {}
75 
76     object
get_stateY77     get_state() const
78     {
79         return format % bpl::make_tuple(a, b, c, d);
80     }
81 
82     int a; char b; std::string c; double d;
83 };
84 
85 
86 struct X {
87 
XX88     X() {}
89 
XX90     X(int a, char b = 'D', std::string c = "constructor", double d = 0.0)
91     : state(format % bpl::make_tuple(a, b, c, d))
92     {}
93 
XX94     X(std::string s, bool b)
95     : state("Got exactly two arguments from constructor: string(%s); bool(%s); " % bpl::make_tuple(s, b*1))
96     {}
97 
98     object
barX99     bar(int a, char b = 'D', std::string c = "default", double d = 0.0) const
100     {
101         return format % bpl::make_tuple(a, b, c, d);
102     }
103 
104     Y const&
bar2X105     bar2(int a = 0, char b = 'D', std::string c = "default", double d = 0.0)
106     {
107         // tests zero arg member function and return_internal_reference policy
108         y.a = a;
109         y.b = b;
110         y.c = c;
111         y.d = d;
112         return y;
113     }
114 
115     object
fooX116     foo(int a, bool b=false) const
117     {
118         return "int(%s); bool(%s); " % bpl::make_tuple(a, b*1);
119     }
120 
121     object
fooX122     foo(std::string a, bool b=false) const
123     {
124         return "string(%s); bool(%s); " % bpl::make_tuple(a, b*1);
125     }
126 
127     object
fooX128     foo(list a, list b, bool c=false) const
129     {
130         return "list(%s); list(%s); bool(%s); " % bpl::make_tuple(a, b, c*1);
131     }
132 
133     object
get_stateX134     get_state() const
135     {
136         return state;
137     }
138 
139     Y y;
140     object state;
141 };
142 
143 BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_bar_stubs, bar, 1, 4)
144 BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_bar_stubs2, bar2, 0, 4)
145 BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_foo_2_stubs, foo, 1, 2)
146 BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_foo_3_stubs, foo, 2, 3)
147 
148 ///////////////////////////////////////////////////////////////////////////////
149 
BOOST_PYTHON_MODULE(defaults_ext)150 BOOST_PYTHON_MODULE(defaults_ext)
151 {
152     def("foo", foo, foo_stubs());
153     def("bar", (object(*)(int, char, std::string, double))0, bar_stubs());
154 
155     class_<Y>("Y", init<>("doc of Y init")) // this should work
156         .def("get_state", &Y::get_state)
157         ;
158 
159     class_<X>("X",no_init)
160 
161         .def(init<optional<int, char, std::string, double> >("doc of init", args("self", "a", "b", "c", "d")))
162         .def(init<std::string, bool>(args("self", "s", "b"))[default_call_policies()]) // what's a good policy here?
163         .def("get_state", &X::get_state)
164         .def("bar", &X::bar, X_bar_stubs())
165         .def("bar2", &X::bar2, X_bar_stubs2("doc of X::bar2")[return_internal_reference<>()])
166         .def("foo", (object(X::*)(std::string, bool) const)0, X_foo_2_stubs())
167         .def("foo", (object(X::*)(int, bool) const)0, X_foo_2_stubs())
168         .def("foo", (object(X::*)(list, list, bool) const)0, X_foo_3_stubs())
169         ;
170 }
171 
172 #include "module_tail.cpp"
173 
174