Skip to content

Commit

Permalink
mod: improve generic component error handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
quesnel committed May 24, 2024
1 parent 5f100d5 commit 8cc1067
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 9 deletions.
43 changes: 43 additions & 0 deletions lib/include/irritator/modeling.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,11 @@ class generic_component
using child_limiter = static_limiter<i32, 64, 64 * 16>;
using connection_limiter = static_limiter<i32, 64 * 4, 64 * 16 * 4>;

struct children_error {};
struct connection_error {};
struct input_connection_error {};
struct output_connection_error {};

generic_component() noexcept;

generic_component(const child_limiter child_limit,
Expand Down Expand Up @@ -393,6 +398,15 @@ class generic_component
const std::span<parameter> parameters = {}) noexcept;

u64 make_next_unique_id() const noexcept { return next_unique_id++; }

static auto build_error_handlers(log_manager& l) noexcept;
static void format_connection_error(log_entry& e) noexcept;
static void format_connection_full_error(log_entry& e) noexcept;
static void format_input_connection_error(log_entry& e) noexcept;
static void format_input_connection_full_error(log_entry& e) noexcept;
static void format_output_connection_error(log_entry& e) noexcept;
static void format_output_connection_full_error(log_entry& e) noexcept;
static void format_children_error(log_entry& e) noexcept;
};

struct grid_component {
Expand Down Expand Up @@ -1743,6 +1757,35 @@ inline auto graph_component::build_error_handlers(log_manager& l) noexcept
});
}

inline auto generic_component::build_error_handlers(log_manager& l) noexcept
{
return std::make_tuple(
[&](connection_error, container_full_error) {
l.push([&](auto& e) { format_input_connection_error(e); });
},
[&](connection_error, already_exist_error) {
l.push([&](auto& e) { format_input_connection_error(e); });
},
[&](connection_error, container_full_error) {
l.push([&](auto& e) { format_input_connection_error(e); });
},
[&](input_connection_error, already_exist_error) {
l.push([&](auto& e) { format_input_connection_error(e); });
},
[&](input_connection_error, container_full_error) {
l.push([&](auto& e) { format_input_connection_full_error(e); });
},
[&](output_connection_error, already_exist_error) {
l.push([&](auto& e) { format_output_connection_error(e); });
},
[&](output_connection_error, container_full_error) {
l.push([&](auto& e) { format_output_connection_full_error(e); });
},
[&](children_error, container_full_error) {
l.push([&](auto& e) { format_children_error(e); });
});
}

} // namespace irt

#endif
70 changes: 61 additions & 9 deletions lib/src/modeling-generic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ result<child_id> generic_component::copy_to(
const auto src_idx = get_index(src_id);

if (not dst.children.can_alloc())
return new_error(modeling::part::children);
return new_error(children_error{}, container_full_error{});

auto& new_c = dst.children.alloc();
const auto new_c_id = dst.children.get_id(new_c);
Expand Down Expand Up @@ -136,18 +136,18 @@ status generic_component::connect([[maybe_unused]] const modeling& mod,
const connection::port p_dst) noexcept
{
if (exists(src, p_src, dst, p_dst))
return new_error(modeling::part::connections);
return new_error(connection_error{}, already_exist_error{});

if (src.type == child_type::model) {
if (dst.type == child_type::model) {
if (not is_ports_compatible(
src.id.mdl_type, p_src.model, dst.id.mdl_type, p_dst.model))
return new_error(modeling::part::connections);
return new_error(connection_error{}, incompatibility_error{});
}
}

if (not connections.can_alloc(1))
return new_error(modeling::part::connections);
return new_error(connection_error{}, container_full_error{});

connections.alloc(children.get_id(src), p_src, children.get_id(dst), p_dst);

Expand All @@ -159,7 +159,11 @@ status generic_component::connect_input(const port_id x,
const connection::port port) noexcept
{
if (not input_connections.can_alloc(1))
return new_error(modeling::part::connections);
return new_error(input_connection_error{}, container_full_error{});

for (const auto& con : input_connections)
if (con.x == x and con.dst == children.get_id(dst) and con.port == port)
return new_error(input_connection_error{}, already_exist_error{});

input_connections.alloc(x, children.get_id(dst), port);

Expand All @@ -171,7 +175,11 @@ status generic_component::connect_output(const port_id y,
const connection::port port) noexcept
{
if (not output_connections.can_alloc(1))
return new_error(modeling::part::connections);
return new_error(output_connection_error{}, container_full_error{});

for (const auto& con : output_connections)
if (con.y == y and con.src == children.get_id(src) and con.port == port)
return new_error(output_connection_error{}, already_exist_error{});

output_connections.alloc(y, children.get_id(src), port);

Expand All @@ -187,10 +195,10 @@ status generic_component::import(
{
table<child_id, child_id> src_to_this;

for (const auto& c : children) {
if (not this->children.can_alloc())
return new_error(modeling::part::children);
if (not this->children.can_alloc(children.size()))
return new_error(children_error{}, container_full_error{});

for (const auto& c : children) {
auto& new_c = this->children.alloc();
new_c.type = c.type;
new_c.id = c.id;
Expand Down Expand Up @@ -256,4 +264,48 @@ status generic_component::import(
return success();
}

void generic_component::format_connection_error(log_entry& e) noexcept
{
e.buffer = "Internal connection already exists in this generic component";
e.level = log_level::notice;
}

void generic_component::format_connection_full_error(log_entry& e) noexcept
{
e.buffer = "Internal connection list is full in this generic component";
e.level = log_level::error;
}

void generic_component::format_input_connection_error(log_entry& e) noexcept
{
e.buffer = "Input connection already exists in this generic component";
e.level = log_level::notice;
}

void generic_component::format_input_connection_full_error(
log_entry& e) noexcept
{
e.buffer = "Input connection list is full in this generic component";
e.level = log_level::error;
}

void generic_component::format_output_connection_error(log_entry& e) noexcept
{
e.buffer = "Input connection already exists in this generic component";
e.level = log_level::notice;
}

void generic_component::format_output_connection_full_error(
log_entry& e) noexcept
{
e.buffer = "Output connection list is full in this generic component";
e.level = log_level::error;
}

void generic_component::format_children_error(log_entry& e) noexcept
{
e.buffer = "Not enough available space for model in this generic component";
e.level = log_level::error;
}

} // namespace irt

0 comments on commit 8cc1067

Please sign in to comment.