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