Skip to content

Commit cb6acf8

Browse files
authored
Apply new factory registration mechanism (#14)
* [all] Update plugin registration mechanism * remove getModuleComponentList * update namespaces to keep only ::pluginexample * add comment in config.h.in and use MODULE_NAME/VERSION
1 parent bed3da1 commit cb6acf8

15 files changed

+105
-75
lines changed

PluginExample_test/MyBehaviorModel_test.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,17 @@ using std::vector;
2626

2727
#include <sofa/testing/BaseTest.h>
2828
using sofa::testing::BaseTest;
29-
3029
using ::testing::Types;
3130

31+
using namespace pluginexample;
32+
3233
namespace {
3334

3435
class MyBehaviorModel_test : public BaseTest,
3536
public ::testing::WithParamInterface<unsigned>
3637
{
3738
public:
38-
using MyBehaviorModel = sofa::component::behaviormodel::MyBehaviorModel;
39+
using MyBehaviorModel = pluginexample::MyBehaviorModel;
3940

4041
void doTearDown() override
4142
{

src/PluginExample/MyBehaviorModel.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include <sofa/core/ObjectFactory.h>
2525

2626

27-
namespace sofa::component::behaviormodel
27+
namespace pluginexample
2828
{
2929

3030
MyBehaviorModel::MyBehaviorModel()
@@ -52,8 +52,11 @@ void MyBehaviorModel::updatePosition(double dt)
5252
SOFA_UNUSED(dt);
5353
}
5454

55-
int MyBehaviorModelClass = core::RegisterObject("Dummy component with a custom widget.").add< MyBehaviorModel >();
56-
55+
void registerMyBehaviorModel(sofa::core::ObjectFactory* factory)
56+
{
57+
factory->registerObjects(core::ObjectRegistrationData("Dummy component with a custom widget.")
58+
.add< MyBehaviorModel >());
59+
}
5760

58-
} // namespace sofa::component::behaviormodel
61+
} // namespace pluginexample
5962

src/PluginExample/MyBehaviorModel.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include <sofa/core/BehaviorModel.h>
2727

2828

29-
namespace sofa::component::behaviormodel
29+
namespace pluginexample
3030
{
3131

3232
/**
@@ -52,5 +52,5 @@ class SOFA_PLUGINEXAMPLE_API MyBehaviorModel : public sofa::core::BehaviorModel
5252
};
5353

5454

55-
} // sofa::component::behaviormodel
55+
} // namespace pluginexample
5656

src/PluginExample/MyDataWidgetUnsigned.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include <sofa/helper/Factory.h>
2525

2626

27-
namespace sofa::gui::qt
27+
namespace pluginexample
2828
{
2929

3030
/*
@@ -112,4 +112,4 @@ void MyDataWidgetUnsigned::change()
112112
}
113113

114114

115-
} // namespace sofa::gui::qt
115+
} // namespace pluginexample

src/PluginExample/MyDataWidgetUnsigned.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#include <QString>
3232

3333

34-
namespace sofa::gui::qt
34+
namespace pluginexample
3535
{
3636

3737
/**
@@ -65,4 +65,4 @@ protected slots:
6565
};
6666

6767

68-
} // namespace sofa::gui::qt
68+
} // namespace pluginexample

src/PluginExample/MyMappingPendulumInPlane.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,21 @@
2727
#include <sofa/defaulttype/VecTypes.h>
2828

2929

30-
namespace sofa::component::mapping
30+
namespace pluginexample
3131
{
3232

3333
using namespace sofa::defaulttype;
3434

35-
36-
int MyMappingPendulumInPlaneClass = core::RegisterObject("Mapping from an angle to a point in 2D")
35+
void registerMyMappingPendulumInPlane(sofa::core::ObjectFactory* factory)
36+
{
37+
factory->registerObjects(core::ObjectRegistrationData("Mapping from an angle to a point in 2D.")
3738
.add< MyMappingPendulumInPlane<Vec1Types, Vec3Types> >()
38-
.add< MyMappingPendulumInPlane<Vec1Types, Vec2Types> >()
39-
;
39+
.add< MyMappingPendulumInPlane<Vec1Types, Vec2Types> >());
40+
}
4041

4142
template class SOFA_PLUGINEXAMPLE_API MyMappingPendulumInPlane<Vec1Types, Vec3Types>;
4243
template class SOFA_PLUGINEXAMPLE_API MyMappingPendulumInPlane<Vec1Types, Vec2Types>;
4344

4445

4546

46-
} // namespace sofa::component::mapping
47-
47+
} // namespace pluginexample

src/PluginExample/MyMappingPendulumInPlane.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include <sofa/helper/OptionsGroup.h>
2929

3030

31-
namespace sofa::component::mapping
31+
namespace pluginexample
3232
{
3333

3434
using type::vector;
@@ -84,5 +84,5 @@ class MyMappingPendulumInPlane: public core::Mapping<TIn, TOut>
8484
vector<Vec2> gap;
8585
};
8686

87-
} // namespace sofa::component::mapping
87+
} // namespace pluginexample
8888

src/PluginExample/MyMappingPendulumInPlane.inl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ using std::cerr;
3232
using std::endl;
3333

3434

35-
namespace sofa::component::mapping
35+
namespace pluginexample
3636
{
3737

3838
using helper::ReadAccessor;
@@ -206,4 +206,4 @@ void MyMappingPendulumInPlane<In, Out>::applyDJT(const core::MechanicalParams* m
206206
}
207207

208208

209-
} // namespace sofa::component::mapping
209+
} // namespace pluginexample

src/PluginExample/MyProjectiveConstraintSet.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,22 @@
2424
#include <sofa/core/ObjectFactory.h>
2525

2626

27-
namespace sofa::component::projectiveconstraintset
27+
namespace pluginexample
2828
{
29-
29+
3030
using namespace sofa::defaulttype;
3131

32-
33-
int MyProjectiveConstraintSetClass = core::RegisterObject("just an example of templated component")
32+
void registerMyProjectiveConstraintSet(sofa::core::ObjectFactory* factory)
33+
{
34+
factory->registerObjects(core::ObjectRegistrationData("Just an example of templated component.")
3435
.add< MyProjectiveConstraintSet<Vec3Types> >()
3536
.add< MyProjectiveConstraintSet<Vec1Types> >()
36-
.add< MyProjectiveConstraintSet<Rigid3Types> >()
37-
;
37+
.add< MyProjectiveConstraintSet<Rigid3Types> >());
38+
}
3839

3940
template class SOFA_PLUGINEXAMPLE_API MyProjectiveConstraintSet<Rigid3Types>;
4041
template class SOFA_PLUGINEXAMPLE_API MyProjectiveConstraintSet<Vec3Types>;
4142

4243

4344

44-
} // namespace sofa::component::projectiveconstraintset
45+
} // namespace pluginexample

src/PluginExample/MyProjectiveConstraintSet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include <sofa/defaulttype/VecTypes.h>
2929

3030

31-
namespace sofa::component::projectiveconstraintset
31+
namespace pluginexample
3232
{
3333

3434
template <class DataTypes>

src/PluginExample/MyProjectiveConstraintSet.inl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include <PluginExample/MyProjectiveConstraintSet.h>
2525

2626

27-
namespace sofa::component::projectiveconstraintset
27+
namespace pluginexample
2828
{
2929

3030
template <class DataTypes>
@@ -52,4 +52,4 @@ void MyProjectiveConstraintSet<DataTypes>::reinit()
5252

5353

5454

55-
} // namespace sofa::component::projectiveconstraintset
55+
} // namespace pluginexample

src/PluginExample/MyVisualModel.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include <sofa/core/ObjectFactory.h>
2525

2626

27-
namespace sofa::component::visual
27+
namespace pluginexample
2828
{
2929

3030
MyVisualModel::MyVisualModel()
@@ -44,8 +44,11 @@ void MyVisualModel::reinit()
4444
{
4545
}
4646

47-
int MyVisualModelClass = core::RegisterObject("Dummy visual component.").add< MyVisualModel >();
48-
47+
void registerMyVisualModel(sofa::core::ObjectFactory* factory)
48+
{
49+
factory->registerObjects(core::ObjectRegistrationData("Dummy visual component.")
50+
.add< MyVisualModel >());
51+
}
4952

50-
} // namespace sofa::component::visual
53+
} // namespace pluginexample
5154

src/PluginExample/MyVisualModel.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#include <sofa/component/visual/VisualModelImpl.h>
2828

2929

30-
namespace sofa::component::visual
30+
namespace pluginexample
3131
{
3232

3333
/**
@@ -53,5 +53,5 @@ class SOFA_PLUGINEXAMPLE_API MyVisualModel : public sofa::component::visual::Vis
5353
};
5454

5555

56-
} // sofa::component::visual
56+
} // namespace pluginexample
5757

src/PluginExample/config.h.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323

2424
#include <sofa/config.h>
2525

26+
// adding SOFA namespace to ease using SOFA classes within the plugin
27+
using namespace sofa;
28+
2629
#define PLUGINEXAMPLE_VERSION @PROJECT_VERSION@
2730

2831
#ifdef SOFA_BUILD_PLUGINEXAMPLE
@@ -32,3 +35,8 @@
3235
# define SOFA_PLUGINEXAMPLE_API SOFA_IMPORT_DYNAMIC_LIBRARY
3336
#endif
3437

38+
namespace pluginexample
39+
{
40+
constexpr const char* MODULE_NAME = "@PROJECT_NAME@";
41+
constexpr const char* MODULE_VERSION = "@PROJECT_VERSION@";
42+
} // namespace pluginexample

src/PluginExample/initPluginExample.cpp

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,49 +23,63 @@
2323

2424
#include <sofa/core/ObjectFactory.h>
2525
using sofa::core::ObjectFactory;
26+
#include <sofa/helper/system/PluginManager.h>
2627

27-
extern "C" {
28-
SOFA_PLUGINEXAMPLE_API void initExternalModule();
29-
SOFA_PLUGINEXAMPLE_API const char* getModuleName();
30-
SOFA_PLUGINEXAMPLE_API const char* getModuleVersion();
31-
SOFA_PLUGINEXAMPLE_API const char* getModuleLicense();
32-
SOFA_PLUGINEXAMPLE_API const char* getModuleDescription();
33-
SOFA_PLUGINEXAMPLE_API const char* getModuleComponentList();
34-
}
35-
36-
void initExternalModule()
28+
namespace pluginexample
3729
{
38-
static bool first = true;
39-
if (first)
30+
extern void registerMyBehaviorModel(sofa::core::ObjectFactory* factory);
31+
extern void registerMyMappingPendulumInPlane(sofa::core::ObjectFactory* factory);
32+
extern void registerMyProjectiveConstraintSet(sofa::core::ObjectFactory* factory);
33+
extern void registerMyVisualModel(sofa::core::ObjectFactory* factory);
34+
35+
36+
extern "C" {
37+
SOFA_PLUGINEXAMPLE_API void initExternalModule();
38+
SOFA_PLUGINEXAMPLE_API const char* getModuleName();
39+
SOFA_PLUGINEXAMPLE_API const char* getModuleVersion();
40+
SOFA_PLUGINEXAMPLE_API const char* getModuleLicense();
41+
SOFA_PLUGINEXAMPLE_API const char* getModuleDescription();
42+
SOFA_PLUGINEXAMPLE_API void registerObjects(sofa::core::ObjectFactory* factory);
43+
}
44+
45+
void initExternalModule()
4046
{
41-
first = false;
47+
static bool first = true;
48+
if (first)
49+
{
50+
first = false;
51+
52+
// make sure that this plugin is registered into the PluginManager
53+
sofa::helper::system::PluginManager::getInstance().registerPlugin(MODULE_NAME);
54+
}
4255
}
43-
}
4456

45-
const char* getModuleName()
46-
{
47-
return sofa_tostring(SOFA_TARGET);
48-
}
57+
const char* getModuleName()
58+
{
59+
return MODULE_NAME;
60+
}
4961

50-
const char* getModuleVersion()
51-
{
52-
return sofa_tostring(PLUGINEXAMPLE_VERSION);
53-
}
62+
const char* getModuleVersion()
63+
{
64+
return MODULE_VERSION;
65+
}
5466

55-
const char* getModuleLicense()
56-
{
57-
return "LGPL";
58-
}
67+
const char* getModuleLicense()
68+
{
69+
return "LGPL";
70+
}
5971

60-
const char* getModuleDescription()
61-
{
62-
return "Simple example of a Sofa plugin.";
63-
}
72+
const char* getModuleDescription()
73+
{
74+
return "Simple example of a Sofa plugin.";
75+
}
6476

65-
const char* getModuleComponentList()
66-
{
67-
/// string containing the names of the classes provided by the plugin
68-
static std::string classes = ObjectFactory::getInstance()->listClassesFromTarget(sofa_tostring(SOFA_TARGET));
69-
return classes.c_str();
70-
}
77+
void registerObjects(sofa::core::ObjectFactory* factory)
78+
{
79+
registerMyBehaviorModel(factory);
80+
registerMyMappingPendulumInPlane(factory);
81+
registerMyProjectiveConstraintSet(factory);
82+
registerMyVisualModel(factory);
83+
}
7184

85+
}

0 commit comments

Comments
 (0)