Skip to content

Commit 1d4a4ec

Browse files
committed
code cleaning
1 parent 9e1b6ed commit 1d4a4ec

File tree

28 files changed

+265
-322
lines changed

28 files changed

+265
-322
lines changed

examples/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@ add_subdirectory(ports)
88
add_subdirectory(hello)
99
add_subdirectory(power_train)
1010
add_subdirectory(multiport_mutation)
11-
add_subdirectory(unit_tests_mutations)

examples/count/main.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
using namespace reactor;
66
using namespace std::chrono_literals;
77

8-
class Count : public Reactor {
8+
class Count final : public Reactor {
99
private:
1010
// actions
1111
Timer timer{"timer", this};
1212
LogicalAction<int> counter{"counter", this};
1313

1414
// reactions_
15-
Reaction r_init{"r_init", 1, false, this, [this]() { this->init(); }};
16-
Reaction r_counter{"r_counter", 2, false, this, [this]() { this->print_count(); }};
15+
Reaction r_init{"r_init", 1, this, [this]() { this->init(); }};
16+
Reaction r_counter{"r_counter", 2, this, [this]() { this->print_count(); }};
1717

1818
public:
1919
explicit Count(Environment* env)

examples/hello/main.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using namespace reactor;
77
using namespace std::chrono_literals;
88

9-
class Hello : public Reactor {
9+
class Hello final : public Reactor {
1010
private:
1111
// actions
1212
Timer timer{"timer", this, 1s, 2s};
Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,39 @@
1-
//
2-
// Created by tanneberger on 11/17/24.
3-
//
4-
5-
#ifndef CONSUMER_HH
6-
#define CONSUMER_HH
1+
#ifndef CONSUMER_HH //NOLINT
2+
#define CONSUMER_HH //NOLINT
73

84
#include <reactor-cpp/reactor-cpp.hh>
95

106
using namespace reactor;
117
using namespace std::chrono_literals;
128

13-
class Consumer : public Reactor {
14-
private:
15-
class Inner: public Scope {
16-
Inner(Reactor* reactor, std::size_t index) : Scope(reactor), index_(index) {}
17-
std::size_t index_;
18-
19-
[[maybe_unused]] const Inner& __lf_inner = *this;
9+
class Consumer final: public Reactor { // NOLINT
10+
class Inner : public Scope {
11+
Inner(Reactor* reactor, std::size_t index)
12+
: Scope(reactor)
13+
, index_(index) {}
14+
std::size_t index_ = 0;
2015

21-
void reaction_1([[maybe_unused]] const Input<unsigned>& in) {
22-
std::cout << "consumer: " << index_ << " received value:" << *in.get() << std::endl;
16+
void reaction_1(const Input<unsigned>& in) const {
17+
std::cout << "consumer: " << index_ << " received value:" << *in.get() << '\n';
2318
}
2419

2520
friend Consumer;
2621
};
2722

28-
Inner __lf_inner;
29-
Reaction handle{"handle", 1, this, [this]() { __lf_inner.reaction_1(this->in); }};
23+
Inner _lf_inner;
24+
Reaction handle{"handle", 1, this, [this]() { _lf_inner.reaction_1(this->in); }};
25+
3026
public:
31-
Consumer(const std::string& name, Environment* env, std::size_t index) : Reactor(name, env), __lf_inner(this, index) {
32-
std::cout << "creating instance of consumer" << std::endl;
27+
Consumer(const std::string& name, Environment* env, std::size_t index)
28+
: Reactor(name, env)
29+
, _lf_inner(this, index) {
30+
std::cout << "creating instance of consumer" << '\n';
3331
}
34-
~Consumer() override {
35-
std::cout << "Consumer Object is deleted" << std::endl;
36-
};
32+
~Consumer() override { std::cout << "Consumer Object is deleted" << '\n'; };
3733

38-
Input<unsigned> in{"in", this};
34+
Input<unsigned> in{"in", this}; //NOLINT
3935

40-
void assemble() override {
41-
handle.declare_trigger(&in);
42-
}
36+
void assemble() override { handle.declare_trigger(&in); }
4337
};
4438

45-
46-
#endif //CONSUMER_HH
39+
#endif // CONSUMER_HH
Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
//
2-
// Created by tanneberger on 11/17/24.
3-
//
4-
5-
#ifndef LOAD_BALANCER_HH
6-
#define LOAD_BALANCER_HH
1+
#ifndef LOAD_BALANCER_HH //NOLINT
2+
#define LOAD_BALANCER_HH //NOLINT
73

84
#include <reactor-cpp/reactor-cpp.hh>
95

@@ -13,29 +9,31 @@
139
using namespace reactor;
1410
using namespace std::chrono_literals;
1511

16-
class LoadBalancer : public Reactor {
17-
private:
18-
class Inner: public MutableScope {
19-
Inner(Reactor* reactor) : MutableScope(reactor) {}
20-
[[maybe_unused]] const Inner& __lf_inner = *this;
12+
class LoadBalancer final: public Reactor { // NOLINT
13+
class Inner : public MutableScope {
14+
explicit Inner(Reactor* reactor)
15+
: MutableScope(reactor) {}
2116

2217
// reaction bodies
23-
void reaction_1(const Input<unsigned>& inbound, LogicalAction<unsigned>& scale_action, Multiport<Output<unsigned>>& outbound) {
24-
if (rand() % 30 == 0) {
25-
scale_action.schedule(rand() % 20 + 1);
18+
static void reaction_1(const Input<unsigned>& inbound, LogicalAction<unsigned>& scale_action,
19+
Multiport<Output<unsigned>>& outbound) {
20+
if (std::rand() % 30 == 0) { //NOLINT
21+
scale_action.schedule(std::rand() % 20 + 1); //NOLINT
2622
}
27-
unsigned sel = rand() % outbound.size();
28-
std::cout << "Sending out to:" << sel << std::endl;
23+
const unsigned sel = std::rand() % outbound.size(); //NOLINT
24+
std::cout << "Sending out to:" << sel << '\n';
2925
outbound[sel].set(inbound.get());
3026
}
3127

32-
void reaction_2(ModifableMultiport<Output<unsigned>>&outbound, [[maybe_unused]] const LogicalAction<unsigned>& scale, Output<unsigned>& scale_bank) {
28+
void reaction_2(ModifableMultiport<Output<unsigned>>& outbound,
29+
[[maybe_unused]] const LogicalAction<unsigned>& scale, Output<unsigned>& scale_bank) {
3330
ModifableMultiport<Output<unsigned>>* temp = &outbound;
3431
std::size_t new_size = *scale.get();
3532

3633
auto antideps = (outbound[0]).anti_dependencies();
3734

38-
auto change_size = std::make_shared<MutationChangeOutputMultiportSize<unsigned>>(temp, this->reactor_, antideps, new_size);
35+
const auto change_size =
36+
std::make_shared<MutationChangeOutputMultiportSize<unsigned>>(temp, this->reactor_, antideps, new_size);
3937

4038
add_to_transaction(change_size);
4139

@@ -47,37 +45,40 @@ private:
4745
friend LoadBalancer;
4846
};
4947

50-
Inner __lf_inner;
51-
Reaction process{"process", 2, this, [this]() { __lf_inner.reaction_1(this->inbound, this->scale_action, this->out); }};
52-
Reaction scale{"scale", 1, this, [this]() { __lf_inner.reaction_2(this->out, this->scale_action, this->scale_bank); }};
48+
Inner _lf_inner;
49+
Reaction process{"process", 2, this,
50+
[this]() { Inner::reaction_1(this->inbound, this->scale_action, this->out); }};
51+
Reaction scale{"scale", 1, this,
52+
[this]() { _lf_inner.reaction_2(this->out, this->scale_action, this->scale_bank); }};
5353

5454
public:
5555
LoadBalancer(const std::string& name, Environment* env)
56-
: Reactor(name, env), __lf_inner(this) {
57-
std::cout << "creating instance of load balancer" << std::endl;
56+
: Reactor(name, env)
57+
, _lf_inner(this) {
58+
std::cout << "creating instance of load balancer" << '\n';
5859
out.reserve(4);
5960
for (size_t _lf_idx = 0; _lf_idx < 4; _lf_idx++) {
60-
std::string _lf_port_name = out.name() + "_" + std::to_string(_lf_idx);
61+
std::string _lf_port_name = out.name() + "_" + std::to_string(_lf_idx);
6162
out.emplace_back(_lf_port_name, this);
6263
}
6364
}
65+
~LoadBalancer() override = default;
66+
6467

65-
LogicalAction<unsigned> scale_action{"scale", this, 1us};
66-
ModifableMultiport<Output<unsigned>> out{"out"};
68+
LogicalAction<unsigned> scale_action{"scale", this, 1us}; //NOLINT
69+
ModifableMultiport<Output<unsigned>> out{"out"}; //NOLINT
6770
Input<unsigned> inbound{"inbound", this}; // NOLINT
68-
Output<unsigned> scale_bank{"scale_bank", this};
71+
Output<unsigned> scale_bank{"scale_bank", this}; //NOLINT
6972

7073
void assemble() override {
7174
std::cout << "assemble LoadBalancer\n";
72-
for (auto& __lf_port : out) {
73-
process.declare_antidependency(&__lf_port);
75+
for (auto& _lf_port : out) {
76+
process.declare_antidependency(&_lf_port);
7477
}
7578
process.declare_trigger(&inbound);
7679
scale.declare_trigger(&scale_action);
7780
scale.declare_antidependency(&scale_bank);
7881
}
7982
};
8083

81-
82-
83-
#endif //LOAD_BALANCER_HH
84+
#endif // LOAD_BALANCER_HH

examples/multiport_mutation/main.cc

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,92 +3,87 @@
33
#include <reactor-cpp/mutations/bank.hh>
44
#include <reactor-cpp/mutations/connection.hh>
55

6+
#include "../../lib/mutation/bank.cc"
7+
#include "../../lib/mutation/connection.cc"
68
#include "./consumer.hh"
79
#include "./load_balancer.hh"
810
#include "./producer.hh"
9-
#include "../../lib/mutation/bank.cc"
10-
#include "../../lib/mutation/connection.cc"
1111
#include <reactor-cpp/reactor-cpp.hh>
1212

13-
class Deployment : public Reactor {
14-
std::unique_ptr<Producer> producer_;
15-
std::unique_ptr<LoadBalancer> load_balancer_;
16-
std::vector<std::unique_ptr<Consumer>> consumers_;
17-
18-
Reaction scale_bank{"scale_bank", 1, this, [this](){this->__inner.reaction_1(this->scale, this->consumers_, load_balancer_->out);}};
19-
20-
public:
21-
22-
class Inner: public MutableScope {
13+
class Deployment final : public Reactor { //NOLINT
14+
class Inner : public MutableScope {
2315
int state = 0;
24-
[[maybe_unused]] const Inner& __lf_inner = *this;
25-
public:
2616

27-
Inner(Reactor* reactor) : MutableScope(reactor) {}
28-
void reaction_1(const Input<unsigned>& scale, std::vector<std::unique_ptr<Consumer>>& reactor_bank, ModifableMultiport<Output<unsigned>>& load_balancer) {
17+
public:
18+
Inner(Reactor* reactor)
19+
: MutableScope(reactor) {}
20+
void reaction_1(const Input<unsigned>& scale, std::vector<std::unique_ptr<Consumer>>& reactor_bank,
21+
ModifableMultiport<Output<unsigned>>& load_balancer) {
2922
std::size_t new_size = *scale.get();
3023
std::size_t old_size = reactor_bank.size();
3124

32-
std::function<std::unique_ptr<Consumer>(Reactor*, std::size_t)> lambda = [](Reactor* reactor, std::size_t index) {
33-
std::string __lf_inst_name = "consumer_" + std::to_string(index);
34-
return std::make_unique<Consumer>(__lf_inst_name, reactor->environment(), index);
25+
std::function lambda = [](Reactor* reactor, std::size_t index) {
26+
std::string _lf_inst_name = "consumer_" + std::to_string(index);
27+
return std::make_unique<Consumer>(_lf_inst_name, reactor->environment(), index);
3528
};
36-
auto change_size = std::make_shared<MutationChangeBankSize<std::unique_ptr<Consumer>>>(&reactor_bank, this->reactor_, new_size, lambda);
29+
30+
auto change_size = std::make_shared<MutationChangeBankSize<std::unique_ptr<Consumer>>>(
31+
&reactor_bank, this->reactor_, new_size, lambda);
3732

3833
add_to_transaction(change_size);
3934

40-
// old topology
4135
commit_transaction();
42-
// new topology
43-
44-
if (old_size > new_size) {
4536

46-
for (auto i = 0; i < old_size - new_size; i++) {
47-
}
48-
} else {
49-
std::cout << "load_balancer size:" << load_balancer.size() << " bank size: " << reactor_bank.size() << std::endl;
37+
if (old_size < new_size) {
5038
for (auto i = 0; i < new_size; i++) {
51-
auto add_conn = std::make_shared<MutationAddConnection<Output<unsigned>, Input<unsigned>>>(&load_balancer[i], &reactor_bank[i].get()->in, reactor_);
52-
add_to_transaction(add_conn);
39+
auto add_conn = std::make_shared<MutationAddConnection<Output<unsigned>, Input<unsigned>>>(
40+
&load_balancer[i], &reactor_bank[i].get()->in, reactor_);
41+
add_to_transaction(add_conn);
5342
}
54-
commit_transaction(true);
5543
}
5644

57-
std::cout << "new bank size:" << reactor_bank.size() << std::endl;
45+
commit_transaction(true);
5846
}
5947

6048
friend LoadBalancer;
6149
};
6250

63-
Inner __inner;
51+
std::unique_ptr<Producer> producer_;
52+
std::unique_ptr<LoadBalancer> load_balancer_;
53+
std::vector<std::unique_ptr<Consumer>> consumers_;
54+
55+
Reaction scale_bank{"scale_bank", 1, this,
56+
[this]() { this->_inner.reaction_1(this->scale, this->consumers_, load_balancer_->out); }};
6457

65-
Deployment(const std::string& name, Environment* env) : Reactor(name, env), __inner(this),
66-
producer_(std::make_unique<Producer>("producer", environment())),
67-
load_balancer_(std::make_unique<LoadBalancer>("load_balancer", environment())) {
68-
std::cout << "creating instance of deployment" << std::endl;
58+
Inner _inner;
59+
public:
60+
Deployment(const std::string& name, Environment* env)
61+
: Reactor(name, env)
62+
, _inner(this)
63+
, producer_(std::make_unique<Producer>("producer", environment()))
64+
, load_balancer_(std::make_unique<LoadBalancer>("load_balancer", environment())) {
65+
std::cout << "creating instance of deployment" << '\n';
6966
consumers_.reserve(4);
70-
for (size_t __lf_idx = 0; __lf_idx < 4; __lf_idx++) {
71-
std::string __lf_inst_name = "consumer_" + std::to_string(__lf_idx);
72-
consumers_.push_back(std::make_unique<Consumer>(__lf_inst_name, environment(), __lf_idx));
67+
for (size_t _lf_idx = 0; _lf_idx < 4; _lf_idx++) {
68+
std::string _lf_inst_name = "consumer_" + std::to_string(_lf_idx);
69+
consumers_.push_back(std::make_unique<Consumer>(_lf_inst_name, environment(), _lf_idx));
7370
}
7471
}
7572
~Deployment() override = default;
7673

77-
Input<unsigned> scale{"scale", this};
74+
Input<unsigned> scale{"scale", this}; // NOLINT
7875

7976
void assemble() override {
80-
for (size_t __lf_idx = 0; __lf_idx < 4; __lf_idx++) {
81-
environment()->draw_connection(load_balancer_->out[__lf_idx], consumers_[__lf_idx]->in, ConnectionProperties{});
77+
for (size_t _lf_idx = 0; _lf_idx < 4; _lf_idx++) {
78+
environment()->draw_connection(load_balancer_->out[_lf_idx], consumers_[_lf_idx]->in, ConnectionProperties{});
8279
environment()->draw_connection(producer_->value, load_balancer_->inbound, ConnectionProperties{});
8380
}
8481
environment()->draw_connection(load_balancer_->scale_bank, scale, ConnectionProperties{});
8582
scale_bank.declare_trigger(&this->scale);
8683
}
8784
};
8885

89-
9086
auto main() -> int {
91-
//srand(time(nullptr));
9287
Environment env{4, true};
9388
auto deployment = std::make_unique<Deployment>("c1", &env);
9489
env.optimize();

0 commit comments

Comments
 (0)