mirror of
https://github.com/boostorg/python.git
synced 2026-01-24 18:12:43 +00:00
Added call policies to def(init<...>) and added tests to see that the call policies is working in default.cpp
[SVN r15191]
This commit is contained in:
@@ -168,9 +168,22 @@ class class_ : public objects::class_base
|
||||
}
|
||||
|
||||
template <BOOST_PP_ENUM_PARAMS(BOOST_PYTHON_MAX_ARITY, class T)>
|
||||
self& def(init<BOOST_PP_ENUM_PARAMS(BOOST_PYTHON_MAX_ARITY, T)> const& i, char const* doc = 0)
|
||||
self& def(init<BOOST_PP_ENUM_PARAMS(BOOST_PYTHON_MAX_ARITY, T)> const& i)
|
||||
{
|
||||
define_init(*this, i, doc);
|
||||
define_init(*this, i, default_call_policies(), 0);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <class CallPolicyOrDoc, BOOST_PP_ENUM_PARAMS(BOOST_PYTHON_MAX_ARITY, class T)>
|
||||
self& def(
|
||||
init<BOOST_PP_ENUM_PARAMS(BOOST_PYTHON_MAX_ARITY, T)> const& i,
|
||||
CallPolicyOrDoc const& policy_or_doc,
|
||||
char const* doc = 0)
|
||||
{
|
||||
typedef detail::def_helper<CallPolicyOrDoc> helper;
|
||||
define_init(*this, i,
|
||||
helper::get_policy(policy_or_doc),
|
||||
helper::get_doc(policy_or_doc, doc));
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
@@ -352,12 +352,12 @@ namespace detail {
|
||||
template <int N>
|
||||
struct define_class_init_helper {
|
||||
|
||||
template <class ClassT, class ArgsT>
|
||||
static void apply(ClassT& cl, ArgsT const& args, char const* doc)
|
||||
template <class ClassT, class CallPoliciesT, class ArgsT>
|
||||
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<ArgsT>::sequence next;
|
||||
define_class_init_helper<N-1>::apply(cl, next, doc);
|
||||
define_class_init_helper<N-1>::apply(cl, policies, next, doc);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -374,10 +374,10 @@ namespace detail {
|
||||
template <>
|
||||
struct define_class_init_helper<0> {
|
||||
|
||||
template <class ClassT, class ArgsT>
|
||||
static void apply(ClassT& cl, ArgsT const& args, char const* doc)
|
||||
template <class ClassT, class CallPoliciesT, class ArgsT>
|
||||
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 <class ClassT, class InitT>
|
||||
template <class ClassT, class CallPoliciesT, class InitT>
|
||||
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<n_defaults_plus_1>::apply(cl, args_t(), doc);
|
||||
detail::define_class_init_helper<n_defaults_plus_1>::apply(cl, policies, args_t(), doc);
|
||||
}
|
||||
|
||||
}} // namespace boost::python
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <boost/python/tuple.hpp>
|
||||
#include <boost/python/list.hpp>
|
||||
#include <boost/python/module.hpp>
|
||||
#include <boost/python/return_internal_reference.hpp>
|
||||
|
||||
#if defined(_AIX) && defined(__EDG_VERSION__) && __EDG_VERSION__ < 245
|
||||
# include <iostream> // 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>("Y", no_init)
|
||||
.def("get_state", &Y::get_state)
|
||||
;
|
||||
|
||||
class_<X>("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<int, char, std::string>())
|
||||
.def_init(args<int, char, std::string, double>())
|
||||
# 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())
|
||||
;
|
||||
|
||||
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user