54942abb58da9bf0810245c79a197d904848e1ee
[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 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         switch(opid){
81                 case Py_LT:
82                         return PyBool_from_bool( o1->id->operator<(*(o2->id)) );
83                         break;
84                 case Py_LE:
85                         return PyBool_from_bool( o1->id->operator<(*(o2->id)) || o1->id->operator<(*(o2->id)) );
86                         break;
87                 case Py_EQ:
88                         return PyBool_from_bool( o1->id->operator==(*(o2->id)) );
89                         break;
90                 case Py_NE:
91                         return PyBool_from_bool( o1->id->operator!=(*(o2->id)) );
92                         break;
93                 case Py_GT:
94                         return PyBool_from_bool(!( o1->id->operator<(*(o2->id)) || o1->id->operator<(*(o2->id)) ));
95                         break;
96                 case Py_GE:
97                         return PyBool_from_bool(!( o1->id->operator<(*(o2->id)) ));
98                         break;
99         }
100         
101         Py_RETURN_NONE;
102 }
103
104 /*----------------------Id instance definitions ----------------------------*/
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
117 static PyObject *Id_first_get(BPy_Id *self, void *UNUSED(closure))
118 {
119         return PyLong_FromLong(self->id->getFirst());
120 }
121
122 static int Id_first_set(BPy_Id *self, PyObject *value, void *UNUSED(closure))
123 {
124         int scalar;
125         if ((scalar = PyLong_AsLong(value)) == -1 && PyErr_Occurred()) {
126                 PyErr_SetString(PyExc_TypeError, "value must be an integer");
127                 return -1;
128         }
129         self->id->setFirst(scalar);
130         return 0;
131 }
132
133 PyDoc_STRVAR(Id_second_doc,
134 "The second number constituting the Id.\n"
135 "\n"
136 ":type: int"
137 );
138
139 static PyObject *Id_second_get(BPy_Id *self, void *UNUSED(closure))
140 {
141         return PyLong_FromLong(self->id->getSecond());
142 }
143
144 static int Id_second_set(BPy_Id *self, PyObject *value, void *UNUSED(closure))
145 {
146         int scalar;
147         if ((scalar = PyLong_AsLong(value)) == -1 && PyErr_Occurred()) {
148                 PyErr_SetString(PyExc_TypeError, "value must be an integer");
149                 return -1;
150         }
151         self->id->setSecond(scalar);
152         return 0;
153 }
154
155 static PyGetSetDef BPy_Id_getseters[] = {
156         {(char *)"first", (getter)Id_first_get, (setter)Id_first_set, (char *)Id_first_doc, NULL},
157         {(char *)"second", (getter)Id_second_get, (setter)Id_second_set, (char *)Id_second_doc, NULL},
158         {NULL, NULL, NULL, NULL, NULL}  /* Sentinel */
159 };
160
161 /*-----------------------BPy_Id type definition ------------------------------*/
162
163 PyTypeObject Id_Type = {
164         PyVarObject_HEAD_INIT(NULL, 0)
165         "Id",                           /* tp_name */
166         sizeof(BPy_Id),                 /* tp_basicsize */
167         0,                              /* tp_itemsize */
168         (destructor)Id___dealloc__,     /* tp_dealloc */
169         0,                              /* tp_print */
170         0,                              /* tp_getattr */
171         0,                              /* tp_setattr */
172         0,                              /* tp_reserved */
173         (reprfunc)Id___repr__,          /* tp_repr */
174         0,                              /* tp_as_number */
175         0,                              /* tp_as_sequence */
176         0,                              /* tp_as_mapping */
177         0,                              /* tp_hash  */
178         0,                              /* tp_call */
179         0,                              /* tp_str */
180         0,                              /* tp_getattro */
181         0,                              /* tp_setattro */
182         0,                              /* tp_as_buffer */
183         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
184         Id___doc__,                     /* tp_doc */
185         0,                              /* tp_traverse */
186         0,                              /* tp_clear */
187         (richcmpfunc)Id_RichCompare,    /* tp_richcompare */
188         0,                              /* tp_weaklistoffset */
189         0,                              /* tp_iter */
190         0,                              /* tp_iternext */
191         BPy_Id_methods,                 /* tp_methods */
192         0,                              /* tp_members */
193         BPy_Id_getseters,               /* tp_getset */
194         0,                              /* tp_base */
195         0,                              /* tp_dict */
196         0,                              /* tp_descr_get */
197         0,                              /* tp_descr_set */
198         0,                              /* tp_dictoffset */
199         (initproc)Id___init__,          /* tp_init */
200         0,                              /* tp_alloc */
201         PyType_GenericNew,              /* tp_new */
202 };
203
204 ///////////////////////////////////////////////////////////////////////////////////////////
205
206 #ifdef __cplusplus
207 }
208 #endif