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
35 changes: 35 additions & 0 deletions Sofa/framework/Core/simutest/objectmodel/BaseLink_simutest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#include <boost/unordered/detail/fca.hpp>
#include <sofa/core/objectmodel/Base.h>
using sofa::core::objectmodel::Base ;
using sofa::core::objectmodel::ComponentState;
Expand All @@ -31,6 +32,7 @@ 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 @@ -56,6 +58,7 @@ class BaseLink_test : public BaseSimulationTest,
scene << "<?xml version='1.0'?>"
"<Node name='Root' gravity='0 -9.81 0' time='0' animate='0' > \n"
" <RequiredPlugin pluginName='Sofa.Component.SceneUtility' /> \n"
" <RequiredPlugin pluginName='Sofa.Component.StateContainer' /> \n"
" <DefaultAnimationLoop /> \n"
" <DefaultVisualManagerLoop /> \n"
" <MechanicalObject name='mstate0'/> \n"
Expand All @@ -79,6 +82,38 @@ class BaseLink_test : public BaseSimulationTest,
}
};

class FakeComponent : public BaseComponent {
public:
SOFA_CLASS(FakeComponent, BaseComponent);

sofa::MultiLink<FakeComponent, sofa::core::objectmodel::BaseComponent, sofa::BaseLink::FLAG_DOUBLELINK> l_target;

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

TEST_F(BaseLink_test, remove)
{
FakeComponent owner;
FakeComponent target1;
FakeComponent target2;

ASSERT_FALSE(owner.l_target.remove(&target1));

ASSERT_TRUE(owner.l_target.add(&target1, ""));
ASSERT_TRUE(owner.l_target.add(&target2, ""));
ASSERT_EQ(owner.l_target.size(), size_t(2));
ASSERT_EQ(owner.l_target.get(0), &target1);
ASSERT_EQ(owner.l_target.get(1), &target2);

ASSERT_TRUE(owner.l_target.remove(&target1));
ASSERT_EQ(owner.l_target.size(), size_t(1));
ASSERT_EQ(owner.l_target.get(0), &target2);

ASSERT_FALSE(owner.l_target.remove(&target1));
ASSERT_FALSE(owner.l_target.remove(nullptr));
}

//////////////////////// Testing valid path //////////////////////////////////////
class MultiLink_simutest : public BaseLink_test {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ void BaseComponent::addSlave(BaseComponent::SPtr s)
const BaseComponent::SPtr previous = s->getMaster();
if (previous == this) return;
if (previous)
previous->l_slaves.remove(s);
previous->l_slaves.remove(s.get());
l_slaves.add(s);
if (previous)
this->getContext()->notifyMoveSlave(previous.get(), this, s.get());
Expand All @@ -234,7 +234,7 @@ void BaseComponent::addSlave(BaseComponent::SPtr s)

void BaseComponent::removeSlave(BaseComponent::SPtr s)
{
if (l_slaves.remove(s))
if (l_slaves.remove(s.get()))
{
this->getContext()->notifyRemoveSlave(this, s.get());
}
Expand Down
3 changes: 3 additions & 0 deletions Sofa/framework/Core/src/sofa/core/objectmodel/BaseLink.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ class SOFA_CORE_API BaseLink
/// Change the link's target at the provided index.
bool set(Base* baseptr, size_t index=0) { return _doSet_(baseptr, index); }

bool remove(Base* baseptr) {return _doRemove_(baseptr); }

protected:
virtual bool _doSet_(Base* target, const size_t index=0) = 0;
virtual Base* _doGetOwner_() const = 0 ;
Expand All @@ -183,6 +185,7 @@ class SOFA_CORE_API BaseLink
virtual bool _doAdd_(Base* target, const std::string&) = 0;
virtual void _doClear_() = 0;
virtual std::string _doGetLinkedPath_(const size_t=0) const = 0;
virtual bool _doRemove_(Base* target) = 0;

unsigned int m_flags;
std::string m_name;
Expand Down
6 changes: 3 additions & 3 deletions Sofa/framework/Core/src/sofa/core/objectmodel/Link.h
Original file line number Diff line number Diff line change
Expand Up @@ -408,11 +408,11 @@ class TLink : public BaseLink
return add(ptr, path);
}

bool remove(DestPtr v)
bool _doRemove_(Base* target) override
{
if (!v)
if (!target)
return false;
return removeAt(TraitsContainer::find(m_value,v));
return removeAt(TraitsContainer::find(m_value,castTo<DestType*>(target)));
}

bool removeAt(std::size_t index)
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 @@ -654,7 +654,7 @@ bool Node::doRemoveObject(sofa::core::objectmodel::BaseComponent::SPtr sobj)
dmsg_warning_when(sobj == nullptr) << "Trying to remove a nullptr object";

this->clearObjectContext(sobj);
object.remove(sobj);
object.remove(sobj.get());
sofa::core::objectmodel::BaseComponent* obj = sobj.get();

if(obj != nullptr && !obj->removeInNode( this ) )
Expand Down Expand Up @@ -1192,7 +1192,7 @@ void Node::doRemoveChild(BaseNode::SPtr node)
{
const Node::SPtr dagnode = sofa::core::objectmodel::SPtr_static_cast<Node>(node);
setDirtyDescendancy();
child.remove(dagnode);
child.remove(dagnode.get());
dagnode->l_parents.remove(this);
}

Expand Down