Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[All] Refactor create/cancreate in ObjectFactory #5195

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class SOFA_COMPONENT_COLLISION_DETECTION_ALGORITHM_API BruteForceDetection final
template<class T>
static typename T::SPtr create(T*, sofa::core::objectmodel::BaseContext* context, sofa::core::objectmodel::BaseObjectDescription* arg)
{
// TODO(dmarchal, 08/01/2025): Update the create function to the new factory creation process.
// this one is funny as it behave like a Prefab, thus creating and adding other component in the graph.
if (context)
{
if (const BruteForceBroadPhase::SPtr broadPhase = sofa::core::objectmodel::New<BruteForceBroadPhase>())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class SOFA_COMPONENT_COLLISION_DETECTION_ALGORITHM_API DirectSAP final : public
template<class T>
static typename T::SPtr create(T*, sofa::core::objectmodel::BaseContext* context, sofa::core::objectmodel::BaseObjectDescription* arg)
{
// TODO(dmarchal, 08/01/2025): Update the create function to the new factory creation process.
// this one is funny as it behave like a Prefab, thus creating and adding other component in the graph.
if (context)
{
if (const BruteForceBroadPhase::SPtr broadPhase = sofa::core::objectmodel::New<BruteForceBroadPhase>())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class SOFA_COMPONENT_COLLISION_DETECTION_ALGORITHM_API RayTraceDetection final :
template<class T>
static typename T::SPtr create(T*, sofa::core::objectmodel::BaseContext* context, sofa::core::objectmodel::BaseObjectDescription* arg)
{
// TODO(dmarchal, 08/01/2025): Update the create function to the new factory creation process.
// this one is funny as it behave like a Prefab, thus creating and adding other component in the graph.
const BruteForceBroadPhase::SPtr broadPhase = sofa::core::objectmodel::New<BruteForceBroadPhase>();
broadPhase->setName("bruteForceBroadPhase");
if (context) context->addObject(broadPhase);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public :
"' found in the context node.");
return false;
}
return BaseObject::canCreate(obj, context, arg);
return true;
}

sofa::core::topology::BaseMeshTopology* getCollisionTopology() override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class PointCollisionModel : public core::CollisionModel
"' found in the context node.");
return false;
}
return BaseObject::canCreate(obj, context, arg);
return true;
}

void computeBBox(const core::ExecParams* params, bool onlyVisible) override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,16 @@ class SphereCollisionModel : public core::CollisionModel
return false;
}

return BaseObject::canCreate(obj, context, arg);
return true;
}

template<class T>
static typename T::SPtr create(T*, core::objectmodel::BaseContext* context, core::objectmodel::BaseObjectDescription* arg)
{
typename T::SPtr obj;

// TODO(dmarchal, 08/01/2025): Update the create function to the new factory creation process.
// duplicated code, path and context searching for states (probably to move into init)
if( context)
{
auto* _mstate = dynamic_cast<core::behavior::MechanicalState<TDataTypes>*>(context->getMechanicalState());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class TriangleCollisionModel : public core::CollisionModel
"' found in the context node.");
return false;
}
return BaseObject::canCreate(obj, context, arg);
return true;
}

void computeBBox(const core::ExecParams* params, bool onlyVisible=false) override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public :
template<class T>
static typename T::SPtr create(T*, core::objectmodel::BaseContext* context, core::objectmodel::BaseObjectDescription* arg)
{
// TODO(dmarchal, 08/01/2025): Update the create function to the new factory creation process.
// not sure how to handle this one...
typename T::SPtr obj = sofa::core::objectmodel::New<T>();
if (context)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,15 @@ class SOFA_COMPONENT_COLLISION_RESPONSE_CONTACT_API ContactListener : public vir
return false;
}

return BaseObject::canCreate(obj, context, arg);
return true;
}

