Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 17 additions & 48 deletions orc-rt/include/orc-rt/Error.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef ORC_RT_ERROR_H
#define ORC_RT_ERROR_H

#include "orc-rt/CallableTraitsHelper.h"
#include "orc-rt/Compiler.h"
#include "orc-rt/RTTI.h"

Expand Down Expand Up @@ -137,31 +138,27 @@ template <typename ErrT, typename... ArgTs> Error make_error(ArgTs &&...Args) {
return make_error(std::make_unique<ErrT>(std::forward<ArgTs>(Args)...));
}

/// Traits class for selecting and applying error handlers.
template <typename HandlerT>
struct ErrorHandlerTraits
: public ErrorHandlerTraits<
decltype(&std::remove_reference_t<HandlerT>::operator())> {};
namespace detail {

template <typename RetT, typename ArgT> struct ErrorHandlerTraitsImpl;

// Specialization functions of the form 'Error (const ErrT&)'.
template <typename ErrT> struct ErrorHandlerTraits<Error (&)(ErrT &)> {
// Specialization for Error(ErrT&).
template <typename ErrT> struct ErrorHandlerTraitsImpl<Error, ErrT &> {
static bool appliesTo(const ErrorInfoBase &E) {
return E.template isA<ErrT>();
}

template <typename HandlerT>
static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) {
assert(appliesTo(*E) && "Applying incorrect handler");
return H(static_cast<ErrT &>(*E));
}
};

// Specialization functions of the form 'void (const ErrT&)'.
template <typename ErrT> struct ErrorHandlerTraits<void (&)(ErrT &)> {
// Specialization for void(ErrT&).
template <typename ErrT> struct ErrorHandlerTraitsImpl<void, ErrT &> {
static bool appliesTo(const ErrorInfoBase &E) {
return E.template isA<ErrT>();
}

template <typename HandlerT>
static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) {
assert(appliesTo(*E) && "Applying incorrect handler");
Expand All @@ -170,13 +167,12 @@ template <typename ErrT> struct ErrorHandlerTraits<void (&)(ErrT &)> {
}
};

/// Specialization for functions of the form 'Error (std::unique_ptr<ErrT>)'.
// Specialization for Error(std::unique_ptr<ErrT>).
template <typename ErrT>
struct ErrorHandlerTraits<Error (&)(std::unique_ptr<ErrT>)> {
struct ErrorHandlerTraitsImpl<Error, std::unique_ptr<ErrT>> {
static bool appliesTo(const ErrorInfoBase &E) {
return E.template isA<ErrT>();
}

template <typename HandlerT>
static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) {
assert(appliesTo(*E) && "Applying incorrect handler");
Expand All @@ -185,13 +181,12 @@ struct ErrorHandlerTraits<Error (&)(std::unique_ptr<ErrT>)> {
}
};

/// Specialization for functions of the form 'void (std::unique_ptr<ErrT>)'.
// Specialization for void(std::unique_ptr<ErrT>).
template <typename ErrT>
struct ErrorHandlerTraits<void (&)(std::unique_ptr<ErrT>)> {
struct ErrorHandlerTraitsImpl<void, std::unique_ptr<ErrT>> {
static bool appliesTo(const ErrorInfoBase &E) {
return E.template isA<ErrT>();
}

template <typename HandlerT>
static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) {
assert(appliesTo(*E) && "Applying incorrect handler");
Expand All @@ -201,37 +196,11 @@ struct ErrorHandlerTraits<void (&)(std::unique_ptr<ErrT>)> {
}
};

// Specialization for member functions of the form 'RetT (const ErrT&)'.
template <typename C, typename RetT, typename ErrT>
class ErrorHandlerTraits<RetT (C::*)(ErrT &)>
: public ErrorHandlerTraits<RetT (&)(ErrT &)> {};

// Specialization for member functions of the form 'RetT (const ErrT&) const'.
template <typename C, typename RetT, typename ErrT>
class ErrorHandlerTraits<RetT (C::*)(ErrT &) const>
: public ErrorHandlerTraits<RetT (&)(ErrT &)> {};

// Specialization for member functions of the form 'RetT (const ErrT&)'.
template <typename C, typename RetT, typename ErrT>
class ErrorHandlerTraits<RetT (C::*)(const ErrT &)>
: public ErrorHandlerTraits<RetT (&)(ErrT &)> {};

// Specialization for member functions of the form 'RetT (const ErrT&) const'.
template <typename C, typename RetT, typename ErrT>
class ErrorHandlerTraits<RetT (C::*)(const ErrT &) const>
: public ErrorHandlerTraits<RetT (&)(ErrT &)> {};

/// Specialization for member functions of the form
/// 'RetT (std::unique_ptr<ErrT>)'.
template <typename C, typename RetT, typename ErrT>
class ErrorHandlerTraits<RetT (C::*)(std::unique_ptr<ErrT>)>
: public ErrorHandlerTraits<RetT (&)(std::unique_ptr<ErrT>)> {};

/// Specialization for member functions of the form
/// 'RetT (std::unique_ptr<ErrT>) const'.
template <typename C, typename RetT, typename ErrT>
class ErrorHandlerTraits<RetT (C::*)(std::unique_ptr<ErrT>) const>
: public ErrorHandlerTraits<RetT (&)(std::unique_ptr<ErrT>)> {};
} // namespace detail.

template <typename C>
struct ErrorHandlerTraits
: public CallableTraitsHelper<detail::ErrorHandlerTraitsImpl, C> {};

inline Error handleErrorsImpl(std::unique_ptr<ErrorInfoBase> Payload) {
return make_error(std::move(Payload));
Expand Down
Loading