Tissue Forge C++ 0.2.1
Interactive, particle-based physics, chemistry and biology modeling and simulation environment
Loading...
Searching...
No Matches
tf_py.h
Go to the documentation of this file.
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
25#ifndef _SOURCE_LANGS_PY_TF_PY_H_
26#define _SOURCE_LANGS_PY_TF_PY_H_
27
29#if defined(_MSC_VER) || defined(_WIN32)
30# pragma warning(push)
31# pragma warning(disable: 4510 4610 4512 4005)
32# include <corecrt.h>
33# if defined(_DEBUG)
34# define TF_DEBUG_MARKER
35# undef _DEBUG
36# endif
37#endif
38
39#include <Python.h>
40
41#if defined(_MSC_VER) || defined(_WIN32)
42# if defined(TF_DEBUG_MARKER)
43# define _DEBUG
44# undef TF_DEBUG_MARKER
45# endif
46# pragma warning(pop)
47#endif
48
49#include <tf_port.h>
50#include <types/tf_types.h>
51#include <types/tf_cast.h>
52
53#include <Magnum/Magnum.h>
54#include <Magnum/Math/Vector3.h>
55#include <Magnum/Math/Vector4.h>
56#include <Magnum/Math/Matrix3.h>
57
58#include <string>
59
60
61namespace TissueForge {
62
63
64 template<>
65 Magnum::Vector2 cast(PyObject *obj);
66
67 template<>
68 Magnum::Vector3 cast(PyObject *obj);
69
70 template<>
71 Magnum::Vector4 cast(PyObject *obj);
72
73 template<>
74 Magnum::Vector2i cast(PyObject *obj);
75
76 template<>
77 Magnum::Vector3i cast(PyObject *obj);
78
79 template<>
80 fVector2 cast(PyObject *obj);
81
82 template<>
83 fVector3 cast(PyObject *obj);
84
85 template<>
86 fVector4 cast(PyObject *obj);
87
88 template<>
89 dVector2 cast(PyObject *obj);
90
91 template<>
92 dVector3 cast(PyObject *obj);
93
94 template<>
95 dVector4 cast(PyObject *obj);
96
97 template<>
98 iVector2 cast(PyObject *obj);
99
100 template<>
101 iVector3 cast(PyObject *obj);
102
103 template<>
104 PyObject* cast<int16_t, PyObject*>(const int16_t &i);
105
106 template<>
107 PyObject* cast<uint16_t, PyObject*>(const uint16_t &i);
108
109 template<>
110 PyObject* cast<uint32_t, PyObject*>(const uint32_t &i);
111
112 template<>
113 PyObject* cast<uint64_t, PyObject*>(const uint64_t &i);
114
115 template<>
116 PyObject* cast<float, PyObject*>(const float &f);
117
118 template<>
119 PyObject* cast<double, PyObject*>(const double &f);
120
121 template<>
122 float cast(PyObject *obj);
123
124 template<>
125 double cast(PyObject *obj);
126
127 template<>
128 PyObject* cast<bool, PyObject*>(const bool &f);
129
130 template<>
131 bool cast(PyObject *obj);
132
133 template<>
134 PyObject* cast<int, PyObject*>(const int &i);
135
136 template<>
137 int cast(PyObject *obj);
138
139 template<>
140 PyObject* cast<std::string, PyObject*>(const std::string &s);
141
142 template<>
143 std::string cast(PyObject *o);
144
145 template<>
146 int16_t cast(PyObject *o);
147
148 template<>
149 uint16_t cast(PyObject *o);
150
151 template<>
152 uint32_t cast(PyObject *o);
153
154 template<>
155 uint64_t cast(PyObject *o);
156
157
158 namespace py {
159
160
161 CAPI_FUNC(PyObject*) Import_ImportString(const std::string &name);
162 CAPI_FUNC(PyObject*) iPython_Get();
163 CAPI_FUNC(bool) terminalInteractiveShell();
164 CAPI_FUNC(bool) ZMQInteractiveShell();
165
169 template <typename T>
170 bool check(PyObject *o);
171
177 PyObject *py_arg(const char* name, int index, PyObject *_args, PyObject *_kwargs);
178
182 std::string repr(PyObject *o);
183 std::string str(PyObject *o);
184
188 std::string pyerror_str();
189
190 template<typename T>
191 T arg(const char* name, int index, PyObject *args, PyObject *kwargs) {
192 PyObject *value = py_arg(name, index, args, kwargs);
193 if(value) {
194 return cast<PyObject, T>(value);
195 }
196 throw std::runtime_error(std::string("missing argument ") + name);
197 };
198
199 template<typename T>
200 T arg(const char* name, int index, PyObject *args, PyObject *kwargs, T deflt) {
201
202 PyObject *value = py_arg(name, index, args, kwargs);
203 if(value) {
204 return cast<PyObject, T>(value);
205 }
206 return deflt;
207 };
208
209 };
210
211}
212
213#endif // _SOURCE_LANGS_PY_TF_PY_H_
PyObject * py_arg(const char *name, int index, PyObject *_args, PyObject *_kwargs)
std::string pyerror_str()
std::string repr(PyObject *o)
bool check(PyObject *o)
Include Python header, disable linking to pythonX_d.lib on Windows in debug mode.
Definition tfAngleConfig.h:26