|
| 1 | +// |
| 2 | +// Created by tanneberger on 1/13/25. |
| 3 | +// |
| 4 | + |
| 5 | +#ifndef MULTIPORT_TO_BANK_HH |
| 6 | +#define MULTIPORT_TO_BANK_HH |
| 7 | + |
| 8 | +#include <reactor-cpp/mutations.hh> |
| 9 | +#include <reactor-cpp/multiport.hh> |
| 10 | +#include <reactor-cpp/port.hh> |
| 11 | +#include <reactor-cpp/mutations/multiport.hh> |
| 12 | +#include <reactor-cpp/mutations/bank.hh> |
| 13 | +#include <reactor-cpp/mutations/connection.hh> |
| 14 | +#include <reactor-cpp/reactor.hh> |
| 15 | + |
| 16 | +#include "../../lib/mutations/bank.cc" |
| 17 | +#include "../../lib/mutations/connection.cc" |
| 18 | +#include "../../lib/mutations/multiport.cc" |
| 19 | + |
| 20 | +#include <functional> |
| 21 | + |
| 22 | +namespace reactor { |
| 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_); |
| 50 | + |
| 51 | + change_multiport_size->run(); |
| 52 | + |
| 53 | + auto change_bank_size = std::make_shared<MutationChangeBankSize<std::unique_ptr<ReactorType>>>( |
| 54 | + bank_, (*bank_)[0]->environment(), new_size_, create_lambda_); |
| 55 | + |
| 56 | + change_bank_size->run(); |
| 57 | + |
| 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); |
| 61 | + |
| 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); |
| 68 | + |
| 69 | + add_conn->run(); |
| 70 | + } |
| 71 | + |
| 72 | + auto change_multiport_size = |
| 73 | + std::make_shared<MutationChangeOutputMultiportSize<unsigned>>(multiport_, new_size_); |
| 74 | + |
| 75 | + change_multiport_size->run(); |
| 76 | + |
| 77 | + auto change_bank_size = std::make_shared<MutationChangeBankSize<std::unique_ptr<ReactorType>>>( |
| 78 | + bank_, (*bank_)[0]->environment(), new_size_, create_lambda_); |
| 79 | + |
| 80 | + change_bank_size->run(); |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + return Success; |
| 85 | + } |
| 86 | + |
| 87 | + auto rollback() -> MutationResult { |
| 88 | + return Success; |
| 89 | + } |
| 90 | + }; |
| 91 | +} |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | +#endif //MULTIPORT_TO_BANK_HH |
0 commit comments