From acbb5be6ab4b88bb22776b91afa7d634495b8c41 Mon Sep 17 00:00:00 2001 From: Joel de Guzman Date: Sat, 7 Sep 2002 01:35:42 +0000 Subject: [PATCH] Added call policies to def(init<...>) and added tests to see that the call policies is working in default.cpp [SVN r15191] --- include/boost/python/class.hpp | 17 ++++++++++++-- include/boost/python/init.hpp | 20 ++++++++--------- test/defaults.cpp | 41 +++++++++++++++++++++++++++------- test/defaults.py | 11 ++++++++- 4 files changed, 68 insertions(+), 21 deletions(-) diff --git a/include/boost/python/class.hpp b/include/boost/python/class.hpp index 4c7a1383..d215f33f 100644 --- a/include/boost/python/class.hpp +++ b/include/boost/python/class.hpp @@ -168,9 +168,22 @@ class class_ : public objects::class_base } template - self& def(init const& i, char const* doc = 0) + self& def(init const& i) { - define_init(*this, i, doc); + define_init(*this, i, default_call_policies(), 0); + return *this; + } + + template + self& def( + init const& i, + CallPolicyOrDoc const& policy_or_doc, + char const* doc = 0) + { + typedef detail::def_helper helper; + define_init(*this, i, + helper::get_policy(policy_or_doc), + helper::get_doc(policy_or_doc, doc)); return *this; } diff --git a/include/boost/python/init.hpp b/include/boost/python/init.hpp index 617ed30a..b00bd2d5 100644 --- a/include/boost/python/init.hpp +++ b/include/boost/python/init.hpp @@ -352,12 +352,12 @@ namespace detail { template struct define_class_init_helper { - template - static void apply(ClassT& cl, ArgsT const& args, char const* doc) + template + static void apply(ClassT& cl, CallPoliciesT const& policies, ArgsT const& args, char const* doc) { - cl.def_init(args, default_call_policies(), doc); + cl.def_init(args, policies, doc); typename boost::mpl::pop_back::sequence next; - define_class_init_helper::apply(cl, next, doc); + define_class_init_helper::apply(cl, policies, next, doc); } }; @@ -374,10 +374,10 @@ namespace detail { template <> struct define_class_init_helper<0> { - template - static void apply(ClassT& cl, ArgsT const& args, char const* doc) + template + static void apply(ClassT& cl, CallPoliciesT const& policies, ArgsT const& args, char const* doc) { - cl.def_init(args, default_call_policies(), doc); + cl.def_init(args, policies, doc); } }; } @@ -403,13 +403,13 @@ namespace detail { // __init__(int) // /////////////////////////////////////////////////////////////////////////////// -template +template void -define_init(ClassT& cl, InitT const& i, char const* doc) +define_init(ClassT& cl, InitT const& i, CallPoliciesT const& policies, char const* doc) { enum { n_defaults_plus_1 = InitT::n_defaults + 1 }; typedef typename InitT::sequence args_t; - detail::define_class_init_helper::apply(cl, args_t(), doc); + detail::define_class_init_helper::apply(cl, policies, args_t(), doc); } }} // namespace boost::python diff --git a/test/defaults.cpp b/test/defaults.cpp index cacb6ef7..8cd3fe7b 100644 --- a/test/defaults.cpp +++ b/test/defaults.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #if defined(_AIX) && defined(__EDG_VERSION__) && __EDG_VERSION__ < 245 # include // works around a KCC intermediate code generation bug @@ -69,6 +70,20 @@ BOOST_PYTHON_FUNCTION_OVERLOADS(foo_stubs, foo, 1, 4) // Overloaded member functions with default arguments // /////////////////////////////////////////////////////////////////////////////// +struct Y { + + Y() {} + + object + get_state() const + { + return format % make_tuple(a, b, c, d); + } + + int a; char b; std::string c; double d; +}; + + struct X { X() {} @@ -83,11 +98,15 @@ struct X { return format % make_tuple(a, b, c, d); } - object - bar2(int a = 0, char b = 'D', std::string c = "default", double d = 0.0) const + Y const& + bar2(int a = 0, char b = 'D', std::string c = "default", double d = 0.0) { - // tests zero arg member function - return format % make_tuple(a, b, c, d); + // tests zero arg member function and return_internal_reference policy + y.a = a; + y.b = b; + y.c = c; + y.d = d; + return y; } object @@ -114,11 +133,12 @@ struct X { return state; } + Y y; object state; }; BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_bar_stubs, bar, 1, 4) -BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_bar2_stubs, bar2, 0, 4) // tests zero arg member function +BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_bar_stubs2, bar2, 0, 4) BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_foo_2_stubs, foo, 1, 2) BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_foo_3_stubs, foo, 2, 3) @@ -126,7 +146,7 @@ BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_foo_3_stubs, foo, 2, 3) BOOST_PYTHON_MODULE_INIT(defaults_ext) { - def("foo", foo, foo_stubs(), default_call_policies()); + def("foo", foo, foo_stubs()); def("bar", (object(*)(int, char, std::string, double))0, bar_stubs()); // Show that this works with the old obsolete module version of def(). @@ -135,6 +155,10 @@ BOOST_PYTHON_MODULE_INIT(defaults_ext) .def("barfoo", (object(*)(int, char, std::string, double))0, bar_stubs()) ; + class_("Y", no_init) + .def("get_state", &Y::get_state) + ; + class_("X") # if (!defined(BOOST_INTEL_CXX_VERSION) || BOOST_INTEL_CXX_VERSION > 600) @@ -145,9 +169,10 @@ BOOST_PYTHON_MODULE_INIT(defaults_ext) .def_init(args()) .def_init(args()) # endif - .def("get_state", &X::get_state) + .def("get_state", &X::get_state) .def("bar", &X::bar, X_bar_stubs()) - .def("foo", (object(X::*)(std::string, bool) const)0, X_foo_2_stubs(), default_call_policies()) + .def("bar2", &X::bar2, X_bar_stubs2(), return_internal_reference<>()) + .def("foo", (object(X::*)(std::string, bool) const)0, X_foo_2_stubs()) .def("foo", (object(X::*)(int, bool) const)0, X_foo_2_stubs()) .def("foo", (object(X::*)(list, list, bool) const)0, X_foo_3_stubs()) ; diff --git a/test/defaults.py b/test/defaults.py index ac890de2..701ba386 100644 --- a/test/defaults.py +++ b/test/defaults.py @@ -56,7 +56,16 @@ >>> x = X(1, 'X', "Phoenix", 3.65) >>> x.get_state() 'int(1); char(X); string(Phoenix); double(3.65); ' - +>>> x.bar2().get_state() +'int(0); char(D); string(default); double(0.0); ' +>>> x.bar2(1).get_state() +'int(1); char(D); string(default); double(0.0); ' +>>> x.bar2(1, 'K').get_state() +'int(1); char(K); string(default); double(0.0); ' +>>> x.bar2(1, 'K', "Kim").get_state() +'int(1); char(K); string(Kim); double(0.0); ' +>>> x.bar2(1, 'K', "Kim", 9.9).get_state() +'int(1); char(K); string(Kim); double(9.9); ' """ def run(args = None):