[29] | 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 | |
---|
| 20 | #if BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x580)) |
---|
| 21 | # define make_tuple boost::python::make_tuple |
---|
| 22 | #endif |
---|
| 23 | |
---|
| 24 | char const* const format = "int(%s); char(%s); string(%s); double(%s); "; |
---|
| 25 | |
---|
| 26 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 27 | // |
---|
| 28 | // Overloaded functions |
---|
| 29 | // |
---|
| 30 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 31 | object |
---|
| 32 | bar(int a, char b, std::string c, double d) |
---|
| 33 | { |
---|
| 34 | return format % make_tuple(a, b, c, d); |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | object |
---|
| 38 | bar(int a, char b, std::string c) |
---|
| 39 | { |
---|
| 40 | return format % make_tuple(a, b, c, 0.0); |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | object |
---|
| 44 | bar(int a, char b) |
---|
| 45 | { |
---|
| 46 | return format % make_tuple(a, b, "default", 0.0); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | object |
---|
| 50 | bar(int a) |
---|
| 51 | { |
---|
| 52 | return format % make_tuple(a, 'D', "default", 0.0); |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | BOOST_PYTHON_FUNCTION_OVERLOADS(bar_stubs, bar, 1, 4) |
---|
| 56 | |
---|
| 57 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 58 | // |
---|
| 59 | // Functions with default arguments |
---|
| 60 | // |
---|
| 61 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 62 | object |
---|
| 63 | foo(int a, char b = 'D', std::string c = "default", double d = 0.0) |
---|
| 64 | { |
---|
| 65 | return format % make_tuple(a, b, c, d); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | BOOST_PYTHON_FUNCTION_OVERLOADS(foo_stubs, foo, 1, 4) |
---|
| 69 | |
---|
| 70 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 71 | // |
---|
| 72 | // Overloaded member functions with default arguments |
---|
| 73 | // |
---|
| 74 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 75 | struct Y { |
---|
| 76 | |
---|
| 77 | Y() {} |
---|
| 78 | |
---|
| 79 | object |
---|
| 80 | get_state() const |
---|
| 81 | { |
---|
| 82 | return format % make_tuple(a, b, c, d); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | int a; char b; std::string c; double d; |
---|
| 86 | }; |
---|
| 87 | |
---|
| 88 | |
---|
| 89 | struct X { |
---|
| 90 | |
---|
| 91 | X() {} |
---|
| 92 | |
---|
| 93 | X(int a, char b = 'D', std::string c = "constructor", double d = 0.0) |
---|
| 94 | : state(format % make_tuple(a, b, c, d)) |
---|
| 95 | {} |
---|
| 96 | |
---|
| 97 | X(std::string s, bool b) |
---|
| 98 | : state("Got exactly two arguments from constructor: string(%s); bool(%s); " % make_tuple(s, b*1)) |
---|
| 99 | {} |
---|
| 100 | |
---|
| 101 | object |
---|
| 102 | bar(int a, char b = 'D', std::string c = "default", double d = 0.0) const |
---|
| 103 | { |
---|
| 104 | return format % make_tuple(a, b, c, d); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | Y const& |
---|
| 108 | bar2(int a = 0, char b = 'D', std::string c = "default", double d = 0.0) |
---|
| 109 | { |
---|
| 110 | // tests zero arg member function and return_internal_reference policy |
---|
| 111 | y.a = a; |
---|
| 112 | y.b = b; |
---|
| 113 | y.c = c; |
---|
| 114 | y.d = d; |
---|
| 115 | return y; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | object |
---|
| 119 | foo(int a, bool b=false) const |
---|
| 120 | { |
---|
| 121 | return "int(%s); bool(%s); " % make_tuple(a, b*1); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | object |
---|
| 125 | foo(std::string a, bool b=false) const |
---|
| 126 | { |
---|
| 127 | return "string(%s); bool(%s); " % make_tuple(a, b*1); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | object |
---|
| 131 | foo(list a, list b, bool c=false) const |
---|
| 132 | { |
---|
| 133 | return "list(%s); list(%s); bool(%s); " % make_tuple(a, b, c*1); |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | object |
---|
| 137 | get_state() const |
---|
| 138 | { |
---|
| 139 | return state; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | Y y; |
---|
| 143 | object state; |
---|
| 144 | }; |
---|
| 145 | |
---|
| 146 | BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_bar_stubs, bar, 1, 4) |
---|
| 147 | BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_bar_stubs2, bar2, 0, 4) |
---|
| 148 | BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_foo_2_stubs, foo, 1, 2) |
---|
| 149 | BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_foo_3_stubs, foo, 2, 3) |
---|
| 150 | |
---|
| 151 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 152 | |
---|
| 153 | BOOST_PYTHON_MODULE(defaults_ext) |
---|
| 154 | { |
---|
| 155 | def("foo", foo, foo_stubs()); |
---|
| 156 | def("bar", (object(*)(int, char, std::string, double))0, bar_stubs()); |
---|
| 157 | |
---|
| 158 | class_<Y>("Y", init<>("doc of Y init")) // this should work |
---|
| 159 | .def("get_state", &Y::get_state) |
---|
| 160 | ; |
---|
| 161 | |
---|
| 162 | class_<X>("X") |
---|
| 163 | |
---|
| 164 | .def(init<int, optional<char, std::string, double> >("doc of init")) |
---|
| 165 | .def(init<std::string, bool>()[default_call_policies()]) // what's a good policy here? |
---|
| 166 | .def("get_state", &X::get_state) |
---|
| 167 | .def("bar", &X::bar, X_bar_stubs()) |
---|
| 168 | .def("bar2", &X::bar2, X_bar_stubs2("doc of X::bar2")[return_internal_reference<>()]) |
---|
| 169 | .def("foo", (object(X::*)(std::string, bool) const)0, X_foo_2_stubs()) |
---|
| 170 | .def("foo", (object(X::*)(int, bool) const)0, X_foo_2_stubs()) |
---|
| 171 | .def("foo", (object(X::*)(list, list, bool) const)0, X_foo_3_stubs()) |
---|
| 172 | ; |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | #include "module_tail.cpp" |
---|
| 176 | |
---|