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)
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>
14 #if defined(_AIX) && defined(__EDG_VERSION__) && __EDG_VERSION__ < 245
15 # include <iostream> // works around a KCC intermediate code generation bug
18 using namespace boost::python
;
19 namespace bpl
= boost::python
;
21 char const* const format
= "int(%s); char(%s); string(%s); double(%s); ";
23 ///////////////////////////////////////////////////////////////////////////////
25 // Overloaded functions
27 ///////////////////////////////////////////////////////////////////////////////
29 bar(int a
, char b
, std::string c
, double d
)
31 return format
% bpl::make_tuple(a
, b
, c
, d
);
35 bar(int a
, char b
, std::string c
)
37 return format
% bpl::make_tuple(a
, b
, c
, 0.0);
43 return format
% bpl::make_tuple(a
, b
, "default", 0.0);
49 return format
% bpl::make_tuple(a
, 'D', "default", 0.0);
52 BOOST_PYTHON_FUNCTION_OVERLOADS(bar_stubs
, bar
, 1, 4)
54 ///////////////////////////////////////////////////////////////////////////////
56 // Functions with default arguments
58 ///////////////////////////////////////////////////////////////////////////////
60 foo(int a
, char b
= 'D', std::string c
= "default", double d
= 0.0)
62 return format
% bpl::make_tuple(a
, b
, c
, d
);
65 BOOST_PYTHON_FUNCTION_OVERLOADS(foo_stubs
, foo
, 1, 4)
67 ///////////////////////////////////////////////////////////////////////////////
69 // Overloaded member functions with default arguments
71 ///////////////////////////////////////////////////////////////////////////////
79 return format
% bpl::make_tuple(a
, b
, c
, d
);
82 int a
; char b
; std::string c
; double d
;
90 X(int a
, char b
= 'D', std::string c
= "constructor", double d
= 0.0)
91 : state(format
% bpl::make_tuple(a
, b
, c
, d
))
94 X(std::string s
, bool b
)
95 : state("Got exactly two arguments from constructor: string(%s); bool(%s); " % bpl::make_tuple(s
, b
*1))
99 bar(int a
, char b
= 'D', std::string c
= "default", double d
= 0.0) const
101 return format
% bpl::make_tuple(a
, b
, c
, d
);
105 bar2(int a
= 0, char b
= 'D', std::string c
= "default", double d
= 0.0)
107 // tests zero arg member function and return_internal_reference policy
116 foo(int a
, bool b
=false) const
118 return "int(%s); bool(%s); " % bpl::make_tuple(a
, b
*1);
122 foo(std::string a
, bool b
=false) const
124 return "string(%s); bool(%s); " % bpl::make_tuple(a
, b
*1);
128 foo(list a
, list b
, bool c
=false) const
130 return "list(%s); list(%s); bool(%s); " % bpl::make_tuple(a
, b
, c
*1);
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)
148 ///////////////////////////////////////////////////////////////////////////////
150 BOOST_PYTHON_MODULE(defaults_ext
)
152 def("foo", foo
, foo_stubs());
153 def("bar", (object(*)(int, char, std::string
, double))0, bar_stubs());
155 class_
<Y
>("Y", init
<>("doc of Y init")) // this should work
156 .def("get_state", &Y::get_state
)
159 class_
<X
>("X",no_init
)
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())
172 #include "module_tail.cpp"