Skip to content

Commit 6e6ac6f

Browse files
committed
code cleaning
1 parent 9e1b6ed commit 6e6ac6f

File tree

28 files changed

+247
-298
lines changed

28 files changed

+247
-298
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: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
//
2-
// Created by tanneberger on 11/17/24.
3-
//
4-
51
#ifndef CONSUMER_HH
62
#define CONSUMER_HH
73

@@ -11,36 +7,35 @@ using namespace reactor;
117
using namespace std::chrono_literals;
128

139
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_;
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;
1815

19-
[[maybe_unused]] const Inner& __lf_inner = *this;
16+
const Inner& _lf_inner = *this;
2017

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

2522
friend Consumer;
2623
};
2724

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

3836
Input<unsigned> in{"in", this};
3937

40-
void assemble() override {
41-
handle.declare_trigger(&in);
42-
}
38+
void assemble() override { handle.declare_trigger(&in); }
4339
};
4440

45-
46-
#endif //CONSUMER_HH
41+
#endif // CONSUMER_HH
Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
//
2-
// Created by tanneberger on 11/17/24.
3-
//
4-
51
#ifndef LOAD_BALANCER_HH
62
#define LOAD_BALANCER_HH
73

@@ -13,29 +9,32 @@
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 {
13+
class Inner : public MutableScope {
14+
explicit Inner(Reactor* reactor)
15+
: MutableScope(reactor) {}
16+
const Inner& _lf_inner = *this;
2117

2218
// 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);
19+
static void reaction_1(const Input<unsigned>& inbound, LogicalAction<unsigned>& scale_action,
20+
Multiport<Output<unsigned>>& outbound) {
21+
if (std::rand() % 30 == 0) { //NOLINT
22+
scale_action.schedule(std::rand() % 20 + 1); //NOLINT
2623
}
27-
unsigned sel = rand() % outbound.size();
28-
std::cout << "Sending out to:" << sel << std::endl;
24+
const unsigned sel = std::rand() % outbound.size(); //NOLINT
25+
std::cout << "Sending out to:" << sel << '\n';
2926
outbound[sel].set(inbound.get());
3027
}
3128

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

3634
auto antideps = (outbound[0]).anti_dependencies();
3735

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

4039
add_to_transaction(change_size);
4140

@@ -47,20 +46,25 @@ private:
4746
friend LoadBalancer;
4847
};
4948

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); }};
49+
Inner _lf_inner;
50+
Reaction process{"process", 2, this,
51+
[this]() { _lf_inner.reaction_1(this->inbound, this->scale_action, this->out); }};
52+
Reaction scale{"scale", 1, this,
53+
[this]() { _lf_inner.reaction_2(this->out, this->scale_action, this->scale_bank); }};
5354

5455
public:
5556
LoadBalancer(const std::string& name, Environment* env)
56-
: Reactor(name, env), __lf_inner(this) {
57-
std::cout << "creating instance of load balancer" << std::endl;
57+
: Reactor(name, env)
58+
, _lf_inner(this) {
59+
std::cout << "creating instance of load balancer" << '\n';
5860
out.reserve(4);
5961
for (size_t _lf_idx = 0; _lf_idx < 4; _lf_idx++) {
60-
std::string _lf_port_name = out.name() + "_" + std::to_string(_lf_idx);
62+
std::string _lf_port_name = out.name() + "_" + std::to_string(_lf_idx);
6163
out.emplace_back(_lf_port_name, this);
6264
}
6365
}
66+
~LoadBalancer() override = default;
67+
6468

6569
LogicalAction<unsigned> scale_action{"scale", this, 1us};
6670
ModifableMultiport<Output<unsigned>> out{"out"};
@@ -69,15 +73,13 @@ public:
6973

7074
void assemble() override {
7175
std::cout << "assemble LoadBalancer\n";
72-
for (auto& __lf_port : out) {
73-
process.declare_antidependency(&__lf_port);
76+
for (auto& _lf_port : out) {
77+
process.declare_antidependency(&_lf_port);
7478
}
7579
process.declare_trigger(&inbound);
7680
scale.declare_trigger(&scale_action);
7781
scale.declare_antidependency(&scale_bank);
7882
}
7983
};
8084

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

examples/multiport_mutation/main.cc

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,92 +3,89 @@
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 {
13+
class Deployment final : public Reactor {
1414
std::unique_ptr<Producer> producer_;
1515
std::unique_ptr<LoadBalancer> load_balancer_;
1616
std::vector<std::unique_ptr<Consumer>> consumers_;
1717

18-
Reaction scale_bank{"scale_bank", 1, this, [this](){this->__inner.reaction_1(this->scale, this->consumers_, load_balancer_->out);}};
18+
Reaction scale_bank{"scale_bank", 1, this,
19+
[this]() { this->_inner.reaction_1(this->scale, this->consumers_, load_balancer_->out); }};
1920

2021
public:
21-
22-
class Inner: public MutableScope {
22+
class Inner : public MutableScope {
2323
int state = 0;
24-
[[maybe_unused]] const Inner& __lf_inner = *this;
25-
public:
24+
const Inner& _lf_inner = *this;
2625

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) {
26+
public:
27+
Inner(Reactor* reactor)
28+
: MutableScope(reactor) {}
29+
void reaction_1(const Input<unsigned>& scale, std::vector<std::unique_ptr<Consumer>>& reactor_bank,
30+
ModifableMultiport<Output<unsigned>>& load_balancer) {
2931
std::size_t new_size = *scale.get();
3032
std::size_t old_size = reactor_bank.size();
3133

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);
34+
std::function lambda = [](Reactor* reactor, std::size_t index) {
35+
std::string _lf_inst_name = "consumer_" + std::to_string(index);
36+
return std::make_unique<Consumer>(_lf_inst_name, reactor->environment(), index);
3537
};
36-
auto change_size = std::make_shared<MutationChangeBankSize<std::unique_ptr<Consumer>>>(&reactor_bank, this->reactor_, new_size, lambda);
38+
39+
auto change_size = std::make_shared<MutationChangeBankSize<std::unique_ptr<Consumer>>>(
40+
&reactor_bank, this->reactor_, new_size, lambda);
3741

3842
add_to_transaction(change_size);
3943

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

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;
46+
if (old_size < new_size) {
5047
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);
48+
auto add_conn = std::make_shared<MutationAddConnection<Output<unsigned>, Input<unsigned>>>(
49+
&load_balancer[i], &reactor_bank[i].get()->in, reactor_);
50+
add_to_transaction(add_conn);
5351
}
54-
commit_transaction(true);
5552
}
5653

57-
std::cout << "new bank size:" << reactor_bank.size() << std::endl;
54+
commit_transaction(true);
5855
}
5956

6057
friend LoadBalancer;
6158
};
6259

63-
Inner __inner;
60+
Inner _inner;
6461

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;
62+
Deployment(const std::string& name, Environment* env)
63+
: Reactor(name, env)
64+
, _inner(this)
65+
, producer_(std::make_unique<Producer>("producer", environment()))
66+
, load_balancer_(std::make_unique<LoadBalancer>("load_balancer", environment())) {
67+
std::cout << "creating instance of deployment" << '\n';
6968
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));
69+
for (size_t _lf_idx = 0; _lf_idx < 4; _lf_idx++) {
70+
std::string _lf_inst_name = "consumer_" + std::to_string(_lf_idx);
71+
consumers_.push_back(std::make_unique<Consumer>(_lf_inst_name, environment(), _lf_idx));
7372
}
7473
}
7574
~Deployment() override = default;
7675

