123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- #include "ModuleBase.h"
- #include <iostream>
- 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<double> default_val = { 1 };
- inputs.push_back(std::make_shared<Input>(this, "Switch", 0, default_val));
- inputs.push_back(std::make_shared<Input>(this, "Default", 0, default_val));
- outputs.push_back(std::make_shared<Output>(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<std::string>() == "size")
- {
- size_t length = config_parsed[i]["value"].get<size_t>();
- 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<Input>(this, "Value", inputs.size() - 1, std::vector<double>{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<std::string>() == "size")
- {
- ret[i]["value"] = inputs.size() - 1;
- }
- }
- }
- return ret.dump();
- }
- state ModuleSwitch::update(){
- std::vector<double> 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);
- }
- }
|