Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion Sofa/framework/Core/simutest/objectmodel/BaseLink_simutest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ using sofa::testing::BaseSimulationTest ;
using sofa::simulation::Node ;

#include <sofa/core/objectmodel/BaseComponent.h>
using sofa::core::objectmodel::BaseObject;
using sofa::core::objectmodel::BaseComponent;

#include <sofa/core/PathResolver.h>
using sofa::core::PathResolver;
Expand All @@ -41,6 +41,8 @@ using sofa::defaulttype::Rigid3Types;
#include <sofa/defaulttype/VecTypes.h>
using sofa::defaulttype::Vec3Types;

using sofa::core::objectmodel::BaseLink;

namespace
{

Expand Down Expand Up @@ -79,6 +81,41 @@ class BaseLink_test : public BaseSimulationTest,
}
};

class FakeComponent : public BaseComponent
{
public:
SOFA_CLASS(FakeComponent, BaseComponent);
sofa::MultiLink<FakeComponent, BaseComponent, BaseLink::FLAG_DOUBLELINK> l_target;

FakeComponent()
: l_target(initLink("target","link for test"))
{}

};

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 {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions Sofa/framework/Core/src/sofa/core/objectmodel/BaseLink.h
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
Expand All @@ -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;

Expand Down
52 changes: 28 additions & 24 deletions Sofa/framework/Core/src/sofa/core/objectmodel/Link.h
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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<DestType*>(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
Expand Down Expand Up @@ -577,7 +581,7 @@ class MultiLink : public TLink<TOwnerType,TDestType,TFlags|BaseLink::FLAG_MULTIL
MultiLink(const BaseLink::InitLink<OwnerType>& init, DestPtr val)
: Inherit(init), m_validator(nullptr)
{
if (val) this->add(val);
if (val) this->_doAdd_(sofa::core::castToBase(TraitsDestPtr::get(val)));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a bit confused about this change. Could you explain?

}

virtual ~MultiLink()
Expand Down Expand Up @@ -654,7 +658,7 @@ class SingleLink : public TLink<TOwnerType,TDestType,TFlags&~BaseLink::FLAG_MULT
SingleLink(const BaseLink::InitLink<OwnerType>& 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()
Expand Down
4 changes: 2 additions & 2 deletions Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -1175,7 +1175,7 @@ void Node::doAddChild(BaseNode::SPtr node)
{
const Node::SPtr dagnode = sofa::core::objectmodel::SPtr_static_cast<Node>(node);
setDirtyDescendancy();
child.add(dagnode);
child.add(dagnode.get());
dagnode->l_parents.add(this);
dagnode->l_parents.updateLinks(); // to fix load-time unresolved links
}
Expand Down
Loading