From a6632a14e2161474eec59ebba5759bc80fb5702b Mon Sep 17 00:00:00 2001 From: Lucas Burel Date: Mon, 22 Dec 2025 16:39:09 +0100 Subject: [PATCH] cleaning add functions --- .../sofa/core/objectmodel/BaseComponent.cpp | 2 +- .../Core/src/sofa/core/objectmodel/BaseLink.h | 2 + .../Core/src/sofa/core/objectmodel/Link.h | 52 ++++++++++--------- .../Core/src/sofa/simulation/Node.cpp | 4 +- 4 files changed, 33 insertions(+), 27 deletions(-) diff --git a/Sofa/framework/Core/src/sofa/core/objectmodel/BaseComponent.cpp b/Sofa/framework/Core/src/sofa/core/objectmodel/BaseComponent.cpp index 827aa9659bb..fb2b16f2d8f 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); - 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 d5c71fc6d57..cad9af993e2 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); } @@ -181,6 +182,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; diff --git a/Sofa/framework/Core/src/sofa/core/objectmodel/Link.h b/Sofa/framework/Core/src/sofa/core/objectmodel/Link.h index b1519d4bc89..e5821c8e254 100644 --- a/Sofa/framework/Core/src/sofa/core/objectmodel/Link.h +++ b/Sofa/framework/Core/src/sofa/core/objectmodel/Link.h @@ -377,27 +377,6 @@ class TLink : public BaseLink return true; } - bool add(DestPtr v) - { - if (!v) - return false; - const std::size_t index = TraitsContainer::add(m_value,v); - updateCounter(); - added(v, index); - return true; - } - - bool add(DestPtr v, const std::string& path) - { - if (!v && path.empty()) - return false; - std::size_t index = TraitsContainer::add(m_value,v); - TraitsValueType::setPath(m_value[index],path); - updateCounter(); - added(v, index); - return true; - } - bool addPath(const std::string& path) { if (path.empty()) @@ -503,7 +482,32 @@ class TLink : public BaseLink } /// TLink:adding accepts nullptr (for a not yet resolved link). - return TLink::add(destptr, path); + std::size_t index = TraitsContainer::add(m_value, destptr); + TraitsValueType::setPath(m_value[index], path); + updateCounter(); + added(destptr, index); + return true; + } + + 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). + const std::size_t index = TraitsContainer::add(m_value, destptr); + updateCounter(); + added(destptr, index); + return true;; } /// Returns false on type mismatch @@ -577,7 +581,7 @@ class MultiLink : public TLink& init, DestPtr val) : Inherit(init), m_validator(nullptr) { - if (val) this->add(val); + if (val) this->_doAdd_(sofa::core::castToBase(TraitsDestPtr::get(val))); } virtual ~MultiLink() @@ -654,7 +658,7 @@ class SingleLink : public TLink& init, DestPtr val) : Inherit(init), m_validator(nullptr) { - if (val) this->add(val); + if (val) this->_doAdd_(sofa::core::castToBase(TraitsDestPtr::get(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 bcc0dd329fa..97bc1e887c6 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp @@ -628,7 +628,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); @@ -1175,7 +1175,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 }