Blender  V2.93
BPy_UnaryPredicate0D.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_UnaryPredicate0D.h"
22 
23 #include "BPy_Convert.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 using namespace Freestyle;
33 
35 
36 //-------------------MODULE INITIALIZATION--------------------------------
38 {
39  if (module == nullptr) {
40  return -1;
41  }
42 
43  if (PyType_Ready(&UnaryPredicate0D_Type) < 0) {
44  return -1;
45  }
46  Py_INCREF(&UnaryPredicate0D_Type);
47  PyModule_AddObject(module, "UnaryPredicate0D", (PyObject *)&UnaryPredicate0D_Type);
48 
49  if (PyType_Ready(&FalseUP0D_Type) < 0) {
50  return -1;
51  }
52  Py_INCREF(&FalseUP0D_Type);
53  PyModule_AddObject(module, "FalseUP0D", (PyObject *)&FalseUP0D_Type);
54 
55  if (PyType_Ready(&TrueUP0D_Type) < 0) {
56  return -1;
57  }
58  Py_INCREF(&TrueUP0D_Type);
59  PyModule_AddObject(module, "TrueUP0D", (PyObject *)&TrueUP0D_Type);
60 
61  return 0;
62 }
63 
64 //------------------------INSTANCE METHODS ----------------------------------
65 
66 static char UnaryPredicate0D___doc__[] =
67  "Base class for unary predicates that work on\n"
68  ":class:`Interface0DIterator`. A UnaryPredicate0D is a functor that\n"
69  "evaluates a condition on an Interface0DIterator and returns true or\n"
70  "false depending on whether this condition is satisfied or not. The\n"
71  "UnaryPredicate0D is used by invoking its __call__() method. Any\n"
72  "inherited class must overload the __call__() method.\n"
73  "\n"
74  ".. method:: __init__()\n"
75  "\n"
76  " Default constructor.\n"
77  "\n"
78  ".. method:: __call__(it)\n"
79  "\n"
80  " Must be overload by inherited classes.\n"
81  "\n"
82  " :arg it: The Interface0DIterator pointing onto the Interface0D at\n"
83  " which we wish to evaluate the predicate.\n"
84  " :type it: :class:`Interface0DIterator`\n"
85  " :return: True if the condition is satisfied, false otherwise.\n"
86  " :rtype: bool\n";
87 
88 static int UnaryPredicate0D___init__(BPy_UnaryPredicate0D *self, PyObject *args, PyObject *kwds)
89 {
90  static const char *kwlist[] = {nullptr};
91 
92  if (!PyArg_ParseTupleAndKeywords(args, kwds, "", (char **)kwlist)) {
93  return -1;
94  }
95  self->up0D = new UnaryPredicate0D();
96  self->up0D->py_up0D = (PyObject *)self;
97  return 0;
98 }
99 
101 {
102  delete self->up0D;
103  Py_TYPE(self)->tp_free((PyObject *)self);
104 }
105 
107 {
108  return PyUnicode_FromFormat("type: %s - address: %p", Py_TYPE(self)->tp_name, self->up0D);
109 }
110 
112  PyObject *args,
113  PyObject *kwds)
114 {
115  static const char *kwlist[] = {"it", nullptr};
116  PyObject *py_if0D_it;
117 
118  if (!PyArg_ParseTupleAndKeywords(
119  args, kwds, "O!", (char **)kwlist, &Interface0DIterator_Type, &py_if0D_it)) {
120  return nullptr;
121  }
122 
123  Interface0DIterator *if0D_it = ((BPy_Interface0DIterator *)py_if0D_it)->if0D_it;
124 
125  if (!if0D_it) {
126  string class_name(Py_TYPE(self)->tp_name);
127  PyErr_SetString(PyExc_RuntimeError, (class_name + " has no Interface0DIterator").c_str());
128  return nullptr;
129  }
130  if (typeid(*(self->up0D)) == typeid(UnaryPredicate0D)) {
131  PyErr_SetString(PyExc_TypeError, "__call__ method not properly overridden");
132  return nullptr;
133  }
134  if (self->up0D->operator()(*if0D_it) < 0) {
135  if (!PyErr_Occurred()) {
136  string class_name(Py_TYPE(self)->tp_name);
137  PyErr_SetString(PyExc_RuntimeError, (class_name + " __call__ method failed").c_str());
138  }
139  return nullptr;
140  }
141  return PyBool_from_bool(self->up0D->result);
142 }
143 
144 /*----------------------UnaryPredicate0D get/setters ----------------------------*/
145 
146 PyDoc_STRVAR(UnaryPredicate0D_name_doc,
147  "The name of the unary 0D predicate.\n"
148  "\n"
149  ":type: str");
150 
151 static PyObject *UnaryPredicate0D_name_get(BPy_UnaryPredicate0D *self, void *UNUSED(closure))
152 {
153  return PyUnicode_FromString(Py_TYPE(self)->tp_name);
154 }
155 
156 static PyGetSetDef BPy_UnaryPredicate0D_getseters[] = {
157  {"name",
159  (setter) nullptr,
160  UnaryPredicate0D_name_doc,
161  nullptr},
162  {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
163 };
164 
165 /*-----------------------BPy_UnaryPredicate0D type definition ------------------------------*/
166 
167 PyTypeObject UnaryPredicate0D_Type = {
168  PyVarObject_HEAD_INIT(nullptr, 0) "UnaryPredicate0D", /* tp_name */
169  sizeof(BPy_UnaryPredicate0D), /* tp_basicsize */
170  0, /* tp_itemsize */
171  (destructor)UnaryPredicate0D___dealloc__, /* tp_dealloc */
172  0, /* tp_vectorcall_offset */
173  nullptr, /* tp_getattr */
174  nullptr, /* tp_setattr */
175  nullptr, /* tp_reserved */
176  (reprfunc)UnaryPredicate0D___repr__, /* tp_repr */
177  nullptr, /* tp_as_number */
178  nullptr, /* tp_as_sequence */
179  nullptr, /* tp_as_mapping */
180  nullptr, /* tp_hash */
181  (ternaryfunc)UnaryPredicate0D___call__, /* tp_call */
182  nullptr, /* tp_str */
183  nullptr, /* tp_getattro */
184  nullptr, /* tp_setattro */
185  nullptr, /* tp_as_buffer */
186  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
187  UnaryPredicate0D___doc__, /* tp_doc */
188  nullptr, /* tp_traverse */
189  nullptr, /* tp_clear */
190  nullptr, /* tp_richcompare */
191  0, /* tp_weaklistoffset */
192  nullptr, /* tp_iter */
193  nullptr, /* tp_iternext */
194  nullptr, /* tp_methods */
195  nullptr, /* tp_members */
196  BPy_UnaryPredicate0D_getseters, /* tp_getset */
197  nullptr, /* tp_base */
198  nullptr, /* tp_dict */
199  nullptr, /* tp_descr_get */
200  nullptr, /* tp_descr_set */
201  0, /* tp_dictoffset */
202  (initproc)UnaryPredicate0D___init__, /* tp_init */
203  nullptr, /* tp_alloc */
204  PyType_GenericNew, /* tp_new */
205 };
206 
208 
209 #ifdef __cplusplus
210 }
211 #endif
#define UNUSED(x)
PyObject * PyBool_from_bool(bool b)
Definition: BPy_Convert.cpp:73
PyTypeObject FalseUP0D_Type
PyTypeObject Interface0DIterator_Type
PyTypeObject TrueUP0D_Type
int UnaryPredicate0D_Init(PyObject *module)
static char UnaryPredicate0D___doc__[]
PyTypeObject UnaryPredicate0D_Type
PyDoc_STRVAR(UnaryPredicate0D_name_doc, "The name of the unary 0D predicate.\n" "\n" ":type: str")
static PyObject * UnaryPredicate0D___repr__(BPy_UnaryPredicate0D *self)
static void UnaryPredicate0D___dealloc__(BPy_UnaryPredicate0D *self)
static PyObject * UnaryPredicate0D_name_get(BPy_UnaryPredicate0D *self, void *UNUSED(closure))
static int UnaryPredicate0D___init__(BPy_UnaryPredicate0D *self, PyObject *args, PyObject *kwds)
static PyObject * UnaryPredicate0D___call__(BPy_UnaryPredicate0D *self, PyObject *args, PyObject *kwds)
static PyGetSetDef BPy_UnaryPredicate0D_getseters[]
static struct PyModuleDef module
PyObject * self
Definition: bpy_driver.c:185
inherits from class Rep
Definition: AppCanvas.cpp:32