Skip to content

Commit a64bd54

Browse files
committed
vectored_error_handler -> default_error_handler
1 parent 186bf50 commit a64bd54

File tree

6 files changed

+105
-10
lines changed

6 files changed

+105
-10
lines changed

examples/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ add_executable(core_api core_api.cpp)
5555
target_link_libraries(core_api Boost::openmethod)
5656
add_test(NAME core_api COMMAND core_api)
5757

58-
add_executable(vectored_error_handler vectored_error_handler.cpp)
59-
target_link_libraries(vectored_error_handler Boost::openmethod)
60-
add_test(NAME vectored_error_handler COMMAND vectored_error_handler)
58+
add_executable(default_error_handler default_error_handler.cpp)
59+
target_link_libraries(default_error_handler Boost::openmethod)
60+
add_test(NAME default_error_handler COMMAND default_error_handler)
6161

6262
add_executable(throw_error_handler throw_error_handler.cpp)
6363
target_link_libraries(throw_error_handler Boost::openmethod)
File renamed without changes.

include/boost/openmethod/default_registry.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@
1111
#include <boost/openmethod/policies/vptr_vector.hpp>
1212
#include <boost/openmethod/policies/stderr_output.hpp>
1313
#include <boost/openmethod/policies/fast_perfect_hash.hpp>
14-
#include <boost/openmethod/policies/vectored_error_handler.hpp>
14+
#include <boost/openmethod/policies/default_error_handler.hpp>
1515

1616
namespace boost::openmethod {
1717

1818
namespace policies {
1919

2020
struct release : registry<
2121
std_rtti, fast_perfect_hash, vptr_vector,
22-
vectored_error_handler, stderr_output> {};
22+
default_error_handler, stderr_output> {};
2323

2424
struct debug
2525
: registry<
26-
std_rtti, fast_perfect_hash, vptr_vector, vectored_error_handler,
26+
std_rtti, fast_perfect_hash, vptr_vector, default_error_handler,
2727
runtime_checks, stderr_output, trace> {};
2828

2929
} // namespace policies
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright (c) 2018-2025 Jean-Louis Leroy
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// See accompanying file LICENSE_1_0.txt
4+
// or copy at http://www.boost.org/LICENSE_1_0.txt)
5+
6+
#ifndef BOOST_OPENMETHOD_POLICY_VECTORED_ERROR_HPP
7+
#define BOOST_OPENMETHOD_POLICY_VECTORED_ERROR_HPP
8+
9+
#include <boost/openmethod/registry.hpp>
10+
11+
#include <functional>
12+
#include <variant>
13+
14+
namespace boost::openmethod::policies {
15+
16+
struct default_error_handler : error_handler {
17+
using error_variant = std::variant<
18+
openmethod_error, not_implemented_error, unknown_class_error,
19+
hash_search_error, type_mismatch_error, static_slot_error,
20+
static_stride_error>;
21+
22+
using function_type = std::function<void(const error_variant& error)>;
23+
24+
template<class Registry>
25+
class fn {
26+
public:
27+
template<class Error>
28+
static auto error(const Error& error) -> void {
29+
handler(error_variant(error));
30+
}
31+
32+
static auto set(function_type handler) -> function_type {
33+
auto prev = handler;
34+
fn::handler = handler;
35+
36+
return prev;
37+
}
38+
39+
static auto default_handler(const error_variant& error_v) {
40+
using namespace detail;
41+
using namespace policies;
42+
43+
if constexpr (Registry::template has_policy<output>) {
44+
auto& os = Registry::template policy<policies::output>::os;
45+
46+
if (auto error = std::get_if<not_implemented_error>(&error_v)) {
47+
os << "no applicable overrider for ";
48+
Registry::template policy<policies::rtti>::type_name(
49+
error->method, os);
50+
os << "(";
51+
auto comma = "";
52+
53+
for (auto ti :
54+
range{error->types, error->types + error->arity}) {
55+
os << comma;
56+
Registry::template policy<policies::rtti>::type_name(
57+
ti, os);
58+
comma = ", ";
59+
}
60+
61+
os << ")\n";
62+
} else if (
63+
auto error = std::get_if<unknown_class_error>(&error_v)) {
64+
os << "unknown class ";
65+
Registry::template policy<policies::rtti>::type_name(
66+
error->type, os);
67+
os << "\n";
68+
} else if (
69+
auto error = std::get_if<type_mismatch_error>(&error_v)) {
70+
os << "invalid method table for ";
71+
Registry::template policy<policies::rtti>::type_name(
72+
error->type, os);
73+
os << "\n";
74+
} else if (
75+
auto error = std::get_if<hash_search_error>(&error_v)) {
76+
os << "could not find hash factors after "
77+
<< error->attempts << "s using " << error->buckets
78+
<< " buckets\n";
79+
}
80+
}
81+
}
82+
83+
private:
84+
static function_type
85+
handler; // Cannot be inline static because it confuses MSVC
86+
};
87+
};
88+
89+
template<class Registry>
90+
typename default_error_handler::function_type
91+
default_error_handler::fn<Registry>::handler = default_handler;
92+
93+
} // namespace boost::openmethod::policies
94+
95+
#endif

include/boost/openmethod/policies/vectored_error_handler.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace boost::openmethod::policies {
1515

16-
struct vectored_error_handler : error_handler {
16+
struct default_error_handler : error_handler {
1717
using error_variant = std::variant<
1818
openmethod_error, not_implemented_error, unknown_class_error,
1919
hash_search_error, type_mismatch_error, static_slot_error,
@@ -87,8 +87,8 @@ struct vectored_error_handler : error_handler {
8787
};
8888

8989
template<class Registry>
90-
typename vectored_error_handler::function_type
91-
vectored_error_handler::fn<Registry>::handler = default_handler;
90+
typename default_error_handler::function_type
91+
default_error_handler::fn<Registry>::handler = default_handler;
9292

9393
} // namespace boost::openmethod::policies
9494

test/test_blackbox.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ BOOST_OPENMETHOD_OVERRIDE(
523523
}
524524

525525
void test_handler(
526-
const policies::vectored_error_handler::error_variant& error_v) {
526+
const policies::default_error_handler::error_variant& error_v) {
527527
if (auto error = std::get_if<not_implemented_error>(&error_v)) {
528528
throw *error;
529529
}

0 commit comments

Comments
 (0)