defaults.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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/def.hpp>
  6. #include <boost/python/module.hpp>
  7. #include <boost/python/class.hpp>
  8. #include <boost/python/tuple.hpp>
  9. #include <boost/python/list.hpp>
  10. #include <boost/python/overloads.hpp>
  11. #include <boost/python/return_internal_reference.hpp>
  12. #if defined(_AIX) && defined(__EDG_VERSION__) && __EDG_VERSION__ < 245
  13. # include <iostream> // works around a KCC intermediate code generation bug
  14. #endif
  15. using namespace boost::python;
  16. namespace bpl = boost::python;
  17. char const* const format = "int(%s); char(%s); string(%s); double(%s); ";
  18. ///////////////////////////////////////////////////////////////////////////////
  19. //
  20. // Overloaded functions
  21. //
  22. ///////////////////////////////////////////////////////////////////////////////
  23. object
  24. bar(int a, char b, std::string c, double d)
  25. {
  26. return format % bpl::make_tuple(a, b, c, d);
  27. }
  28. object
  29. bar(int a, char b, std::string c)
  30. {
  31. return format % bpl::make_tuple(a, b, c, 0.0);
  32. }
  33. object
  34. bar(int a, char b)
  35. {
  36. return format % bpl::make_tuple(a, b, "default", 0.0);
  37. }
  38. object
  39. bar(int a)
  40. {
  41. return format % bpl::make_tuple(a, 'D', "default", 0.0);
  42. }
  43. BOOST_PYTHON_FUNCTION_OVERLOADS(bar_stubs, bar, 1, 4)
  44. ///////////////////////////////////////////////////////////////////////////////
  45. //
  46. // Functions with default arguments
  47. //
  48. ///////////////////////////////////////////////////////////////////////////////
  49. object
  50. foo(int a, char b = 'D', std::string c = "default", double d = 0.0)
  51. {
  52. return format % bpl::make_tuple(a, b, c, d);
  53. }
  54. BOOST_PYTHON_FUNCTION_OVERLOADS(foo_stubs, foo, 1, 4)
  55. ///////////////////////////////////////////////////////////////////////////////
  56. //
  57. // Overloaded member functions with default arguments
  58. //
  59. ///////////////////////////////////////////////////////////////////////////////
  60. struct Y {
  61. Y() {}
  62. object
  63. get_state() const
  64. {
  65. return format % bpl::make_tuple(a, b, c, d);
  66. }
  67. int a; char b; std::string c; double d;
  68. };
  69. struct X {
  70. X() {}
  71. X(int a, char b = 'D', std::string c = "constructor", double d = 0.0)
  72. : state(format % bpl::make_tuple(a, b, c, d))
  73. {}
  74. X(std::string s, bool b)
  75. : state("Got exactly two arguments from constructor: string(%s); bool(%s); " % bpl::make_tuple(s, b*1))
  76. {}
  77. object
  78. bar(int a, char b = 'D', std::string c = "default", double d = 0.0) const
  79. {
  80. return format % bpl::make_tuple(a, b, c, d);
  81. }
  82. Y const&
  83. bar2(int a = 0, char b = 'D', std::string c = "default", double d = 0.0)
  84. {
  85. // tests zero arg member function and return_internal_reference policy
  86. y.a = a;
  87. y.b = b;
  88. y.c = c;
  89. y.d = d;
  90. return y;
  91. }
  92. object
  93. foo(int a, bool b=false) const
  94. {
  95. return "int(%s); bool(%s); " % bpl::make_tuple(a, b*1);
  96. }
  97. object
  98. foo(std::string a, bool b=false) const
  99. {
  100. return "string(%s); bool(%s); " % bpl::make_tuple(a, b*1);
  101. }
  102. object
  103. foo(list a, list b, bool c=false) const
  104. {
  105. return "list(%s); list(%s); bool(%s); " % bpl::make_tuple(a, b, c*1);
  106. }
  107. object
  108. get_state() const
  109. {
  110. return state;
  111. }
  112. Y y;
  113. object state;
  114. };
  115. BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_bar_stubs, bar, 1, 4)
  116. BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_bar_stubs2, bar2, 0, 4)
  117. BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_foo_2_stubs, foo, 1, 2)
  118. BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_foo_3_stubs, foo, 2, 3)
  119. ///////////////////////////////////////////////////////////////////////////////
  120. BOOST_PYTHON_MODULE(defaults_ext)
  121. {
  122. def("foo", foo, foo_stubs());
  123. def("bar", (object(*)(int, char, std::string, double))0, bar_stubs());
  124. class_<Y>("Y", init<>("doc of Y init")) // this should work
  125. .def("get_state", &Y::get_state)
  126. ;
  127. class_<X>("X",no_init)
  128. .def(init<optional<int, char, std::string, double> >("doc of init", args("self", "a", "b", "c", "d")))
  129. .def(init<std::string, bool>(args("self", "s", "b"))[default_call_policies()]) // what's a good policy here?
  130. .def("get_state", &X::get_state)
  131. .def("bar", &X::bar, X_bar_stubs())
  132. .def("bar2", &X::bar2, X_bar_stubs2("doc of X::bar2")[return_internal_reference<>()])
  133. .def("foo", (object(X::*)(std::string, bool) const)0, X_foo_2_stubs())
  134. .def("foo", (object(X::*)(int, bool) const)0, X_foo_2_stubs())
  135. .def("foo", (object(X::*)(list, list, bool) const)0, X_foo_3_stubs())
  136. ;
  137. }
  138. #include "module_tail.cpp"