forked from hoffstadt/DearPyGui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmvCallbackRegistry.cpp
More file actions
173 lines (140 loc) · 5.01 KB
/
Copy pathmvCallbackRegistry.cpp
File metadata and controls
173 lines (140 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "mvPyUtils.h"
#pragma hdrstop
#include "mvCallbackRegistry.h"
#include "mvContext.h"
#include "mvItemRegistry.h"
void mvRunTasks(bool early /* = false */)
{
auto& tasks = early? GContext->callbackRegistry->earlyTasks : GContext->callbackRegistry->tasks;
while (!tasks.empty())
{
mvFunctionWrapper t;
tasks.wait_and_pop(t);
t();
}
}
void mvFrameCallback(i32 frame)
{
auto callbackRegistry = GContext->callbackRegistry; // for brevity
if (frame > callbackRegistry->highestFrame)
return;
if (callbackRegistry->frameCallbacks.count(frame) == 0)
return;
// We have to use `std::unordered_map::at()` instead of indexing it with `[]`:
// `mvPyObject` has no default constructor, whereas `operator[]` can insert
// a new value into the map and therefore requires a default constructor.
auto callback = std::make_shared<mvPyObject>(std::move(callbackRegistry->frameCallbacks.at(frame)));
auto user_data = std::make_shared<mvPyObject>(std::move(callbackRegistry->frameCallbacksUserData.at(frame)));
// Since mvPyObject objects remaining in the maps are now "empty", it's safe to
// delete them right here even though we don't own the GIL.
callbackRegistry->frameCallbacks.erase(frame);
callbackRegistry->frameCallbacksUserData.erase(frame);
mvAddOwnerlessCallback(callback, user_data, (mvUUID)frame, "", []() -> PyObject* { return nullptr; });
}
bool mvRunCallbacks()
{
GContext->callbackRegistry->running = true;
mvGlobalIntepreterLock gil;
while (GContext->callbackRegistry->running)
{
mvFunctionWrapper t2;
Py_BEGIN_ALLOW_THREADS;
GContext->callbackRegistry->calls.wait_and_pop(t2);
Py_END_ALLOW_THREADS;
t2();
GContext->callbackRegistry->callCount--;
}
return true;
}
void mvAddCallback(const std::weak_ptr<void>& owner,
PyObject* callback,
const std::shared_ptr<mvPyObject>& user_data,
mvUUID sender,
const std::string& alias)
{
mvAddCallback(owner, callback, user_data, sender, alias, []() -> PyObject* { return nullptr; });
}
void mvAddOwnerlessCallback(const std::shared_ptr<mvPyObject>& callback,
const std::shared_ptr<mvPyObject>& user_data,
mvUUID sender,
const std::string& alias)
{
mvAddOwnerlessCallback(callback, user_data, sender, alias, []() -> PyObject* { return nullptr; });
}
void mvRunOwnedCallback(const std::weak_ptr<void>& owner, PyObject* callback, PyObject* user_data, mvUUID sender /* = 0 */, const std::string& sender_alias /* = "" */, PyObject* app_data /* = nullptr */)
{
auto liveOwner = owner.lock();
if (liveOwner)
{
// Make our own callback ref
mvPyObject ownCallback(callback, true);
{
// We need to lock the mutex while releasing the `owner` pointer. When `liveOwner`
// goes out of scope, the mvAppItem that it holds might get deleted, thus
// invalidating all mvAppItem* pointers that we might be holding in various DPG functions
// e.g. in another thread. By locking the mutex and explicitly resetting
// `liveOwner`, we guarantee that destruction of mvAppItem only occurs with
// the mutex locked, and thus cannot happen in the middle of an API function
// that also locks the mutex.
mvPySafeLockGuard lk(GContext->mutex);
liveOwner.reset();
}
mvRunCallback(ownCallback, user_data, sender, sender_alias, app_data);
}
}
void mvRunCallback(PyObject* callback, PyObject* user_data, mvUUID sender, const std::string& sender_alias, PyObject* app_data)
{
if (callback == nullptr)
return;
if (!PyCallable_Check(callback))
{
mvThrowPythonError(mvErrorCode::mvNone, "Callable not callable.");
PyErr_Print();
return;
}
//PyErr_Clear();
if (PyErr_Occurred())
PyErr_Print();
PyObject* fc = PyObject_GetAttrString(callback, "__code__");
if (fc) {
PyObject* ac = PyObject_GetAttrString(fc, "co_argcount");
if (ac) {
i32 count = PyLong_AsLong(ac);
if (PyMethod_Check(callback))
count--;
mvPyObject pArgs(count > 0 ? PyTuple_New(count) : nullptr);
if (count > 0)
{
PyTuple_SetItem(pArgs, 0, ToPyUUID(sender, sender_alias));
if (count > 1)
{
if (app_data == nullptr)
app_data = Py_None;
// Need an owned ref here: PyTuple_SetItem takes ownership;
// this also handles Py_None correctly (need to incref it).
Py_INCREF(app_data);
PyTuple_SetItem(pArgs, 1, app_data);
if (count > 2)
{
if (user_data == nullptr)
user_data = Py_None;
// Need an owned ref here: PyTuple_SetItem takes ownership;
// this also handles Py_None correctly (need to incref it).
Py_INCREF(user_data);
PyTuple_SetItem(pArgs, 2, user_data);
// If the callback takes more parms, just pass None in there
for (int i = 3; i < count; i++)
PyTuple_SetItem(pArgs, i, GetPyNone());
}
}
}
// perform the actual call
mvPyObject result(PyObject_CallObject(callback, pArgs));
// check if call succeeded
if (!result.isOk())
PyErr_Print();
Py_DECREF(ac);
}
Py_DECREF(fc);
}
}