#include "ModuleBase.h" #include namespace mdd{ class ModuleSwitch : public ModuleBase { public: ModuleSwitch(); bool configure(const std::string& config) override; std::string getConfiguration() override; state update() override; }; ModuleSwitch::ModuleSwitch() :ModuleBase(R"JSON( [{ "name":"size", "value": 1 }])JSON") { std::vector default_val = { 1 }; inputs.push_back(std::make_shared(this, "Switch", 0, default_val)); inputs.push_back(std::make_shared(this, "Default", 0, default_val)); outputs.push_back(std::make_shared(this, "Value", 0, default_val)); key = "ModuleSwitch"; setName("Switch"); } bool ModuleSwitch::configure(const std::string& config) { json config_parsed = json::parse(config); for (size_t i = 0; i < config_parsed.size(); i++) { if (config_parsed[i].contains("name")) { if (config_parsed[i]["name"].get() == "size") { size_t length = config_parsed[i]["value"].get(); if (length != inputs.size() -1) { size_t length_before = inputs.size(); if (length_before != 1) { inputs.back()->setName("Value"); } for (size_t j = length_before - 1; j < length; j++) { inputs.push_back(std::make_shared(this, "Value", inputs.size() - 1, std::vector{1})); } for (size_t j = length; j < length_before-1; j++) { inputs.pop_back(); } if (length != 0) { inputs.back()->setName("Default"); } } return true; } } } return false; } std::string ModuleSwitch::getConfiguration() { json ret = json::parse(ModuleBase::getConfiguration()); for (size_t i = 0; i < ret.size(); i++) { if (ret[i].contains("name")) { if (ret[i]["name"].get() == "size") { ret[i]["value"] = inputs.size() - 1; } } } return ret.dump(); } state ModuleSwitch::update(){ std::vector ret; size_t length = getInput(0)->getValue().size(); for (size_t i = 0; i < length; i++) { int index = (int)getInput(0)->getValue()[i]; if (index == 0) { index = 1; } if (index > inputs.size() - 1) { index = inputs.size() - 1; } for (size_t j = 0; j < getInput(index)->getValue().size(); j++) { ret.push_back(getInput(index)->getValue()[j]); } } return getOutput(0)->setValue(ret); } }