template<class T>
static typename T::SPtr create(T* , core::objectmodel::BaseContext* context, core::objectmodel::BaseObjectDescription* arg)
{
// TODO(dmarchal, 08/01/2025): Update the create function to the new factory creation process.
// it seems there is some creative attribute hacking in this one. See if the behavior can be reimplemented
// using a mix of parse/init
core::CollisionModel* collModel1 = nullptr;
core::CollisionModel* collModel2 = nullptr;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class Distances : public core::DataEngine
arg->logError("Data attribute 'targetPath' does not point to a mechanical state of data type '" + std::string(DataTypes::Name()) +"'.");
error = true;
}
return !error && core::DataEngine::canCreate(obj, context, arg);
return !error;
}
/// Construction method called by ObjectFactory.
///
Expand All @@ -200,6 +200,9 @@ class Distances : public core::DataEngine
template<class T>
static typename T::SPtr create(T*, core::objectmodel::BaseContext* context, core::objectmodel::BaseObjectDescription* arg )
{
// TODO(dmarchal, 08/01/2025): Update the create function to the new factory creation process.
// probably remove the constructor with arguments and replace that with SinglLink to delegate at
// init/parse the initiatilization.
typename T::SPtr obj = sofa::core::objectmodel::New<T>(
( arg?dynamic_cast<sofa::component::topology::container::dynamic::DynamicSparseGridTopologyContainer*> ( arg->findObject ( arg->getAttribute ( "hexaContainerPath","../.." ) ) ) :nullptr ),
( arg?dynamic_cast<core::behavior::MechanicalState<DataTypes>*> ( arg->findObject ( arg->getAttribute ( "targetPath",".." ) ) ) :nullptr ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class PairBoxROI : public core::DataEngine
}
}

return BaseObject::canCreate(obj, context, arg);
return true;
}

protected:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class PlaneROI : public core::DataEngine
}
}

return BaseObject::canCreate(obj, context, arg);
return true;
}

protected:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class ProximityROI : public core::DataEngine
}
}

return BaseObject::canCreate(obj, context, arg);
return true;
}

public:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class SubsetTopology : public core::DataEngine
}
}

return BaseObject::canCreate(obj, context, arg);
return true;
}

protected:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class ValuesFromPositions : public core::DataEngine
}
}

return BaseObject::canCreate(obj, context, arg);
return true;
}

protected:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class LCPForceFeedback : public MechanicalStateForceFeedback<TDataTypes>
return false;
}

return core::objectmodel::BaseObject::canCreate(obj, context, arg);
return true;
}

/// Override method to lock or unlock the force feedback computation. According to parameter, value == true (resp. false) will lock (resp. unlock) mutex @sa lockForce
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class SOFA_COMPONENT_IO_MESH_API BlenderExporter: public core::objectmodel::Base
"' found in the context node.");
return false;
}
return BaseObject::canCreate(obj, context, arg);
return true;
}

protected:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class PrecomputedWarpPreconditioner : public sofa::component::linearsolver::Matr
arg->logError(std::string("No mechanical state with the datatype '") + TDataTypes::Name() + "' found in the context node.");
return false;
}
return sofa::core::objectmodel::BaseObject::canCreate(obj, context, arg);
return true;
}

protected :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class SOFA_COMPONENT_PLAYBACK_API CompareState: public ReadState
arg->logError("No mechanical state found in the context node.");
return false;
}
return BaseObject::canCreate(obj, context, arg);
return true;
}

/// Return the total errors (position and velocity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class SOFA_COMPONENT_PLAYBACK_API CompareTopology: public ReadTopology
arg->logError("Cannot find a mesh topology in the current context");
return false;
}
return BaseObject::canCreate(obj, context, arg);
return true;
}

/// Return the total number of errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class SOFA_COMPONENT_PLAYBACK_API ReadState: public core::objectmodel::BaseObjec
arg->logError("No mechanical state found in the context node.");
return false;
}
return BaseObject::canCreate(obj, context, arg);
return true;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class SOFA_COMPONENT_PLAYBACK_API ReadTopology: public core::objectmodel::BaseOb
return false;
}

