9254badd95d132a48dfda8fc9e83f29887e8b557
[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 static char 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 char *kwlist[] = {"first", "second", NULL};
59
60     if (! PyArg_ParseTupleAndKeywords(args, kwds, "|ii", 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 char Id_getFirst___doc__[] =
80 ".. method:: getFirst()\n"
81 "\n"
82 "   Returns the first Id number.\n"
83 "\n"
84 "   :return: The first Id number.\n"
85 "   :rtype: int\n";
86
87 static PyObject *Id_getFirst( BPy_Id *self ) {
88         return PyLong_FromLong( self->id->getFirst() );
89 }
90
91 static char Id_getSecond___doc__[] =
92 ".. method:: getSecond()\n"
93 "\n"
94 "   Returns the second Id number.\n"
95 "\n"
96 "   :return: The second Id number.\n"
97 "   :rtype: int\n";
98
99 static PyObject *Id_getSecond( BPy_Id *self) {
100         return PyLong_FromLong( self->id->getSecond() );
101 }
102
103 static char Id_setFirst___doc__[] =
104 ".. method:: setFirst(iFirst)\n"
105 "\n"
106 "   Sets the first number constituting the Id.\n"
107 "\n"
108 "   :arg iFirst: The first number constituting the Id.\n"
109 "   :type iFirst: int\n";
110
111 static PyObject *Id_setFirst( BPy_Id *self , PyObject *args) {
112         unsigned int i;
113
114         if( !PyArg_ParseTuple(args, "i", &i) )
115                 return NULL;
116
117         self->id->setFirst( i );
118
119         Py_RETURN_NONE;
120 }
121
122 static char Id_setSecond___doc__[] =
123 ".. method:: setSecond(iSecond)\n"
124 "\n"
125 "   Sets the second number constituting the Id.\n"
126 "\n"
127 "   :arg iSecond: The second number constituting the Id.\n"
128 "   :type iSecond: int\n";
129
130 static PyObject *Id_setSecond( BPy_Id *self , PyObject *args) {
131         unsigned int i;
132
133         if( !PyArg_ParseTuple(args, "i", &i) )
134                 return NULL;
135
136         self->id->setSecond( i );
137
138         Py_RETURN_NONE;
139 }
140
141 static PyObject * Id_RichCompare(BPy_Id *o1, BPy_Id *o2, int opid) {
142         switch(opid){
143                 case Py_LT:
144                         return PyBool_from_bool( o1->id->operator<(*(o2->id)) );
145                         break;
146                 case Py_LE:
147                         return PyBool_from_bool( o1->id->operator<(*(o2->id)) || o1->id->operator<(*(o2->id)) );
148                         break;
149                 case Py_EQ:
150                         return PyBool_from_bool( o1->id->operator==(*(o2->id)) );
151                         break;
152                 case Py_NE:
153                         return PyBool_from_bool( o1->id->operator!=(*(o2->id)) );
154                         break;
155                 case Py_GT:
156                         return PyBool_from_bool(!( o1->id->operator<(*(o2->id)) || o1->id->operator<(*(o2->id)) ));
157                         break;
158                 case Py_GE:
159                         return PyBool_from_bool(!( o1->id->operator<(*(o2->id)) ));
160                         break;
161         }
162         
163         Py_RETURN_NONE;
164 }
165
166 /*----------------------Id instance definitions ----------------------------*/
167 static PyMethodDef BPy_Id_methods[] = {
168         {"getFirst", ( PyCFunction ) Id_getFirst, METH_NOARGS, Id_getFirst___doc__},
169         {"getSecond", ( PyCFunction ) Id_getSecond, METH_NOARGS, Id_getSecond___doc__},
170         {"setFirst", ( PyCFunction ) Id_setFirst, METH_VARARGS, Id_setFirst___doc__},
171         {"setSecond", ( PyCFunction ) Id_setSecond, METH_VARARGS, Id_setSecond___doc__},
172         {NULL, NULL, 0, NULL}
173 };
174
175 /*-----------------------BPy_Id type definition ------------------------------*/
176
177 PyTypeObject Id_Type = {
178         PyVarObject_HEAD_INIT(NULL, 0)
179         "Id",                           /* tp_name */
180         sizeof(BPy_Id),                 /* tp_basicsize */
181         0,                              /* tp_itemsize */
182         (destructor)Id___dealloc__,     /* tp_dealloc */
183         0,                              /* tp_print */
184         0,                              /* tp_getattr */
185         0,                              /* tp_setattr */
186         0,                              /* tp_reserved */
187         (reprfunc)Id___repr__,          /* tp_repr */
188         0,                              /* tp_as_number */
189         0,                              /* tp_as_sequence */
190         0,                              /* tp_as_mapping */
191         0,                              /* tp_hash  */
192         0,                              /* tp_call */
193         0,                              /* tp_str */
194         0,                              /* tp_getattro */
195         0,                              /* tp_setattro */
196         0,                              /* tp_as_buffer */
197         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
198         Id___doc__,                     /* tp_doc */
199         0,                              /* tp_traverse */
200         0,                              /* tp_clear */
201         (richcmpfunc)Id_RichCompare,    /* tp_richcompare */
202         0,                              /* tp_weaklistoffset */
203         0,                              /* tp_iter */
204         0,                              /* tp_iternext */
205         BPy_Id_methods,                 /* tp_methods */
206         0,                              /* tp_members */
207         0,                              /* tp_getset */
208         0,                              /* tp_base */
209         0,                              /* tp_dict */
210         0,                              /* tp_descr_get */
211         0,                              /* tp_descr_set */
212         0,                              /* tp_dictoffset */
213         (initproc)Id___init__,          /* tp_init */
214         0,                              /* tp_alloc */
215         PyType_GenericNew,              /* tp_new */
216 };
217
218 ///////////////////////////////////////////////////////////////////////////////////////////
219
220 #ifdef __cplusplus
221 }
222 #endif