Fix for copy-and-paste bugs in Id rich comparison. Also made code style clean-up.
[blender.git] / source / blender / freestyle / intern / python / BPy_Id.cpp
1 #include "BPy_Id.h"
2
3 #include "BPy_Convert.h"
4
5 #ifdef __cplusplus
6 extern "C" {
7 #endif
8
9 ///////////////////////////////////////////////////////////////////////////////////////////
10
11 //-------------------MODULE INITIALIZATION--------------------------------
12 int Id_Init(PyObject *module)
13 {
14         if (module == NULL)
15                 return -1;
16
17         if (PyType_Ready(&Id_Type) < 0)
18                 return -1;
19
20         Py_INCREF(&Id_Type);
21         PyModule_AddObject(module, "Id", (PyObject *)&Id_Type);
22         return 0;
23 }
24
25 //------------------------INSTANCE METHODS ----------------------------------
26
27 PyDoc_STRVAR(Id_doc,
28 ".. method:: __init__()\n"
29 "\n"
30 "   Default constructor.\n"
31 "\n"
32 ".. method:: __init__(iBrother)\n"
33 "\n"
34 "   Copy constructor.\n"
35 "\n"
36 "   :arg iBrother: An Id object.\n"
37 "   :type iBrother: :class:`Id`\n"
38 "\n"
39 ".. method:: __init__(iFirst)\n"
40 "\n"
41 "   Builds an Id from an integer. The second number is set to 0.\n"
42 "\n"
43 "   :arg iFirst: The first Id number.\n"
44 "   :type iFirst: int\n"
45 "\n"
46 ".. method:: __init__(iFirst, iSecond)\n"
47 "\n"
48 "   Builds the Id from the two numbers.\n"
49 "\n"
50 "   :arg iFirst: The first Id number.\n"
51 "   :type iFirst: int\n"
52 "   :arg iSecond: The second Id number.\n"
53 "   :type iSecond: int\n");
54
55 static int Id_init(BPy_Id *self, PyObject *args, PyObject *kwds)
56 {
57         int first = 0, second = 0;
58         static const char *kwlist[] = {"first", "second", NULL};
59
60         if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ii", (char**)kwlist, &first, &second))
61                 return -1;
62
63         self->id = new Id(first, second);
64
65         return 0;
66 }
67
68 static void Id_dealloc(BPy_Id* self)
69 {
70         delete self->id;
71         Py_TYPE(self)->tp_free((PyObject*)self);
72 }
73
74 static PyObject * Id_repr(BPy_Id* self)
75 {
76         return PyUnicode_FromFormat("[ first: %i, second: %i ](BPy_Id)", self->id->getFirst(), self->id->getSecond());
77 }
78
79 static PyObject * Id_RichCompare(BPy_Id *o1, BPy_Id *o2, int opid)
80 {
81         switch(opid){
82                 case Py_LT:
83                         return PyBool_from_bool(o1->id->operator<(*(o2->id)));
84                         break;
85                 case Py_LE:
86                         return PyBool_from_bool(o1->id->operator<(*(o2->id)) || o1->id->operator==(*(o2->id)));
87                         break;
88                 case Py_EQ:
89                         return PyBool_from_bool(o1->id->operator==(*(o2->id)));
90                         break;
91                 case Py_NE:
92                         return PyBool_from_bool(o1->id->operator!=(*(o2->id)));
93                         break;
94                 case Py_GT:
95                         return PyBool_from_bool(!(o1->id->operator<(*(o2->id)) || o1->id->operator==(*(o2->id))));
96                         break;
97                 case Py_GE:
98                         return PyBool_from_bool(!(o1->id->operator<(*(o2->id))));
99                         break;
100         }
101         
102         Py_RETURN_NONE;
103 }
104
105 static PyMethodDef BPy_Id_methods[] = {
106         {NULL, NULL, 0, NULL}
107 };
108
109 /*----------------------Id get/setters ----------------------------*/
110
111 PyDoc_STRVAR(Id_first_doc,
112 "The first number constituting the Id.\n"
113 "\n"
114 ":type: int");
115
116 static PyObject *Id_first_get(BPy_Id *self, void *UNUSED(closure))
117 {
118         return PyLong_FromLong(self->id->getFirst());
119 }
120
121 static int Id_first_set(BPy_Id *self, PyObject *value, void *UNUSED(closure))
122 {
123         int scalar;
124         if ((scalar = PyLong_AsLong(value)) == -1 && PyErr_Occurred()) {
125                 PyErr_SetString(PyExc_TypeError, "value must be an integer");
126                 return -1;
127         }
128         self->id->setFirst(scalar);
129         return 0;
130 }
131
132 PyDoc_STRVAR(Id_second_doc,
133 "The second number constituting the Id.\n"
134 "\n"
135 ":type: int");
136
137 static PyObject *Id_second_get(BPy_Id *self, void *UNUSED(closure))
138 {
139         return PyLong_FromLong(self->id->getSecond());
140 }
141
142 static int Id_second_set(BPy_Id *self, PyObject *value, void *UNUSED(closure))
143 {
144         int scalar;
145         if ((scalar = PyLong_AsLong(value)) == -1 && PyErr_Occurred()) {
146                 PyErr_SetString(PyExc_TypeError, "value must be an integer");
147                 return -1;
148         }
149         self->id->setSecond(scalar);
150         return 0;
151 }
152
153 static PyGetSetDef BPy_Id_getseters[] = {
154         {(char *)"first", (getter)Id_first_get, (setter)Id_first_set, (char *)Id_first_doc, NULL},
155         {(char *)"second", (getter)Id_second_get, (setter)Id_second_set, (char *)Id_second_doc, NULL},
156         {NULL, NULL, NULL, NULL, NULL}  /* Sentinel */
157 };
158
159 /*-----------------------BPy_Id type definition ------------------------------*/
160
161 PyTypeObject Id_Type = {
162         PyVarObject_HEAD_INIT(NULL, 0)
163         "Id",                           /* tp_name */
164         sizeof(BPy_Id),                 /* tp_basicsize */
165         0,                              /* tp_itemsize */
166         (destructor)Id_dealloc,         /* tp_dealloc */
167         0,                              /* tp_print */
168         0,                              /* tp_getattr */
169         0,                              /* tp_setattr */
170         0,                              /* tp_reserved */
171         (reprfunc)Id_repr,              /* tp_repr */
172         0,                              /* tp_as_number */
173         0,                              /* tp_as_sequence */
174         0,                              /* tp_as_mapping */
175         0,                              /* tp_hash  */
176         0,                              /* tp_call */
177         0,                              /* tp_str */
178         0,                              /* tp_getattro */
179         0,                              /* tp_setattro */
180         0,                              /* tp_as_buffer */
181         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
182         Id_doc,                         /* tp_doc */
183         0,                              /* tp_traverse */
184         0,                              /* tp_clear */
185         (richcmpfunc)Id_RichCompare,    /* tp_richcompare */
186         0,                              /* tp_weaklistoffset */
187         0,                              /* tp_iter */
188         0,                              /* tp_iternext */
189         BPy_Id_methods,                 /* tp_methods */
190         0,                              /* tp_members */
191         BPy_Id_getseters,               /* tp_getset */
192         0,                              /* tp_base */
193         0,                              /* tp_dict */
194         0,                              /* tp_descr_get */
195         0,                              /* tp_descr_set */
196         0,                              /* tp_dictoffset */
197         (initproc)Id_init,              /* tp_init */
198         0,                              /* tp_alloc */
199         PyType_GenericNew,              /* tp_new */
200 };
201
202 ///////////////////////////////////////////////////////////////////////////////////////////
203
204 #ifdef __cplusplus
205 }
206 #endif