|
| 1 | +// Copyright David Abrahams 2002. Permission to copy, use, |
| 2 | +// modify, sell and distribute this software is granted provided this |
| 3 | +// copyright notice appears in all copies. This software is provided |
| 4 | +// "as is" without express or implied warranty, and with no claim as |
| 5 | +// to its suitability for any purpose. |
| 6 | +#include <boost/python/module.hpp> |
| 7 | +#include <boost/python/class.hpp> |
| 8 | +#include <boost/python/list.hpp> |
| 9 | + |
| 10 | +#if defined(_AIX) && defined(__EDG_VERSION__) && __EDG_VERSION__ < 245 |
| 11 | +# include <iostream> // works around a KCC intermediate code generation bug |
| 12 | +#endif |
| 13 | + |
| 14 | +using namespace boost::python; |
| 15 | +using namespace std; |
| 16 | + |
| 17 | +/////////////////////////////////////////////////////////////////////////////// |
| 18 | +object |
| 19 | +bar(int a, char b, std::string c, double d) |
| 20 | +{ |
| 21 | + list abcd; |
| 22 | + abcd.append(a); |
| 23 | + abcd.append(b); |
| 24 | + abcd.append(c); |
| 25 | + abcd.append(d); |
| 26 | + return "int(%s); char(%s); string(%s); double(%s); " % tuple(abcd); |
| 27 | +} |
| 28 | + |
| 29 | +object |
| 30 | +bar(int a, char b, std::string c) |
| 31 | +{ |
| 32 | + list abcd; |
| 33 | + abcd.append(a); |
| 34 | + abcd.append(b); |
| 35 | + abcd.append(c); |
| 36 | + abcd.append(0.0); |
| 37 | + return "int(%s); char(%s); string(%s); double(%s); " % tuple(abcd); |
| 38 | +} |
| 39 | + |
| 40 | +object |
| 41 | +bar(int a, char b) |
| 42 | +{ |
| 43 | + list abcd; |
| 44 | + abcd.append(a); |
| 45 | + abcd.append(b); |
| 46 | + abcd.append("default"); |
| 47 | + abcd.append(0.0); |
| 48 | + return "int(%s); char(%s); string(%s); double(%s); " % tuple(abcd); |
| 49 | +} |
| 50 | + |
| 51 | +object |
| 52 | +bar(int a) |
| 53 | +{ |
| 54 | + list abcd; |
| 55 | + abcd.append(a); |
| 56 | + abcd.append('D'); |
| 57 | + abcd.append("default"); |
| 58 | + abcd.append(0.0); |
| 59 | + return "int(%s); char(%s); string(%s); double(%s); " % tuple(abcd); |
| 60 | +} |
| 61 | + |
| 62 | +BOOST_PYTHON_FUNCTION_GENERATOR(bar_stubs, bar, 1, 4) |
| 63 | + |
| 64 | +/////////////////////////////////////////////////////////////////////////////// |
| 65 | +object |
| 66 | +foo(int a, char b = 'D', std::string c = "default", double d = 0.0) |
| 67 | +{ |
| 68 | + list abcd; |
| 69 | + abcd.append(a); |
| 70 | + abcd.append(b); |
| 71 | + abcd.append(c); |
| 72 | + abcd.append(d); |
| 73 | + return "int(%s); char(%s); string(%s); double(%s); " % tuple(abcd); |
| 74 | +} |
| 75 | + |
| 76 | +BOOST_PYTHON_FUNCTION_GENERATOR(foo_stubs, foo, 1, 4) |
| 77 | + |
| 78 | +/////////////////////////////////////////////////////////////////////////////// |
| 79 | + |
| 80 | +struct X { |
| 81 | + |
| 82 | + object |
| 83 | + bar(int a, char b = 'D', std::string c = "default", double d = 0.0) const |
| 84 | + { |
| 85 | + list abcd; |
| 86 | + abcd.append(a); |
| 87 | + abcd.append(b); |
| 88 | + abcd.append(c); |
| 89 | + abcd.append(d); |
| 90 | + return "int(%s); char(%s); string(%s); double(%s); " % tuple(abcd); |
| 91 | + } |
| 92 | +}; |
| 93 | + |
| 94 | +BOOST_PYTHON_MEM_FUN_GENERATOR(X_bar_stubs, bar, 1, 4) |
| 95 | + |
| 96 | +/////////////////////////////////////////////////////////////////////////////// |
| 97 | + |
| 98 | +BOOST_PYTHON_MODULE_INIT(defaults_ext) |
| 99 | +{ |
| 100 | + module m("defaults_ext"); |
| 101 | + m.def("foo", foo, foo_stubs()); |
| 102 | + m.def("bar", signature<object(*)(int, char, std::string, double)>(), bar_stubs()); |
| 103 | + |
| 104 | + class_<X> xc("X"); |
| 105 | + m.add(xc); |
| 106 | + |
| 107 | + xc.def_init(); |
| 108 | + xc.def("bar", X::bar, X_bar_stubs()); |
| 109 | +} |
| 110 | + |
| 111 | +#include "module_tail.cpp" |
0 commit comments