diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 3d40852e..5b9bbf09 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -8,4 +8,3 @@ add_subdirectory(ports) add_subdirectory(hello) add_subdirectory(power_train) add_subdirectory(multiport_mutation) -add_subdirectory(unit_tests_mutations) diff --git a/examples/count/main.cc b/examples/count/main.cc index ca20df5d..08012a2b 100644 --- a/examples/count/main.cc +++ b/examples/count/main.cc @@ -5,15 +5,15 @@ using namespace reactor; using namespace std::chrono_literals; -class Count : public Reactor { +class Count final : public Reactor { private: // actions Timer timer{"timer", this}; LogicalAction counter{"counter", this}; // reactions_ - Reaction r_init{"r_init", 1, false, this, [this]() { this->init(); }}; - Reaction r_counter{"r_counter", 2, false, this, [this]() { this->print_count(); }}; + Reaction r_init{"r_init", 1, this, [this]() { this->init(); }}; + Reaction r_counter{"r_counter", 2, this, [this]() { this->print_count(); }}; public: explicit Count(Environment* env) diff --git a/examples/hello/main.cc b/examples/hello/main.cc index 3b02fd89..2f6c88f8 100644 --- a/examples/hello/main.cc +++ b/examples/hello/main.cc @@ -6,7 +6,7 @@ using namespace reactor; using namespace std::chrono_literals; -class Hello : public Reactor { +class Hello final : public Reactor { private: // actions Timer timer{"timer", this, 1s, 2s}; diff --git a/examples/multiport_mutation/consumer.hh b/examples/multiport_mutation/consumer.hh index b2ac7324..59107611 100644 --- a/examples/multiport_mutation/consumer.hh +++ b/examples/multiport_mutation/consumer.hh @@ -1,46 +1,39 @@ -// -// Created by tanneberger on 11/17/24. -// - -#ifndef CONSUMER_HH -#define CONSUMER_HH +#ifndef CONSUMER_HH //NOLINT +#define CONSUMER_HH //NOLINT #include using namespace reactor; using namespace std::chrono_literals; -class Consumer : public Reactor { -private: - class Inner: public Scope { - Inner(Reactor* reactor, std::size_t index) : Scope(reactor), index_(index) {} - std::size_t index_; - - [[maybe_unused]] const Inner& __lf_inner = *this; +class Consumer final: public Reactor { // NOLINT + class Inner : public Scope { + Inner(Reactor* reactor, std::size_t index) + : Scope(reactor) + , index_(index) {} + std::size_t index_ = 0; - void reaction_1([[maybe_unused]] const Input& in) { - std::cout << "consumer: " << index_ << " received value:" << *in.get() << std::endl; + void reaction_1(const Input& in) const { + std::cout << "consumer: " << index_ << " received value:" << *in.get() << '\n'; } friend Consumer; }; - Inner __lf_inner; - Reaction handle{"handle", 1, this, [this]() { __lf_inner.reaction_1(this->in); }}; + Inner _lf_inner; + Reaction handle{"handle", 1, this, [this]() { _lf_inner.reaction_1(this->in); }}; + public: - Consumer(const std::string& name, Environment* env, std::size_t index) : Reactor(name, env), __lf_inner(this, index) { - std::cout << "creating instance of consumer" << std::endl; + Consumer(const std::string& name, Environment* env, std::size_t index) + : Reactor(name, env) + , _lf_inner(this, index) { + std::cout << "creating instance of consumer" << '\n'; } - ~Consumer() override { - std::cout << "Consumer Object is deleted" << std::endl; - }; + ~Consumer() override { std::cout << "Consumer Object is deleted" << '\n'; }; - Input in{"in", this}; + Input in{"in", this}; //NOLINT - void assemble() override { - handle.declare_trigger(&in); - } + void assemble() override { handle.declare_trigger(&in); } }; - -#endif //CONSUMER_HH +#endif // CONSUMER_HH diff --git a/examples/multiport_mutation/load_balancer.hh b/examples/multiport_mutation/load_balancer.hh index 8e744279..6754355f 100644 --- a/examples/multiport_mutation/load_balancer.hh +++ b/examples/multiport_mutation/load_balancer.hh @@ -1,9 +1,5 @@ -// -// Created by tanneberger on 11/17/24. -// - -#ifndef LOAD_BALANCER_HH -#define LOAD_BALANCER_HH +#ifndef LOAD_BALANCER_HH //NOLINT +#define LOAD_BALANCER_HH //NOLINT #include @@ -13,29 +9,31 @@ using namespace reactor; using namespace std::chrono_literals; -class LoadBalancer : public Reactor { -private: - class Inner: public MutableScope { - Inner(Reactor* reactor) : MutableScope(reactor) {} - [[maybe_unused]] const Inner& __lf_inner = *this; +class LoadBalancer final: public Reactor { + class Inner : public MutableScope { + explicit Inner(Reactor* reactor) + : MutableScope(reactor) {} // reaction bodies - void reaction_1(const Input& inbound, LogicalAction& scale_action, Multiport>& outbound) { - if (rand() % 30 == 0) { - scale_action.schedule(rand() % 20 + 1); + static void reaction_1(const Input& inbound, LogicalAction& scale_action, + Multiport>& outbound) { + if (std::rand() % 30 == 0) { //NOLINT + scale_action.schedule(std::rand() % 20 + 1); //NOLINT } - unsigned sel = rand() % outbound.size(); - std::cout << "Sending out to:" << sel << std::endl; + const unsigned sel = std::rand() % outbound.size(); //NOLINT + std::cout << "Sending out to:" << sel << '\n'; outbound[sel].set(inbound.get()); } - void reaction_2(ModifableMultiport>&outbound, [[maybe_unused]] const LogicalAction& scale, Output& scale_bank) { + void reaction_2(ModifableMultiport>& outbound, + [[maybe_unused]] const LogicalAction& scale, Output& scale_bank) { ModifableMultiport>* temp = &outbound; std::size_t new_size = *scale.get(); auto antideps = (outbound[0]).anti_dependencies(); - auto change_size = std::make_shared>(temp, this->reactor_, antideps, new_size); + const auto change_size = + std::make_shared>(temp, this->reactor_, antideps, new_size); add_to_transaction(change_size); @@ -47,30 +45,35 @@ private: friend LoadBalancer; }; - Inner __lf_inner; - Reaction process{"process", 2, this, [this]() { __lf_inner.reaction_1(this->inbound, this->scale_action, this->out); }}; - Reaction scale{"scale", 1, this, [this]() { __lf_inner.reaction_2(this->out, this->scale_action, this->scale_bank); }}; + Inner _lf_inner; + Reaction process{"process", 2, this, + [this]() { Inner::reaction_1(this->inbound, this->scale_action, this->out); }}; + Reaction scale{"scale", 1, this, + [this]() { _lf_inner.reaction_2(this->out, this->scale_action, this->scale_bank); }}; public: LoadBalancer(const std::string& name, Environment* env) - : Reactor(name, env), __lf_inner(this) { - std::cout << "creating instance of load balancer" << std::endl; + : Reactor(name, env) + , _lf_inner(this) { + std::cout << "creating instance of load balancer" << '\n'; out.reserve(4); for (size_t _lf_idx = 0; _lf_idx < 4; _lf_idx++) { - std::string _lf_port_name = out.name() + "_" + std::to_string(_lf_idx); + std::string _lf_port_name = out.name() + "_" + std::to_string(_lf_idx); out.emplace_back(_lf_port_name, this); } } + ~LoadBalancer() override = default; + - LogicalAction scale_action{"scale", this, 1us}; - ModifableMultiport> out{"out"}; + LogicalAction scale_action{"scale", this, 1us}; //NOLINT + ModifableMultiport> out{"out"}; //NOLINT Input inbound{"inbound", this}; // NOLINT - Output scale_bank{"scale_bank", this}; + Output scale_bank{"scale_bank", this}; //NOLINT void assemble() override { std::cout << "assemble LoadBalancer\n"; - for (auto& __lf_port : out) { - process.declare_antidependency(&__lf_port); + for (auto& _lf_port : out) { + process.declare_antidependency(&_lf_port); } process.declare_trigger(&inbound); scale.declare_trigger(&scale_action); @@ -78,6 +81,4 @@ public: } }; - - -#endif //LOAD_BALANCER_HH +#endif // LOAD_BALANCER_HH diff --git a/examples/multiport_mutation/main.cc b/examples/multiport_mutation/main.cc index 3956b8e6..c3339ac9 100644 --- a/examples/multiport_mutation/main.cc +++ b/examples/multiport_mutation/main.cc @@ -3,73 +3,71 @@ #include #include +#include "../../lib/mutation/bank.cc" +#include "../../lib/mutation/connection.cc" #include "./consumer.hh" #include "./load_balancer.hh" #include "./producer.hh" -#include "../../lib/mutation/bank.cc" -#include "../../lib/mutation/connection.cc" #include -class Deployment : public Reactor { +class Deployment final : public Reactor { //NOLINT std::unique_ptr producer_; std::unique_ptr load_balancer_; std::vector> consumers_; - Reaction scale_bank{"scale_bank", 1, this, [this](){this->__inner.reaction_1(this->scale, this->consumers_, load_balancer_->out);}}; + Reaction scale_bank{"scale_bank", 1, this, + [this]() { this->_inner.reaction_1(this->scale, this->consumers_, load_balancer_->out); }}; public: - -class Inner: public MutableScope { + class Inner : public MutableScope { int state = 0; - [[maybe_unused]] const Inner& __lf_inner = *this; -public: - Inner(Reactor* reactor) : MutableScope(reactor) {} - void reaction_1(const Input& scale, std::vector>& reactor_bank, ModifableMultiport>& load_balancer) { + public: + Inner(Reactor* reactor) + : MutableScope(reactor) {} + void reaction_1(const Input& scale, std::vector>& reactor_bank, + ModifableMultiport>& load_balancer) { std::size_t new_size = *scale.get(); std::size_t old_size = reactor_bank.size(); - std::function(Reactor*, std::size_t)> lambda = [](Reactor* reactor, std::size_t index) { - std::string __lf_inst_name = "consumer_" + std::to_string(index); - return std::make_unique(__lf_inst_name, reactor->environment(), index); + std::function lambda = [](Reactor* reactor, std::size_t index) { + std::string _lf_inst_name = "consumer_" + std::to_string(index); + return std::make_unique(_lf_inst_name, reactor->environment(), index); }; - auto change_size = std::make_shared>>(&reactor_bank, this->reactor_, new_size, lambda); + + auto change_size = std::make_shared>>( + &reactor_bank, this->reactor_, new_size, lambda); add_to_transaction(change_size); - // old topology commit_transaction(); - // new topology - - if (old_size > new_size) { - for (auto i = 0; i < old_size - new_size; i++) { - } - } else { - std::cout << "load_balancer size:" << load_balancer.size() << " bank size: " << reactor_bank.size() << std::endl; + if (old_size < new_size) { for (auto i = 0; i < new_size; i++) { - auto add_conn = std::make_shared, Input>>(&load_balancer[i], &reactor_bank[i].get()->in, reactor_); - add_to_transaction(add_conn); + auto add_conn = std::make_shared, Input>>( + &load_balancer[i], &reactor_bank[i].get()->in, reactor_); + add_to_transaction(add_conn); } - commit_transaction(true); } - std::cout << "new bank size:" << reactor_bank.size() << std::endl; + commit_transaction(true); } friend LoadBalancer; }; - Inner __inner; + Inner _inner; - Deployment(const std::string& name, Environment* env) : Reactor(name, env), __inner(this), - producer_(std::make_unique("producer", environment())), - load_balancer_(std::make_unique("load_balancer", environment())) { - std::cout << "creating instance of deployment" << std::endl; + Deployment(const std::string& name, Environment* env) + : Reactor(name, env) + , _inner(this) + , producer_(std::make_unique("producer", environment())) + , load_balancer_(std::make_unique("load_balancer", environment())) { + std::cout << "creating instance of deployment" << '\n'; consumers_.reserve(4); - for (size_t __lf_idx = 0; __lf_idx < 4; __lf_idx++) { - std::string __lf_inst_name = "consumer_" + std::to_string(__lf_idx); - consumers_.push_back(std::make_unique(__lf_inst_name, environment(), __lf_idx)); + for (size_t _lf_idx = 0; _lf_idx < 4; _lf_idx++) { + std::string _lf_inst_name = "consumer_" + std::to_string(_lf_idx); + consumers_.push_back(std::make_unique(_lf_inst_name, environment(), _lf_idx)); } } ~Deployment() override = default; @@ -77,8 +75,8 @@ class Inner: public MutableScope { Input scale{"scale", this}; void assemble() override { - for (size_t __lf_idx = 0; __lf_idx < 4; __lf_idx++) { - environment()->draw_connection(load_balancer_->out[__lf_idx], consumers_[__lf_idx]->in, ConnectionProperties{}); + for (size_t _lf_idx = 0; _lf_idx < 4; _lf_idx++) { + environment()->draw_connection(load_balancer_->out[_lf_idx], consumers_[_lf_idx]->in, ConnectionProperties{}); environment()->draw_connection(producer_->value, load_balancer_->inbound, ConnectionProperties{}); } environment()->draw_connection(load_balancer_->scale_bank, scale, ConnectionProperties{}); @@ -86,9 +84,7 @@ class Inner: public MutableScope { } }; - auto main() -> int { - //srand(time(nullptr)); Environment env{4, true}; auto deployment = std::make_unique("c1", &env); env.optimize(); diff --git a/examples/multiport_mutation/producer.hh b/examples/multiport_mutation/producer.hh index 7a998cbb..b2e9fbcd 100644 --- a/examples/multiport_mutation/producer.hh +++ b/examples/multiport_mutation/producer.hh @@ -1,9 +1,5 @@ -// -// Created by tanneberger on 11/17/24. -// - -#ifndef PRODUCER_HH -#define PRODUCER_HH +#ifndef PRODUCER_HH //NOLINT +#define PRODUCER_HH //NOLINT #include @@ -13,29 +9,34 @@ using namespace std::chrono_literals; class Producer : public Reactor { private: Timer timer{"timer", this, 1s, 1s}; - Reaction r_timer{"r_timer", 1, this, [this]() { __lf_inner.reaction_1(this->value);}}; + Reaction r_timer{"r_timer", 1, this, [this]() { _lf_inner.reaction_1(this->value); }}; + + class Inner : public Scope { + unsigned int counter_ = 0; - class Inner: public Scope { - unsigned itr = 0; - [[maybe_unused]] const Inner& __lf_inner = *this; void reaction_1([[maybe_unused]] Output& out) { - std::cout << "producing value:" << itr << std::endl; - out.set(itr++); + std::cout << "producing value:" << counter_ << "\n"; + out.set(counter_++); } - explicit Inner(Reactor* reactor) : Scope(reactor) {} + + explicit Inner(Reactor* reactor) + : Scope(reactor) {} friend Producer; }; - Inner __lf_inner; + Inner _lf_inner; + public: - Producer(const std::string& name, Environment* env) : Reactor(name, env), __lf_inner(this) { - std::cout << "creating instance of producer" << std::endl; + Producer(const std::string& name, Environment* env) + : Reactor(name, env) + , _lf_inner(this) { + std::cout << "creating instance of producer\n"; } Producer() = delete; ~Producer() override = default; - Output value{"value", this}; + Output value{"value", this}; // NOLINT void assemble() override { r_timer.declare_trigger(&timer); @@ -43,4 +44,4 @@ public: } }; -#endif //PRODUCER_HH +#endif // PRODUCER_HH diff --git a/examples/ports/main.cc b/examples/ports/main.cc index d1dda5d1..cc164d85 100644 --- a/examples/ports/main.cc +++ b/examples/ports/main.cc @@ -5,7 +5,7 @@ using namespace reactor; using namespace std::chrono_literals; -class Trigger : public Reactor { +class Trigger final : public Reactor { private: Timer timer; Reaction r_timer{"r_timer", 1, this, [this]() { on_timer(); }}; @@ -25,7 +25,7 @@ class Trigger : public Reactor { void on_timer() { trigger.set(); } }; -class Counter : public Reactor { +class Counter final : public Reactor { private: int value_{0}; Reaction r_trigger{"r_trigger", 1, this, [this]() { on_trigger(); }}; @@ -49,7 +49,7 @@ class Counter : public Reactor { } }; -class Printer : public Reactor { +class Printer final : public Reactor { private: Reaction r_value{"r_value", 1, this, [this]() { this->on_value(); }}; @@ -67,7 +67,7 @@ class Printer : public Reactor { void on_value() { std::cout << this->name() << ": " << *value.get() << '\n'; } }; -class Adder : public Reactor { +class Adder final : public Reactor { private: Reaction r_add{"r_add", 1, this, [this]() { this->add(); }}; diff --git a/examples/power_train/main.cc b/examples/power_train/main.cc index 155c3501..7badae84 100644 --- a/examples/power_train/main.cc +++ b/examples/power_train/main.cc @@ -4,7 +4,7 @@ using namespace reactor; -class LeftPedal : public Reactor { +class LeftPedal final : public Reactor { public: // ports Output angle{"angle", this}; // NOLINT @@ -30,7 +30,7 @@ class LeftPedal : public Reactor { } }; -class RightPedal : public Reactor { +class RightPedal final : public Reactor { public: // ports Output angle{"angle", this}; // NOLINT @@ -60,7 +60,7 @@ class RightPedal : public Reactor { } }; -class BrakeControl : public Reactor { +class BrakeControl final : public Reactor { public: // ports Input angle{"angle", this}; // NOLINT @@ -81,7 +81,7 @@ class BrakeControl : public Reactor { } }; -class EngineControl : public Reactor { +class EngineControl final : public Reactor { public: // ports Input angle{"angle", this}; // NOLINT @@ -118,7 +118,7 @@ class EngineControl : public Reactor { } }; -class Brake : public Reactor { +class Brake final : public Reactor { public: // ports Input force{"force", this}; // NOLINT @@ -136,11 +136,11 @@ class Brake : public Reactor { void assemble() override { r1.declare_trigger(&force); } }; -class Engine : public Reactor { +class Engine final : public Reactor { public: // ports Input torque{"torque", this}; // NOLINT - Multiport control_input; + private: // reactions_ Reaction r1{"1", 1, this, [this]() { reaction_1(); }}; @@ -180,12 +180,3 @@ auto main() -> int { return 0; } - -class ReactionScope : public MutableScope { - void reaction_0() { - MutationChangeMultiportSize change_multiport_width{&this->self_->control_input, 6}; - this->add_to_transaction(&change_multiport_width); - - this->commit_transaction(); - } -}; diff --git a/examples/unit_tests_mutations/CMakeLists.txt b/examples/unit_tests_mutations/CMakeLists.txt deleted file mode 100644 index 0843149f..00000000 --- a/examples/unit_tests_mutations/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -add_executable(unit_tests_mutations main.cc) -target_link_libraries(unit_tests_mutations reactor-cpp) -add_dependencies(examples unit_tests_mutations) diff --git a/examples/unit_tests_mutations/main.cc b/examples/unit_tests_mutations/main.cc deleted file mode 100644 index 0066d705..00000000 --- a/examples/unit_tests_mutations/main.cc +++ /dev/null @@ -1,82 +0,0 @@ -#include - -#include "../../lib/mutation/multiport.cc" -#include -#include - -using namespace std::chrono_literals; - -class TestMultiport: public reactor::Reactor { - reactor::ModifableMultiport> test_multiport_{"modifable_multiports_"}; - reactor::Timer timer_{"timer", this, 1s}; - reactor::LogicalAction scale{"scale", this}; - reactor::Reaction trigger_reaction_{"trigger_reaction", 1, this, [this](){this->__inner.reaction_1(scale);}}; - reactor::Reaction test_reaction_{"test_reaction", 2, this, [this](){this->__inner.reaction_2(test_multiport_, scale);}}; - reactor::Reaction validate_reaction_{"validate_reaction", 3, this, [this](){this->__inner.reaction_3(test_multiport_);}}; -public: - -class Inner: public reactor::MutableScope { - int state = 0; - std::vector sizes = {4, 5, 6, 5, 4, 3}; - [[maybe_unused]] const Inner& __lf_inner = *this; -public: - - Inner(Reactor* reactor) : MutableScope(reactor) {} - - void reaction_1(reactor::LogicalAction& scale) { - int size = sizes[state]; - state = (state + 1) % sizes.size(); - std::cout << "set: " << size << std::endl; - scale.schedule(size); - } - - void reaction_2(reactor::ModifableMultiport>& test_multiport, reactor::LogicalAction& scale) { - reactor::ModifableMultiport>* temp = &test_multiport; - std::size_t new_size = *scale.get(); - - auto anti_dep = test_multiport[0].anti_dependencies(); - reactor::MutationChangeOutputMultiportSize change_size{temp, this->reactor_, anti_dep, new_size}; - add_to_transaction(&change_size); - - commit_transaction(); - } - - void reaction_3(reactor::ModifableMultiport>& test_multiport) { - for (auto i = 0; i < test_multiport.size(); i++) { - std::cout << test_multiport[i].fqn() << "/" << std::endl; - } - } - }; - - Inner __inner; - - TestMultiport(const std::string& name, reactor::Environment* env) : Reactor(name, env), __inner(this) { - std::cout << "creating instance of deployment" << std::endl; - - test_multiport_.reserve(4); - for (size_t _lf_idx = 0; _lf_idx < 4; _lf_idx++) { - std::string _lf_port_name = test_multiport_.name() + "_" + std::to_string(_lf_idx); - test_multiport_.emplace_back(_lf_port_name, this); - } - } - ~TestMultiport() override = default; - - void assemble() override { - trigger_reaction_.declare_trigger(&timer_); - trigger_reaction_.declare_schedulable_action(&scale); - test_reaction_.declare_trigger(&scale); - - } -}; - - -auto main() -> int { - // srand(time(nullptr)); - reactor::Environment env{4}; - auto test_multiport = std::make_unique("test_multiport", &env); - env.optimize(); - env.assemble(); - auto thread = env.startup(); - thread.join(); - return 0; -} diff --git a/include/reactor-cpp/environment.hh b/include/reactor-cpp/environment.hh index fdfb30b7..8e416ba1 100644 --- a/include/reactor-cpp/environment.hh +++ b/include/reactor-cpp/environment.hh @@ -103,16 +103,16 @@ public: template void remove_connection(Port* source, Port* sink) { if (top_environment_ == nullptr || top_environment_ == this) { log::Debug() << "remove connection: " << source->fqn() << " -/-> " << sink->fqn(); - auto properties = graph_.remove_edge(source, sink); + graph_.remove_edge(source, sink); } else { - return top_environment_->remove_connection(source, sink); + top_environment_->remove_connection(source, sink); } } void remove_top_level_reactor(Reactor* reactor) { auto elements_erased = top_level_reactors_.erase(reactor); if (elements_erased == 0) { - std::cout << "no elements erased" << std::endl; + std::cout << "no elements erased" << '\n'; } } diff --git a/include/reactor-cpp/graph.hh b/include/reactor-cpp/graph.hh index 08af8d65..739d8b33 100644 --- a/include/reactor-cpp/graph.hh +++ b/include/reactor-cpp/graph.hh @@ -9,6 +9,7 @@ #ifndef REACTOR_CPP_GRAPH_HH #define REACTOR_CPP_GRAPH_HH +#include #include #include #include @@ -64,16 +65,15 @@ public: } } - auto remove_edge(E source, E destinations) noexcept -> std::optional

{ + auto remove_edge(E source, E destinations) noexcept { if (graph_.find(source) == std::end(graph_)) { - return std::nullopt; - } else { - auto conns = std::find_if(std::begin(graph_[source]), std::end(graph_[source]), - [destinations](auto val) { return val.second == destinations; }); + return; + } + auto conns = std::find_if(std::begin(graph_[source]), std::end(graph_[source]), + [destinations](auto val) { return val.second == destinations; }); - if (conns != std::end(graph_[source])) { - graph_[source].erase(conns); - } + if (conns != std::end(graph_[source])) { + graph_[source].erase(conns); } } diff --git a/include/reactor-cpp/multiport.hh b/include/reactor-cpp/multiport.hh index d0aa4888..28d57bce 100644 --- a/include/reactor-cpp/multiport.hh +++ b/include/reactor-cpp/multiport.hh @@ -14,6 +14,7 @@ #include #include #include +#include #include #include "assert.hh" @@ -49,10 +50,10 @@ protected: void register_port(BasePort& port, size_t idx); public: - explicit BaseMultiport(const std::string& name) - : multiport_name_(name){}; + explicit BaseMultiport(std::string name) + : multiport_name_(std::move(name)){}; ~BaseMultiport() = default; - auto name() const -> std::string { return multiport_name_; } + [[nodiscard]] auto name() const -> std::string { return multiport_name_; } }; template class MutationChangeMultiportSize; @@ -82,14 +83,14 @@ public: auto operator[](std::size_t index) noexcept -> T& { return ports_[index]; } auto operator[](std::size_t index) const noexcept -> const T& { return ports_[index]; } - auto begin() noexcept -> iterator { return ports_.begin(); }; - auto begin() const noexcept -> const_iterator { return ports_.begin(); }; - auto cbegin() const noexcept -> const_iterator { return ports_.cbegin(); }; - auto end() noexcept -> iterator { return ports_.end(); }; - auto end() const noexcept -> const_iterator { return ports_.end(); }; - auto cend() const noexcept -> const_iterator { return ports_.cend(); }; + [[nodiscard]] auto begin() noexcept -> iterator { return ports_.begin(); }; + [[nodiscard]] auto begin() const noexcept -> const_iterator { return ports_.begin(); }; + [[nodiscard]] auto cbegin() const noexcept -> const_iterator { return ports_.cbegin(); }; + [[nodiscard]] auto end() noexcept -> iterator { return ports_.end(); }; + [[nodiscard]] auto end() const noexcept -> const_iterator { return ports_.end(); }; + [[nodiscard]] auto cend() const noexcept -> const_iterator { return ports_.cend(); }; - auto size() const noexcept -> size_type { return ports_.size(); }; + [[nodiscard]] auto size() const noexcept -> size_type { return ports_.size(); }; [[nodiscard]] auto empty() const noexcept -> bool { return ports_.empty(); }; [[nodiscard]] auto present_indices_unsorted() const noexcept -> std::vector { diff --git a/include/reactor-cpp/mutations.hh b/include/reactor-cpp/mutations.hh index a7fab5df..3aaa1eee 100644 --- a/include/reactor-cpp/mutations.hh +++ b/include/reactor-cpp/mutations.hh @@ -1,22 +1,30 @@ -#ifndef MUTATIONS_HH -#define MUTATIONS_HH +#ifndef REACTOR_CPP_MUTATIONS_HH +#define REACTOR_CPP_MUTATIONS_HH + +#include namespace reactor { class Reactor; class Environment; -enum MutationResult { +enum MutationResult : std::int8_t { Success = 0, NotMatchingBankSize = 1, }; class Mutation { public: + Mutation() = default; + Mutation(const Mutation& other) = default; + Mutation(Mutation&& other) = default; virtual ~Mutation() = default; + auto operator=(const Mutation& other) -> Mutation& = default; + auto operator=(Mutation&& other) -> Mutation& = default; + virtual auto run() -> MutationResult = 0; virtual auto rollback() -> MutationResult = 0; }; } // namespace reactor -#endif // MUTATIONS_HH +#endif // REACTOR_CPP_MUTATIONS_HH diff --git a/include/reactor-cpp/mutations/bank.hh b/include/reactor-cpp/mutations/bank.hh index f77fe040..4081b691 100644 --- a/include/reactor-cpp/mutations/bank.hh +++ b/include/reactor-cpp/mutations/bank.hh @@ -2,8 +2,8 @@ // Created by tanneberger on 11/18/24. // -#ifndef MUTATION_BANK_HH -#define MUTATION_BANK_HH +#ifndef REACTOR_CPP_MUTATIONS_BANK_HH +#define REACTOR_CPP_MUTATIONS_BANK_HH #include @@ -14,28 +14,38 @@ namespace reactor { class Reactor; class Environment; -template class MutationChangeBankSize : public reactor::Mutation { +template class MutationChangeBankSize final : public Mutation { std::vector* bank_ = nullptr; std::size_t desired_size_ = 0; std::size_t size_before_application_ = 0; Reactor* reactor_ = nullptr; - std::function create_lambda_; + std::function create_lambda_; - void change_size(std::size_t); + void change_size(std::size_t new_size); public: - MutationChangeBankSize(std::vector* bank, Reactor* reactor, std::size_t size, - std::function); MutationChangeBankSize() = default; - explicit MutationChangeBankSize(const std::vector& other) + MutationChangeBankSize(const MutationChangeBankSize& other) noexcept : bank_(other.bank_) , desired_size_(other.desired_size_) - , size_before_application_(other.size_before_application_) {} + , size_before_application_(other.size_before_application_) + , reactor_(other.reactor_) + , create_lambda_(other.create_lambda_){}; + MutationChangeBankSize(MutationChangeBankSize&& other) noexcept + : bank_(other.bank_) + , desired_size_(other.desired_size_) + , size_before_application_(other.size_before_application_) + , reactor_(other.reactor_) + , create_lambda_(other.create_lambda_){}; + explicit MutationChangeBankSize(std::vector* bank, Reactor* reactor, std::size_t size, + std::function create_lambda); ~MutationChangeBankSize() override = default; + auto operator=(const MutationChangeBankSize& other) -> MutationChangeBankSize& = default; + auto operator=(MutationChangeBankSize&& other) -> MutationChangeBankSize& = default; auto run() -> MutationResult override; auto rollback() -> MutationResult override; }; } // namespace reactor -#endif // MUTATION_BANK_HH +#endif // REACTOR_CPP_MUTATIONS_BANK_HH diff --git a/include/reactor-cpp/mutations/connection.hh b/include/reactor-cpp/mutations/connection.hh index 72a78235..25bb9b37 100644 --- a/include/reactor-cpp/mutations/connection.hh +++ b/include/reactor-cpp/mutations/connection.hh @@ -2,8 +2,8 @@ // Created by tanneberger on 11/18/24. // -#ifndef MUTATION_CONNECTION_HH -#define MUTATION_CONNECTION_HH +#ifndef REACTOR_CPP_MUTATIONS_CONNECTION_HH +#define REACTOR_CPP_MUTATIONS_CONNECTION_HH #include "../mutations.hh" @@ -19,18 +19,25 @@ private: Reactor* reactor_{}; public: - MutationAddConnection(A* source, B* sink, Reactor* reactor); + explicit MutationAddConnection(A* source, B* sink, Reactor* reactor); MutationAddConnection(const MutationAddConnection& other) : source_(other.source_) , sink_(other.sink_) , connection_(other.connection_) , reactor_(other.reactor_) {} + MutationAddConnection(MutationAddConnection&& other) noexcept + : source_(other.source_) + , sink_(other.sink_) + , connection_(other.connection_) + , reactor_(other.reactor_) {} MutationAddConnection() = default; ~MutationAddConnection() override = default; + auto operator=(const MutationAddConnection& other) -> MutationAddConnection& = default; + auto operator=(MutationAddConnection&& other) -> MutationAddConnection& = default; auto run() -> MutationResult override; auto rollback() -> MutationResult override; }; } // namespace reactor -#endif // MUTATION_CONNECTION_HH +#endif // REACTOR_CPP_MUTATIONS_CONNECTION_HH diff --git a/include/reactor-cpp/mutations/multiport.hh b/include/reactor-cpp/mutations/multiport.hh index da8e407e..268d6267 100644 --- a/include/reactor-cpp/mutations/multiport.hh +++ b/include/reactor-cpp/mutations/multiport.hh @@ -2,8 +2,8 @@ // Created by tanneberger on 11/11/24. // -#ifndef MUTATION_MULTIPORT_HH -#define MUTATION_MULTIPORT_HH +#ifndef REACTOR_CPP_MUTATIONS_MULTIPORT_HH +#define REACTOR_CPP_MUTATIONS_MULTIPORT_HH #include @@ -23,7 +23,7 @@ private: std::size_t size_before_application_ = 0; Reactor* reactor_{}; - void change_size(std::size_t); + void change_size(std::size_t new_size); public: MutationChangeOutputMultiportSize(ModifableMultiport>* multiport, Reactor* reactor, @@ -33,11 +33,17 @@ public: : multiport_(other.multiport_) , desired_size_(other.desired_size_) , size_before_application_(other.size_before_application_) {} + MutationChangeOutputMultiportSize(MutationChangeOutputMultiportSize&& other) noexcept + : multiport_(other.multiport_) + , desired_size_(other.desired_size_) + , size_before_application_(other.size_before_application_) {} ~MutationChangeOutputMultiportSize() override = default; + auto operator=(const MutationChangeOutputMultiportSize& other) -> MutationChangeOutputMultiportSize& = default; + auto operator=(MutationChangeOutputMultiportSize&& other) -> MutationChangeOutputMultiportSize& = default; auto run() -> MutationResult override; auto rollback() -> MutationResult override; }; } // namespace reactor -#endif // MUTATION_MULTIPORT_HH +#endif // REACTOR_CPP_MUTATIONS_MULTIPORT_HH diff --git a/include/reactor-cpp/port.hh b/include/reactor-cpp/port.hh index e5b3da56..ecb0b059 100644 --- a/include/reactor-cpp/port.hh +++ b/include/reactor-cpp/port.hh @@ -77,7 +77,7 @@ public: void set_inward_binding(BasePort* port) noexcept { if (port != nullptr) { std::cout << port->fqn() << "(" << port << ")" - << " --> " << this->fqn() << "(" << this << ")" << std::endl; + << " --> " << this->fqn() << "(" << this << ")" << '\n'; } inward_binding_ = port; @@ -116,11 +116,6 @@ public: friend class Scheduler; }; -inline auto operator==(const BasePort& a, const BasePort& b) -> bool { - bool equal = ((const ReactorElement&)a) == ((const ReactorElement&)b); - return equal; -} - template class Port : public BasePort { private: ImmutableValuePtr value_ptr_{nullptr}; @@ -189,7 +184,7 @@ public: Input(Input&&) = default; - ~Input() { std::cout << "Input port gets deallocated:" << this->fqn() << std::endl; } + ~Input() override { std::cout << "Input port gets deallocated:" << this->fqn() << "\n"; } }; template class Output : public Port { // NOLINT(cppcoreguidelines-special-member-functions) @@ -199,7 +194,7 @@ public: Output(Output&&) noexcept = default; - ~Output() { std::cout << "Output port gets deallocated: " << this->fqn() << std::endl; } + ~Output() override { std::cout << "Output port gets deallocated: " << this->fqn() << "\n"; } }; } // namespace reactor diff --git a/include/reactor-cpp/reaction.hh b/include/reactor-cpp/reaction.hh index 80246a28..e4cac716 100644 --- a/include/reactor-cpp/reaction.hh +++ b/include/reactor-cpp/reaction.hh @@ -27,7 +27,7 @@ private: std::set dependencies_; const int priority_ = -1; - unsigned int index_ = -1; + int index_ = -1; std::function body_{nullptr}; @@ -63,7 +63,7 @@ public: void startup() final {} void shutdown() final {} void trigger(); - void set_index(unsigned index); + void set_index(int index); template void set_deadline(Dur deadline, const std::function& handler) { set_deadline_impl(std::chrono::duration_cast(deadline), handler); diff --git a/include/reactor-cpp/scopes.hh b/include/reactor-cpp/scopes.hh index ff3712c6..fe1de9f4 100644 --- a/include/reactor-cpp/scopes.hh +++ b/include/reactor-cpp/scopes.hh @@ -6,8 +6,8 @@ * Tassilo Tanneberger */ -#ifndef REACTOR_CPP_SCOPS_HH -#define REACTOR_CPP_SCOPS_HH +#ifndef REACTOR_CPP_SCOPES_HH +#define REACTOR_CPP_SCOPES_HH #include "environment.hh" #include "logical_time.hh" @@ -18,20 +18,24 @@ namespace reactor { class Scope { private: - Reactor* reactor; + Reactor* reactor_; public: - Scope(Reactor* reactor) - : reactor(reactor) {} - - auto get_physical_time() const -> reactor::TimePoint { return reactor->get_physical_time(); } - auto get_tag() const -> reactor::Tag { return reactor->get_tag(); } - auto get_logical_time() const -> reactor::TimePoint { return reactor->get_logical_time(); } - auto get_microstep() const -> reactor::mstep_t { return reactor->get_microstep(); } - auto get_elapsed_logical_time() const -> reactor::Duration { return reactor->get_elapsed_logical_time(); } - auto get_elapsed_physical_time() const -> reactor::Duration { return reactor->get_elapsed_physical_time(); } - auto environment() const -> reactor::Environment* { return reactor->environment(); } - void request_stop() const { return environment()->sync_shutdown(); } + explicit Scope(Reactor* reactor) + : reactor_(reactor) {} + + [[nodiscard]] static auto get_physical_time() noexcept -> TimePoint { return Reactor::get_physical_time(); } + [[nodiscard]] auto get_tag() const noexcept -> Tag { return reactor_->get_tag(); } + [[nodiscard]] auto get_logical_time() const noexcept -> TimePoint { return reactor_->get_logical_time(); } + [[nodiscard]] auto get_microstep() const noexcept -> mstep_t { return reactor_->get_microstep(); } + [[nodiscard]] auto get_elapsed_logical_time() const noexcept -> Duration { + return reactor_->get_elapsed_logical_time(); + } + [[nodiscard]] auto get_elapsed_physical_time() const noexcept -> Duration { + return reactor_->get_elapsed_physical_time(); + } + [[nodiscard]] auto environment() const noexcept -> Environment* { return reactor_->environment(); } + void request_stop() const { environment()->sync_shutdown(); } }; class MutableScope : public Scope { @@ -45,7 +49,19 @@ public: , transaction_(reactor) , reactor_(reactor) , env_(reactor->environment()) {} + MutableScope(const MutableScope& other) + : Scope(other.reactor_) + , transaction_(other.transaction_) + , reactor_(other.reactor_) + , env_(other.env_) {} + MutableScope(MutableScope&& other) noexcept + : Scope(other.reactor_) + , transaction_(std::move(other.transaction_)) + , reactor_(other.reactor_) + , env_(other.env_) {} ~MutableScope() = default; + auto operator=(const MutableScope& other) -> MutableScope& = default; + auto operator=(MutableScope&& other) -> MutableScope& = default; void commit_transaction(bool recalculate = false); void add_to_transaction(const std::shared_ptr& mutation); @@ -53,4 +69,4 @@ public: } // namespace reactor -#endif // REACTOR_CPP_SCOPS_HH +#endif // REACTOR_CPP_SCOPES_HH diff --git a/include/reactor-cpp/transaction.hh b/include/reactor-cpp/transaction.hh index 3b06496b..b0ce287c 100644 --- a/include/reactor-cpp/transaction.hh +++ b/include/reactor-cpp/transaction.hh @@ -26,6 +26,10 @@ private: public: explicit Transaction(Reactor* parent); + Transaction(const Transaction& other) = default; + Transaction(Transaction&& other) = default; + auto operator=(const Transaction& other) -> Transaction& = default; + auto operator=(Transaction&& other) -> Transaction& = default; ~Transaction() = default; void push_back(const std::shared_ptr& mutation); diff --git a/lib/action.cc b/lib/action.cc index 04a52a41..ed8a43a7 100644 --- a/lib/action.cc +++ b/lib/action.cc @@ -29,7 +29,7 @@ void BaseAction::register_trigger(Reaction* reaction) { validate(this->container() == reaction->container(), "Action triggers must belong to the same reactor as the triggered " "reaction"); - [[maybe_unused]] bool result = triggers_.insert(reaction).second; + [[maybe_unused]] const bool result = triggers_.insert(reaction).second; reactor_assert(result); } @@ -40,7 +40,7 @@ void BaseAction::register_scheduler(Reaction* reaction) { // the reaction must belong to the same reactor as this action validate(this->container() == reaction->container(), "Scheduable actions must belong to the same reactor as the " "triggered reaction"); - [[maybe_unused]] bool result = schedulers_.insert(reaction).second; + [[maybe_unused]] const bool result = schedulers_.insert(reaction).second; reactor_assert(result); } @@ -62,8 +62,8 @@ void Timer::cleanup() noexcept { BaseAction::cleanup(); // schedule the timer again if (period_ != Duration::zero()) { - Tag now = Tag::from_logical_time(environment()->logical_time()); - Tag next = now.delay(period_); + const Tag now = Tag::from_logical_time(environment()->logical_time()); + const Tag next = now.delay(period_); environment()->scheduler()->schedule_sync(this, next); } } @@ -74,7 +74,7 @@ ShutdownTrigger::ShutdownTrigger(const std::string& name, Reactor* container) void ShutdownTrigger::setup() noexcept { BaseAction::setup(); } void ShutdownTrigger::shutdown() { - Tag tag = Tag::from_logical_time(environment()->logical_time()).delay(); + const Tag tag = Tag::from_logical_time(environment()->logical_time()).delay(); environment()->scheduler()->schedule_sync(this, tag); } diff --git a/lib/environment.cc b/lib/environment.cc index 9b86fd7a..ca08e670 100644 --- a/lib/environment.cc +++ b/lib/environment.cc @@ -41,7 +41,7 @@ Environment::Environment(const std::string& name, Environment* containing_enviro , top_environment_(containing_environment_->top_environment_) , scheduler_(this) , timeout_(containing_environment->timeout()) { - [[maybe_unused]] bool result = containing_environment->contained_environments_.insert(this).second; + [[maybe_unused]] const bool result = containing_environment->contained_environments_.insert(this).second; reactor_assert(result); } @@ -50,7 +50,7 @@ void Environment::register_reactor(Reactor* reactor) { validate(this->phase() == Phase::Construction || this->phase() == Phase::Mutation, "Reactors may only be registered during construction phase!"); validate(reactor->is_top_level(), "The environment may only contain top level reactors!"); - [[maybe_unused]] bool result = top_level_reactors_.insert(reactor).second; + [[maybe_unused]] const bool result = top_level_reactors_.insert(reactor).second; reactor_assert(result); } @@ -58,7 +58,7 @@ void Environment::register_input_action(BaseAction* action) { reactor_assert(action != nullptr); validate(this->phase() == Phase::Construction || this->phase() == Phase::Assembly, "Input actions may only be registered during construction or assembly phase!"); - [[maybe_unused]] bool result = input_actions_.insert(action).second; + [[maybe_unused]] const bool result = input_actions_.insert(action).second; reactor_assert(result); run_forever_ = true; } @@ -279,7 +279,7 @@ void Environment::calculate_indexes() { } log_.debug() << "Reactions sorted by index:"; - unsigned int index = 0; + int index = 0; while (!graph.empty()) { // find nodes with degree zero and assign index std::set degree_zero; diff --git a/lib/mutation/bank.cc b/lib/mutation/bank.cc index a358d88b..850642ae 100644 --- a/lib/mutation/bank.cc +++ b/lib/mutation/bank.cc @@ -6,8 +6,9 @@ #include "reactor-cpp/action.hh" template -reactor::MutationChangeBankSize::MutationChangeBankSize(std::vector* bank, Reactor* reactor, std::size_t size, - std::function create_lambda) +reactor::MutationChangeBankSize::MutationChangeBankSize( + std::vector* bank, Reactor* reactor, std::size_t size, + std::function create_lambda) : bank_(bank) , reactor_(reactor) , desired_size_(size) @@ -34,7 +35,7 @@ template void reactor::MutationChangeBankSize::change_size(std::siz bank_->push_back(create_lambda_(reactor_, current_size + i)); (*bank_)[bank_->size() - 1]->assemble(); } - std::cout << "created new reactors" << std::endl; + std::cout << "created new reactors" << '\n'; } } template auto reactor::MutationChangeBankSize::run() -> MutationResult { diff --git a/lib/reaction.cc b/lib/reaction.cc index 6b373948..20858170 100644 --- a/lib/reaction.cc +++ b/lib/reaction.cc @@ -125,7 +125,7 @@ void Reaction::set_deadline_impl(Duration deadline, const std::functiondeadline_handler_ = handler; } -void Reaction::set_index(unsigned index) { +void Reaction::set_index(int index) { validate(this->environment()->phase() == Phase::Assembly || this->environment()->phase() == Phase::Mutation, "Reaction indexes may only be set during assembly phase!"); this->index_ = index; diff --git a/lib/reactor.cc b/lib/reactor.cc index 08bc8863..b73263de 100644 --- a/lib/reactor.cc +++ b/lib/reactor.cc @@ -76,7 +76,7 @@ void Reactor::register_reactor([[maybe_unused]] Reactor* reactor) { if (std::find(std::begin(reactors_), std::end(reactors_), reactor) == std::end(reactors_)) { reactors_.push_back(reactor); } else { - std::cout << "duplicate insertion!" << std::endl; + std::cout << "duplicate insertion!" << '\n'; } //[[maybe_unused]] bool result = reactors_.insert(reactor).second; @@ -164,8 +164,8 @@ void Reactor::remove_inputs(BasePort* base_port) { }; void Reactor::remove_child_reactor(const Reactor* base_reactor) { - auto index = std::find_if(std::begin(reactors_), std::end(reactors_), - [base_reactor](const Reactor* other) { return base_reactor == other; }); + const auto index = std::find_if(std::begin(reactors_), std::end(reactors_), + [base_reactor](const Reactor* other) { return base_reactor == other; }); if (index != std::end(reactors_)) { reactors_.erase(index); diff --git a/lib/transaction.cc b/lib/transaction.cc index 3092b937..decb77b9 100644 --- a/lib/transaction.cc +++ b/lib/transaction.cc @@ -10,14 +10,14 @@ reactor::Transaction::Transaction(Reactor* parent) auto reactor::Transaction::execute(bool recalculate) -> MutationResult { this->environment_->start_mutation(); - for (auto mutation : mutations_) { + for (const auto& mutation : mutations_) { mutation->run(); } if (recalculate) { this->environment_->clear_dependency_graph(); - for (const auto* reactor : this->environment_->top_level_reactors()) { - this->environment_->build_dependency_graph((Reactor*)reactor); + for (auto* reactor : this->environment_->top_level_reactors()) { + this->environment_->build_dependency_graph(reactor); } this->environment_->calculate_indexes();