#include "ModuleBase.h" //#include namespace mdd { class ModuleMerge : public ModuleBase { private: public: ModuleMerge(); int addModuleInput(); int removeModuleInput(); std::string setInputName(const std::string& input_id, const std::string& new_name); bool configure(const std::string& config) override; std::string getConfiguration() override; state update() override; }; ModuleMerge::ModuleMerge() :ModuleBase(R"JSON( [{ "name":"inputs", "value":[2] }])JSON") { std::vector default_val = { 1 }; inputs.push_back(std::make_shared(this, "Value", 0, default_val)); inputs.push_back(std::make_shared(this, "Value", 1, default_val)); outputs.push_back(std::make_shared(this, "Value", 0, default_val)); key = "ModuleMerge"; setName("Merge"); } int ModuleMerge::addModuleInput() { std::vector default_val = { 1 }; inputs.push_back(std::make_shared(this, "Value", inputs.size(), default_val)); return inputs.size(); } int ModuleMerge::removeModuleInput() { inputs.pop_back(); return inputs.size(); } std::string ModuleMerge::setInputName(const std::string& input_id, const std::string& new_name) { for (int id = 0; id < inputs.size(); ++id) { if (getInput(id)->getID() == input_id) { getInput(id)->setName(new_name); return getInput(id)->getID(); } } return ""; } bool ModuleMerge::configure(const std::string& config) { bool success = false; success = ModuleBase::configure(config); json config_parsed = json::parse(config); if (!config_parsed.is_array()) { return success; } for (size_t i = 0; i < config_parsed.size(); i++) { if (config_parsed[i].contains("name")) { if (config_parsed[i]["name"].get() == "inputs") { size_t new_length = config_parsed[i]["value"][0].get(); size_t length = inputs.size(); if (length - new_length > 0) { inputs.erase(inputs.end() - (length - new_length), inputs.end()); } else { for (size_t j = length; j < new_length; j++) { inputs.push_back(std::make_shared(this, "Value", j, std::vector {1})); } } } } } return false; } std::string ModuleMerge::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() == "inputs") { size_t length = inputs.size(); for (size_t j = 0; j < length; j++) { json in; in["type"] = inputs[j]->getType(); in["value"] = inputs[j]->getValue(); ret[i]["value"].push_back(in); } } } } return ret.dump(); } state ModuleMerge::update() { std::vector ret; for (size_t i = 0; i < inputs.size(); i++) { for (size_t j = 0; j < getInput(i)->getValue().size(); j++) { ret.push_back(getInput(i)->getValue()[j]); } } return getOutput(0)->setValue(ret); } }