Blender  V2.93
BPy_Id.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include "BPy_Id.h"
22 
23 #include "BPy_Convert.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 using namespace Freestyle;
30 
32 
33 //-------------------MODULE INITIALIZATION--------------------------------
34 int Id_Init(PyObject *module)
35 {
36  if (module == nullptr) {
37  return -1;
38  }
39 
40  if (PyType_Ready(&Id_Type) < 0) {
41  return -1;
42  }
43 
44  Py_INCREF(&Id_Type);
45  PyModule_AddObject(module, "Id", (PyObject *)&Id_Type);
46  return 0;
47 }
48 
49 //------------------------INSTANCE METHODS ----------------------------------
50 
52  Id_doc,
53  "Class for representing an object Id.\n"
54  "\n"
55  ".. method:: __init__(brother)\n"
56  " __init__(first=0, second=0)\n"
57  "\n"
58  " Build the Id from two numbers or another :class:`Id` using the copy constructor.\n"
59  "\n"
60  " :arg brother: An Id object.\n"
61  " :type brother: :class:`Id`"
62  " :arg first: The first number.\n"
63  " :type first: int\n"
64  " :arg second: The second number.\n"
65  " :type second: int\n");
66 
67 static int Id_init(BPy_Id *self, PyObject *args, PyObject *kwds)
68 {
69  static const char *kwlist_1[] = {"brother", nullptr};
70  static const char *kwlist_2[] = {"first", "second", nullptr};
71  PyObject *brother;
72  int first = 0, second = 0;
73 
74  if (PyArg_ParseTupleAndKeywords(args, kwds, "O!", (char **)kwlist_1, &Id_Type, &brother)) {
75  self->id = new Id(*(((BPy_Id *)brother)->id));
76  }
77  else if ((void)PyErr_Clear(),
78  PyArg_ParseTupleAndKeywords(args, kwds, "|ii", (char **)kwlist_2, &first, &second)) {
79  self->id = new Id(first, second);
80  }
81  else {
82  PyErr_SetString(PyExc_TypeError, "invalid argument(s)");
83  return -1;
84  }
85  return 0;
86 }
87 
88 static void Id_dealloc(BPy_Id *self)
89 {
90  delete self->id;
91  Py_TYPE(self)->tp_free((PyObject *)self);
92 }
93 
94 static PyObject *Id_repr(BPy_Id *self)
95 {
96  return PyUnicode_FromFormat(
97  "[ first: %i, second: %i ](BPy_Id)", self->id->getFirst(), self->id->getSecond());
98 }
99 
100 static PyObject *Id_RichCompare(BPy_Id *o1, BPy_Id *o2, int opid)
101 {
102  switch (opid) {
103  case Py_LT:
104  return PyBool_from_bool(o1->id->operator<(*(o2->id)));
105  case Py_LE:
106  return PyBool_from_bool(o1->id->operator<(*(o2->id)) || o1->id->operator==(*(o2->id)));
107  case Py_EQ:
108  return PyBool_from_bool(o1->id->operator==(*(o2->id)));
109  case Py_NE:
110  return PyBool_from_bool(o1->id->operator!=(*(o2->id)));
111  case Py_GT:
112  return PyBool_from_bool(!(o1->id->operator<(*(o2->id)) || o1->id->operator==(*(o2->id))));
113  case Py_GE:
114  return PyBool_from_bool(!(o1->id->operator<(*(o2->id))));
115  }
116  Py_RETURN_NONE;
117 }
118 
119 /*----------------------Id get/setters ----------------------------*/
120 
121 PyDoc_STRVAR(Id_first_doc,
122  "The first number constituting the Id.\n"
123  "\n"
124  ":type: int");
125 
126 static PyObject *Id_first_get(BPy_Id *self, void *UNUSED(closure))
127 {
128  return PyLong_FromLong(self->id->getFirst());
129 }
130 
131 static int Id_first_set(BPy_Id *self, PyObject *value, void *UNUSED(closure))
132 {
133  int scalar;
134  if ((scalar = PyLong_AsLong(value)) == -1 && PyErr_Occurred()) {
135  PyErr_SetString(PyExc_TypeError, "value must be an integer");
136  return -1;
137  }
138  self->id->setFirst(scalar);
139  return 0;
140 }
141 
142 PyDoc_STRVAR(Id_second_doc,
143  "The second number constituting the Id.\n"
144  "\n"
145  ":type: int");
146 
147 static PyObject *Id_second_get(BPy_Id *self, void *UNUSED(closure))
148 {
149  return PyLong_FromLong(self->id->getSecond());
150 }
151 
152 static int Id_second_set(BPy_Id *self, PyObject *value, void *UNUSED(closure))
153 {
154  int scalar;
155  if ((scalar = PyLong_AsLong(value)) == -1 && PyErr_Occurred()) {
156  PyErr_SetString(PyExc_TypeError, "value must be an integer");
157  return -1;
158  }
159  self->id->setSecond(scalar);
160  return 0;
161 }
162 
163 static PyGetSetDef BPy_Id_getseters[] = {
164  {"first", (getter)Id_first_get, (setter)Id_first_set, Id_first_doc, nullptr},
165  {"second", (getter)Id_second_get, (setter)Id_second_set, Id_second_doc, nullptr},
166  {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
167 };
168 
169 /*-----------------------BPy_Id type definition ------------------------------*/
170 
171 PyTypeObject Id_Type = {
172  PyVarObject_HEAD_INIT(nullptr, 0) "Id", /* tp_name */
173  sizeof(BPy_Id), /* tp_basicsize */
174  0, /* tp_itemsize */
175  (destructor)Id_dealloc, /* tp_dealloc */
176  0, /* tp_vectorcall_offset */
177  nullptr, /* tp_getattr */
178  nullptr, /* tp_setattr */
179  nullptr, /* tp_reserved */
180  (reprfunc)Id_repr, /* tp_repr */
181  nullptr, /* tp_as_number */
182  nullptr, /* tp_as_sequence */
183  nullptr, /* tp_as_mapping */
184  nullptr, /* tp_hash */
185  nullptr, /* tp_call */
186  nullptr, /* tp_str */
187  nullptr, /* tp_getattro */
188  nullptr, /* tp_setattro */
189  nullptr, /* tp_as_buffer */
190  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
191  Id_doc, /* tp_doc */
192  nullptr, /* tp_traverse */
193  nullptr, /* tp_clear */
194  (richcmpfunc)Id_RichCompare, /* tp_richcompare */
195  0, /* tp_weaklistoffset */
196  nullptr, /* tp_iter */
197  nullptr, /* tp_iternext */
198  nullptr, /* tp_methods */
199  nullptr, /* tp_members */
200  BPy_Id_getseters, /* tp_getset */
201  nullptr, /* tp_base */
202  nullptr, /* tp_dict */
203  nullptr, /* tp_descr_get */
204  nullptr, /* tp_descr_set */
205  0, /* tp_dictoffset */
206  (initproc)Id_init, /* tp_init */
207  nullptr, /* tp_alloc */
208  PyType_GenericNew, /* tp_new */
209 };
210 
212 
213 #ifdef __cplusplus
214 }
215 #endif
#define UNUSED(x)
PyObject * PyBool_from_bool(bool b)
Definition: BPy_Convert.cpp:73
static int Id_second_set(BPy_Id *self, PyObject *value, void *UNUSED(closure))
Definition: BPy_Id.cpp:152
static PyObject * Id_second_get(BPy_Id *self, void *UNUSED(closure))
Definition: BPy_Id.cpp:147
PyTypeObject Id_Type
Definition: BPy_Id.cpp:171
static void Id_dealloc(BPy_Id *self)
Definition: BPy_Id.cpp:88
PyDoc_STRVAR(Id_doc, "Class for representing an object Id.\n" "\n" ".. method:: __init__(brother)\n" " __init__(first=0, second=0)\n" "\n" " Build the Id from two numbers or another :class:`Id` using the copy constructor.\n" "\n" " :arg brother: An Id object.\n" " :type brother: :class:`Id`" " :arg first: The first number.\n" " :type first: int\n" " :arg second: The second number.\n" " :type second: int\n")
static int Id_init(BPy_Id *self, PyObject *args, PyObject *kwds)
Definition: BPy_Id.cpp:67
static PyGetSetDef BPy_Id_getseters[]
Definition: BPy_Id.cpp:163
static PyObject * Id_repr(BPy_Id *self)
Definition: BPy_Id.cpp:94
int Id_Init(PyObject *module)
Definition: BPy_Id.cpp:34
static int Id_first_set(BPy_Id *self, PyObject *value, void *UNUSED(closure))
Definition: BPy_Id.cpp:131
static PyObject * Id_RichCompare(BPy_Id *o1, BPy_Id *o2, int opid)
Definition: BPy_Id.cpp:100
static PyObject * Id_first_get(BPy_Id *self, void *UNUSED(closure))
Definition: BPy_Id.cpp:126
static struct PyModuleDef module
PyObject * self
Definition: bpy_driver.c:185
inherits from class Rep
Definition: AppCanvas.cpp:32
Definition: BPy_Id.h:42
PyObject_HEAD Freestyle::Id * id
Definition: BPy_Id.h:43