Skip to content

Commit c3d55f4

Browse files
committed
code cleaning
1 parent 9e1b6ed commit c3d55f4

File tree

6 files changed

+95
-82
lines changed

6 files changed

+95
-82
lines changed

examples/multiport_mutation/consumer.hh

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,35 @@ using namespace reactor;
1111
using namespace std::chrono_literals;
1212

1313
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_;
14+
class Inner : public Scope {
15+
Inner(Reactor* reactor, std::size_t index)
16+
: Scope(reactor)
17+
, index_(index) {}
18+
std::size_t index_ = 0;
1819

19-
[[maybe_unused]] const Inner& __lf_inner = *this;
20+
const Inner& _lf_inner = *this;
2021

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

2526
friend Consumer;
2627
};
2728

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

3840
Input<unsigned> in{"in", this};
3941

40-
void assemble() override {
41-
handle.declare_trigger(&in);
42-
}
42+
void assemble() override { handle.declare_trigger(&in); }
4343
};
4444

45-
46-
#endif //CONSUMER_HH
45+
#endif // CONSUMER_HH

examples/multiport_mutation/load_balancer.hh

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ using namespace std::chrono_literals;
1515

1616
class LoadBalancer : public Reactor {
1717
private:
18-
class Inner: public MutableScope {
19-
Inner(Reactor* reactor) : MutableScope(reactor) {}
20-
[[maybe_unused]] const Inner& __lf_inner = *this;
18+
class Inner : public MutableScope {
19+
Inner(Reactor* reactor)
20+
: MutableScope(reactor) {}
21+
const Inner& __lf_inner = *this;
2122

2223
// reaction bodies
23-
void reaction_1(const Input<unsigned>& inbound, LogicalAction<unsigned>& scale_action, Multiport<Output<unsigned>>& outbound) {
24+
void reaction_1(const Input<unsigned>& inbound, LogicalAction<unsigned>& scale_action,
25+
Multiport<Output<unsigned>>& outbound) {
2426
if (rand() % 30 == 0) {
2527
scale_action.schedule(rand() % 20 + 1);
2628
}
@@ -29,13 +31,15 @@ private:
2931
outbound[sel].set(inbound.get());
3032
}
3133

32-
void reaction_2(ModifableMultiport<Output<unsigned>>&outbound, [[maybe_unused]] const LogicalAction<unsigned>& scale, Output<unsigned>& scale_bank) {
34+
void reaction_2(ModifableMultiport<Output<unsigned>>& outbound,
35+
[[maybe_unused]] const LogicalAction<unsigned>& scale, Output<unsigned>& scale_bank) {
3336
ModifableMultiport<Output<unsigned>>* temp = &outbound;
3437
std::size_t new_size = *scale.get();
3538

3639
auto antideps = (outbound[0]).anti_dependencies();
3740

38-
auto change_size = std::make_shared<MutationChangeOutputMultiportSize<unsigned>>(temp, this->reactor_, antideps, new_size);
41+
auto change_size =
42+
std::make_shared<MutationChangeOutputMultiportSize<unsigned>>(temp, this->reactor_, antideps, new_size);
3943

4044
add_to_transaction(change_size);
4145

@@ -48,16 +52,19 @@ private:
4852
};
4953

5054
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); }};
55+
Reaction process{"process", 2, this,
56+
[this]() { __lf_inner.reaction_1(this->inbound, this->scale_action, this->out); }};
57+
Reaction scale{"scale", 1, this,
58+
[this]() { __lf_inner.reaction_2(this->out, this->scale_action, this->scale_bank); }};
5359

5460
public:
5561
LoadBalancer(const std::string& name, Environment* env)
56-
: Reactor(name, env), __lf_inner(this) {
62+
: Reactor(name, env)
63+
, __lf_inner(this) {
5764
std::cout << "creating instance of load balancer" << std::endl;
5865
out.reserve(4);
5966
for (size_t _lf_idx = 0; _lf_idx < 4; _lf_idx++) {
60-
std::string _lf_port_name = out.name() + "_" + std::to_string(_lf_idx);
67+
std::string _lf_port_name = out.name() + "_" + std::to_string(_lf_idx);
6168
out.emplace_back(_lf_port_name, this);
6269
}
6370
}
@@ -78,6 +85,4 @@ public:
7885
}
7986
};
8087

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

examples/multiport_mutation/main.cc

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,68 +3,67 @@
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

1313
class Deployment : 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) {
34+
std::function lambda = [](Reactor* reactor, std::size_t index) {
3335
std::string __lf_inst_name = "consumer_" + std::to_string(index);
3436
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

6360
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())) {
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())) {
6867
std::cout << "creating instance of deployment" << std::endl;
6968
consumers_.reserve(4);
7069
for (size_t __lf_idx = 0; __lf_idx < 4; __lf_idx++) {
@@ -86,9 +85,8 @@ class Inner: public MutableScope {
8685
}
8786
};
8887

89-
9088
auto main() -> int {
91-
//srand(time(nullptr));
89+
// srand(time(nullptr));
9290
Environment env{4, true};
9391
auto deployment = std::make_unique<Deployment>("c1", &env);
9492
env.optimize();

examples/multiport_mutation/producer.hh

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,29 @@ using namespace std::chrono_literals;
1313
class Producer : public Reactor {
1414
private:
1515
Timer timer{"timer", this, 1s, 1s};
16-
Reaction r_timer{"r_timer", 1, this, [this]() { __lf_inner.reaction_1(this->value);}};
16+
Reaction r_timer{"r_timer", 1, this, [this]() { __lf_inner.reaction_1(this->value); }};
17+
18+
class Inner : public Scope {
19+
unsigned int counter_ = 0;
20+
const Inner& __lf_inner = *this;
1721

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

2730
friend Producer;
2831
};
2932

3033
Inner __lf_inner;
34+
3135
public:
32-
Producer(const std::string& name, Environment* env) : Reactor(name, env), __lf_inner(this) {
36+
Producer(const std::string& name, Environment* env)
37+
: Reactor(name, env)
38+
, __lf_inner(this) {
3339
std::cout << "creating instance of producer" << std::endl;
3440
}
3541
Producer() = delete;
@@ -43,4 +49,4 @@ public:
4349
}
4450
};
4551

46-
#endif //PRODUCER_HH
52+
#endif // PRODUCER_HH

examples/power_train/main.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ class Engine : public Reactor {
141141
// ports
142142
Input<void> torque{"torque", this}; // NOLINT
143143
Multiport<int> control_input;
144+
144145
private:
145146
// reactions_
146147
Reaction r1{"1", 1, this, [this]() { reaction_1(); }};

examples/unit_tests_mutations/main.cc

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,25 @@
66

77
using namespace std::chrono_literals;
88

9-
class TestMultiport: public reactor::Reactor {
9+
class TestMultiport : public reactor::Reactor {
1010
reactor::ModifableMultiport<reactor::Output<int>> test_multiport_{"modifable_multiports_"};
1111
reactor::Timer timer_{"timer", this, 1s};
1212
reactor::LogicalAction<int> scale{"scale", this};
13-
reactor::Reaction trigger_reaction_{"trigger_reaction", 1, this, [this](){this->__inner.reaction_1(scale);}};
14-
reactor::Reaction test_reaction_{"test_reaction", 2, this, [this](){this->__inner.reaction_2(test_multiport_, scale);}};
15-
reactor::Reaction validate_reaction_{"validate_reaction", 3, this, [this](){this->__inner.reaction_3(test_multiport_);}};
16-
public:
13+
reactor::Reaction trigger_reaction_{"trigger_reaction", 1, this, [this]() { this->__inner.reaction_1(scale); }};
14+
reactor::Reaction test_reaction_{"test_reaction", 2, this,
15+
[this]() { this->__inner.reaction_2(test_multiport_, scale); }};
16+
reactor::Reaction validate_reaction_{"validate_reaction", 3, this,
17+
[this]() { this->__inner.reaction_3(test_multiport_); }};
1718

18-
class Inner: public reactor::MutableScope {
19+
public:
20+
class Inner : public reactor::MutableScope {
1921
int state = 0;
2022
std::vector<int> sizes = {4, 5, 6, 5, 4, 3};
2123
[[maybe_unused]] const Inner& __lf_inner = *this;
22-
public:
2324

24-
Inner(Reactor* reactor) : MutableScope(reactor) {}
25+
public:
26+
Inner(Reactor* reactor)
27+
: MutableScope(reactor) {}
2528

2629
void reaction_1(reactor::LogicalAction<int>& scale) {
2730
int size = sizes[state];
@@ -30,7 +33,8 @@ class Inner: public reactor::MutableScope {
3033
scale.schedule(size);
3134
}
3235

33-
void reaction_2(reactor::ModifableMultiport<reactor::Output<int>>& test_multiport, reactor::LogicalAction<int>& scale) {
36+
void reaction_2(reactor::ModifableMultiport<reactor::Output<int>>& test_multiport,
37+
reactor::LogicalAction<int>& scale) {
3438
reactor::ModifableMultiport<reactor::Output<int>>* temp = &test_multiport;
3539
std::size_t new_size = *scale.get();
3640

@@ -50,12 +54,14 @@ class Inner: public reactor::MutableScope {
5054

5155
Inner __inner;
5256

53-
TestMultiport(const std::string& name, reactor::Environment* env) : Reactor(name, env), __inner(this) {
57+
TestMultiport(const std::string& name, reactor::Environment* env)
58+
: Reactor(name, env)
59+
, __inner(this) {
5460
std::cout << "creating instance of deployment" << std::endl;
5561

5662
test_multiport_.reserve(4);
5763
for (size_t _lf_idx = 0; _lf_idx < 4; _lf_idx++) {
58-
std::string _lf_port_name = test_multiport_.name() + "_" + std::to_string(_lf_idx);
64+
std::string _lf_port_name = test_multiport_.name() + "_" + std::to_string(_lf_idx);
5965
test_multiport_.emplace_back(_lf_port_name, this);
6066
}
6167
}
@@ -65,11 +71,9 @@ class Inner: public reactor::MutableScope {
6571
trigger_reaction_.declare_trigger(&timer_);
6672
trigger_reaction_.declare_schedulable_action(&scale);
6773
test_reaction_.declare_trigger(&scale);
68-
6974
}
7075
};
7176

72-
7377
auto main() -> int {
7478
// srand(time(nullptr));
7579
reactor::Environment env{4};

0 commit comments

Comments
 (0)