From 47b3dd44ac09b455464a90a23eb526cf1bcba166 Mon Sep 17 00:00:00 2001 From: hugtalbot Date: Tue, 8 Apr 2025 09:23:56 +0200 Subject: [PATCH 1/4] [all] Update plugin registration mechanism --- src/PluginExample/MyBehaviorModel.cpp | 7 +++-- .../MyMappingPendulumInPlane.cpp | 9 +++--- .../MyProjectiveConstraintSet.cpp | 9 +++--- src/PluginExample/MyVisualModel.cpp | 7 +++-- src/PluginExample/initPluginExample.cpp | 29 +++++++++++++++++++ 5 files changed, 49 insertions(+), 12 deletions(-) diff --git a/src/PluginExample/MyBehaviorModel.cpp b/src/PluginExample/MyBehaviorModel.cpp index 904b992..d86f85d 100644 --- a/src/PluginExample/MyBehaviorModel.cpp +++ b/src/PluginExample/MyBehaviorModel.cpp @@ -52,8 +52,11 @@ void MyBehaviorModel::updatePosition(double dt) SOFA_UNUSED(dt); } -int MyBehaviorModelClass = core::RegisterObject("Dummy component with a custom widget.").add< MyBehaviorModel >(); - +void registerMyBehaviorModel(sofa::core::ObjectFactory* factory) +{ + factory->registerObjects(core::ObjectRegistrationData("Dummy component with a custom widget.") + .add< MyBehaviorModel >()); +} } // namespace sofa::component::behaviormodel diff --git a/src/PluginExample/MyMappingPendulumInPlane.cpp b/src/PluginExample/MyMappingPendulumInPlane.cpp index c482911..8c278af 100644 --- a/src/PluginExample/MyMappingPendulumInPlane.cpp +++ b/src/PluginExample/MyMappingPendulumInPlane.cpp @@ -32,11 +32,12 @@ namespace sofa::component::mapping using namespace sofa::defaulttype; - -int MyMappingPendulumInPlaneClass = core::RegisterObject("Mapping from an angle to a point in 2D") +void registerMyMappingPendulumInPlane(sofa::core::ObjectFactory* factory) +{ + factory->registerObjects(core::ObjectRegistrationData("Mapping from an angle to a point in 2D.") .add< MyMappingPendulumInPlane >() - .add< MyMappingPendulumInPlane >() -; + .add< MyMappingPendulumInPlane >()); +} template class SOFA_PLUGINEXAMPLE_API MyMappingPendulumInPlane; template class SOFA_PLUGINEXAMPLE_API MyMappingPendulumInPlane; diff --git a/src/PluginExample/MyProjectiveConstraintSet.cpp b/src/PluginExample/MyProjectiveConstraintSet.cpp index 21d7052..26bf8d4 100644 --- a/src/PluginExample/MyProjectiveConstraintSet.cpp +++ b/src/PluginExample/MyProjectiveConstraintSet.cpp @@ -29,12 +29,13 @@ namespace sofa::component::projectiveconstraintset using namespace sofa::defaulttype; - -int MyProjectiveConstraintSetClass = core::RegisterObject("just an example of templated component") +void registerMyProjectiveConstraintSet(sofa::core::ObjectFactory* factory) +{ + factory->registerObjects(core::ObjectRegistrationData("Just an example of templated component.") .add< MyProjectiveConstraintSet >() .add< MyProjectiveConstraintSet >() - .add< MyProjectiveConstraintSet >() -; + .add< MyProjectiveConstraintSet >()); +} template class SOFA_PLUGINEXAMPLE_API MyProjectiveConstraintSet; template class SOFA_PLUGINEXAMPLE_API MyProjectiveConstraintSet; diff --git a/src/PluginExample/MyVisualModel.cpp b/src/PluginExample/MyVisualModel.cpp index 7358c51..28b942c 100644 --- a/src/PluginExample/MyVisualModel.cpp +++ b/src/PluginExample/MyVisualModel.cpp @@ -44,8 +44,11 @@ void MyVisualModel::reinit() { } -int MyVisualModelClass = core::RegisterObject("Dummy visual component.").add< MyVisualModel >(); - +void registerMyVisualModel(sofa::core::ObjectFactory* factory) +{ + factory->registerObjects(core::ObjectRegistrationData("Dummy visual component.") + .add< MyVisualModel >()); +} } // namespace sofa::component::visual diff --git a/src/PluginExample/initPluginExample.cpp b/src/PluginExample/initPluginExample.cpp index 305b98c..2145ed2 100644 --- a/src/PluginExample/initPluginExample.cpp +++ b/src/PluginExample/initPluginExample.cpp @@ -23,6 +23,24 @@ #include using sofa::core::ObjectFactory; +#include + +namespace sofa::component::behaviormodel +{ + extern void registerMyBehaviorModel(sofa::core::ObjectFactory* factory); +} +namespace sofa::component::mapping +{ + extern void registerMyMappingPendulumInPlane(sofa::core::ObjectFactory* factory); +} +namespace sofa::component::projectiveconstraintset +{ + extern void registerMyProjectiveConstraintSet(sofa::core::ObjectFactory* factory); +} +namespace sofa::component::visual +{ + extern void registerMyVisualModel(sofa::core::ObjectFactory* factory); +} extern "C" { SOFA_PLUGINEXAMPLE_API void initExternalModule(); @@ -31,6 +49,7 @@ extern "C" { SOFA_PLUGINEXAMPLE_API const char* getModuleLicense(); SOFA_PLUGINEXAMPLE_API const char* getModuleDescription(); SOFA_PLUGINEXAMPLE_API const char* getModuleComponentList(); + SOFA_PLUGINEXAMPLE_API void registerObjects(sofa::core::ObjectFactory* factory); } void initExternalModule() @@ -39,6 +58,9 @@ void initExternalModule() if (first) { first = false; + + // make sure that this plugin is registered into the PluginManager + sofa::helper::system::PluginManager::getInstance().registerPlugin(sofa_tostring(SOFA_TARGET)); } } @@ -69,3 +91,10 @@ const char* getModuleComponentList() return classes.c_str(); } +void registerObjects(sofa::core::ObjectFactory* factory) +{ + sofa::component::behaviormodel::registerMyBehaviorModel(factory); + sofa::component::mapping::registerMyMappingPendulumInPlane(factory); + sofa::component::projectiveconstraintset::registerMyProjectiveConstraintSet(factory); + sofa::component::visual::registerMyVisualModel(factory); +} From 248053a4e8b4281ff83f9c12568021700ebf34b0 Mon Sep 17 00:00:00 2001 From: hugtalbot Date: Tue, 8 Apr 2025 12:53:33 +0200 Subject: [PATCH 2/4] remove getModuleComponentList --- src/PluginExample/initPluginExample.cpp | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/PluginExample/initPluginExample.cpp b/src/PluginExample/initPluginExample.cpp index 2145ed2..6babded 100644 --- a/src/PluginExample/initPluginExample.cpp +++ b/src/PluginExample/initPluginExample.cpp @@ -48,7 +48,6 @@ extern "C" { SOFA_PLUGINEXAMPLE_API const char* getModuleVersion(); SOFA_PLUGINEXAMPLE_API const char* getModuleLicense(); SOFA_PLUGINEXAMPLE_API const char* getModuleDescription(); - SOFA_PLUGINEXAMPLE_API const char* getModuleComponentList(); SOFA_PLUGINEXAMPLE_API void registerObjects(sofa::core::ObjectFactory* factory); } @@ -84,13 +83,6 @@ const char* getModuleDescription() return "Simple example of a Sofa plugin."; } -const char* getModuleComponentList() -{ - /// string containing the names of the classes provided by the plugin - static std::string classes = ObjectFactory::getInstance()->listClassesFromTarget(sofa_tostring(SOFA_TARGET)); - return classes.c_str(); -} - void registerObjects(sofa::core::ObjectFactory* factory) { sofa::component::behaviormodel::registerMyBehaviorModel(factory); @@ -98,3 +90,4 @@ void registerObjects(sofa::core::ObjectFactory* factory) sofa::component::projectiveconstraintset::registerMyProjectiveConstraintSet(factory); sofa::component::visual::registerMyVisualModel(factory); } + From 817967c5c48d076021d69d65004cbf7e4c4258fb Mon Sep 17 00:00:00 2001 From: hugtalbot Date: Mon, 14 Apr 2025 17:45:52 +0200 Subject: [PATCH 3/4] update namespaces to keep only ::pluginexample --- PluginExample_test/MyBehaviorModel_test.cpp | 5 +- src/PluginExample/MyBehaviorModel.cpp | 4 +- src/PluginExample/MyBehaviorModel.h | 4 +- src/PluginExample/MyDataWidgetUnsigned.cpp | 4 +- src/PluginExample/MyDataWidgetUnsigned.h | 4 +- .../MyMappingPendulumInPlane.cpp | 5 +- src/PluginExample/MyMappingPendulumInPlane.h | 4 +- .../MyMappingPendulumInPlane.inl | 4 +- .../MyProjectiveConstraintSet.cpp | 6 +- src/PluginExample/MyProjectiveConstraintSet.h | 2 +- .../MyProjectiveConstraintSet.inl | 4 +- src/PluginExample/MyVisualModel.cpp | 4 +- src/PluginExample/MyVisualModel.h | 4 +- src/PluginExample/config.h.in | 7 ++ src/PluginExample/initPluginExample.cpp | 92 +++++++++---------- 15 files changed, 76 insertions(+), 77 deletions(-) diff --git a/PluginExample_test/MyBehaviorModel_test.cpp b/PluginExample_test/MyBehaviorModel_test.cpp index e746b52..55e2540 100644 --- a/PluginExample_test/MyBehaviorModel_test.cpp +++ b/PluginExample_test/MyBehaviorModel_test.cpp @@ -26,16 +26,17 @@ using std::vector; #include using sofa::testing::BaseTest; - using ::testing::Types; +using namespace pluginexample; + namespace { class MyBehaviorModel_test : public BaseTest, public ::testing::WithParamInterface { public: - using MyBehaviorModel = sofa::component::behaviormodel::MyBehaviorModel; + using MyBehaviorModel = pluginexample::MyBehaviorModel; void doTearDown() override { diff --git a/src/PluginExample/MyBehaviorModel.cpp b/src/PluginExample/MyBehaviorModel.cpp index d86f85d..80d9cb0 100644 --- a/src/PluginExample/MyBehaviorModel.cpp +++ b/src/PluginExample/MyBehaviorModel.cpp @@ -24,7 +24,7 @@ #include -namespace sofa::component::behaviormodel +namespace pluginexample { MyBehaviorModel::MyBehaviorModel() @@ -58,5 +58,5 @@ void registerMyBehaviorModel(sofa::core::ObjectFactory* factory) .add< MyBehaviorModel >()); } -} // namespace sofa::component::behaviormodel +} // namespace pluginexample diff --git a/src/PluginExample/MyBehaviorModel.h b/src/PluginExample/MyBehaviorModel.h index 7c6742e..47c788d 100644 --- a/src/PluginExample/MyBehaviorModel.h +++ b/src/PluginExample/MyBehaviorModel.h @@ -26,7 +26,7 @@ #include -namespace sofa::component::behaviormodel +namespace pluginexample { /** @@ -52,5 +52,5 @@ class SOFA_PLUGINEXAMPLE_API MyBehaviorModel : public sofa::core::BehaviorModel }; -} // sofa::component::behaviormodel +} // namespace pluginexample diff --git a/src/PluginExample/MyDataWidgetUnsigned.cpp b/src/PluginExample/MyDataWidgetUnsigned.cpp index 417f9cf..83b85cf 100644 --- a/src/PluginExample/MyDataWidgetUnsigned.cpp +++ b/src/PluginExample/MyDataWidgetUnsigned.cpp @@ -24,7 +24,7 @@ #include -namespace sofa::gui::qt +namespace pluginexample { /* @@ -112,4 +112,4 @@ void MyDataWidgetUnsigned::change() } -} // namespace sofa::gui::qt +} // namespace pluginexample \ No newline at end of file diff --git a/src/PluginExample/MyDataWidgetUnsigned.h b/src/PluginExample/MyDataWidgetUnsigned.h index e14b98f..64f1a73 100644 --- a/src/PluginExample/MyDataWidgetUnsigned.h +++ b/src/PluginExample/MyDataWidgetUnsigned.h @@ -31,7 +31,7 @@ #include -namespace sofa::gui::qt +namespace pluginexample { /** @@ -65,4 +65,4 @@ protected slots: }; -} // namespace sofa::gui::qt +} // namespace pluginexample \ No newline at end of file diff --git a/src/PluginExample/MyMappingPendulumInPlane.cpp b/src/PluginExample/MyMappingPendulumInPlane.cpp index 8c278af..ec32394 100644 --- a/src/PluginExample/MyMappingPendulumInPlane.cpp +++ b/src/PluginExample/MyMappingPendulumInPlane.cpp @@ -27,7 +27,7 @@ #include -namespace sofa::component::mapping +namespace pluginexample { using namespace sofa::defaulttype; @@ -44,5 +44,4 @@ template class SOFA_PLUGINEXAMPLE_API MyMappingPendulumInPlane -namespace sofa::component::mapping +namespace pluginexample { using type::vector; @@ -84,5 +84,5 @@ class MyMappingPendulumInPlane: public core::Mapping vector gap; }; -} // namespace sofa::component::mapping +} // namespace pluginexample diff --git a/src/PluginExample/MyMappingPendulumInPlane.inl b/src/PluginExample/MyMappingPendulumInPlane.inl index 4cf1413..319e793 100644 --- a/src/PluginExample/MyMappingPendulumInPlane.inl +++ b/src/PluginExample/MyMappingPendulumInPlane.inl @@ -32,7 +32,7 @@ using std::cerr; using std::endl; -namespace sofa::component::mapping +namespace pluginexample { using helper::ReadAccessor; @@ -206,4 +206,4 @@ void MyMappingPendulumInPlane::applyDJT(const core::MechanicalParams* m } -} // namespace sofa::component::mapping +} // namespace pluginexample diff --git a/src/PluginExample/MyProjectiveConstraintSet.cpp b/src/PluginExample/MyProjectiveConstraintSet.cpp index 26bf8d4..f95c58a 100644 --- a/src/PluginExample/MyProjectiveConstraintSet.cpp +++ b/src/PluginExample/MyProjectiveConstraintSet.cpp @@ -24,9 +24,9 @@ #include -namespace sofa::component::projectiveconstraintset +namespace pluginexample { - + using namespace sofa::defaulttype; void registerMyProjectiveConstraintSet(sofa::core::ObjectFactory* factory) @@ -42,4 +42,4 @@ template class SOFA_PLUGINEXAMPLE_API MyProjectiveConstraintSet; -} // namespace sofa::component::projectiveconstraintset +} // namespace pluginexample diff --git a/src/PluginExample/MyProjectiveConstraintSet.h b/src/PluginExample/MyProjectiveConstraintSet.h index 68e015e..9f9e45c 100644 --- a/src/PluginExample/MyProjectiveConstraintSet.h +++ b/src/PluginExample/MyProjectiveConstraintSet.h @@ -28,7 +28,7 @@ #include -namespace sofa::component::projectiveconstraintset +namespace pluginexample { template diff --git a/src/PluginExample/MyProjectiveConstraintSet.inl b/src/PluginExample/MyProjectiveConstraintSet.inl index 145b815..7518796 100644 --- a/src/PluginExample/MyProjectiveConstraintSet.inl +++ b/src/PluginExample/MyProjectiveConstraintSet.inl @@ -24,7 +24,7 @@ #include -namespace sofa::component::projectiveconstraintset +namespace pluginexample { template @@ -52,4 +52,4 @@ void MyProjectiveConstraintSet::reinit() -} // namespace sofa::component::projectiveconstraintset +} // namespace pluginexample diff --git a/src/PluginExample/MyVisualModel.cpp b/src/PluginExample/MyVisualModel.cpp index 28b942c..d1aabbc 100644 --- a/src/PluginExample/MyVisualModel.cpp +++ b/src/PluginExample/MyVisualModel.cpp @@ -24,7 +24,7 @@ #include -namespace sofa::component::visual +namespace pluginexample { MyVisualModel::MyVisualModel() @@ -50,5 +50,5 @@ void registerMyVisualModel(sofa::core::ObjectFactory* factory) .add< MyVisualModel >()); } -} // namespace sofa::component::visual +} // namespace pluginexample diff --git a/src/PluginExample/MyVisualModel.h b/src/PluginExample/MyVisualModel.h index 7777a91..50bdf8e 100644 --- a/src/PluginExample/MyVisualModel.h +++ b/src/PluginExample/MyVisualModel.h @@ -27,7 +27,7 @@ #include -namespace sofa::component::visual +namespace pluginexample { /** @@ -53,5 +53,5 @@ class SOFA_PLUGINEXAMPLE_API MyVisualModel : public sofa::component::visual::Vis }; -} // sofa::component::visual +} // namespace pluginexample diff --git a/src/PluginExample/config.h.in b/src/PluginExample/config.h.in index f43f092..6f71ff4 100644 --- a/src/PluginExample/config.h.in +++ b/src/PluginExample/config.h.in @@ -23,6 +23,8 @@ #include +using namespace sofa; + #define PLUGINEXAMPLE_VERSION @PROJECT_VERSION@ #ifdef SOFA_BUILD_PLUGINEXAMPLE @@ -32,3 +34,8 @@ # define SOFA_PLUGINEXAMPLE_API SOFA_IMPORT_DYNAMIC_LIBRARY #endif +namespace pluginexample +{ + constexpr const char* MODULE_NAME = "@PROJECT_NAME@"; + constexpr const char* MODULE_VERSION = "@PROJECT_VERSION@"; +} // namespace pluginexample \ No newline at end of file diff --git a/src/PluginExample/initPluginExample.cpp b/src/PluginExample/initPluginExample.cpp index 6babded..fb89964 100644 --- a/src/PluginExample/initPluginExample.cpp +++ b/src/PluginExample/initPluginExample.cpp @@ -25,69 +25,61 @@ using sofa::core::ObjectFactory; #include -namespace sofa::component::behaviormodel +namespace pluginexample { extern void registerMyBehaviorModel(sofa::core::ObjectFactory* factory); -} -namespace sofa::component::mapping -{ extern void registerMyMappingPendulumInPlane(sofa::core::ObjectFactory* factory); -} -namespace sofa::component::projectiveconstraintset -{ extern void registerMyProjectiveConstraintSet(sofa::core::ObjectFactory* factory); -} -namespace sofa::component::visual -{ extern void registerMyVisualModel(sofa::core::ObjectFactory* factory); -} -extern "C" { - SOFA_PLUGINEXAMPLE_API void initExternalModule(); - SOFA_PLUGINEXAMPLE_API const char* getModuleName(); - SOFA_PLUGINEXAMPLE_API const char* getModuleVersion(); - SOFA_PLUGINEXAMPLE_API const char* getModuleLicense(); - SOFA_PLUGINEXAMPLE_API const char* getModuleDescription(); - SOFA_PLUGINEXAMPLE_API void registerObjects(sofa::core::ObjectFactory* factory); -} -void initExternalModule() -{ - static bool first = true; - if (first) + extern "C" { + SOFA_PLUGINEXAMPLE_API void initExternalModule(); + SOFA_PLUGINEXAMPLE_API const char* getModuleName(); + SOFA_PLUGINEXAMPLE_API const char* getModuleVersion(); + SOFA_PLUGINEXAMPLE_API const char* getModuleLicense(); + SOFA_PLUGINEXAMPLE_API const char* getModuleDescription(); + SOFA_PLUGINEXAMPLE_API void registerObjects(sofa::core::ObjectFactory* factory); + } + + void initExternalModule() { - first = false; + static bool first = true; + if (first) + { + first = false; - // make sure that this plugin is registered into the PluginManager - sofa::helper::system::PluginManager::getInstance().registerPlugin(sofa_tostring(SOFA_TARGET)); + // make sure that this plugin is registered into the PluginManager + sofa::helper::system::PluginManager::getInstance().registerPlugin(sofa_tostring(SOFA_TARGET)); + } } -} -const char* getModuleName() -{ - return sofa_tostring(SOFA_TARGET); -} + const char* getModuleName() + { + return sofa_tostring(SOFA_TARGET); + } -const char* getModuleVersion() -{ - return sofa_tostring(PLUGINEXAMPLE_VERSION); -} + const char* getModuleVersion() + { + return sofa_tostring(PLUGINEXAMPLE_VERSION); + } -const char* getModuleLicense() -{ - return "LGPL"; -} + const char* getModuleLicense() + { + return "LGPL"; + } -const char* getModuleDescription() -{ - return "Simple example of a Sofa plugin."; -} + const char* getModuleDescription() + { + return "Simple example of a Sofa plugin."; + } -void registerObjects(sofa::core::ObjectFactory* factory) -{ - sofa::component::behaviormodel::registerMyBehaviorModel(factory); - sofa::component::mapping::registerMyMappingPendulumInPlane(factory); - sofa::component::projectiveconstraintset::registerMyProjectiveConstraintSet(factory); - sofa::component::visual::registerMyVisualModel(factory); -} + void registerObjects(sofa::core::ObjectFactory* factory) + { + registerMyBehaviorModel(factory); + registerMyMappingPendulumInPlane(factory); + registerMyProjectiveConstraintSet(factory); + registerMyVisualModel(factory); + } +} \ No newline at end of file From 3a3b3bdcf9fb82e6d1d710e82a6600c5613ffb6f Mon Sep 17 00:00:00 2001 From: hugtalbot Date: Mon, 14 Apr 2025 17:49:01 +0200 Subject: [PATCH 4/4] add comment in config.h.in and use MODULE_NAME/VERSION --- src/PluginExample/config.h.in | 1 + src/PluginExample/initPluginExample.cpp | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/PluginExample/config.h.in b/src/PluginExample/config.h.in index 6f71ff4..cfe0a78 100644 --- a/src/PluginExample/config.h.in +++ b/src/PluginExample/config.h.in @@ -23,6 +23,7 @@ #include +// adding SOFA namespace to ease using SOFA classes within the plugin using namespace sofa; #define PLUGINEXAMPLE_VERSION @PROJECT_VERSION@ diff --git a/src/PluginExample/initPluginExample.cpp b/src/PluginExample/initPluginExample.cpp index fb89964..ad32107 100644 --- a/src/PluginExample/initPluginExample.cpp +++ b/src/PluginExample/initPluginExample.cpp @@ -50,18 +50,18 @@ namespace pluginexample first = false; // make sure that this plugin is registered into the PluginManager - sofa::helper::system::PluginManager::getInstance().registerPlugin(sofa_tostring(SOFA_TARGET)); + sofa::helper::system::PluginManager::getInstance().registerPlugin(MODULE_NAME); } } const char* getModuleName() { - return sofa_tostring(SOFA_TARGET); + return MODULE_NAME; } const char* getModuleVersion() { - return sofa_tostring(PLUGINEXAMPLE_VERSION); + return MODULE_VERSION; } const char* getModuleLicense()