Tissue Forge C++ 0.2.1
Interactive, particle-based physics, chemistry and biology modeling and simulation environment
Loading...
Searching...
No Matches
tfEventPyExecutor.h
1/*******************************************************************************
2 * This file is part of Tissue Forge.
3 * Copyright (c) 2022-2024 T.J. Sego
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 ******************************************************************************/
19
20#ifndef _SOURCE_LANGS_PY_TFEVENTPYEXECUTOR_H_
21#define _SOURCE_LANGS_PY_TFEVENTPYEXECUTOR_H_
22
23#include "tf_py.h"
24
25
26namespace TissueForge {
27
28
29 namespace py {
30
31
32 template<typename event_t>
34
41 if(!hasExecutorPyCallable() || !activeEvent) return E_ABORT;
42
43 PyObject *result = PyObject_CallObject(executorPyCallable, NULL);
44
45 if(result == NULL) {
46 PyObject *err = PyErr_Occurred();
47 PyErr_Clear();
48 return E_FAIL;
49 }
50 Py_DECREF(result);
51
52 return S_OK;
53 }
54
61 HRESULT invoke(event_t &ke) {
62 activeEvent = &ke;
63
64 return invoke();
65 }
66
72 event_t *getEvent() {
73 return activeEvent;
74 }
75
82 bool hasExecutorPyCallable() { return executorPyCallable != NULL; }
83
89 void setExecutorPyCallable(PyObject *callable) {
91 executorPyCallable = callable;
92 Py_INCREF(callable);
93 }
94
100 void maybeSetExecutorPyCallable(PyObject *callable) {
101 if(hasExecutorPyCallable()) return;
102 executorPyCallable = callable;
103 Py_INCREF(callable);
104 }
105
111 if(hasExecutorPyCallable()) Py_DECREF(executorPyCallable);
112 executorPyCallable = NULL;
113 }
114
115 protected:
116
117 // The current event object, if any
118 event_t *activeEvent = NULL;
119
120 // The executor callback from the python layer
121 PyObject *executorPyCallable = NULL;
122
123 };
124
125}};
126
127#endif // _SOURCE_LANGS_PY_TFEVENTPYEXECUTOR_H_
Tissue Forge Python language support.
Definition tf_bindPy.h:36
Include Python header, disable linking to pythonX_d.lib on Windows in debug mode.
Definition tfAngleConfig.h:26
Definition tfEventPyExecutor.h:33
bool hasExecutorPyCallable()
Tests whether the executor callback from the python layer has been set.
Definition tfEventPyExecutor.h:82
HRESULT invoke(event_t &ke)
Issues call to execute event callback in python layer on new event.
Definition tfEventPyExecutor.h:61
void maybeSetExecutorPyCallable(PyObject *callable)
Sets the executor callback from the python layer if it has not yet been set.
Definition tfEventPyExecutor.h:100
HRESULT invoke()
Issues call to execute event callback in python layer on existing event.
Definition tfEventPyExecutor.h:40
void setExecutorPyCallable(PyObject *callable)
Sets the executor callback from the python layer.
Definition tfEventPyExecutor.h:89
event_t * getEvent()
Gets the current event object.
Definition tfEventPyExecutor.h:72
void resetExecutorPyCallable()
Resets the executor callback from the python layer.
Definition tfEventPyExecutor.h:110
int32_t HRESULT
Definition tf_port.h:255