From f6924369823f9df92a35d037e4d0aaffc837f7eb Mon Sep 17 00:00:00 2001
From: Christoph Pohl <christoph.pohl@kit.edu>
Date: Thu, 7 Dec 2023 18:05:38 +0100
Subject: [PATCH] Fix splicing in SkillBlueprints

---
 .../provider/SkillProviderComponentPlugin.cpp  |  4 ++--
 .../blueprints/SkillWithContextBlueprint.h     | 18 ++++++++++++++++--
 .../detail/SkillImplementationWrapper.cpp      |  5 +++--
 .../detail/SkillImplementationWrapper.h        |  5 +++--
 4 files changed, 24 insertions(+), 8 deletions(-)

diff --git a/source/RobotAPI/libraries/skills/provider/SkillProviderComponentPlugin.cpp b/source/RobotAPI/libraries/skills/provider/SkillProviderComponentPlugin.cpp
index 3d775f587..730ce95a6 100644
--- a/source/RobotAPI/libraries/skills/provider/SkillProviderComponentPlugin.cpp
+++ b/source/RobotAPI/libraries/skills/provider/SkillProviderComponentPlugin.cpp
@@ -210,7 +210,7 @@ namespace armarx::plugins
                 auto it =
                     skillExecutions.emplace(std::piecewise_construct,
                                             std::make_tuple(executionId),
-                                            std::make_tuple(*fac,
+                                            std::make_tuple(fac,
                                                             executionId,
                                                             executionRequest.parameters,
                                                             executionRequest.callbackInterface));
@@ -265,7 +265,7 @@ namespace armarx::plugins
                 auto it =
                     skillExecutions.emplace(std::piecewise_construct,
                                             std::make_tuple(executionId),
-                                            std::make_tuple(*fac,
+                                            std::make_tuple(fac,
                                                             executionId,
                                                             executionRequest.parameters,
                                                             executionRequest.callbackInterface));
diff --git a/source/RobotAPI/libraries/skills/provider/blueprints/SkillWithContextBlueprint.h b/source/RobotAPI/libraries/skills/provider/blueprints/SkillWithContextBlueprint.h
index 0efc94c81..db852e076 100644
--- a/source/RobotAPI/libraries/skills/provider/blueprints/SkillWithContextBlueprint.h
+++ b/source/RobotAPI/libraries/skills/provider/blueprints/SkillWithContextBlueprint.h
@@ -49,19 +49,33 @@ namespace armarx
             virtual std::unique_ptr<Skill>
             createSkill(const ProviderID& pid) const override
             {
+                if (not isConnected_)
+                {
+                    return nullptr;
+                }
                 auto s = _createSkill();
                 s->setProviderId(pid);
                 static_cast<SkillT*>(s.get())->connectTo(context_);
                 return s;
             }
 
+            virtual SkillDescription
+            createSkillDescription(const skills::ProviderID& pid) const
+            {
+                auto s = _createSkill();
+                s->setProviderId(pid);
+                return s->getSkillDescription();
+            }
+
             void
-            connectTo(const ContextT& services)
+            connectTo(const ContextT& context)
             {
-                context_ = services;
+                context_ = context;
+                isConnected_ = true;
             }
 
         private:
+            std::atomic_bool isConnected_ = false;
             ContextT context_;
         };
     } // namespace skills
diff --git a/source/RobotAPI/libraries/skills/provider/detail/SkillImplementationWrapper.cpp b/source/RobotAPI/libraries/skills/provider/detail/SkillImplementationWrapper.cpp
index 25fe34efa..c93bbe8fa 100644
--- a/source/RobotAPI/libraries/skills/provider/detail/SkillImplementationWrapper.cpp
+++ b/source/RobotAPI/libraries/skills/provider/detail/SkillImplementationWrapper.cpp
@@ -5,7 +5,7 @@ namespace armarx
     namespace skills::detail
     {
         SkillRuntime::SkillRuntime(
-            const skills::SkillBlueprint& fac,
+            const skills::SkillBlueprint* fac,
             const skills::SkillExecutionID& execId,
             const aron::data::DictPtr& initial_parameters,
             const skills::callback::dti::SkillProviderCallbackInterfacePrx& callbackInterface) :
@@ -120,7 +120,8 @@ namespace armarx
             ARMARX_INFO_S << "Construct skill: " << skillName;
 
             updateStatus(SkillStatus::Constructing);
-            this->skill = this->factory.createSkill(providerId);
+            ARMARX_CHECK_NOT_NULL(factory) << "Skill Factory is a nullptr";
+            this->skill = this->factory->createSkill(providerId);
             this->skill->setExecutorName(executorName);
             this->skill->setManager(manager);
             this->skill->setCallback([&](const SkillStatus s, const armarx::aron::data::DictPtr& d)
diff --git a/source/RobotAPI/libraries/skills/provider/detail/SkillImplementationWrapper.h b/source/RobotAPI/libraries/skills/provider/detail/SkillImplementationWrapper.h
index e5a5b1f82..935fa227b 100644
--- a/source/RobotAPI/libraries/skills/provider/detail/SkillImplementationWrapper.h
+++ b/source/RobotAPI/libraries/skills/provider/detail/SkillImplementationWrapper.h
@@ -1,5 +1,6 @@
 #pragma once
 
+#include <experimental/memory>
 #include <shared_mutex>
 
 #include <RobotAPI/interface/skills/SkillManagerInterface.h>
@@ -18,7 +19,7 @@ namespace armarx
             class SkillRuntime
             {
             private:
-                const skills::SkillBlueprint factory;
+                const std::experimental::observer_ptr<const skills::SkillBlueprint> factory;
 
                 std::unique_ptr<Skill> skill;
                 mutable std::mutex executionMutex;
@@ -33,7 +34,7 @@ namespace armarx
                 std::thread execution;
 
                 // ctor
-                SkillRuntime(const skills::SkillBlueprint& fac,
+                SkillRuntime(const skills::SkillBlueprint* fac,
                              const skills::SkillExecutionID&,
                              const aron::data::DictPtr&,
                              const skills::callback::dti::SkillProviderCallbackInterfacePrx&);
-- 
GitLab