minor cleanups

This commit is contained in:
Wenzel Jakob 2016-05-03 13:28:40 +02:00
parent aadc2f3d8c
commit ad69634907
4 changed files with 20 additions and 17 deletions

View File

@ -225,9 +225,6 @@ using cast_op_type = typename std::conditional<std::is_pointer<typename std::rem
typename std::add_pointer<typename intrinsic_type<T>::type>::type,
typename std::add_lvalue_reference<typename intrinsic_type<T>::type>::type>::type;
/// Thrown then trying to cast a null pointer into a reference argument
class invalid_reference_cast : public std::exception { };
/// Generic type caster for objects stored on the heap
template <typename type> class type_caster_base : public type_caster_generic {
public:
@ -256,7 +253,7 @@ public:
template <typename T> using cast_op_type = pybind11::detail::cast_op_type<T>;
operator type*() { return (type *) value; }
operator type&() { if (!value) throw invalid_reference_cast(); return *((type *) value); }
operator type&() { if (!value) throw cast_error(); return *((type *) value); }
protected:
typedef void *(*Constructor)(const void *stream);
@ -763,7 +760,7 @@ public:
NAMESPACE_END(detail)
template <typename T> inline T cast(handle handle) {
template <typename T> T cast(handle handle) {
typedef detail::type_caster<typename detail::intrinsic_type<T>::type> type_caster;
type_caster conv;
if (!conv.load(handle, true))
@ -771,7 +768,7 @@ template <typename T> inline T cast(handle handle) {
return conv.operator typename type_caster::template cast_op_type<T>();
}
template <typename T> inline object cast(const T &value, return_value_policy policy = return_value_policy::automatic_reference, handle parent = handle()) {
template <typename T> object cast(const T &value, return_value_policy policy = return_value_policy::automatic_reference, handle parent = handle()) {
if (policy == return_value_policy::automatic)
policy = std::is_pointer<T>::value ? return_value_policy::take_ownership : return_value_policy::copy;
else if (policy == return_value_policy::automatic_reference)
@ -779,11 +776,11 @@ template <typename T> inline object cast(const T &value, return_value_policy pol
return object(detail::type_caster<typename detail::intrinsic_type<T>::type>::cast(value, policy, parent), false);
}
template <typename T> inline T handle::cast() const { return pybind11::cast<T>(*this); }
template <typename T> T handle::cast() const { return pybind11::cast<T>(*this); }
template <> inline void handle::cast() const { return; }
template <return_value_policy policy = return_value_policy::automatic_reference,
typename... Args> inline tuple make_tuple(Args&&... args_) {
typename... Args> tuple make_tuple(Args&&... args_) {
const size_t size = sizeof...(Args);
std::array<object, size> args {
{ object(detail::type_caster<typename detail::intrinsic_type<Args>::type>::cast(
@ -799,7 +796,7 @@ template <return_value_policy policy = return_value_policy::automatic_reference,
return result;
}
template <typename... Args> inline object handle::call(Args&&... args) const {
template <typename... Args> object handle::call(Args&&... args) const {
tuple args_tuple = pybind11::make_tuple(std::forward<Args>(args)...);
object result(PyObject_CallObject(m_ptr, args_tuple.ptr()), false);
if (!result)

View File

@ -280,7 +280,7 @@ template<size_t N, size_t ...S> struct make_index_sequence : make_index_sequence
template<size_t ...S> struct make_index_sequence <0, S...> { typedef index_sequence<S...> type; };
/// Strip the class from a method type
template <typename T> struct remove_class {};
template <typename T> struct remove_class { };
template <typename C, typename R, typename... A> struct remove_class<R (C::*)(A...)> { typedef R type(A...); };
template <typename C, typename R, typename... A> struct remove_class<R (C::*)(A...) const> { typedef R type(A...); };
@ -305,12 +305,18 @@ to_string(T value) { return std::to_string((int) value); }
NAMESPACE_END(detail)
#define PYBIND11_RUNTIME_EXCEPTION(name) \
class name : public std::runtime_error { public: \
name(const std::string &w) : std::runtime_error(w) { }; \
name(const char *s) : std::runtime_error(s) { }; \
name() : std::runtime_error("") { } \
};
// C++ bindings of core Python exceptions
struct stop_iteration : public std::runtime_error { public: stop_iteration(const std::string &w="") : std::runtime_error(w) {} };
struct index_error : public std::runtime_error { public: index_error(const std::string &w="") : std::runtime_error(w) {} };
struct error_already_set : public std::runtime_error { public: error_already_set() : std::runtime_error(detail::error_string()) {} };
/// Thrown when pybind11::cast or handle::call fail due to a type casting error
struct cast_error : public std::runtime_error { public: cast_error(const std::string &w = "") : std::runtime_error(w) {} };
class error_already_set : public std::runtime_error { public: error_already_set() : std::runtime_error(detail::error_string()) {} };
PYBIND11_RUNTIME_EXCEPTION(stop_iteration)
PYBIND11_RUNTIME_EXCEPTION(index_error)
PYBIND11_RUNTIME_EXCEPTION(cast_error) /// Thrown when pybind11::cast or handle::call fail due to a type casting error
[[noreturn]] PYBIND11_NOINLINE inline void pybind11_fail(const char *reason) { throw std::runtime_error(reason); }
[[noreturn]] PYBIND11_NOINLINE inline void pybind11_fail(const std::string &reason) { throw std::runtime_error(reason); }

View File

@ -413,7 +413,7 @@ protected:
try {
if (kwargs_consumed == nkwargs)
result = it->impl(it, args_, parent);
} catch (detail::invalid_reference_cast &) {
} catch (cast_error &) {
result = PYBIND11_TRY_NEXT_OVERLOAD;
}

View File

@ -126,7 +126,7 @@ public:
return result;
}
template <typename T> inline T cast() const { return operator object().cast<T>(); }
template <typename T> T cast() const { return operator object().cast<T>(); }
operator bool() const {
if (attr) {