7776
Input<unsigned> scale{"scale", this};
7877

7978
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{});
79+
for (size_t _lf_idx = 0; _lf_idx < 4; _lf_idx++) {
80+
environment()->draw_connection(load_balancer_->out[_lf_idx], consumers_[_lf_idx]->in, ConnectionProperties{});
8281
environment()->draw_connection(producer_->value, load_balancer_->inbound, ConnectionProperties{});
8382
}
8483
environment()->draw_connection(load_balancer_->scale_bank, scale, ConnectionProperties{});
8584
scale_bank.declare_trigger(&this->scale);
8685
}
8786
};
8887

89-
9088
auto main() -> int {
91-
//srand(time(nullptr));
9289
Environment env{4, true};
9390
auto deployment = std::make_unique<Deployment>("c1", &env);
9491
env.optimize();
Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
//
2-
// Created by tanneberger on 11/17/24.
3-
//
4-
51
#ifndef PRODUCER_HH
62
#define PRODUCER_HH
73

@@ -13,23 +9,29 @@ using namespace std::chrono_literals;
139
class Producer : public Reactor {
1410
private:
1511
Timer timer{"timer", this, 1s, 1s};
16-
Reaction r_timer{"r_timer", 1, this, [this]() { __lf_inner.reaction_1(this->value);}};
12+
Reaction r_timer{"r_timer", 1, this, [this]() { __lf_inner.reaction_1(this->value); }};
13+
14+
class Inner : public Scope {
15+
unsigned int counter_ = 0;
16+
const Inner& __lf_inner = *this;
1717

18-
class Inner: public Scope {
19-
unsigned itr = 0;
20-
[[maybe_unused]] const Inner& __lf_inner = *this;
2118
void reaction_1([[maybe_unused]] Output<unsigned>& out) {
22-
std::cout << "producing value:" << itr << std::endl;
23-
out.set(itr++);
19+
std::cout << "producing value:" << counter_ << std::endl;
20+
out.set(counter_++);
2421
}
25-
explicit Inner(Reactor* reactor) : Scope(reactor) {}
22+
23+
explicit Inner(Reactor* reactor)
24+
: Scope(reactor) {}
2625

2726
friend Producer;
2827
};
2928

3029
Inner __lf_inner;
30+
3131
public:
32-
Producer(const std::string& name, Environment* env) : Reactor(name, env), __lf_inner(this) {
32+
Producer(const std::string& name, Environment* env)
33+
: Reactor(name, env)
34+
, __lf_inner(this) {
3335
std::cout << "creating instance of producer" << std::endl;
3436
}
3537
Producer() = delete;
@@ -43,4 +45,4 @@ public:
4345
}
4446
};
4547

46-
#endif //PRODUCER_HH
48+
#endif // PRODUCER_HH

0 commit comments

Comments
 (0)