Browse Source
cleaning stormpy a bit before extending further
cleaning stormpy a bit before extending further
Former-commit-id: 41b75c595c
tempestpy_adaptions
sjunges
9 years ago
3 changed files with 109 additions and 111 deletions
@ -0,0 +1,44 @@ |
|||||
|
#pragma once |
||||
|
#include <boost/python.hpp> |
||||
|
|
||||
|
|
||||
|
namespace boost { namespace python { namespace converter { |
||||
|
|
||||
|
template <class T> |
||||
|
PyObject* shared_ptr_to_python(std::shared_ptr<T> const& x) |
||||
|
{ |
||||
|
if (!x) |
||||
|
return python::detail::none(); |
||||
|
else if (shared_ptr_deleter* d = std::get_deleter<shared_ptr_deleter>(x)) |
||||
|
return incref( d->owner.get() ); |
||||
|
else |
||||
|
return converter::registered<std::shared_ptr<T> const&>::converters.to_python(&x); |
||||
|
} |
||||
|
|
||||
|
/// @brief Adapter a non-member function that returns a unique_ptr to |
||||
|
/// a python function object that returns a raw pointer but |
||||
|
/// explicitly passes ownership to Python. |
||||
|
template<typename T, typename ...Args> |
||||
|
object adapt_unique(std::unique_ptr<T> (*fn)(Args...)) |
||||
|
{ |
||||
|
return make_function( |
||||
|
[fn](Args... args) { return fn(args...).release(); }, |
||||
|
return_value_policy<manage_new_object>(), |
||||
|
boost::mpl::vector<T*, Args...>() |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
/// @brief Adapter a member function that returns a unique_ptr to |
||||
|
/// a python function object that returns a raw pointer but |
||||
|
/// explicitly passes ownership to Python. |
||||
|
template<typename T, typename C, typename ...Args> |
||||
|
object adapt_unique(std::unique_ptr<T> (C::*fn)(Args...)) |
||||
|
{ |
||||
|
return make_function( |
||||
|
[fn](C& self, Args... args) { return (self.*fn)(args...).release(); }, |
||||
|
python::return_value_policy<manage_new_object>(), |
||||
|
boost::mpl::vector<T*, C&, Args...>() |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
}}} // namespace boost::python::converter |
@ -0,0 +1,56 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
#include <boost/python.hpp> |
||||
|
|
||||
|
template<typename Source, typename Target> |
||||
|
void shared_ptr_implicitly_convertible() { |
||||
|
boost::python::implicitly_convertible<std::shared_ptr<Source>, std::shared_ptr<Target>>(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
template<typename T> |
||||
|
void register_shared_ptr() { |
||||
|
boost::python::register_ptr_to_python<std::shared_ptr<T>>(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
namespace dtl{ |
||||
|
// primary class template |
||||
|
template<typename S, typename T, typename Enable = void> |
||||
|
struct ImplConversionSharedPtr { |
||||
|
void c() { shared_ptr_implicitly_convertible<S, T>(); } |
||||
|
}; |
||||
|
|
||||
|
// specialized class template |
||||
|
template<typename S, typename T> |
||||
|
struct ImplConversionSharedPtr<S, T, typename std::enable_if<std::is_same<T, void>::value>::type> { |
||||
|
void c() { } |
||||
|
}; |
||||
|
|
||||
|
template<typename B> |
||||
|
struct bases_holder { |
||||
|
typedef boost::python::bases<B> Type; |
||||
|
}; |
||||
|
|
||||
|
template<> |
||||
|
struct bases_holder<void> { |
||||
|
typedef boost::python::bases<> Type; |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
template<typename C, typename B=void, typename NC=void> |
||||
|
boost::python::class_<C, std::shared_ptr<C>, typename dtl::bases_holder<B>::Type, NC> defineClass(char const* name, char const* docstring, typename std::enable_if_t<std::is_default_constructible<C>::value>::type* = 0) { |
||||
|
auto inst = boost::python::class_<C, std::shared_ptr<C>, typename dtl::bases_holder<B>::Type, NC>(name, docstring); |
||||
|
register_shared_ptr<C>(); |
||||
|
dtl::ImplConversionSharedPtr<C,B>().c(); |
||||
|
return inst; |
||||
|
}; |
||||
|
|
||||
|
template<typename C, typename B=void, typename NC=void> |
||||
|
boost::python::class_<C, std::shared_ptr<C>, typename dtl::bases_holder<B>::Type, NC> defineClass(char const* name, char const* docstring) { |
||||
|
auto inst = boost::python::class_<C, std::shared_ptr<C>, typename dtl::bases_holder<B>::Type, NC>(name, docstring, boost::python::no_init); |
||||
|
register_shared_ptr<C>(); |
||||
|
dtl::ImplConversionSharedPtr<C,B>().c(); |
||||
|
return inst; |
||||
|
}; |
Write
Preview
Loading…
Cancel
Save
Reference in new issue