From e342d409906ad0314fb65e6b939404c8277ba31c Mon Sep 17 00:00:00 2001 From: Jean-Louis Leroy Date: Fri, 21 Jul 2023 22:03:56 -0400 Subject: [PATCH] headers only - big --- include/yorel/yomm2/core.hpp | 141 +++++++++++++++++++++++++++----- include/yorel/yomm2/runtime.hpp | 106 ------------------------ 2 files changed, 119 insertions(+), 128 deletions(-) diff --git a/include/yorel/yomm2/core.hpp b/include/yorel/yomm2/core.hpp index 6775dfeb..bb47d9ca 100644 --- a/include/yorel/yomm2/core.hpp +++ b/include/yorel/yomm2/core.hpp @@ -2,6 +2,7 @@ #define YOREL_YOMM2_CORE_INCLUDED #include +#include #include #include #include @@ -58,9 +59,23 @@ using error_type = std::variant< method_table_error>; using error_handler_type = void (*)(const error_type& error); - error_handler_type set_error_handler(error_handler_type handler); +// deprecated + +struct method_call_error { + resolution_error::status_type code; + static constexpr auto not_implemented = resolution_error::no_definition; + static constexpr auto ambiguous = resolution_error::ambiguous; + std::string_view method_name; +}; + +using method_call_error_handler = void (*)( + const method_call_error& error, size_t arity, + const std::type_info* const tis[]); + +// end deprecated + namespace detail { #ifdef NDEBUG @@ -78,7 +93,97 @@ union word { using ti_ptr = const std::type_info*; -extern yOMM2_API error_handler_type error_handler; +template +struct range { + Iterator first, last; + Iterator begin() const { + return first; + } + Iterator end() const { + return last; + } +}; + +template +range(Iterator b, Iterator e) -> range; + +struct tip { + const ti_ptr ptr; +}; + +inline std::ostream& operator<<(std::ostream& os, tip t) { + return os << t.ptr->name() << "(" << t.ptr << ")"; +} + +inline std::ostream& +operator<<(std::ostream& os, const range& tips) { + os << "("; + const char* sep = ""; + for (auto t : tips) { + os << sep << tip{t}; + sep = ", "; + } + + return os << ")"; +} + +inline void default_method_call_error_handler( + const method_call_error& error, size_t arity, const ti_ptr ti_ptrs[]) { + if constexpr (bool(debug)) { + const char* explanation[] = { + "no applicable definition", "ambiguous call"}; + std::cerr << explanation[error.code - resolution_error::no_definition] + << " for " << error.method_name << "("; + auto comma = ""; + for (auto ti : range{ti_ptrs, ti_ptrs + arity}) { + std::cerr << comma << ti->name(); + comma = ", "; + } + std::cerr << ")\n" << std::flush; + } + abort(); +} + +inline method_call_error_handler method_call_error_handler_p = + default_method_call_error_handler; + +inline void default_error_handler(const error_type& error_v) { + if (auto error = std::get_if(&error_v)) { + method_call_error old_error; + old_error.code = error->status; + old_error.method_name = error->method->name(); + method_call_error_handler_p( + std::move(old_error), error->arity, error->tis); + abort(); + } + + if (auto error = std::get_if(&error_v)) { + if constexpr (bool(debug)) { + std::cerr << "unknown class " << error->ti->name() << "\n"; + } + abort(); + } + + if (auto error = std::get_if(&error_v)) { + if constexpr (bool(debug)) { + std::cerr << "invalid method table for " << error->ti->name() + << "\n"; + } + abort(); + } + + if (auto error = std::get_if(&error_v)) { + if constexpr (bool(debug)) { + std::cerr << "could not find hash factors after " << error->attempts + << " in " << error->duration.count() << "s using " + << error->buckets << " buckets\n"; + } + abort(); + } +} + +inline error_handler_type yOMM2_API error_handler = + detail::default_error_handler; struct hash_function { std::uintptr_t mult; @@ -149,28 +254,17 @@ class virtual_ptr; } // namespace yomm2 } // namespace yorel -#include +#include "detail.hpp" namespace yorel { namespace yomm2 { -// deprecated - -struct method_call_error { - resolution_error::status_type code; - static constexpr auto not_implemented = resolution_error::no_definition; - static constexpr auto ambiguous = resolution_error::ambiguous; - std::string_view method_name; -}; - -using method_call_error_handler = void (*)( - const method_call_error& error, size_t arity, - const std::type_info* const tis[]); - -yOMM2_API method_call_error_handler -set_method_call_error_handler(method_call_error_handler handler); - -// end deprecated +inline method_call_error_handler yOMM2_API +set_method_call_error_handler(method_call_error_handler handler) { + auto prev = detail::method_call_error_handler_p; + detail::method_call_error_handler_p = handler; + return prev; +} template struct virtual_; @@ -200,14 +294,17 @@ struct abstract_policy { ; }; -struct yOMM2_API global_context : virtual abstract_policy { +struct global_context : virtual abstract_policy { static struct context context; }; -struct yOMM2_API global_catalog : virtual abstract_policy { +struct global_catalog : virtual abstract_policy { static struct catalog catalog; }; +inline catalog yOMM2_API global_catalog::catalog; +inline context yOMM2_API global_context::context; + template struct with_method_tables { template diff --git a/include/yorel/yomm2/runtime.hpp b/include/yorel/yomm2/runtime.hpp index 7fa754b7..5aeeef9e 100644 --- a/include/yorel/yomm2/runtime.hpp +++ b/include/yorel/yomm2/runtime.hpp @@ -13,7 +13,6 @@ #include // for abort, getenv #include #include // for operator<<, setw -#include // for operator<<, ostream #include // for back_insert_iterator #include // for list, _List_iterator #include // for map @@ -125,40 +124,6 @@ struct with_indent { } }; -template -struct range { - Iterator first, last; - Iterator begin() const { - return first; - } - Iterator end() const { - return last; - } -}; - -template -range(Iterator b, Iterator e) -> range; - -struct tip { - const ti_ptr ptr; -}; - -inline std::ostream& operator<<(std::ostream& os, tip t) { - return os << t.ptr->name() << "(" << t.ptr << ")"; -} - -inline std::ostream& -operator<<(std::ostream& os, const range& tips) { - os << "("; - const char* sep = ""; - for (auto t : tips) { - os << sep << tip{t}; - sep = ", "; - } - - return os << ")"; -} - inline std::ostream* log_on(std::ostream* os) { auto prev = logs; logs = os; @@ -1200,63 +1165,6 @@ void runtime::print(const dispatch_stats_t& stats) const { trace << stats.concrete_ambiguous << "\n"; } -inline void default_method_call_error_handler( - const method_call_error& error, size_t arity, const ti_ptr ti_ptrs[]) { - if constexpr (bool(debug)) { - const char* explanation[] = { - "no applicable definition", "ambiguous call"}; - std::cerr << explanation[error.code - resolution_error::no_definition] - << " for " << error.method_name << "("; - auto comma = ""; - for (auto ti : range{ti_ptrs, ti_ptrs + arity}) { - std::cerr << comma << ti->name(); - comma = ", "; - } - std::cerr << ")\n" << std::flush; - } - abort(); -} - -inline method_call_error_handler method_call_error_handler_p = - default_method_call_error_handler; - -inline void default_error_handler(const error_type& error_v) { - if (auto error = std::get_if(&error_v)) { - method_call_error old_error; - old_error.code = error->status; - old_error.method_name = error->method->name(); - method_call_error_handler_p( - std::move(old_error), error->arity, error->tis); - abort(); - } - - if (auto error = std::get_if(&error_v)) { - if constexpr (bool(debug)) { - std::cerr << "unknown class " << error->ti->name() << "\n"; - } - abort(); - } - - if (auto error = std::get_if(&error_v)) { - if constexpr (bool(debug)) { - std::cerr << "invalid method table for " << error->ti->name() - << "\n"; - } - abort(); - } - - if (auto error = std::get_if(&error_v)) { - if constexpr (bool(debug)) { - std::cerr << "could not find hash factors after " << error->attempts - << " in " << error->duration.count() << "s using " - << error->buckets << " buckets\n"; - } - abort(); - } -} - -inline error_handler_type error_handler = default_error_handler; - } // namespace detail template @@ -1269,26 +1177,12 @@ inline void update() { update(); } -namespace policy { - -inline catalog global_catalog::catalog; -inline context global_context::context; - -} // namespace policy - inline error_handler_type set_error_handler(error_handler_type handler) { auto prev = detail::error_handler; detail::error_handler = handler; return prev; } -inline method_call_error_handler -set_method_call_error_handler(method_call_error_handler handler) { - auto prev = detail::method_call_error_handler_p; - detail::method_call_error_handler_p = handler; - return prev; -} - } // namespace yomm2 } // namespace yorel