Commit e1834eb9 authored by Gauthier Quesnel's avatar Gauthier Quesnel
Browse files

WIP lib: adding modeling code source

parent 425a4828
Pipeline #40179 passed with stage
in 56 minutes and 45 seconds
// Copyright (c) 2020 INRA 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 ORG_VLEPROJECT_IRRITATOR_2021_MODELING_HPP
#define ORG_VLEPROJECT_IRRITATOR_2021_MODELING_HPP
#include <irritator/core.hpp>
#include <irritator/ext.hpp>
namespace irt {
enum class component_id = u64;
enum class parameter_id = u64;
struct edge;
struct vertices;
struct port;
struct component;
struct modeling;
enum class edge_type : i8
{
model, component;
};
void
destroy_vertice(modeling& m, component& parent, int index);
void
unref_vertice(modeling& m, component& parent, int index);
void
destroy_edge(modeling& m, component& c, int index);
struct edge
{
u64 src; // model_id or component_id
u64 dst; // model_id or component_id
node_type type_src; // model or component
node_type type_dst; // model or component
i8 port_src; // output port index
i8 port_dst; // input port index
};
struct vertice
{
u64 id; // model_id or component_id
node_type type;
};
struct port
{
u64 id;
node_type type;
i8 port;
};
struct component
{
vector<vertice> vertices;
vector<edge> edges;
vector<port> x;
vector<port> y;
hierarchy<component> tree;
vector<vertice> parameters;
vector<vertice> observables;
};
struct modeling
{
small_string<16> name;
data_array<component, component_id> components;
data_array<model, model_id> models;
};
}
#endif
\ No newline at end of file
// Copyright (c) 2020 INRA 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)
#include <irritator/modeling.hpp>
namespace irt {
static void
destroy_model(modeling& m, model& m);
static void
destroy_component(modeling& m, component& c);
void
destroy_vertice(modeling& m, component& parent, int index)
{
irt_assert(index >= 0 && index < c.vertices.size());
u64 id = parent.vertices[index].id;
if (parent.vertices[index].type == vertice_type::model) {
auto mdl_id = to_enum<model_id>(id);
if (auto* mdl = m.models.try_to_get(mdl_id); mdl) {
destroy_model(*mdl);
}
} else {
auto compo_id = to_enum<component_id>(id);
if (auto* compo = m.components.try_to_get(compo_id); compo) {
destroy_component(m, *compo);
}
}
c.vertices.pop_and_swap(index);
}
void
unref_vertice(modeling& m, component& parent, int index)
{
irt_assert(index >= 0 && index < c.vertices.size());
u64 id = parent.vertices[index].id;
if (parent.vertices[index].type == vertice_type::model) {
auto mdl_id = to_enum<model_id>(id);
if (auto* mdl = m.models.try_to_get(mdl_id); mdl) {
destroy_model(*mdl);
}
}
c.vertices.pop_and_swap(index);
}
void
destroy_edge(modeling& m, component& c, int index)
{
irt_assert(index >= 0 && index < c.edges.size());
c.edges.pop_and_swap(index);
}
static void
destroy_model(modeling& m, model& m)
{
m.models.free(m);
}
static void
destroy_component(modeling& m, component& c)
{
for (auto i = 0, e = c.vertices.size(); i != e; ++i) {
u64 id = parent.vertices[i].id;
if (parent.vertices[i].type == vertice_type::model) {
auto mdl_id = to_enum<model_id>(id);
m.models.free(mdl_id);
} else {
auto compo_id = to_enum<component_id>(id);
if (auto* compo = m.components.try_to_get(compo_id); compo) {
destroy_component(m, *compo);
m.components.free(*compo);
}
}
}
c.vertice.clear();
c.edge.clear();
c.x.clear();
c.y.clear();
c.tree.remove_from_hierarchy();
c.parameters.clear();
c.observable.clear();
m.components.free(c);
}
} // namespac eirt
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment