Made the Freestyle Python API compatible with Python 3.
[blender.git] / source / blender / freestyle / intern / python / BPy_UnaryPredicate0D.cpp
1 #include "BPy_UnaryPredicate0D.h"
2
3 #include "BPy_Convert.h"
4 #include "Iterator/BPy_Interface0DIterator.h"
5 #include "UnaryPredicate0D/BPy_FalseUP0D.h"
6 #include "UnaryPredicate0D/BPy_TrueUP0D.h"
7
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11
12 ///////////////////////////////////////////////////////////////////////////////////////////
13
14 /*---------------  Python API function prototypes for UnaryPredicate0D instance  -----------*/
15 static int UnaryPredicate0D___init__(BPy_UnaryPredicate0D *self, PyObject *args, PyObject *kwds);
16 static void UnaryPredicate0D___dealloc__(BPy_UnaryPredicate0D *self);
17 static PyObject * UnaryPredicate0D___repr__(BPy_UnaryPredicate0D *self);
18 static PyObject * UnaryPredicate0D___call__( BPy_UnaryPredicate0D *self, PyObject *args, PyObject *kwds);
19
20 static PyObject * UnaryPredicate0D_getName( BPy_UnaryPredicate0D *self );
21
22 /*----------------------UnaryPredicate0D instance definitions ----------------------------*/
23 static PyMethodDef BPy_UnaryPredicate0D_methods[] = {
24         {"getName", ( PyCFunction ) UnaryPredicate0D_getName, METH_NOARGS, "Returns the string of the name of the UnaryPredicate0D."},
25         {NULL, NULL, 0, NULL}
26 };
27
28 /*-----------------------BPy_UnaryPredicate0D type definition ------------------------------*/
29
30 PyTypeObject UnaryPredicate0D_Type = {
31         PyVarObject_HEAD_INIT(NULL, 0)
32         "UnaryPredicate0D",             /* tp_name */
33         sizeof(BPy_UnaryPredicate0D),   /* tp_basicsize */
34         0,                              /* tp_itemsize */
35         (destructor)UnaryPredicate0D___dealloc__, /* tp_dealloc */
36         0,                              /* tp_print */
37         0,                              /* tp_getattr */
38         0,                              /* tp_setattr */
39         0,                              /* tp_reserved */
40         (reprfunc)UnaryPredicate0D___repr__, /* tp_repr */
41         0,                              /* tp_as_number */
42         0,                              /* tp_as_sequence */
43         0,                              /* tp_as_mapping */
44         0,                              /* tp_hash  */
45         (ternaryfunc)UnaryPredicate0D___call__, /* tp_call */
46         0,                              /* tp_str */
47         0,                              /* tp_getattro */
48         0,                              /* tp_setattro */
49         0,                              /* tp_as_buffer */
50         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
51         "UnaryPredicate0D objects",      /* tp_doc */
52         0,                              /* tp_traverse */
53         0,                              /* tp_clear */
54         0,                              /* tp_richcompare */
55         0,                              /* tp_weaklistoffset */
56         0,                              /* tp_iter */
57         0,                              /* tp_iternext */
58         BPy_UnaryPredicate0D_methods,   /* tp_methods */
59         0,                              /* tp_members */
60         0,                              /* tp_getset */
61         0,                              /* tp_base */
62         0,                              /* tp_dict */
63         0,                              /* tp_descr_get */
64         0,                              /* tp_descr_set */
65         0,                              /* tp_dictoffset */
66         (initproc)UnaryPredicate0D___init__, /* tp_init */
67         0,                              /* tp_alloc */
68         PyType_GenericNew,              /* tp_new */
69 };
70
71 //-------------------MODULE INITIALIZATION--------------------------------
72 int UnaryPredicate0D_Init( PyObject *module )
73 {
74         if( module == NULL )
75                 return -1;
76
77         if( PyType_Ready( &UnaryPredicate0D_Type ) < 0 )
78                 return -1;
79         Py_INCREF( &UnaryPredicate0D_Type );
80         PyModule_AddObject(module, "UnaryPredicate0D", (PyObject *)&UnaryPredicate0D_Type);
81         
82         if( PyType_Ready( &FalseUP0D_Type ) < 0 )
83                 return -1;
84         Py_INCREF( &FalseUP0D_Type );
85         PyModule_AddObject(module, "FalseUP0D", (PyObject *)&FalseUP0D_Type);
86         
87         if( PyType_Ready( &TrueUP0D_Type ) < 0 )
88                 return -1;
89         Py_INCREF( &TrueUP0D_Type );
90         PyModule_AddObject(module, "TrueUP0D", (PyObject *)&TrueUP0D_Type);
91         
92         return 0;
93 }
94
95 //------------------------INSTANCE METHODS ----------------------------------
96
97 int UnaryPredicate0D___init__(BPy_UnaryPredicate0D *self, PyObject *args, PyObject *kwds)
98 {
99     if ( !PyArg_ParseTuple(args, "") )
100         return -1;
101         self->up0D = new UnaryPredicate0D();
102         self->up0D->py_up0D = (PyObject *) self;
103         return 0;
104 }
105
106 void UnaryPredicate0D___dealloc__(BPy_UnaryPredicate0D* self)
107 {
108         if (self->up0D)
109                 delete self->up0D;
110     Py_TYPE(self)->tp_free((PyObject*)self);
111 }
112
113
114 PyObject * UnaryPredicate0D___repr__(BPy_UnaryPredicate0D* self)
115 {
116     return PyUnicode_FromFormat("type: %s - address: %p", self->up0D->getName().c_str(), self->up0D );
117 }
118
119
120 PyObject * UnaryPredicate0D_getName( BPy_UnaryPredicate0D *self )
121 {
122         return PyUnicode_FromFormat( self->up0D->getName().c_str() );
123 }
124
125 PyObject * UnaryPredicate0D___call__( BPy_UnaryPredicate0D *self, PyObject *args, PyObject *kwds)
126 {
127         PyObject *py_if0D_it;
128
129         if( kwds != NULL ) {
130                 PyErr_SetString(PyExc_TypeError, "keyword argument(s) not supported");
131                 return NULL;
132         }
133         if( !PyArg_ParseTuple(args, "O!", &Interface0DIterator_Type, &py_if0D_it) )
134                 return NULL;
135
136         Interface0DIterator *if0D_it = ((BPy_Interface0DIterator *) py_if0D_it)->if0D_it;
137
138         if( !if0D_it ) {
139                 string msg(self->up0D->getName() + " has no Interface0DIterator");
140                 PyErr_SetString(PyExc_RuntimeError, msg.c_str());
141                 return NULL;
142         }
143         if( typeid(*(self->up0D)) == typeid(UnaryPredicate0D) ) {
144                 PyErr_SetString(PyExc_TypeError, "__call__ method not properly overridden");
145                 return NULL;
146         }
147         if (self->up0D->operator()(*if0D_it) < 0) {
148                 if (!PyErr_Occurred()) {
149                         string msg(self->up0D->getName() + " __call__ method failed");
150                         PyErr_SetString(PyExc_RuntimeError, msg.c_str());
151                 }
152                 return NULL;
153         }
154         return PyBool_from_bool( self->up0D->result );
155 }
156
157
158 ///////////////////////////////////////////////////////////////////////////////////////////
159
160 #ifdef __cplusplus
161 }
162 #endif
163
164
165