Blender  V2.93
BPy_Iterator.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include "BPy_Iterator.h"
22 
23 #include "BPy_Convert.h"
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 using namespace Freestyle;
40 
42 
43 //-------------------MODULE INITIALIZATION--------------------------------
44 int Iterator_Init(PyObject *module)
45 {
46  if (module == nullptr) {
47  return -1;
48  }
49 
50  if (PyType_Ready(&Iterator_Type) < 0) {
51  return -1;
52  }
53  Py_INCREF(&Iterator_Type);
54  PyModule_AddObject(module, "Iterator", (PyObject *)&Iterator_Type);
55 
56  if (PyType_Ready(&AdjacencyIterator_Type) < 0) {
57  return -1;
58  }
59  Py_INCREF(&AdjacencyIterator_Type);
60  PyModule_AddObject(module, "AdjacencyIterator", (PyObject *)&AdjacencyIterator_Type);
61 
62  if (PyType_Ready(&Interface0DIterator_Type) < 0) {
63  return -1;
64  }
65  Py_INCREF(&Interface0DIterator_Type);
66  PyModule_AddObject(module, "Interface0DIterator", (PyObject *)&Interface0DIterator_Type);
67 
68  if (PyType_Ready(&CurvePointIterator_Type) < 0) {
69  return -1;
70  }
71  Py_INCREF(&CurvePointIterator_Type);
72  PyModule_AddObject(module, "CurvePointIterator", (PyObject *)&CurvePointIterator_Type);
73 
74  if (PyType_Ready(&StrokeVertexIterator_Type) < 0) {
75  return -1;
76  }
77  Py_INCREF(&StrokeVertexIterator_Type);
78  PyModule_AddObject(module, "StrokeVertexIterator", (PyObject *)&StrokeVertexIterator_Type);
79 
80  if (PyType_Ready(&SVertexIterator_Type) < 0) {
81  return -1;
82  }
83  Py_INCREF(&SVertexIterator_Type);
84  PyModule_AddObject(module, "SVertexIterator", (PyObject *)&SVertexIterator_Type);
85 
86  if (PyType_Ready(&orientedViewEdgeIterator_Type) < 0) {
87  return -1;
88  }
90  PyModule_AddObject(
91  module, "orientedViewEdgeIterator", (PyObject *)&orientedViewEdgeIterator_Type);
92 
93  if (PyType_Ready(&ViewEdgeIterator_Type) < 0) {
94  return -1;
95  }
96  Py_INCREF(&ViewEdgeIterator_Type);
97  PyModule_AddObject(module, "ViewEdgeIterator", (PyObject *)&ViewEdgeIterator_Type);
98 
99  if (PyType_Ready(&ChainingIterator_Type) < 0) {
100  return -1;
101  }
102  Py_INCREF(&ChainingIterator_Type);
103  PyModule_AddObject(module, "ChainingIterator", (PyObject *)&ChainingIterator_Type);
104 
105  if (PyType_Ready(&ChainPredicateIterator_Type) < 0) {
106  return -1;
107  }
108  Py_INCREF(&ChainPredicateIterator_Type);
109  PyModule_AddObject(module, "ChainPredicateIterator", (PyObject *)&ChainPredicateIterator_Type);
110 
111  if (PyType_Ready(&ChainSilhouetteIterator_Type) < 0) {
112  return -1;
113  }
114  Py_INCREF(&ChainSilhouetteIterator_Type);
115  PyModule_AddObject(module, "ChainSilhouetteIterator", (PyObject *)&ChainSilhouetteIterator_Type);
116 
117  return 0;
118 }
119 
120 //------------------------INSTANCE METHODS ----------------------------------
121 
122 PyDoc_STRVAR(Iterator_doc,
123  "Base class to define iterators.\n"
124  "\n"
125  ".. method:: __init__()\n"
126  "\n"
127  " Default constructor.");
128 
129 static int Iterator_init(BPy_Iterator *self, PyObject *args, PyObject *kwds)
130 {
131  static const char *kwlist[] = {nullptr};
132 
133  if (!PyArg_ParseTupleAndKeywords(args, kwds, "", (char **)kwlist)) {
134  return -1;
135  }
136  self->it = new Iterator();
137  return 0;
138 }
139 
140 static void Iterator_dealloc(BPy_Iterator *self)
141 {
142  delete self->it;
143  Py_TYPE(self)->tp_free((PyObject *)self);
144 }
145 
146 static PyObject *Iterator_repr(BPy_Iterator *self)
147 {
148  return PyUnicode_FromFormat("type: %s - address: %p", Py_TYPE(self)->tp_name, self->it);
149 }
150 
151 PyDoc_STRVAR(Iterator_increment_doc,
152  ".. method:: increment()\n"
153  "\n"
154  " Makes the iterator point the next element.");
155 
156 static PyObject *Iterator_increment(BPy_Iterator *self)
157 {
158  if (self->it->isEnd()) {
159  PyErr_SetString(PyExc_RuntimeError, "cannot increment any more");
160  return nullptr;
161  }
162  self->it->increment();
163  Py_RETURN_NONE;
164 }
165 
166 PyDoc_STRVAR(Iterator_decrement_doc,
167  ".. method:: decrement()\n"
168  "\n"
169  " Makes the iterator point the previous element.");
170 
171 static PyObject *Iterator_decrement(BPy_Iterator *self)
172 {
173  if (self->it->isBegin()) {
174  PyErr_SetString(PyExc_RuntimeError, "cannot decrement any more");
175  return nullptr;
176  }
177  self->it->decrement();
178  Py_RETURN_NONE;
179 }
180 
181 static PyMethodDef BPy_Iterator_methods[] = {
182  {"increment", (PyCFunction)Iterator_increment, METH_NOARGS, Iterator_increment_doc},
183  {"decrement", (PyCFunction)Iterator_decrement, METH_NOARGS, Iterator_decrement_doc},
184  {nullptr, nullptr, 0, nullptr},
185 };
186 
187 /*----------------------Iterator get/setters ----------------------------*/
188 
189 PyDoc_STRVAR(Iterator_name_doc,
190  "The string of the name of this iterator.\n"
191  "\n"
192  ":type: str");
193 
194 static PyObject *Iterator_name_get(BPy_Iterator *self, void *UNUSED(closure))
195 {
196  return PyUnicode_FromString(Py_TYPE(self)->tp_name);
197 }
198 
199 PyDoc_STRVAR(Iterator_is_begin_doc,
200  "True if the iterator points to the first element.\n"
201  "\n"
202  ":type: bool");
203 
204 static PyObject *Iterator_is_begin_get(BPy_Iterator *self, void *UNUSED(closure))
205 {
206  return PyBool_from_bool(self->it->isBegin());
207 }
208 
209 PyDoc_STRVAR(Iterator_is_end_doc,
210  "True if the iterator points to the last element.\n"
211  "\n"
212  ":type: bool");
213 
214 static PyObject *Iterator_is_end_get(BPy_Iterator *self, void *UNUSED(closure))
215 {
216  return PyBool_from_bool(self->it->isEnd());
217 }
218 
219 static PyGetSetDef BPy_Iterator_getseters[] = {
220  {"name", (getter)Iterator_name_get, (setter) nullptr, Iterator_name_doc, nullptr},
221  {"is_begin", (getter)Iterator_is_begin_get, (setter) nullptr, Iterator_is_begin_doc, nullptr},
222  {"is_end", (getter)Iterator_is_end_get, (setter) nullptr, Iterator_is_end_doc, nullptr},
223  {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
224 };
225 
226 /*-----------------------BPy_Iterator type definition ------------------------------*/
227 
228 PyTypeObject Iterator_Type = {
229  PyVarObject_HEAD_INIT(nullptr, 0) "Iterator", /* tp_name */
230  sizeof(BPy_Iterator), /* tp_basicsize */
231  0, /* tp_itemsize */
232  (destructor)Iterator_dealloc, /* tp_dealloc */
233  0, /* tp_vectorcall_offset */
234  nullptr, /* tp_getattr */
235  nullptr, /* tp_setattr */
236  nullptr, /* tp_reserved */
237  (reprfunc)Iterator_repr, /* tp_repr */
238  nullptr, /* tp_as_number */
239  nullptr, /* tp_as_sequence */
240  nullptr, /* tp_as_mapping */
241  nullptr, /* tp_hash */
242  nullptr, /* tp_call */
243  nullptr, /* tp_str */
244  nullptr, /* tp_getattro */
245  nullptr, /* tp_setattro */
246  nullptr, /* tp_as_buffer */
247  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
248  Iterator_doc, /* tp_doc */
249  nullptr, /* tp_traverse */
250  nullptr, /* tp_clear */
251  nullptr, /* tp_richcompare */
252  0, /* tp_weaklistoffset */
253  nullptr, /* tp_iter */
254  nullptr, /* tp_iternext */
255  BPy_Iterator_methods, /* tp_methods */
256  nullptr, /* tp_members */
257  BPy_Iterator_getseters, /* tp_getset */
258  nullptr, /* tp_base */
259  nullptr, /* tp_dict */
260  nullptr, /* tp_descr_get */
261  nullptr, /* tp_descr_set */
262  0, /* tp_dictoffset */
263  (initproc)Iterator_init, /* tp_init */
264  nullptr, /* tp_alloc */
265  PyType_GenericNew, /* tp_new */
266 };
267 
269 
270 #ifdef __cplusplus
271 }
272 #endif
#define UNUSED(x)
PyTypeObject AdjacencyIterator_Type
PyTypeObject ChainPredicateIterator_Type
PyTypeObject ChainSilhouetteIterator_Type
PyTypeObject ChainingIterator_Type
PyObject * PyBool_from_bool(bool b)
Definition: BPy_Convert.cpp:73
PyTypeObject CurvePointIterator_Type
PyTypeObject Interface0DIterator_Type
static PyObject * Iterator_name_get(BPy_Iterator *self, void *UNUSED(closure))
static PyObject * Iterator_decrement(BPy_Iterator *self)
static PyGetSetDef BPy_Iterator_getseters[]
static PyObject * Iterator_repr(BPy_Iterator *self)
static PyObject * Iterator_is_end_get(BPy_Iterator *self, void *UNUSED(closure))
static PyMethodDef BPy_Iterator_methods[]
static void Iterator_dealloc(BPy_Iterator *self)
static PyObject * Iterator_is_begin_get(BPy_Iterator *self, void *UNUSED(closure))
static PyObject * Iterator_increment(BPy_Iterator *self)
static int Iterator_init(BPy_Iterator *self, PyObject *args, PyObject *kwds)
PyTypeObject Iterator_Type
int Iterator_Init(PyObject *module)
PyDoc_STRVAR(Iterator_doc, "Base class to define iterators.\n" "\n" ".. method:: __init__()\n" "\n" " Default constructor.")
PyTypeObject SVertexIterator_Type
PyTypeObject StrokeVertexIterator_Type
PyTypeObject ViewEdgeIterator_Type
PyTypeObject orientedViewEdgeIterator_Type
static struct PyModuleDef module
PyObject * self
Definition: bpy_driver.c:185
inherits from class Rep
Definition: AppCanvas.cpp:32