Skip to content

Commit 486c35c

Browse files
committed
Unify onEvent into m_methodCache
1 parent 10438e3 commit 486c35c

File tree

2 files changed

+6
-56
lines changed

2 files changed

+6
-56
lines changed

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

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ Controller_Trampoline::~Controller_Trampoline()
4747
{
4848
PythonEnvironment::gil acquire {"~Controller_Trampoline"};
4949
m_methodCache.clear();
50-
m_onEventMethod = py::object();
5150
m_pySelf = py::object();
5251
}
5352
}
@@ -60,16 +59,8 @@ void Controller_Trampoline::initializePythonCache()
6059
// Must be called with GIL held
6160
m_pySelf = py::cast(this);
6261

63-
// Cache the fallback "onEvent" method if it exists
64-
if (py::hasattr(m_pySelf, "onEvent"))
65-
{
66-
py::object fct = m_pySelf.attr("onEvent");
67-
if (PyCallable_Check(fct.ptr()))
68-
{
69-
m_hasOnEvent = true;
70-
m_onEventMethod = fct;
71-
}
72-
}
62+
// Pre-cache the fallback "onEvent" method via the standard cache path
63+
getCachedMethod("onEvent");
7364

7465
m_cacheInitialized = true;
7566
}
@@ -122,15 +113,6 @@ void Controller_Trampoline::invalidateMethodCache(const std::string& methodName)
122113
if (!m_cacheInitialized)
123114
return;
124115

125-
if (methodName == "onEvent")
126-
{
127-
// Clear the dedicated fallback cache; handleEvent will re-resolve it lazily
128-
m_hasOnEvent = false;
129-
m_onEventMethod = py::object();
130-
m_onEventDirty = true;
131-
return;
132-
}
133-
134116
// Remove the entry so getCachedMethod will re-resolve it on next call
135117
m_methodCache.erase(methodName);
136118
}
@@ -208,39 +190,16 @@ void Controller_Trampoline::handleEvent(Event* event)
208190
// Build the event-specific method name (e.g., "onAnimateBeginEvent")
209191
std::string methodName = std::string("on") + event->getClassName();
210192

211-
// Try to get the cached method for this specific event type
193+
// Try the event-specific method first, then fall back to generic "onEvent"
212194
py::object method = getCachedMethod(methodName);
195+
if (!method)
196+
method = getCachedMethod("onEvent");
213197

214198
if (method)
215199
{
216-
// Found a specific handler for this event type
217200
bool isHandled = callCachedMethod(method, event);
218201
if (isHandled)
219202
event->setHandled();
220-
return;
221-
}
222-
223-
// Re-resolve "onEvent" if it was invalidated by a __setattr__ call
224-
if (m_onEventDirty)
225-
{
226-
m_onEventDirty = false;
227-
if (py::hasattr(m_pySelf, "onEvent"))
228-
{
229-
py::object fct = m_pySelf.attr("onEvent");
230-
if (PyCallable_Check(fct.ptr()))
231-
{
232-
m_hasOnEvent = true;
233-
m_onEventMethod = fct;
234-
}
235-
}
236-
}
237-
238-
// Fall back to the generic "onEvent" method if available
239-
if (m_hasOnEvent)
240-
{
241-
bool isHandled = callCachedMethod(m_onEventMethod, event);
242-
if (isHandled)
243-
event->setHandled();
244203
}
245204
});
246205
}

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,10 @@ class Controller_Trampoline : public Controller
7474
/// Cached Python self reference (avoids repeated py::cast(this))
7575
pybind11::object m_pySelf;
7676

77-
/// Cache of Python method objects, keyed by method name
77+
/// Cache of Python method objects, keyed by method name (including "onEvent" fallback)
7878
/// Stores the method object if it exists, or an empty object if checked and not found
7979
std::unordered_map<std::string, pybind11::object> m_methodCache;
8080

81-
/// Flag indicating whether the fallback "onEvent" method exists
82-
bool m_hasOnEvent = false;
83-
84-
/// Cached reference to the fallback "onEvent" method
85-
pybind11::object m_onEventMethod;
86-
87-
/// Flag indicating whether the "onEvent" fallback needs re-resolution
88-
bool m_onEventDirty = false;
89-
9081
/// Flag indicating whether the cache has been initialized
9182
bool m_cacheInitialized = false;
9283
};

0 commit comments

Comments
 (0)