mirror of
https://github.com/boostorg/python.git
synced 2026-01-27 19:12:16 +00:00
added call policies to the default stubs.
[SVN r15190]
This commit is contained in:
@@ -175,9 +175,37 @@ class class_ : public objects::class_base
|
||||
}
|
||||
|
||||
template <class Arg1T, class Arg2T>
|
||||
self& def(char const* name, Arg1T arg1, Arg2T const& arg2, char const* doc = 0)
|
||||
self& def(char const* name, Arg1T arg1, Arg2T const& arg2)
|
||||
{
|
||||
dispatch_def(name, arg1, arg2, doc, &arg2);
|
||||
// The arguments may be:
|
||||
// arg1: function or signature
|
||||
// arg2: policy or docstring or stubs
|
||||
|
||||
dispatch_def(&arg2, name, arg1, arg2, (char*)0);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <class Arg1T, class Arg2T, class Arg3T>
|
||||
self& def(char const* name, Arg1T arg1, Arg2T const& arg2, Arg3T const& arg3)
|
||||
{
|
||||
// The arguments may be:
|
||||
// arg1: function or signature
|
||||
// arg2: policy or docstring or stubs
|
||||
// arg3: policy or docstring
|
||||
|
||||
dispatch_def(&arg2, name, arg1, arg2, arg3);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <class Arg1T, class Arg2T, class Arg3T>
|
||||
self& def(char const* name, Arg1T arg1, Arg2T const& arg2, Arg3T const& arg3, char const* doc)
|
||||
{
|
||||
// The arguments are definitely:
|
||||
// arg1: signature
|
||||
// arg2: stubs
|
||||
// arg3: policy
|
||||
|
||||
dispatch_def(&arg2, name, arg1, arg2, arg3, doc);
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -302,30 +330,37 @@ class class_ : public objects::class_base
|
||||
|
||||
template <class Fn, class CallPolicyOrDoc>
|
||||
void dispatch_def(
|
||||
void const*,
|
||||
char const* name,
|
||||
Fn fn,
|
||||
CallPolicyOrDoc const& policy_or_doc,
|
||||
char const* doc,
|
||||
void const*)
|
||||
char const* doc)
|
||||
{
|
||||
typedef detail::def_helper<CallPolicyOrDoc> helper;
|
||||
|
||||
this->def_impl(
|
||||
name, fn, helper::get_policy(policy_or_doc), helper::get_doc(policy_or_doc, doc), &fn);
|
||||
name, fn, helper::get_policy(policy_or_doc),
|
||||
helper::get_doc(policy_or_doc, doc), &fn);
|
||||
|
||||
}
|
||||
|
||||
template <typename StubsT, typename SigT>
|
||||
template <class StubsT, class SigT, class CallPolicyOrDoc>
|
||||
void dispatch_def(
|
||||
detail::func_stubs_base const*,
|
||||
char const* name,
|
||||
SigT sig,
|
||||
StubsT const& stubs,
|
||||
char const* doc,
|
||||
detail::func_stubs_base const*)
|
||||
CallPolicyOrDoc const& policy_or_doc,
|
||||
char const* doc = 0)
|
||||
{
|
||||
typedef detail::def_helper<CallPolicyOrDoc> helper;
|
||||
|
||||
// convert sig to a type_list (see detail::get_signature in signature.hpp)
|
||||
// before calling detail::define_with_defaults.
|
||||
detail::define_with_defaults(name, stubs, *this, detail::get_signature(sig), doc);
|
||||
detail::define_with_defaults(
|
||||
name, stubs, helper::get_policy(policy_or_doc),
|
||||
*this, detail::get_signature(sig),
|
||||
helper::get_doc(policy_or_doc, doc));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
# include <boost/python/scope.hpp>
|
||||
# include <boost/python/signature.hpp>
|
||||
|
||||
namespace boost { namespace python {
|
||||
namespace boost { namespace python {
|
||||
|
||||
namespace detail
|
||||
{
|
||||
@@ -22,11 +22,11 @@ namespace detail
|
||||
template <class Fn, class CallPolicyOrDoc>
|
||||
void
|
||||
dispatch_def(
|
||||
void const*,
|
||||
char const* name,
|
||||
Fn fn,
|
||||
CallPolicyOrDoc const& policy_or_doc,
|
||||
char const* doc,
|
||||
void const*)
|
||||
char const* doc)
|
||||
{
|
||||
typedef detail::def_helper<CallPolicyOrDoc> helper;
|
||||
|
||||
@@ -35,20 +35,26 @@ namespace detail
|
||||
helper::get_doc(policy_or_doc, doc));
|
||||
}
|
||||
|
||||
template <typename StubsT, typename SigT>
|
||||
void
|
||||
dispatch_def(
|
||||
char const* name,
|
||||
SigT sig,
|
||||
StubsT const& stubs,
|
||||
char const* doc,
|
||||
detail::func_stubs_base const*)
|
||||
{
|
||||
// convert sig to a type_list (see detail::get_signature in signature.hpp)
|
||||
// before calling detail::define_with_defaults.
|
||||
scope current;
|
||||
detail::define_with_defaults(name, stubs, current, detail::get_signature(sig), doc);
|
||||
}
|
||||
template <class StubsT, class SigT, class CallPolicyOrDoc>
|
||||
void dispatch_def(
|
||||
detail::func_stubs_base const*,
|
||||
char const* name,
|
||||
SigT sig,
|
||||
StubsT const& stubs,
|
||||
CallPolicyOrDoc const& policy_or_doc,
|
||||
char const* doc = 0)
|
||||
{
|
||||
typedef detail::def_helper<CallPolicyOrDoc> helper;
|
||||
|
||||
// convert sig to a type_list (see detail::get_signature in signature.hpp)
|
||||
// before calling detail::define_with_defaults.
|
||||
|
||||
scope current;
|
||||
detail::define_with_defaults(
|
||||
name, stubs, helper::get_policy(policy_or_doc),
|
||||
current, detail::get_signature(sig),
|
||||
helper::get_doc(policy_or_doc, doc));
|
||||
}
|
||||
}
|
||||
|
||||
template <class Fn>
|
||||
@@ -58,11 +64,36 @@ void def(char const* name, Fn fn)
|
||||
}
|
||||
|
||||
template <class Arg1T, class Arg2T>
|
||||
void def(char const* name, Arg1T arg1, Arg2T const& arg2, char const* doc = 0)
|
||||
void def(char const* name, Arg1T arg1, Arg2T const& arg2)
|
||||
{
|
||||
detail::dispatch_def(name, arg1, arg2, doc, &arg2);
|
||||
// The arguments may be:
|
||||
// arg1: function or signature
|
||||
// arg2: policy or docstring or stubs
|
||||
|
||||
detail::dispatch_def(&arg2, name, arg1, arg2, (char*)0);
|
||||
}
|
||||
|
||||
template <class Arg1T, class Arg2T, class Arg3T>
|
||||
void def(char const* name, Arg1T arg1, Arg2T const& arg2, Arg3T const& arg3)
|
||||
{
|
||||
// The arguments may be:
|
||||
// arg1: function or signature
|
||||
// arg2: policy or docstring or stubs
|
||||
// arg3: policy or docstring
|
||||
|
||||
detail::dispatch_def(&arg2, name, arg1, arg2, arg3);
|
||||
}
|
||||
|
||||
template <class Arg1T, class Arg2T, class Arg3T>
|
||||
void def(char const* name, Arg1T arg1, Arg2T const& arg2, Arg3T const& arg3, char const* doc)
|
||||
{
|
||||
// The arguments are definitely:
|
||||
// arg1: signature
|
||||
// arg2: stubs
|
||||
// arg3: policy
|
||||
|
||||
detail::dispatch_def(&arg2, name, arg1, arg2, arg3, doc);
|
||||
}
|
||||
|
||||
}} // namespace boost::python
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ namespace objects
|
||||
|
||||
namespace detail {
|
||||
|
||||
template <typename Func, class CallPolicies, class NameSpaceT>
|
||||
template <class Func, class CallPolicies, class NameSpaceT>
|
||||
static void name_space_def(
|
||||
NameSpaceT& name_space,
|
||||
char const* name,
|
||||
@@ -48,7 +48,7 @@ static void name_space_def(
|
||||
name, f, policies, doc);
|
||||
}
|
||||
|
||||
template <typename Func, class CallPolicies>
|
||||
template <class Func, class CallPolicies>
|
||||
static void name_space_def(
|
||||
object& name_space,
|
||||
char const* name,
|
||||
@@ -59,12 +59,12 @@ static void name_space_def(
|
||||
)
|
||||
{
|
||||
scope within(name_space);
|
||||
|
||||
|
||||
def(name, f, policies, doc);
|
||||
}
|
||||
|
||||
// For backward compatibility
|
||||
template <typename Func, class CallPolicies, class NameSpaceT>
|
||||
template <class Func, class CallPolicies, class NameSpaceT>
|
||||
static void name_space_def(
|
||||
NameSpaceT& name_space,
|
||||
char const* name,
|
||||
@@ -133,14 +133,19 @@ struct define_stub_function {};
|
||||
template <int N>
|
||||
struct define_with_defaults_helper {
|
||||
|
||||
template <typename StubsT, typename NameSpaceT>
|
||||
template <class StubsT, class CallPolicies, class NameSpaceT>
|
||||
static void
|
||||
def(char const* name, StubsT stubs, NameSpaceT& name_space, char const* doc)
|
||||
def(
|
||||
char const* name,
|
||||
StubsT stubs,
|
||||
CallPolicies const& policies,
|
||||
NameSpaceT& name_space,
|
||||
char const* doc)
|
||||
{
|
||||
// define the NTH stub function of stubs
|
||||
define_stub_function<N>::define(name, stubs, name_space, doc);
|
||||
define_stub_function<N>::define(name, stubs, policies, name_space, doc);
|
||||
// call the next define_with_defaults_helper
|
||||
define_with_defaults_helper<N-1>::def(name, stubs, name_space, doc);
|
||||
define_with_defaults_helper<N-1>::def(name, stubs, policies, name_space, doc);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -148,12 +153,17 @@ struct define_stub_function {};
|
||||
template <>
|
||||
struct define_with_defaults_helper<0> {
|
||||
|
||||
template <typename StubsT, typename NameSpaceT>
|
||||
template <class StubsT, class CallPolicies, class NameSpaceT>
|
||||
static void
|
||||
def(char const* name, StubsT stubs, NameSpaceT& name_space, char const* doc)
|
||||
def(
|
||||
char const* name,
|
||||
StubsT stubs,
|
||||
CallPolicies const& policies,
|
||||
NameSpaceT& name_space,
|
||||
char const* doc)
|
||||
{
|
||||
// define the Oth stub function of stubs
|
||||
define_stub_function<0>::define(name, stubs, name_space, doc);
|
||||
define_stub_function<0>::define(name, stubs, policies, name_space, doc);
|
||||
// return
|
||||
}
|
||||
};
|
||||
@@ -162,11 +172,12 @@ struct define_stub_function {};
|
||||
//
|
||||
// define_with_defaults
|
||||
//
|
||||
// 1. char const* name: function name that will be visible to python
|
||||
// 2. StubsT: a function stubs struct (see defaults_gen.hpp)
|
||||
// 3. NameSpaceT& name_space: a python::class_ or python::module instance
|
||||
// 4. SigT sig: Function signature typelist (see defaults_gen.hpp)
|
||||
// 5. char const* name: doc string
|
||||
// 1. char const* name: function name that will be visible to python
|
||||
// 2. StubsT: a function stubs struct (see defaults_gen.hpp)
|
||||
// 3. CallPolicies& policies: Call policies
|
||||
// 4. NameSpaceT& name_space: a python::class_ or python::module instance
|
||||
// 5. SigT sig: Function signature typelist (see defaults_gen.hpp)
|
||||
// 6. char const* name: doc string
|
||||
//
|
||||
// This is the main entry point. This function recursively defines all
|
||||
// stub functions of StubT (see defaults_gen.hpp) in NameSpaceT name_space which
|
||||
@@ -179,11 +190,12 @@ struct define_stub_function {};
|
||||
// void C::foo(int) mpl::type_list<void, C, int>
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
template <typename StubsT, typename NameSpaceT, typename SigT>
|
||||
template <class StubsT, class CallPolicies, class NameSpaceT, class SigT>
|
||||
inline void
|
||||
define_with_defaults(
|
||||
char const* name,
|
||||
StubsT,
|
||||
CallPolicies const& policies,
|
||||
NameSpaceT& name_space,
|
||||
SigT sig,
|
||||
char const* doc)
|
||||
@@ -204,7 +216,7 @@ struct define_stub_function {};
|
||||
|
||||
typedef typename stubs_type::template gen<SigT> gen_type;
|
||||
define_with_defaults_helper<stubs_type::n_funcs-1>::def
|
||||
(name, gen_type(), name_space, doc);
|
||||
(name, gen_type(), policies, name_space, doc);
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
@@ -220,10 +232,11 @@ struct define_stub_function {};
|
||||
|
||||
template <>
|
||||
struct define_stub_function<BOOST_PP_ITERATION()> {
|
||||
template <typename StubsT, typename NameSpaceT>
|
||||
template <class StubsT, class CallPolicies, class NameSpaceT>
|
||||
static void define(
|
||||
char const* name,
|
||||
StubsT,
|
||||
CallPolicies const& policies,
|
||||
NameSpaceT& name_space,
|
||||
char const* doc
|
||||
)
|
||||
@@ -231,7 +244,7 @@ struct define_stub_function<BOOST_PP_ITERATION()> {
|
||||
detail::name_space_def(name_space,
|
||||
name,
|
||||
&StubsT::BOOST_PP_CAT(func_, BOOST_PP_ITERATION()),
|
||||
default_call_policies(),
|
||||
policies,
|
||||
doc, &name_space);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -125,7 +125,7 @@ namespace detail {
|
||||
|
||||
// Case 1: default case, just push T to the back of ListT
|
||||
|
||||
template <typename ListT, typename T>
|
||||
template <class ListT, class T>
|
||||
struct apply {
|
||||
|
||||
typedef typename boost::mpl::push_back<ListT, T>::sequence sequence;
|
||||
@@ -134,7 +134,7 @@ namespace detail {
|
||||
|
||||
struct append_to_init_helper2 {
|
||||
|
||||
template <typename ListT, typename T>
|
||||
template <class ListT, class T>
|
||||
struct apply {
|
||||
|
||||
// Case 2: optional case, T is an optional, append all
|
||||
@@ -153,14 +153,14 @@ namespace detail {
|
||||
|
||||
// Case 3: nil case, we found a nil, do nothing
|
||||
|
||||
template <typename ListT, typename T>
|
||||
template <class ListT, class T>
|
||||
struct apply {
|
||||
|
||||
typedef ListT sequence;
|
||||
};
|
||||
};
|
||||
|
||||
template <typename ListT, typename T>
|
||||
template <class ListT, class T>
|
||||
struct append_to_init {
|
||||
|
||||
typedef typename boost::mpl::select_type
|
||||
@@ -192,7 +192,7 @@ namespace detail {
|
||||
template <int N>
|
||||
struct check_init_params_helper {
|
||||
|
||||
template <typename ListT>
|
||||
template <class ListT>
|
||||
struct apply {
|
||||
|
||||
// case where size of sequence is not zero
|
||||
@@ -221,7 +221,7 @@ namespace detail {
|
||||
|
||||
// case where size of sequence is zero
|
||||
|
||||
template <typename ListT>
|
||||
template <class ListT>
|
||||
struct apply {
|
||||
|
||||
enum { is_ok = true };
|
||||
@@ -252,20 +252,20 @@ namespace detail {
|
||||
// init<int, string, optional<char, long, double> >::value == 3
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
template <class T>
|
||||
struct count_optionals1 {
|
||||
|
||||
BOOST_STATIC_CONSTANT(int, value = 0);
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
template <class T>
|
||||
struct count_optionals2 {
|
||||
|
||||
BOOST_STATIC_CONSTANT(
|
||||
int, value = boost::mpl::size<typename T::sequence>::value);
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
template <class T>
|
||||
struct count_optionals
|
||||
: boost::mpl::select_type
|
||||
<
|
||||
|
||||
@@ -89,7 +89,9 @@ class module : public detail::module_base
|
||||
{
|
||||
// convert sig to a type_list (see detail::get_signature in signature.hpp)
|
||||
// before calling detail::define_with_defaults.
|
||||
detail::define_with_defaults(name, stubs, *this, detail::get_signature(sig), doc);
|
||||
detail::define_with_defaults(
|
||||
name, stubs, default_call_policies(),
|
||||
*this, detail::get_signature(sig), doc);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -126,7 +126,7 @@ BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_foo_3_stubs, foo, 2, 3)
|
||||
|
||||
BOOST_PYTHON_MODULE_INIT(defaults_ext)
|
||||
{
|
||||
def("foo", foo, foo_stubs());
|
||||
def("foo", foo, foo_stubs(), default_call_policies());
|
||||
def("bar", (object(*)(int, char, std::string, double))0, bar_stubs());
|
||||
|
||||
// Show that this works with the old obsolete module version of def().
|
||||
@@ -147,7 +147,7 @@ BOOST_PYTHON_MODULE_INIT(defaults_ext)
|
||||
# endif
|
||||
.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())
|
||||
.def("foo", (object(X::*)(std::string, bool) const)0, X_foo_2_stubs(), default_call_policies())
|
||||
.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())
|
||||
;
|
||||
|
||||
Reference in New Issue
Block a user