~ajdobbs/maus/event-selection

« back to all changes in this revision

Viewing changes to src/py_cpp/optics/PyOpticsModel.cc

  • Committer: Chris Rogers
  • Date: 2013-07-11 05:48:08 UTC
  • mto: (663.6.290 merge)
  • mto: This revision was merged to the branch mainline in revision 688.
  • Revision ID: chris.rogers@stfc.ac.uk-20130711054808-hd04qt0hl7cty4p5
Working on PyOpticsModel API

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of MAUS: http://micewww.pp.rl.ac.uk:8080/projects/maus
 
2
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 *
 
16
 */
 
17
 
 
18
// These ifdefs are required to avoid cpp compiler warning
 
19
#ifdef _POSIX_C_SOURCE
 
20
#undef _POSIX_C_SOURCE
 
21
#endif
 
22
 
 
23
#ifdef _XOPEN_SOURCE
 
24
#undef _XOPEN_SOURCE
 
25
#endif
 
26
 
 
27
#include <Python.h>
 
28
 
 
29
#include <structmember.h>
 
30
#include <string.h>
 
31
#include <stdio.h>
 
32
 
 
33
#include "src/common_cpp/Utils/Globals.hh"
 
34
#include "src/common_cpp/Optics/OpticsModel.hh"
 
35
#include "src/common_cpp/Optics/LinearApproximationOpticsModel.hh"
 
36
 
 
37
#include "src/py_cpp/optics/PyOpticsModel.hh"
 
38
 
 
39
namespace MAUS {
 
40
namespace PyOpticsModel {
 
41
 
 
42
static PyMemberDef _members[] = {
 
43
{NULL}
 
44
};
 
45
 
 
46
OpticsModel* get_optics_model(PyOpticsModel* py_model) {
 
47
    return py_model->model;
 
48
}
 
49
 
 
50
/// Dummy function
 
51
static PyObject* get(PyObject* self, PyObject* args) {
 
52
    return NULL;
 
53
}
 
54
 
 
55
/// Dummy function
 
56
static PyObject* get_static(PyObject* self, PyObject* args) {
 
57
    return NULL;
 
58
}
 
59
 
 
60
 
 
61
static PyMethodDef _methods[] = {
 
62
{"get", (PyCFunction)get,           METH_VARARGS, "Docstring"},
 
63
{"get_static", (PyCFunction)get_static, METH_STATIC, "Docstring"},
 
64
{NULL}
 
65
};
 
66
 
 
67
static PyTypeObject PyOpticsModelType = {
 
68
    PyObject_HEAD_INIT(NULL)
 
69
    0,                         /*ob_size*/
 
70
    "optics_model.OpticsModel",         /*tp_name*/
 
71
    sizeof(PyOpticsModel),           /*tp_basicsize*/
 
72
    0,                         /*tp_itemsize*/
 
73
    (destructor)_dealloc, /*tp_dealloc*/
 
74
    0,                         /*tp_print*/
 
75
    0,                         /*tp_getattr*/
 
76
    0,                         /*tp_setattr*/
 
77
    0,                         /*tp_compare*/
 
78
    0,                         /*tp_repr*/
 
79
    0,                         /*tp_as_number*/
 
80
    0,                         /*tp_as_sequence*/
 
81
    0,                         /*tp_as_mapping*/
 
82
    0,                         /*tp_hash */
 
83
    0,                         /*tp_call*/
 
84
    0,                         /*tp_str*/
 
85
    0,                         /*tp_getattro*/
 
86
    0,                         /*tp_setattro*/
 
87
    0,                         /*tp_as_buffer*/
 
88
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
 
89
    "optics.OpticsModel docstring",           /* tp_doc */
 
90
    0,                         /* tp_traverse */
 
91
    0,                         /* tp_clear */
 
92
    0,                         /* tp_richcompare */
 
93
    0,                         /* tp_weaklistoffset */
 
94
    0,                         /* tp_iter */
 
95
    0,                         /* tp_iternext */
 
96
    _methods,           /* tp_methods */
 
97
    _members,           /* tp_members */
 
98
    0,                         /* tp_getset */
 
99
    0,                         /* tp_base */
 
100
    0,                         /* tp_dict */
 
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 */
 
108
};
 
109
 
 
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;
 
116
}
 
117
 
 
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__");
 
123
        return 1;
 
124
    }
 
125
    if (optics->model != NULL) {
 
126
        PyErr_SetString(PyExc_RuntimeError,
 
127
                        "C++ OpticsModel was unexpectedly initialised already");
 
128
        return 1;
 
129
    }
 
130
    optics->model = new MAUS::LinearApproximationOpticsModel(
 
131
                                              Globals::GetConfigurationCards());
 
132
    return 0;
 
133
}
 
134
 
 
135
PyObject *_new(PyTypeObject *type, Py_ssize_t nitems) {
 
136
    PyOpticsModel* optics = (PyOpticsModel*)_alloc(type, nitems);
 
137
    return (PyObject*)optics;
 
138
}
 
139
 
 
140
void _dealloc(PyOpticsModel * self) {
 
141
    _free(self);
 
142
}
 
143
 
 
144
void _free(PyOpticsModel * self) {
 
145
    if (self != NULL) {
 
146
        if (self->model != NULL)
 
147
            delete self->model;
 
148
        delete self;
 
149
    }
 
150
}
 
151
 
 
152
PyMODINIT_FUNC initoptics_model(void) {
 
153
    PyOpticsModelType.tp_new = PyType_GenericNew;
 
154
    if (PyType_Ready(&PyOpticsModelType) < 0) return;
 
155
 
 
156
    PyObject* module = Py_InitModule3("optics_model", NULL, "optics model docstring");
 
157
    if (module == NULL) return;
 
158
 
 
159
    PyTypeObject* optics_model_type = &PyOpticsModelType;
 
160
    Py_INCREF(optics_model_type);
 
161
    PyModule_AddObject(module, "OpticsModel", (PyObject*)optics_model_type);
 
162
}
 
163
 
 
164
}  // namespace PyOpticsModel
 
165
}  // namespace MAUS
 
166