MoDeNa  1.0
Software framework facilitating sequential multi-scale modelling
indexset.c
1 /*
2 
3  ooo ooooo oooooooooo. ooooo ooo
4  `88. .888' `888' `Y8b `888b. `8'
5  888b d'888 .ooooo. 888 888 .ooooo. 8 `88b. 8 .oooo.
6  8 Y88. .P 888 d88' `88b 888 888 d88' `88b 8 `88b. 8 `P )88b
7  8 `888' 888 888 888 888 888 888ooo888 8 `88b.8 .oP"888
8  8 Y 888 888 888 888 d88' 888 .o 8 `888 d8( 888
9  o8o o888o `Y8bod8P' o888bood8P' `Y8bod8P' o8o `8 `Y888""8o
10 
11 Copyright
12  2014-2016 MoDeNa Consortium, All rights reserved.
13 
14 License
15  This file is part of Modena.
16 
17  The Modena interface library is free software; you can redistribute it
18  and/or modify it under the terms of the GNU Lesser General Public License
19  as published by the Free Software Foundation, either version 3 of the
20  License, or (at your option) any later version.
21 
22  Modena is distributed in the hope that it will be useful, but WITHOUT ANY
23  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
24  FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
25  details.
26 
27  You should have received a copy of the GNU General Public License along
28  with Modena. If not, see <http://www.gnu.org/licenses/>.
29 */
30 
31 #include "indexset.h"
32 #include "structmember.h"
33 #include "global.h"
34 
35 PyObject *modena_IndexSet = NULL;
36 
37 modena_index_set_t *modena_index_set_new
38 (
39  const char *indexSetId
40 )
41 {
42  // Initialize the Python Interpreter
43  if(!Py_IsInitialized())
44  {
45  Py_Initialize();
46  }
47 
48  // Initialize this module
49  initlibmodena();
50 
51  PyObject *args = PyTuple_New(0);
52  PyObject *kw = Py_BuildValue("{s:s}", "indexSetId", indexSetId);
53 
54  PyObject *pNewObj = PyObject_Call
55  (
56  (PyObject *) &modena_index_set_tType,
57  args,
58  kw
59  );
60 
61  Py_DECREF(args);
62  Py_DECREF(kw);
63  if(!pNewObj)
64  {
65  if(PyErr_ExceptionMatches(modena_DoesNotExist))
66  {
67  PyErr_Clear();
68 
69  PyObject *pRet = PyObject_CallMethod
70  (
71  modena_IndexSet,
72  "exceptionLoad",
73  "(z)",
74  indexSetId
75  );
76  if(!pRet){ Modena_PyErr_Print(); }
77  int ret = PyInt_AsLong(pRet);
78  Py_DECREF(pRet);
79 
80  modena_error_code = ret;
81  return NULL;
82  }
83  else
84  {
85  Modena_PyErr_Print();
86  }
87  }
88 
89  return (modena_index_set_t *) pNewObj;
90 }
91 
92 size_t modena_index_set_get_index
93 (
94  const modena_index_set_t *self,
95  const char* name
96 )
97 {
98  PyObject *pRet = PyObject_CallMethod
99  (
100  self->pIndexSet,
101  "get_index",
102  "(z)",
103  name
104  );
105  if(!pRet){ Modena_PyErr_Print(); }
106  size_t ret = PyInt_AsSsize_t(pRet);
107  Py_DECREF(pRet);
108 
109  return ret;
110 }
111 
112 const char* modena_index_set_get_name
113 (
114  const modena_index_set_t *self,
115  const size_t index
116 )
117 {
118  PyObject *pRet = PyObject_CallMethod
119  (
120  self->pIndexSet,
121  "get_name",
122  "(i)",
123  index
124  );
125  if(!pRet){ Modena_PyErr_Print(); }
126  const char* ret = PyString_AsString(pRet);
127  Py_DECREF(pRet);
128 
129  return ret;
130 }
131 
132 size_t modena_index_set_iterator_start
133 (
134  const modena_index_set_t *self
135 )
136 {
137  return 0;
138 }
139 
140 size_t modena_index_set_iterator_end
141 (
142  const modena_index_set_t *self
143 )
144 {
145  PyObject *pRet = PyObject_CallMethod
146  (
147  self->pIndexSet,
148  "iterator_end",
149  "()"
150  );
151  if(!pRet){ Modena_PyErr_Print(); }
152  size_t ret = PyInt_AsSsize_t(pRet);
153  Py_DECREF(pRet);
154 
155  return ret;
156 }
157 
158 void modena_index_set_destroy(modena_index_set_t *self)
159 {
160  Py_XDECREF(self->pIndexSet);
161 
162  self->ob_type->tp_free((PyObject*)self);
163 }
164 
165 static void modena_index_set_t_dealloc(modena_index_set_t* self)
166 {
167  modena_index_set_destroy(self);
168 }
169 
170 static PyMemberDef modena_index_set_t_members[] = {
171  {NULL} /* Sentinel */
172 };
173 
174 static PyMethodDef modena_index_set_t_methods[] = {
175  {NULL} /* Sentinel */
176 };
177 
178 static int modena_index_set_t_init
179 (
180  modena_index_set_t *self,
181  PyObject *args,
182  PyObject *kwds
183 )
184 {
185  PyObject *pIndexSet=NULL;
186  char *indexSetId=NULL;
187 
188  static char *kwlist[] = {"indexSet", "indexSetId", NULL};
189 
190  if
191  (
192  !PyArg_ParseTupleAndKeywords
193  (
194  args,
195  kwds,
196  "|Os",
197  kwlist,
198  &pIndexSet,
199  &indexSetId
200  )
201  )
202  {
203  Modena_PyErr_Print();
204  }
205 
206  if(!pIndexSet)
207  {
208  self->pIndexSet = PyObject_CallMethod
209  (
210  modena_IndexSet,
211  "load",
212  "(z)",
213  indexSetId
214  );
215 
216  if(!self->pIndexSet)
217  {
218  PyErr_SetString(modena_DoesNotExist, "Index set does not exist");
219 
220  Modena_PyErr_Print();
221  }
222  }
223  else
224  {
225  Py_INCREF(pIndexSet);
226  self->pIndexSet = pIndexSet;
227  }
228 
229  return 0;
230 }
231 
232 static PyObject *modena_index_set_t_new
233 (
234  PyTypeObject *type,
235  PyObject *args,
236  PyObject *kwds
237 )
238 {
239  modena_index_set_t *self;
240 
241  self = (modena_index_set_t *)type->tp_alloc(type, 0);
242  if(self)
243  {
244  self->pIndexSet = NULL;
245  }
246 
247  return (PyObject *)self;
248 }
249 
250 PyTypeObject modena_index_set_tType = {
251  PyObject_HEAD_INIT(NULL)
252  0, /*ob_size*/
253  "modena.modena_index_set_t", /*tp_name*/
254  sizeof(modena_index_set_t), /*tp_basicsize*/
255  0, /*tp_itemsize*/
256  (destructor)modena_index_set_t_dealloc, /*tp_dealloc*/
257  0, /*tp_print*/
258  0, /*tp_getattr*/
259  0, /*tp_setattr*/
260  0, /*tp_compare*/
261  0, /*tp_repr*/
262  0, /*tp_as_number*/
263  0, /*tp_as_sequence*/
264  0, /*tp_as_mapping*/
265  0, /*tp_hash */
266  0, /*tp_call*/
267  0, /*tp_str*/
268  0, /*tp_getattro*/
269  0, /*tp_setattro*/
270  0, /*tp_as_buffer*/
271  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
272  "modena_index_set_t objects", /* tp_doc */
273  0, /* tp_traverse */
274  0, /* tp_clear */
275  0, /* tp_richcompare */
276  0, /* tp_weaklistoffset */
277  0, /* tp_iter */
278  0, /* tp_iternext */
279  modena_index_set_t_methods, /* tp_methods */
280  modena_index_set_t_members, /* tp_members */
281  0, /* tp_getset */
282  0, /* tp_base */
283  0, /* tp_dict */
284  0, /* tp_descr_get */
285  0, /* tp_descr_set */
286  0, /* tp_dictoffset */
287  (initproc)modena_index_set_t_init, /* tp_init */
288  0, /* tp_alloc */
289  modena_index_set_t_new, /* tp_new */
290 };
struct modena_index_set_t modena_index_set_t