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