1
/* This file is part of MAUS: http://micewww.pp.rl.ac.uk:8080/projects/maus
3
* MAUS is free software: you can redistribute it and/or modify
4
* it under the terms of the GNU General Public License as published by
5
* the Free Software Foundation, either version 3 of the License, or
6
* (at your option) any later version.
8
* MAUS is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with MAUS. If not, see <http://www.gnu.org/licenses/>.
18
// These ifdefs are required to avoid cpp compiler warning
19
#ifdef _POSIX_C_SOURCE
20
#undef _POSIX_C_SOURCE
29
#include <structmember.h>
33
#include "src/common_cpp/Utils/Globals.hh"
34
#include "src/common_cpp/Optics/OpticsModel.hh"
35
#include "src/common_cpp/Optics/LinearApproximationOpticsModel.hh"
37
#include "src/py_cpp/optics/PyOpticsModel.hh"
40
namespace PyOpticsModel {
42
static PyMemberDef _members[] = {
46
OpticsModel* get_optics_model(PyOpticsModel* py_model) {
47
return py_model->model;
51
static PyObject* get(PyObject* self, PyObject* args) {
56
static PyObject* get_static(PyObject* self, PyObject* args) {
61
static PyMethodDef _methods[] = {
62
{"get", (PyCFunction)get, METH_VARARGS, "Docstring"},
63
{"get_static", (PyCFunction)get_static, METH_STATIC, "Docstring"},
67
static PyTypeObject PyOpticsModelType = {
68
PyObject_HEAD_INIT(NULL)
70
"optics_model.OpticsModel", /*tp_name*/
71
sizeof(PyOpticsModel), /*tp_basicsize*/
73
(destructor)_dealloc, /*tp_dealloc*/
88
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
89
"optics.OpticsModel docstring", /* tp_doc */
92
0, /* tp_richcompare */
93
0, /* tp_weaklistoffset */
96
_methods, /* tp_methods */
97
_members, /* tp_members */
101
0, /* tp_descr_get */
102
0, /* tp_descr_set */
103
0, /* tp_dictoffset */
104
(initproc)_init, /* tp_init */
105
(allocfunc)_alloc, /* tp_alloc, called by new */
106
(newfunc)_new, /* tp_new */
107
(freefunc)_free, /* tp_free, called by dealloc */
110
PyObject *_alloc(PyTypeObject *type, Py_ssize_t nitems) {
111
PyOpticsModel* optics = (PyOpticsModel*)malloc(sizeof(PyOpticsModel));
112
optics->model = NULL;
113
optics->ob_refcnt = 1;
114
optics->ob_type = type;
115
return (PyObject*)optics;
118
int _init(PyObject* self, PyObject *args, PyObject *kwds) {
119
PyOpticsModel* optics = (PyOpticsModel*)self;
120
if (optics == NULL) {
121
PyErr_SetString(PyExc_TypeError,
122
"Failed to resolve self as OpticsModel in __init__");
125
if (optics->model != NULL) {
126
PyErr_SetString(PyExc_RuntimeError,
127
"C++ OpticsModel was unexpectedly initialised already");
130
optics->model = new MAUS::LinearApproximationOpticsModel(
131
Globals::GetConfigurationCards());
135
PyObject *_new(PyTypeObject *type, Py_ssize_t nitems) {
136
PyOpticsModel* optics = (PyOpticsModel*)_alloc(type, nitems);
137
return (PyObject*)optics;
140
void _dealloc(PyOpticsModel * self) {
144
void _free(PyOpticsModel * self) {
146
if (self->model != NULL)
152
PyMODINIT_FUNC initoptics_model(void) {
153
PyOpticsModelType.tp_new = PyType_GenericNew;
154
if (PyType_Ready(&PyOpticsModelType) < 0) return;
156
PyObject* module = Py_InitModule3("optics_model", NULL, "optics model docstring");
157
if (module == NULL) return;
159
PyTypeObject* optics_model_type = &PyOpticsModelType;
160
Py_INCREF(optics_model_type);
161
PyModule_AddObject(module, "OpticsModel", (PyObject*)optics_model_type);
164
} // namespace PyOpticsModel