2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-30 08:02:38 +00:00

Support for constructor policies

[SVN r13350]
This commit is contained in:
Dave Abrahams
2002-04-02 22:19:22 +00:00
parent aed7e14d4b
commit 81124780d0
6 changed files with 141 additions and 0 deletions

View File

@@ -132,6 +132,19 @@ class class_ : public objects::class_base
return *this;
}
template <class Args, class CallPolicy>
self& def_init(Args const&, CallPolicy policy)
{
def("__init__",
make_constructor<Args>(
policy
// Using runtime type selection works around a CWPro7 bug.
, objects::select_holder<T,held_type>((held_type*)0).get()
)
);
return *this;
}
// Define the default constructor.
self& def_init()
{

View File

@@ -7,6 +7,7 @@
# define INDIRECT_TRAITS_DWA2002131_HPP
# include <boost/type_traits/cv_traits.hpp>
# include <boost/type_traits/composite_traits.hpp>
# include <boost/type_traits/function_traits.hpp>
# include <boost/mpl/select_type.hpp>
namespace boost { namespace python { namespace detail {
@@ -24,6 +25,68 @@ struct is_reference_to_const<T const&>
BOOST_STATIC_CONSTANT(bool, value = true);
};
# if 0 // Corresponding code doesn't work on MSVC yet
template <class T>
struct is_reference_to_function
{
BOOST_STATIC_CONSTANT(bool, value = false);
};
template <class T>
struct is_reference_to_function<T&>
{
BOOST_STATIC_CONSTANT(bool, value = is_function<T>::value);
};
template <class T>
struct is_reference_to_function<T const&>
{
BOOST_STATIC_CONSTANT(bool, value = is_function<T>::value);
};
template <class T>
struct is_reference_to_function<T volatile&>
{
BOOST_STATIC_CONSTANT(bool, value = is_function<T>::value);
};
template <class T>
struct is_reference_to_function<T const volatile&>
{
BOOST_STATIC_CONSTANT(bool, value = is_function<T>::value);
};
# endif
template <class T>
struct is_pointer_to_function
{
BOOST_STATIC_CONSTANT(bool, value = false);
};
template <class T>
struct is_pointer_to_function<T*>
{
BOOST_STATIC_CONSTANT(bool, value = is_function<T>::value);
};
template <class T>
struct is_pointer_to_function<T const*>
{
BOOST_STATIC_CONSTANT(bool, value = is_function<T>::value);
};
template <class T>
struct is_pointer_to_function<T volatile*>
{
BOOST_STATIC_CONSTANT(bool, value = is_function<T>::value);
};
template <class T>
struct is_pointer_to_function<T const volatile*>
{
BOOST_STATIC_CONSTANT(bool, value = is_function<T>::value);
};
template <class T>
struct is_reference_to_non_const
{
@@ -114,6 +177,25 @@ struct is_pointer_help
>::type type;
};
# if 0 // doesn't seem to work yet
template <class T>
struct is_reference_to_function
{
static T t;
BOOST_STATIC_CONSTANT(
bool, value
= sizeof(::boost::detail::is_function_tester(t)) == sizeof(::boost::type_traits::yes_type));
# endif
template <class T>
struct is_pointer_to_function
{
static T t;
BOOST_STATIC_CONSTANT(
bool, value
= sizeof(::boost::detail::is_function_tester(t)) == sizeof(::boost::type_traits::yes_type));
};
template <typename V>
typename is_const_help<V>::type reference_to_const_helper(V&);
outer_no_type

View File

@@ -49,6 +49,20 @@ objects::function* make_constructor(Holder* = 0, ArgList* = 0)
, nargs + 1);
}
template <class ArgList, class Holder, class Policies>
objects::function* make_constructor(Policies const& policies, Holder* = 0, ArgList* = 0)
{
enum { nargs = mpl::size<ArgList>::value };
return new objects::function(
objects::py_function(
::boost::bind<PyObject*>(detail::caller(),
objects::make_holder<nargs>
::template apply<Holder,ArgList>::execute
, _1, _2, policies))
, nargs + 1);
}
}} // namespace boost::python
#endif // MAKE_FUNCTION_DWA20011221_HPP

View File

@@ -8,6 +8,14 @@ int main()
{
using namespace boost::python::detail;
#if 0 // not yet supported
assert(is_reference_to_function<int (&)()>::value);
assert(!is_reference_to_function<int (*)()>::value);
#endif
assert(!is_pointer_to_function<int (&)()>::value);
assert(is_pointer_to_function<int (*)()>::value);
assert(is_reference_to_pointer<int*&>::value);
assert(is_reference_to_pointer<int* const&>::value);
assert(is_reference_to_pointer<int*volatile&>::value);

View File

@@ -60,6 +60,7 @@ struct A
struct B
{
B() : x(0) {}
B(A* x_) : x(x_) {}
inner const* adopt(A* x) { this->x = x; return &x->get_inner(); }
@@ -101,6 +102,7 @@ BOOST_PYTHON_MODULE_INIT(test_pointer_adoption_ext)
.add(
class_<B>("B")
.def_init()
.def_init(args<A*>(), with_custodian_and_ward_postcall<1,2>())
.def("adopt", &B::adopt
// Adopt returns a pointer referring to a subobject of its 2nd argument (1st being "self")

View File

@@ -48,6 +48,28 @@
>>> del b
>>> num_a_instances()
0
Test call policies for constructors here
>>> a = create('second a')
>>> num_a_instances()
1
>>> b = B(a)
>>> num_a_instances()
1
>>> a.content()
'second a'
>>> del a
>>> num_a_instances()
1
>>> b.a_content()
'second a'
>>> del b
>>> num_a_instances()
0
"""
def run(args = None):
import sys