Browse Source

fix getModule

Willi Zschiebsch 4 years ago
parent
commit
cff4e3f3c2
1 changed files with 51 additions and 37 deletions
  1. 51 37
      lib/src/ProcessorBase.cpp

+ 51 - 37
lib/src/ProcessorBase.cpp

@@ -590,61 +590,75 @@ namespace mdd{
     }
 
     std::shared_ptr<IModule> ProcessorBase::getModule(const json& jid){
-        json jid_copy = jid;
         auto jid_typ = jid.find("type");
-        auto jid_copy_pref = jid_copy.find("prefix");
-        //check if called non recursive
+        auto jid_pref = jid.find("prefix");
+        
         json own_prefix = getIdentifier()["prefix"];
-        if (jid_copy_pref->dump() == own_prefix.dump())
+
+        int final_size = 0;
+        if (jid_typ != jid.end())
         {
-            jid_copy_pref->clear();
+            if (*jid_typ == "input" || *jid_typ == "output")
+            {
+                final_size = 1;
+            }
         }
-        //recursive call
-        if (!jid_copy_pref->empty())
+
+        //check if called non recursive
+        int same_counter = 0;
+        for (size_t i = 0; i < own_prefix.size(); i++)
         {
-            jid_copy_pref->erase(jid_copy_pref->begin());
+            if (jid_pref->at(i).get<std::string>() != own_prefix.at(i).get<std::string>())
+            {
+                break;
+            }
+            ++same_counter;
         }
-        int final_size = 0;
-        if (jid_typ != jid.end())
+
+        if (own_prefix.size() == jid_pref->size())
         {
-            std::string sid;
-            if (jid_copy_pref->empty())
+            if (same_counter == jid_pref->size())
             {
-                if (*jid_typ == "input" || *jid_typ == "output")
-                {
-                    final_size = 1;
-                    sid = jid_copy_pref->at(0).get<std::string>();
-                }
-                else
-                {
-                    sid = jid_copy["key"].get<std::string>() + std::to_string(jid_copy["appendix"].get<int>());
-                }
+                return shared_from_this();
             }
-            else {
-                sid = jid_copy_pref->begin()->get<std::string>();
+            else
+            {
+                return nullptr;
             }
+        }
+        std::string sid;
+        ++same_counter;
+        if (same_counter + final_size == jid_pref->size())
+        {
+            sid = jid["key"].get<std::string>() + std::to_string(jid["appendix"].get<int>());
+        }
+        else
+        {
+            sid = jid_pref->at(same_counter).get<std::string>();
+        }
 
-            IModule::Ptr module_ptr;
-            for (auto& module : modules) {
-                if (module->getID() == sid) {
-                    module_ptr = module;
-                    break;
-                }
+        IModule::Ptr module_ptr;
+        for (auto& module : modules) {
+            if (module->getID() == sid) {
+                module_ptr = module;
+                break;
             }
-            if (jid_copy_pref->size() > final_size)
+        }
+        
+        if (module_ptr != nullptr)
+        {
+            if (jid_pref->size() >= final_size)
             {
                 if (ProcessorBase* proc_ptr = dynamic_cast<ProcessorBase*>(&(*module_ptr)))
                 {
-                    return proc_ptr->getModule(jid_copy);
+                    return proc_ptr->getModule(jid);
                 }
             }
-            else if(module_ptr != nullptr)
-            {
-                return module_ptr;
-            }
+            return module_ptr;
+        }
+        else {
+            return shared_from_this();
         }
-        
-        return shared_from_this();
         //return nullptr;
     }