Skip to content

Commit 2bd7ff6

Browse files
committed
Generalize the pybind11 forward declaration of classes.
1 parent 3ebaf13 commit 2bd7ff6

15 files changed

Lines changed: 101 additions & 12 deletions

bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Base.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -449,12 +449,21 @@ py::object BindingBase::setDataValues(Base& self, py::kwargs kwargs)
449449
return py::none();
450450
}
451451

452+
auto getBaseBinding(py::module& m)
453+
{
454+
static py::class_<Base, py_shared_ptr<Base>> base(m, "Base", py::dynamic_attr(), doc::base::BaseClass);
455+
return base;
456+
}
457+
458+
void moduleForwardAddBase(py::module& m)
459+
{
460+
getBaseBinding(m);
461+
}
462+
452463
void moduleAddBase(py::module &m)
453464
{
454-
moduleForwardAddBaseData(m);
455-
moduleForwardAddBaseLink(m);
465+
auto base = getBaseBinding(m);
456466

457-
py::class_<Base, py_shared_ptr<Base>> base(m, "Base", py::dynamic_attr(), doc::base::BaseClass);
458467
/// set & get the name as string. The alternative is to access the data field using
459468
/// obj.name.value = "aName"
460469
base.def("getName", [](Base& b){ return b.getName(); }, sofapython3::doc::base::getName);

bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Base.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ class BindingBase
6868
static std::string getLinkPath(sofa::core::objectmodel::Base& self);
6969
};
7070

71-
71+
/// Forward declaration in pybind11.
72+
/// more details in: https://github.com/sofa-framework/SofaPython3/pull/457
73+
void moduleForwardAddBase(pybind11::module& m);
7274
void moduleAddBase(pybind11::module& m);
7375

7476
} /// namespace sofapython3

bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_BaseData.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,6 @@ void moduleForwardAddBaseData(py::module& m)
207207
void moduleAddBaseData(py::module& m)
208208
{
209209
/// Register the BaseData binding into the pybind11 system.
210-
//py::class_<BaseData, std::unique_ptr<sofa::core::objectmodel::BaseData, pybind11::nodelete>> data(m, "Data", sofapython3::doc::baseData::BaseDataClass);
211-
212210
auto data =getPythonClassForBaseData(m);
213211
data.def("getName", [](BaseData& b){ return b.getName(); }, sofapython3::doc::baseData::getName);
214212
data.def("setName", [](BaseData& b, const std::string& s){ b.setName(s); }, sofapython3::doc::baseData::setName);

bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_BaseData.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
namespace sofapython3 {
2626

27+
/// Forward declaration in pybind11.
28+
/// more details in: https://github.com/sofa-framework/SofaPython3/pull/457
2729
void moduleForwardAddBaseData(pybind11::module& m);
2830
void moduleAddBaseData(pybind11::module& m);
2931

bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_BaseLink.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
namespace sofapython3 {
2626

27+
/// Forward declaration in pybind11.
28+
/// more details in: https://github.com/sofa-framework/SofaPython3/pull/457
2729
void moduleForwardAddBaseLink(pybind11::module& m);
2830
void moduleAddBaseLink(pybind11::module& m);
2931

bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_BaseMeshTopology.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <SofaPython3/Sofa/Core/Binding_Base.h>
2323
#include <SofaPython3/Sofa/Core/Binding_BaseContext.h>
24+
#include <SofaPython3/Sofa/Core/Binding_BaseMeshTopology.h>
2425
#include <SofaPython3/PythonFactory.h>
2526
#include <sofa/core/BaseState.h>
2627
#include <sofa/core/objectmodel/BaseObject.h>
@@ -36,8 +37,21 @@ using namespace sofa::core::topology;
3637

3738
namespace sofapython3 {
3839

40+
41+
auto getPythonClassForBaseMeshTopology(py::module& m)
42+
{
43+
/// Register the BaseData binding into the pybind11 system.
44+
static py::class_<BaseMeshTopology, Topology, py_shared_ptr<BaseMeshTopology>> basemesh(m, "BaseMeshTopology");
45+
return basemesh;
46+
}
47+
48+
void moduleForwardAddBaseMeshTopology(py::module& m)
49+
{
50+
getPythonClassForBaseMeshTopology(m);
51+
}
52+
3953
void moduleAddBaseMeshTopology(py::module& m) {
40-
py::class_<BaseMeshTopology, Topology, py_shared_ptr<BaseMeshTopology>> c (m, "BaseMeshTopology");
54+
auto c = getPythonClassForBaseMeshTopology(m);
4155

4256
/// register the BaseMeshTopology binding in the downcasting subsystem
4357
PythonFactory::registerType<BaseMeshTopology>([](sofa::core::objectmodel::Base* object)

bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_BaseMeshTopology.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424

2525
namespace sofapython3 {
2626

27+
/// Forward declaration in pybind11.
28+
/// more details in: https://github.com/sofa-framework/SofaPython3/pull/457
29+
void moduleForwardAddBaseMeshTopology(pybind11::module &m);
2730
void moduleAddBaseMeshTopology(pybind11::module &m);
2831

2932
} // namespace sofapython3

bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_BaseObject.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,21 @@ py::object __getitem__(BaseObject &self, std::string s)
188188
return getItem(self, s);
189189
}
190190

191-
void moduleAddBaseObject(py::module& m)
191+
auto getBaseObjectBinding(py::module& m)
192192
{
193193
/// Register the BaseObject binding into the pybind11 typing system
194-
py::class_<BaseObject, Base, py_shared_ptr<BaseObject>>p(m, "Object", sofapython3::doc::baseObject::Class);
194+
static py::class_<BaseObject, Base, py_shared_ptr<BaseObject>>p(m, "Object", sofapython3::doc::baseObject::Class);
195+
return p;
196+
}
197+
198+
void moduleForwardAddBaseObject(py::module& m)
199+
{
200+
getBaseObjectBinding(m);
201+
}
202+
203+
void moduleAddBaseObject(py::module& m)
204+
{
205+
auto p = getBaseObjectBinding(m);
195206

196207
/// Register the BaseObject binding into the downcasting subsystem
197208
PythonFactory::registerType<sofa::core::objectmodel::BaseObject>(

bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_BaseObject.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class BaseObject;
2929
namespace sofapython3 {
3030

3131
pybind11::object getItem(const sofa::core::objectmodel::BaseObject & self, const std::string& path);
32+
33+
void moduleForwardAddBaseObject(pybind11::module &m);
3234
void moduleAddBaseObject(pybind11::module &m);
3335

3436
} /// namespace sofapython

bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Mass.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,10 @@ void moduleAddMass(py::module &m) {
9494
declare_mass<sofa::defaulttype::Rigid2dTypes>(m);
9595
}
9696

97+
void moduleForwardAddBaseMass(py::module& m)
98+
{
99+
static py::class_<sofa::core::behavior::BaseMass,
100+
sofa::core::Base, py_shared_ptr<sofa::core::behavior::BaseMass>> basemass(m, "BaseMass");
101+
}
102+
97103
} // namespace sofapython3

0 commit comments

Comments
 (0)