Skip to content

Commit

Permalink
headers only - big
Browse files Browse the repository at this point in the history
  • Loading branch information
jll63 committed Jul 22, 2023
1 parent dbf07e5 commit e342d40
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 128 deletions.
141 changes: 119 additions & 22 deletions include/yorel/yomm2/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define YOREL_YOMM2_CORE_INCLUDED

#include <chrono>
#include <iostream>
#include <memory>
#include <string_view>
#include <variant>
Expand Down Expand Up @@ -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
Expand All @@ -78,7 +93,97 @@ union word {

using ti_ptr = const std::type_info*;

extern yOMM2_API error_handler_type error_handler;
template<typename Iterator>
struct range {
Iterator first, last;
Iterator begin() const {
return first;
}
Iterator end() const {
return last;
}
};

template<typename Iterator>
range(Iterator b, Iterator e) -> range<Iterator>;

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<const ti_ptr*>& 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<resolution_error>(&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<unknown_class_error>(&error_v)) {
if constexpr (bool(debug)) {
std::cerr << "unknown class " << error->ti->name() << "\n";
}
abort();
}

if (auto error = std::get_if<method_table_error>(&error_v)) {
if constexpr (bool(debug)) {
std::cerr << "invalid method table for " << error->ti->name()
<< "\n";
}
abort();
}

if (auto error = std::get_if<hash_search_error>(&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;
Expand Down Expand Up @@ -149,28 +254,17 @@ class virtual_ptr;
} // namespace yomm2
} // namespace yorel

#include <yorel/yomm2/detail.hpp>
#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<typename T>
struct virtual_;
Expand Down Expand Up @@ -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<class Policy>
struct with_method_tables {
template<typename>
Expand Down
106 changes: 0 additions & 106 deletions include/yorel/yomm2/runtime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include <cstdlib> // for abort, getenv
#include <deque>
#include <iomanip> // for operator<<, setw
#include <iostream> // for operator<<, ostream
#include <iterator> // for back_insert_iterator
#include <list> // for list, _List_iterator
#include <map> // for map
Expand Down Expand Up @@ -125,40 +124,6 @@ struct with_indent {
}
};

template<typename Iterator>
struct range {
Iterator first, last;
Iterator begin() const {
return first;
}
Iterator end() const {
return last;
}
};

template<typename Iterator>
range(Iterator b, Iterator e) -> range<Iterator>;

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<const ti_ptr*>& 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;
Expand Down Expand Up @@ -1200,63 +1165,6 @@ void runtime<Policy>::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<resolution_error>(&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<unknown_class_error>(&error_v)) {
if constexpr (bool(debug)) {
std::cerr << "unknown class " << error->ti->name() << "\n";
}
abort();
}

if (auto error = std::get_if<method_table_error>(&error_v)) {
if constexpr (bool(debug)) {
std::cerr << "invalid method table for " << error->ti->name()
<< "\n";
}
abort();
}

if (auto error = std::get_if<hash_search_error>(&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<class Policy>
Expand All @@ -1269,26 +1177,12 @@ inline void update() {
update<default_policy>();
}

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

Expand Down

0 comments on commit e342d40

Please sign in to comment.