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