vectored_error_handler -> default_error_handler

This commit is contained in:
Jean-Louis Leroy
2025-05-20 17:58:41 -04:00
parent 186bf508c2
commit a64bd54c18
6 changed files with 105 additions and 10 deletions

View File

@@ -55,9 +55,9 @@ add_executable(core_api core_api.cpp)
target_link_libraries(core_api Boost::openmethod)
add_test(NAME core_api COMMAND core_api)
add_executable(vectored_error_handler vectored_error_handler.cpp)
target_link_libraries(vectored_error_handler Boost::openmethod)
add_test(NAME vectored_error_handler COMMAND vectored_error_handler)
add_executable(default_error_handler default_error_handler.cpp)
target_link_libraries(default_error_handler Boost::openmethod)
add_test(NAME default_error_handler COMMAND default_error_handler)
add_executable(throw_error_handler throw_error_handler.cpp)
target_link_libraries(throw_error_handler Boost::openmethod)

View File

@@ -11,7 +11,7 @@
#include <boost/openmethod/policies/vptr_vector.hpp>
#include <boost/openmethod/policies/stderr_output.hpp>
#include <boost/openmethod/policies/fast_perfect_hash.hpp>
#include <boost/openmethod/policies/vectored_error_handler.hpp>
#include <boost/openmethod/policies/default_error_handler.hpp>
namespace boost::openmethod {
@@ -19,11 +19,11 @@ namespace policies {
struct release : registry<
std_rtti, fast_perfect_hash, vptr_vector,
vectored_error_handler, stderr_output> {};
default_error_handler, stderr_output> {};
struct debug
: registry<
std_rtti, fast_perfect_hash, vptr_vector, vectored_error_handler,
std_rtti, fast_perfect_hash, vptr_vector, default_error_handler,
runtime_checks, stderr_output, trace> {};
} // namespace policies

View File

@@ -0,0 +1,95 @@
// Copyright (c) 2018-2025 Jean-Louis Leroy
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_OPENMETHOD_POLICY_VECTORED_ERROR_HPP
#define BOOST_OPENMETHOD_POLICY_VECTORED_ERROR_HPP
#include <boost/openmethod/registry.hpp>
#include <functional>
#include <variant>
namespace boost::openmethod::policies {
struct default_error_handler : error_handler {
using error_variant = std::variant<
openmethod_error, not_implemented_error, unknown_class_error,
hash_search_error, type_mismatch_error, static_slot_error,
static_stride_error>;
using function_type = std::function<void(const error_variant& error)>;
template<class Registry>
class fn {
public:
template<class Error>
static auto error(const Error& error) -> void {
handler(error_variant(error));
}
static auto set(function_type handler) -> function_type {
auto prev = handler;
fn::handler = handler;
return prev;
}
static auto default_handler(const error_variant& error_v) {
using namespace detail;
using namespace policies;
if constexpr (Registry::template has_policy<output>) {
auto& os = Registry::template policy<policies::output>::os;
if (auto error = std::get_if<not_implemented_error>(&error_v)) {
os << "no applicable overrider for ";
Registry::template policy<policies::rtti>::type_name(
error->method, os);
os << "(";
auto comma = "";
for (auto ti :
range{error->types, error->types + error->arity}) {
os << comma;
Registry::template policy<policies::rtti>::type_name(
ti, os);
comma = ", ";
}
os << ")\n";
} else if (
auto error = std::get_if<unknown_class_error>(&error_v)) {
os << "unknown class ";
Registry::template policy<policies::rtti>::type_name(
error->type, os);
os << "\n";
} else if (
auto error = std::get_if<type_mismatch_error>(&error_v)) {
os << "invalid method table for ";
Registry::template policy<policies::rtti>::type_name(
error->type, os);
os << "\n";
} else if (
auto error = std::get_if<hash_search_error>(&error_v)) {
os << "could not find hash factors after "
<< error->attempts << "s using " << error->buckets
<< " buckets\n";
}
}
}
private:
static function_type
handler; // Cannot be inline static because it confuses MSVC
};
};
template<class Registry>
typename default_error_handler::function_type
default_error_handler::fn<Registry>::handler = default_handler;
} // namespace boost::openmethod::policies
#endif

View File

@@ -13,7 +13,7 @@
namespace boost::openmethod::policies {
struct vectored_error_handler : error_handler {
struct default_error_handler : error_handler {
using error_variant = std::variant<
openmethod_error, not_implemented_error, unknown_class_error,
hash_search_error, type_mismatch_error, static_slot_error,
@@ -87,8 +87,8 @@ struct vectored_error_handler : error_handler {
};
template<class Registry>
typename vectored_error_handler::function_type
vectored_error_handler::fn<Registry>::handler = default_handler;
typename default_error_handler::function_type
default_error_handler::fn<Registry>::handler = default_handler;
} // namespace boost::openmethod::policies

View File

@@ -523,7 +523,7 @@ BOOST_OPENMETHOD_OVERRIDE(
}
void test_handler(
const policies::vectored_error_handler::error_variant& error_v) {
const policies::default_error_handler::error_variant& error_v) {
if (auto error = std::get_if<not_implemented_error>(&error_v)) {
throw *error;
}