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