|
28 | 28 | namespace sofapython3 { |
29 | 29 |
|
30 | 30 |
|
31 | | -template<class T> |
32 | | -class Trampoline_T |
| 31 | +class TrampolineBase |
33 | 32 | { |
34 | 33 | public: |
35 | | - |
36 | | - virtual ~Trampoline_T(); |
37 | | - |
| 34 | + explicit TrampolineBase(sofa::core::objectmodel::BaseComponent* self); |
| 35 | + virtual ~TrampolineBase(); |
38 | 36 |
|
39 | 37 | void trampoline_handleEvent(sofa::core::objectmodel::Event* event); |
40 | | - |
41 | 38 | std::string trampoline_getClassName() const; |
42 | | - |
43 | | - /// Invalidates a specific entry in the method cache (called when a user reassigns an on* attribute) |
44 | 39 | void invalidateMethodCache(const std::string& methodName); |
45 | 40 |
|
46 | | - static sofa::core::sptr<T> _init_(pybind11::args& /*args*/, pybind11::kwargs& kwargs); |
47 | | - |
48 | | - static void _setattr_(pybind11::object self, const std::string& s, pybind11::object value); |
49 | | - |
50 | 41 | protected: |
51 | | - /// Initializes the Python object cache (m_pySelf and method cache) |
52 | 42 | void initializePythonCache(); |
53 | | - |
54 | | - /// Returns a cached method if it exists, or an empty object if not |
55 | 43 | pybind11::object getCachedMethod(const std::string& methodName); |
56 | | - |
57 | | - /// Calls a cached Python method with the given event |
58 | 44 | bool callCachedMethod(const pybind11::object& method, sofa::core::objectmodel::Event* event); |
59 | | - |
60 | | - /// Legacy method for uncached calls (fallback) |
61 | 45 | bool callScriptMethod(const pybind11::object& self, sofa::core::objectmodel::Event* event, |
62 | | - const std::string& methodName); |
| 46 | + const std::string& methodName); |
63 | 47 |
|
64 | | - /// Cached Python self reference (avoids repeated py::cast(this)) |
65 | | - pybind11::object m_pySelf; |
| 48 | + /// Raw non-owning pointer to the concrete trampoline as BaseComponent. |
| 49 | + /// Safe because TrampolineBase is always embedded in the same object. |
| 50 | + sofa::core::objectmodel::BaseComponent* m_componentSelf { nullptr }; |
66 | 51 |
|
67 | | - /// Cache of Python method objects, keyed by method name (including "onEvent" fallback) |
68 | | - /// Stores the method object if it exists, or an empty object if checked and not found |
| 52 | + pybind11::object m_pySelf; |
69 | 53 | std::unordered_map<std::string, pybind11::object> m_methodCache; |
70 | | - |
71 | | - /// Flag indicating whether the cache has been initialized |
72 | 54 | bool m_cacheInitialized = false; |
73 | 55 | }; |
74 | 56 |
|
75 | 57 |
|
76 | | -/** |
77 | | - * Empty controller shell that allows pybind11 to bind the init and reinit methods (since BaseComponent doesn't have |
78 | | - * them) |
79 | | - */ |
| 58 | +template<class T> |
| 59 | +sofa::core::sptr<T> trampoline_init(pybind11::args& /*args*/, pybind11::kwargs& kwargs); |
| 60 | + |
| 61 | +template<class T> |
| 62 | +void trampoline_setattr(pybind11::object self, const std::string& s, pybind11::object value); |
| 63 | + |
| 64 | + |
| 65 | + |
80 | 66 | class Component : public sofa::core::objectmodel::BaseComponent { |
81 | 67 | public: |
82 | 68 | SOFA_CLASS(Component, sofa::core::objectmodel::BaseComponent); |
83 | | - void init() override {}; |
84 | | - void reinit() override {}; |
| 69 | + void init() override {} |
| 70 | + void reinit() override {} |
85 | 71 | }; |
86 | 72 |
|
87 | | -class Component_Trampoline : public Component, public Trampoline_T<Component_Trampoline> |
| 73 | +class Component_Trampoline : public Component, public TrampolineBase |
88 | 74 | { |
89 | 75 | public: |
90 | 76 | SOFA_CLASS(Component_Trampoline, Component); |
91 | 77 |
|
92 | | - Component_Trampoline() = default; |
93 | | - virtual ~Component_Trampoline() = default; |
| 78 | + Component_Trampoline(); |
| 79 | + ~Component_Trampoline() override = default; |
94 | 80 |
|
95 | 81 | void init() override; |
96 | 82 | void reinit() override; |
97 | 83 | void draw(const sofa::core::visual::VisualParams* params) override; |
98 | | - |
99 | 84 | void handleEvent(sofa::core::objectmodel::Event* event) override; |
100 | | - |
101 | 85 | std::string getClassName() const override; |
102 | | - |
103 | 86 | }; |
104 | 87 |
|
105 | 88 | void moduleAddComponent(pybind11::module &m); |
106 | 89 |
|
107 | 90 | } /// namespace sofapython3 |
108 | | - |
0 commit comments