diff --git a/Sofa/framework/Core/src/sofa/core/Mapping.h b/Sofa/framework/Core/src/sofa/core/Mapping.h index daf5a0b3606..2304fae86f6 100644 --- a/Sofa/framework/Core/src/sofa/core/Mapping.h +++ b/Sofa/framework/Core/src/sofa/core/Mapping.h @@ -232,40 +232,6 @@ class Mapping : public BaseMapping return BaseMapping::canCreate(obj, context, arg); } - /// Construction method called by ObjectFactory. - /// - /// This implementation read the input and output attributes to - /// find the input and output models of this mapping. - template - static typename T::SPtr create(T*, core::objectmodel::BaseContext* context, core::objectmodel::BaseObjectDescription* arg) - { - typename T::SPtr obj = sofa::core::objectmodel::New(); - - if (context) - context->addObject(obj); - - if (arg) - { - std::string inPath, outPath; - if (arg->getAttribute("input")) - inPath = arg->getAttribute("input"); - else - inPath = "@../"; - - if (arg->getAttribute("output")) - outPath = arg->getAttribute("output"); - else - outPath = "@./"; - - obj->fromModel.setPath( inPath ); - obj->toModel.setPath( outPath ); - - obj->parse(arg); - } - - return obj; - } - template static std::string shortName(const T* ptr = nullptr, objectmodel::BaseObjectDescription* arg = nullptr) { diff --git a/Sofa/framework/Core/src/sofa/core/Mapping.inl b/Sofa/framework/Core/src/sofa/core/Mapping.inl index 48624dc065b..abc62d014d7 100644 --- a/Sofa/framework/Core/src/sofa/core/Mapping.inl +++ b/Sofa/framework/Core/src/sofa/core/Mapping.inl @@ -100,6 +100,40 @@ void Mapping::init() { Inherit1::init(); + const auto setLink = [this](LinkType& link, const char* attributeName, const std::string& defaultPath) + { + if (!link) + { + typename LinkType::DestType* state = nullptr; + this->getContext()->findLinkDest(state, defaultPath, nullptr); + + if (state) + { + const std::string linkPath = "@" + state->getPathName(); + msg_warning() + << "The attribute '" << attributeName + << "' of this component has not been defined. It is automatically set to '" + << linkPath << "'. It is recommended to define it to avoid any error."; + link.setPath(linkPath); + } + else + { + msg_error() + << "The attribute '" << attributeName + << "' of this component has not been defined, and could not be found automatically."; + this->d_componentState.setValue(sofa::core::objectmodel::ComponentState::Invalid); + } + } + }; + + setLink(this->fromModel, "input", "@../"); + setLink(this->toModel, "output", "@./"); + + if (this->d_componentState.getValue() == sofa::core::objectmodel::ComponentState::Invalid) + { + return; + } + if(toModel && !testMechanicalState(toModel.get())) { setNonMechanical(); diff --git a/Sofa/framework/Core/src/sofa/core/objectmodel/BaseLink.cpp b/Sofa/framework/Core/src/sofa/core/objectmodel/BaseLink.cpp index f9697caf6ee..d4ecd3c3082 100644 --- a/Sofa/framework/Core/src/sofa/core/objectmodel/BaseLink.cpp +++ b/Sofa/framework/Core/src/sofa/core/objectmodel/BaseLink.cpp @@ -348,7 +348,6 @@ bool BaseLink::updateLinks() Base* ptr; std::string path = getLinkedPath(i); /// Search for path and if any returns the pointer to the proper object. - /// Search for path and if any returns the pointer to the proper object. if(!getLinkedBase() && !path.empty()) { ptr = PathResolver::FindBaseFromClassAndPath(getOwner(), getDestClass(), path); diff --git a/Sofa/framework/Core/src/sofa/core/objectmodel/BaseNode.cpp b/Sofa/framework/Core/src/sofa/core/objectmodel/BaseNode.cpp index 91d4001ec10..9ca34c6cd43 100644 --- a/Sofa/framework/Core/src/sofa/core/objectmodel/BaseNode.cpp +++ b/Sofa/framework/Core/src/sofa/core/objectmodel/BaseNode.cpp @@ -94,7 +94,9 @@ std::string BaseNode::internalGetPathName() const { // no smarter choice without breaking the "Node" heritage str = parents[0]->internalGetPathName(); str += '/'; - str += getName(); + const auto& myName = getName(); + assert(!myName.empty()); + str += myName; } return str; } diff --git a/Sofa/framework/Simulation/Common/src/sofa/simulation/common/xml/NodeElement.cpp b/Sofa/framework/Simulation/Common/src/sofa/simulation/common/xml/NodeElement.cpp index 1e551d5c5eb..5b9babb75fc 100644 --- a/Sofa/framework/Simulation/Common/src/sofa/simulation/common/xml/NodeElement.cpp +++ b/Sofa/framework/Simulation/Common/src/sofa/simulation/common/xml/NodeElement.cpp @@ -33,6 +33,12 @@ using namespace sofa::defaulttype; NodeElement::NodeElement(const std::string& name, const std::string& type, BaseElement* parent) : Element(name, type, parent) { + if (name.empty()) + { + const std::string nodeName = "node"; + msg_warning("XML") << "Node name cannot be empty. An arbitrary name '" << nodeName << "' has been provided"; + attributes["name"] = nodeName; + } } NodeElement::~NodeElement() diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp b/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp index f4f2ce91e80..7000a258b22 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp @@ -73,7 +73,7 @@ namespace sofa::simulation using core::objectmodel::BaseNode; using core::objectmodel::BaseObject; -Node::Node(const std::string& name) +Node::Node() : core::objectmodel::BaseNode() , sofa::core::objectmodel::Context() , child(initLink("child", "Child nodes")) @@ -113,15 +113,21 @@ Node::Node(const std::string& name) , initialized(false) { _context = this; - setName(name); f_printLog.setValue(DEBUG_LINK); } - -Node::~Node() +Node::Node(std::string nodeName) : Node() { + if (nodeName.empty()) + { + nodeName = "node"; + msg_warning(GetClass()->className) << "Node name cannot be empty. An arbitrary name '" << nodeName << "' has been provided"; + } + setName(nodeName); } +Node::~Node() = default; + void Node::parse( sofa::core::objectmodel::BaseObjectDescription* arg ) { Inherit1::parse( arg ); diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.h b/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.h index 2318c243d9f..934ac18cc3d 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.h +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.h @@ -152,7 +152,8 @@ class SOFA_SIMULATION_CORE_API Node : public sofa::core::objectmodel::BaseNode, typedef sofa::core::visual::DisplayFlags DisplayFlags; protected: - Node(const std::string& name=""); + Node(); + explicit Node(std::string nodeName); virtual ~Node() override; public: