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