diff --git a/Sofa/framework/Core/simutest/objectmodel/BaseLink_simutest.cpp b/Sofa/framework/Core/simutest/objectmodel/BaseLink_simutest.cpp index 94bfe56f5ae..03f69825c08 100644 --- a/Sofa/framework/Core/simutest/objectmodel/BaseLink_simutest.cpp +++ b/Sofa/framework/Core/simutest/objectmodel/BaseLink_simutest.cpp @@ -114,6 +114,30 @@ TEST_F(BaseLink_test, remove) ASSERT_FALSE(owner.l_target.remove(nullptr)); } +TEST_F(BaseLink_test, add) +{ + FakeComponent Component1; + Component1.setName("Component1"); + FakeComponent Component2; + Component2.setName("Component2"); + FakeComponent Component3; + Component3.setName("Component3"); + + FakeComponent* ptr; + ptr = &Component2; + + EXPECT_EQ(Component1.l_target.getValueString(), ""); + + Component1.l_target.add(ptr); + EXPECT_EQ(Component1.l_target.getValueString(), "@Component2"); + + ptr = &Component3; + + Component1.l_target.add(ptr); + EXPECT_EQ(Component1.l_target.getValueString(), "@Component2 @Component3"); +} + + //////////////////////// Testing valid path ////////////////////////////////////// class MultiLink_simutest : public BaseLink_test {}; diff --git a/Sofa/framework/Core/src/sofa/core/objectmodel/BaseComponent.cpp b/Sofa/framework/Core/src/sofa/core/objectmodel/BaseComponent.cpp index c611e7b7a3c..52b37938401 100644 --- a/Sofa/framework/Core/src/sofa/core/objectmodel/BaseComponent.cpp +++ b/Sofa/framework/Core/src/sofa/core/objectmodel/BaseComponent.cpp @@ -225,7 +225,7 @@ void BaseComponent::addSlave(BaseComponent::SPtr s) if (previous == this) return; if (previous) previous->l_slaves.remove(s.get()); - l_slaves.add(s); + l_slaves.add(s.get()); if (previous) this->getContext()->notifyMoveSlave(previous.get(), this, s.get()); else diff --git a/Sofa/framework/Core/src/sofa/core/objectmodel/BaseLink.h b/Sofa/framework/Core/src/sofa/core/objectmodel/BaseLink.h index 8106c223e84..4c7c879f97b 100644 --- a/Sofa/framework/Core/src/sofa/core/objectmodel/BaseLink.h +++ b/Sofa/framework/Core/src/sofa/core/objectmodel/BaseLink.h @@ -171,6 +171,7 @@ class SOFA_CORE_API BaseLink /// Add a new target to the link. bool add(Base* baseptr, const std::string& path) { return _doAdd_(baseptr, path); } + bool add(Base* baseptr) { return _doAdd_(baseptr); } /// Change the link's target at the provided index. bool set(Base* baseptr, size_t index=0) { return _doSet_(baseptr, index); } @@ -183,6 +184,7 @@ class SOFA_CORE_API BaseLink virtual void _doSetOwner_(Base* owner) = 0; virtual Base* _doGet_(const size_t=0) const = 0; virtual bool _doAdd_(Base* target, const std::string&) = 0; + virtual bool _doAdd_(Base*) = 0; virtual void _doClear_() = 0; virtual std::string _doGetLinkedPath_(const size_t=0) const = 0; virtual bool _doRemove_(Base* target) = 0; diff --git a/Sofa/framework/Core/src/sofa/core/objectmodel/Link.h b/Sofa/framework/Core/src/sofa/core/objectmodel/Link.h index 5571d3cc7b0..6180eec630f 100644 --- a/Sofa/framework/Core/src/sofa/core/objectmodel/Link.h +++ b/Sofa/framework/Core/src/sofa/core/objectmodel/Link.h @@ -377,7 +377,7 @@ class TLink : public BaseLink return true; } - bool add(DestPtr v) + bool addDestPtr(DestPtr v) { if (!v) return false; @@ -387,7 +387,7 @@ class TLink : public BaseLink return true; } - bool add(DestPtr v, const std::string& path) + bool addDestPtrPath(DestPtr v, const std::string& path) { if (!v && path.empty()) return false; @@ -503,7 +503,25 @@ class TLink : public BaseLink } /// TLink:adding accepts nullptr (for a not yet resolved link). - return TLink::add(destptr, path); + return TLink::addDestPtrPath(destptr, path); + } + + bool _doAdd_(Base* baseptr) override + { + /// If the pointer is null and the path empty we do nothing + if(!baseptr) + return false; + + /// Downcast the pointer to a compatible type and + /// If the types are not compatible with the Link we returns false + auto destptr = castTo(baseptr); + if(!destptr) + { + return false; + } + + /// TLink:adding accepts nullptr (for a not yet resolved link). + return TLink::addDestPtr(destptr); } /// Returns false on type mismatch @@ -577,7 +595,7 @@ class MultiLink : public TLink& init, DestPtr val) : Inherit(init), m_validator(nullptr) { - if (val) this->add(val); + if (val) this->addDestPtr(val); } virtual ~MultiLink() @@ -654,7 +672,7 @@ class SingleLink : public TLink& init, DestPtr val) : Inherit(init), m_validator(nullptr) { - if (val) this->add(val); + if (val) this->addDestPtr(val); } virtual ~SingleLink() diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp b/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp index 11a9c821a38..23303e6ea6b 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp @@ -635,7 +635,7 @@ bool Node::doAddObject(sofa::core::objectmodel::BaseComponent::SPtr sobj, sofa:: { this->setObjectContext(sobj); if(insertionLocation == sofa::core::objectmodel::TypeOfInsertion::AtEnd) - object.add(sobj); + object.add(sobj.get()); else object.addBegin(sobj); @@ -1182,7 +1182,7 @@ void Node::doAddChild(BaseNode::SPtr node) { const Node::SPtr dagnode = sofa::core::objectmodel::SPtr_static_cast(node); setDirtyDescendancy(); - child.add(dagnode); + child.add(dagnode.get()); dagnode->l_parents.add(this); dagnode->l_parents.updateLinks(); // to fix load-time unresolved links }