|
5 | 5 | #ifndef MULTIPORT_TO_BANK_HH |
6 | 6 | #define MULTIPORT_TO_BANK_HH |
7 | 7 |
|
8 | | -#include <reactor-cpp/mutations.hh> |
9 | 8 | #include <reactor-cpp/multiport.hh> |
10 | | -#include <reactor-cpp/port.hh> |
11 | | -#include <reactor-cpp/mutations/multiport.hh> |
| 9 | +#include <reactor-cpp/mutations.hh> |
12 | 10 | #include <reactor-cpp/mutations/bank.hh> |
13 | 11 | #include <reactor-cpp/mutations/connection.hh> |
| 12 | +#include <reactor-cpp/mutations/multiport.hh> |
| 13 | +#include <reactor-cpp/port.hh> |
14 | 14 | #include <reactor-cpp/reactor.hh> |
15 | 15 |
|
16 | 16 | #include "../../lib/mutations/bank.cc" |
|
21 | 21 |
|
22 | 22 | namespace reactor { |
23 | 23 |
|
24 | | - template<class PortType, class ReactorType> |
25 | | - class ResizeMultiportToBank : public Mutation { |
26 | | - ModifableMultiport<Output<PortType>>* multiport_; |
27 | | - std::vector<std::unique_ptr<ReactorType>>* bank_; |
28 | | - std::function<Input<PortType>*(const std::unique_ptr<ReactorType>&)> get_input_port_; |
29 | | - std::function<std::unique_ptr<ReactorType>(Environment* env, std::size_t index)> create_lambda_; |
30 | | - std::size_t new_size_ = 0; |
31 | | - public: |
32 | | - ResizeMultiportToBank(ModifableMultiport<Output<PortType>>* multiport, |
33 | | - std::vector<std::unique_ptr<ReactorType>>* bank, |
34 | | - std::function<Input<PortType>*(const std::unique_ptr<ReactorType>&)> get_input_port, |
35 | | - std::function<std::unique_ptr<ReactorType>(Environment* env, std::size_t index)> create_lambda, |
36 | | - std::size_t new_size) : |
37 | | - multiport_(multiport), bank_(bank), get_input_port_(get_input_port), create_lambda_(create_lambda), new_size_(new_size) {} |
38 | | - |
39 | | - ~ResizeMultiportToBank() = default; |
40 | | - auto run() -> MutationResult { |
41 | | - if (multiport_->size() != bank_->size()) { |
42 | | - return NotMatchingBankSize; |
43 | | - } |
44 | | - auto old_size = multiport_->size(); |
45 | | - |
46 | | - if (new_size_ > old_size) { |
47 | | - // TODO: this is an assumption |
48 | | - auto change_multiport_size = |
49 | | - std::make_shared<MutationChangeOutputMultiportSize<unsigned>>(multiport_, new_size_); |
| 24 | +template <class PortType, class ReactorType> class ResizeMultiportToBank : public Mutation { |
| 25 | + ModifableMultiport<Output<PortType>>* multiport_; |
| 26 | + std::vector<std::unique_ptr<ReactorType>>* bank_; |
| 27 | + std::function<Input<PortType>*(const std::unique_ptr<ReactorType>&)> get_input_port_; |
| 28 | + std::function<std::unique_ptr<ReactorType>(Environment* env, std::size_t index)> create_lambda_; |
| 29 | + std::size_t new_size_ = 0; |
50 | 30 |
|
51 | | - change_multiport_size->run(); |
| 31 | +public: |
| 32 | + ResizeMultiportToBank(ModifableMultiport<Output<PortType>>* multiport, |
| 33 | + std::vector<std::unique_ptr<ReactorType>>* bank, |
| 34 | + std::function<Input<PortType>*(const std::unique_ptr<ReactorType>&)> get_input_port, |
| 35 | + std::function<std::unique_ptr<ReactorType>(Environment* env, std::size_t index)> create_lambda, |
| 36 | + std::size_t new_size) |
| 37 | + : multiport_(multiport) |
| 38 | + , bank_(bank) |
| 39 | + , get_input_port_(get_input_port) |
| 40 | + , create_lambda_(create_lambda) |
| 41 | + , new_size_(new_size) {} |
52 | 42 |
|
53 | | - auto change_bank_size = std::make_shared<MutationChangeBankSize<std::unique_ptr<ReactorType>>>( |
54 | | - bank_, (*bank_)[0]->environment(), new_size_, create_lambda_); |
| 43 | + ~ResizeMultiportToBank() = default; |
| 44 | + auto run() -> MutationResult { |
| 45 | + if (multiport_->size() != bank_->size()) { |
| 46 | + return NotMatchingBankSize; |
| 47 | + } |
| 48 | + auto old_size = multiport_->size(); |
55 | 49 |
|
56 | | - change_bank_size->run(); |
| 50 | + if (new_size_ > old_size) { |
| 51 | + auto change_multiport_size = std::make_shared<MutationChangeOutputMultiportSize<unsigned>>(multiport_, new_size_); |
57 | 52 |
|
58 | | - for (auto i = old_size; i < new_size_; i++) { |
59 | | - auto add_conn = std::make_shared<MutationAddConnection<Output<PortType>, Input<PortType>>>( |
60 | | - &(*multiport_)[i], get_input_port_((*bank_)[i]), (*bank_)[0]->environment(), true); |
| 53 | + change_multiport_size->run(); |
61 | 54 |
|
62 | | - add_conn->run(); |
63 | | - } |
64 | | - } else if (new_size_ < old_size) { |
65 | | - for (auto i = old_size - 1; i >= new_size_; i--) { |
66 | | - auto add_conn = std::make_shared<MutationAddConnection<Output<PortType>, Input<PortType>>>( |
67 | | - &(*multiport_)[i], get_input_port_((*bank_)[i]), (*bank_)[0]->environment(), false); |
| 55 | + auto change_bank_size = std::make_shared<MutationChangeBankSize<std::unique_ptr<ReactorType>>>( |
| 56 | + bank_, (*bank_)[0]->environment(), new_size_, create_lambda_); |
68 | 57 |
|
69 | | - add_conn->run(); |
70 | | - } |
| 58 | + change_bank_size->run(); |
71 | 59 |
|
72 | | - auto change_multiport_size = |
73 | | - std::make_shared<MutationChangeOutputMultiportSize<unsigned>>(multiport_, new_size_); |
| 60 | + for (auto i = old_size; i < new_size_; i++) { |
| 61 | + auto add_conn = std::make_shared<MutationAddConnection<Output<PortType>, Input<PortType>>>( |
| 62 | + &(*multiport_)[i], get_input_port_((*bank_)[i]), (*bank_)[0]->environment(), true); |
74 | 63 |
|
75 | | - change_multiport_size->run(); |
| 64 | + add_conn->run(); |
| 65 | + } |
| 66 | + } else if (new_size_ < old_size) { |
| 67 | + for (auto i = old_size - 1; i >= new_size_; i--) { |
| 68 | + auto add_conn = std::make_shared<MutationAddConnection<Output<PortType>, Input<PortType>>>( |
| 69 | + &(*multiport_)[i], get_input_port_((*bank_)[i]), (*bank_)[0]->environment(), false); |
76 | 70 |
|
77 | | - auto change_bank_size = std::make_shared<MutationChangeBankSize<std::unique_ptr<ReactorType>>>( |
78 | | - bank_, (*bank_)[0]->environment(), new_size_, create_lambda_); |
| 71 | + add_conn->run(); |
| 72 | + } |
79 | 73 |
|
80 | | - change_bank_size->run(); |
81 | | - } |
| 74 | + auto change_multiport_size = std::make_shared<MutationChangeOutputMultiportSize<unsigned>>(multiport_, new_size_); |
82 | 75 |
|
| 76 | + change_multiport_size->run(); |
83 | 77 |
|
84 | | - return Success; |
85 | | - } |
| 78 | + auto change_bank_size = std::make_shared<MutationChangeBankSize<std::unique_ptr<ReactorType>>>( |
| 79 | + bank_, (*bank_)[0]->environment(), new_size_, create_lambda_); |
86 | 80 |
|
87 | | - auto rollback() -> MutationResult { |
88 | | - return Success; |
89 | | - } |
90 | | - }; |
91 | | -} |
| 81 | + change_bank_size->run(); |
| 82 | + } |
92 | 83 |
|
| 84 | + return Success; |
| 85 | + } |
93 | 86 |
|
| 87 | + auto rollback() -> MutationResult { return Success; } |
| 88 | +}; |
| 89 | +} // namespace reactor |
94 | 90 |
|
95 | | -#endif //MULTIPORT_TO_BANK_HH |
| 91 | +#endif // MULTIPORT_TO_BANK_HH |
0 commit comments