return BaseObject::canCreate(obj, context, arg);
return true;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class SOFA_COMPONENT_PLAYBACK_API WriteState: public core::objectmodel::BaseObje
arg->logError("No mechanical state found in the context node.");
return false;
}
return BaseObject::canCreate(obj, context, arg);
return true;
}

};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class SOFA_COMPONENT_PLAYBACK_API WriteTopology: public core::objectmodel::BaseO
return false;
}

return BaseObject::canCreate(obj, context, arg);
return true;
}

};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class PointSetGeometryAlgorithms : public core::topology::GeometryAlgorithms
"' found in the context node.");
return false;
}
return BaseObject::canCreate(obj, context, arg);
return true;
}

/** \brief Called by the state change callback to initialize added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class SOFA_COMPONENT_TOPOLOGY_UTILITY_API TopologicalChangeProcessor: public cor
return false;
}

return BaseObject::canCreate(obj, context, arg);
return true;
}

void draw(const core::visual::VisualParams* vparams) override;
Expand Down
5 changes: 4 additions & 1 deletion Sofa/framework/Core/src/sofa/core/Mapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ class Mapping : public BaseMapping
return false;
}

return BaseMapping::canCreate(obj, context, arg);
return true;
}

/// Construction method called by ObjectFactory.
Expand All @@ -239,6 +239,9 @@ class Mapping : public BaseMapping
template<class T>
static typename T::SPtr create(T*, core::objectmodel::BaseContext* context, core::objectmodel::BaseObjectDescription* arg)
{
// TODO(dmarchal, 08/01/2025): Update the create function to the new factory creation process.
// it seems there is some creative attribute hacking in this one. See if we can implement similar
// behavior using parse/init()
typename T::SPtr obj = sofa::core::objectmodel::New<T>();

if (context)
Expand Down
4 changes: 3 additions & 1 deletion Sofa/framework/Core/src/sofa/core/Multi2Mapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ class Multi2Mapping : public BaseMapping
error = true;
}

return !error && BaseMapping::canCreate(obj, context, arg);
return !error;
}
/// Construction method called by ObjectFactory.
///
Expand All @@ -236,6 +236,8 @@ class Multi2Mapping : public BaseMapping
template<class T>
static typename T::SPtr create(T*, core::objectmodel::BaseContext* context, core::objectmodel::BaseObjectDescription* arg)
{
// TODO(dmarchal, 08/01/2025): Update the create function to the new factory creation process.
// trivial remplacement (remarkably inconstitant behavior with the one implemented in Mapping::create)
typename T::SPtr obj = sofa::core::objectmodel::New<T>();

if (context)
Expand Down
4 changes: 3 additions & 1 deletion Sofa/framework/Core/src/sofa/core/MultiMapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ class MultiMapping : public BaseMapping
return false;
}

return BaseMapping::canCreate(obj, context, arg);
return true;
}

/// Construction method called by ObjectFactory.
Expand All @@ -238,6 +238,8 @@ class MultiMapping : public BaseMapping
template<class T>
static typename T::SPtr create(T*, core::objectmodel::BaseContext* context, core::objectmodel::BaseObjectDescription* arg)
{
// TODO(dmarchal, 08/01/2025): Update the create function to the new factory creation process.
// trivial remplacement (remarkably inconstitant behavior with the one implemented in Mapping::create)
typename T::SPtr obj = sofa::core::objectmodel::New<T>();

if (context)
Expand Down
14 changes: 14 additions & 0 deletions Sofa/framework/Core/src/sofa/core/ObjectFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,20 @@ void findTemplatedCreator(
}
}

void ObjectFactory::postObjectCreation(sptr<objectmodel::BaseObject> obj, objectmodel::BaseContext* context, objectmodel::BaseObjectDescription* arg)
{
if (context)
{
context->addObject(obj);
}

if (arg)
{
obj->parse(arg);
}
}


objectmodel::BaseObject::SPtr ObjectFactory::createObject(objectmodel::BaseContext* context, objectmodel::BaseObjectDescription* arg)
{
objectmodel::BaseObject::SPtr object = nullptr;
Expand Down
Loading
Loading