args.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #include <boost/python/module.hpp>
  6. #include "test_class.hpp"
  7. #include <boost/python/def.hpp>
  8. #include <boost/python/args.hpp>
  9. #include <boost/python/tuple.hpp>
  10. #include <boost/python/class.hpp>
  11. #include <boost/python/overloads.hpp>
  12. #include <boost/python/raw_function.hpp>
  13. #include <boost/python/return_internal_reference.hpp>
  14. using namespace boost::python;
  15. #if BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x580)) || BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1500))
  16. # define make_tuple boost::python::make_tuple
  17. #endif
  18. tuple f(int x = 1, double y = 4.25, char const* z = "wow")
  19. {
  20. return make_tuple(x, y, z);
  21. }
  22. BOOST_PYTHON_FUNCTION_OVERLOADS(f_overloads, f, 0, 3)
  23. typedef test_class<> Y;
  24. struct X
  25. {
  26. X(int a0 = 0, int a1 = 1) : inner0(a0), inner1(a1) {}
  27. tuple f(int x = 1, double y = 4.25, char const* z = "wow")
  28. {
  29. return make_tuple(x, y, z);
  30. }
  31. Y const& inner(bool n) const { return n ? inner1 : inner0; }
  32. Y inner0;
  33. Y inner1;
  34. };
  35. BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_f_overloads, X::f, 0, 3)
  36. tuple raw_func(tuple args, dict kw)
  37. {
  38. return make_tuple(args, kw);
  39. }
  40. BOOST_PYTHON_MODULE(args_ext)
  41. {
  42. def("f", f, (arg("x")=1, arg("y")=4.25, arg("z")="wow")
  43. , "This is f's docstring"
  44. );
  45. def("raw", raw_function(raw_func));
  46. #if defined(BOOST_MSVC) && BOOST_MSVC <= 1200
  47. // MSVC6 gives a fatal error LNK1179: invalid or corrupt file:
  48. // duplicate comdat error if we try to re-use the exact type of f
  49. // here, so substitute long for int.
  50. tuple (*f)(long,double,char const*) = 0;
  51. #endif
  52. def("f1", f, f_overloads("f1's docstring", args("x", "y", "z")));
  53. def("f2", f, f_overloads(args("x", "y", "z")));
  54. def("f3", f, f_overloads(args("x", "y", "z"), "f3's docstring"));
  55. class_<Y>("Y", init<int>(args("value"), "Y's docstring"))
  56. .def("value", &Y::value)
  57. .def("raw", raw_function(raw_func))
  58. ;
  59. class_<X>("X", "This is X's docstring", init<>(args("self")))
  60. .def(init<int, optional<int> >(args("self", "a0", "a1")))
  61. .def("f", &X::f
  62. , "This is X.f's docstring"
  63. , args("self","x", "y", "z"))
  64. // Just to prove that all the different argument combinations work
  65. .def("inner0", &X::inner, return_internal_reference<>(), args("self", "n"), "docstring")
  66. .def("inner1", &X::inner, return_internal_reference<>(), "docstring", args("self", "n"))
  67. .def("inner2", &X::inner, args("self", "n"), return_internal_reference<>(), "docstring")
  68. .def("inner3", &X::inner, "docstring", return_internal_reference<>(), args("self", "n"))
  69. .def("inner4", &X::inner, args("self", "n"), "docstring", return_internal_reference<>())
  70. .def("inner5", &X::inner, "docstring", args("self", "n"), return_internal_reference<>())
  71. .def("f1", &X::f, X_f_overloads(args("self", "x", "y", "z")))
  72. .def("f2", &X::f, X_f_overloads(args("self", "x", "y", "z"), "f2's docstring"))
  73. .def("f2", &X::f, X_f_overloads(args("x", "y", "z"), "f2's docstring"))
  74. ;
  75. def("inner", &X::inner, "docstring", args("self", "n"), return_internal_reference<>());
  76. }
  77. #include "module_tail.cpp"