Blender V4.5
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");
50
51static int Id_init(BPy_Id *self, PyObject *args, PyObject *kwds)
52{
53 static const char *kwlist_1[] = {"brother", nullptr};
54 static const char *kwlist_2[] = {"first", "second", nullptr};
55 PyObject *brother;
56 int first = 0, second = 0;
57
58 if (PyArg_ParseTupleAndKeywords(args, kwds, "O!", (char **)kwlist_1, &Id_Type, &brother)) {
59 self->id = new Id(*(((BPy_Id *)brother)->id));
60 }
61 else if ((void)PyErr_Clear(),
62 PyArg_ParseTupleAndKeywords(args, kwds, "|ii", (char **)kwlist_2, &first, &second))
63 {
64 self->id = new Id(first, second);
65 }
66 else {
67 PyErr_SetString(PyExc_TypeError, "invalid argument(s)");
68 return -1;
69 }
70 return 0;
71}
72
73static void Id_dealloc(BPy_Id *self)
74{
75 delete self->id;
76 Py_TYPE(self)->tp_free((PyObject *)self);
77}
78
79static PyObject *Id_repr(BPy_Id *self)
80{
81 return PyUnicode_FromFormat(
82 "[ first: %i, second: %i ](BPy_Id)", self->id->getFirst(), self->id->getSecond());
83}
84
85static PyObject *Id_RichCompare(BPy_Id *o1, BPy_Id *o2, int opid)
86{
87 switch (opid) {
88 case Py_LT:
89 return PyBool_from_bool(o1->id->operator<(*(o2->id)));
90 case Py_LE:
91 return PyBool_from_bool(o1->id->operator<(*(o2->id)) || o1->id->operator==(*(o2->id)));
92 case Py_EQ:
93 return PyBool_from_bool(o1->id->operator==(*(o2->id)));
94 case Py_NE:
95 return PyBool_from_bool(o1->id->operator!=(*(o2->id)));
96 case Py_GT:
97 return PyBool_from_bool(!(o1->id->operator<(*(o2->id)) || o1->id->operator==(*(o2->id))));
98 case Py_GE:
99 return PyBool_from_bool(!o1->id->operator<(*(o2->id)));
100 }
101 Py_RETURN_NONE;
102}
103
104/*----------------------Id get/setters ----------------------------*/
105
107 /* Wrap. */
108 Id_first_doc,
109 "The first number constituting the Id.\n"
110 "\n"
111 ":type: int");
112
113static PyObject *Id_first_get(BPy_Id *self, void * /*closure*/)
114{
115 return PyLong_FromLong(self->id->getFirst());
116}
117
118static int Id_first_set(BPy_Id *self, PyObject *value, void * /*closure*/)
119{
120 int scalar;
121 if ((scalar = PyLong_AsLong(value)) == -1 && PyErr_Occurred()) {
122 PyErr_SetString(PyExc_TypeError, "value must be an integer");
123 return -1;
124 }
125 self->id->setFirst(scalar);
126 return 0;
127}
128
130 /* Wrap. */
131 Id_second_doc,
132 "The second number constituting the Id.\n"
133 "\n"
134 ":type: int");
135
136static PyObject *Id_second_get(BPy_Id *self, void * /*closure*/)
137{
138 return PyLong_FromLong(self->id->getSecond());
139}
140
141static int Id_second_set(BPy_Id *self, PyObject *value, void * /*closure*/)
142{
143 int scalar;
144 if ((scalar = PyLong_AsLong(value)) == -1 && PyErr_Occurred()) {
145 PyErr_SetString(PyExc_TypeError, "value must be an integer");
146 return -1;
147 }
148 self->id->setSecond(scalar);
149 return 0;
150}
151
152static PyGetSetDef BPy_Id_getseters[] = {
153 {"first", (getter)Id_first_get, (setter)Id_first_set, Id_first_doc, nullptr},
154 {"second", (getter)Id_second_get, (setter)Id_second_set, Id_second_doc, nullptr},
155 {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
156};
157
158/*-----------------------BPy_Id type definition ------------------------------*/
159
160PyTypeObject Id_Type = {
161 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
162 /*tp_name*/ "Id",
163 /*tp_basicsize*/ sizeof(BPy_Id),
164 /*tp_itemsize*/ 0,
165 /*tp_dealloc*/ (destructor)Id_dealloc,
166 /*tp_vectorcall_offset*/ 0,
167 /*tp_getattr*/ nullptr,
168 /*tp_setattr*/ nullptr,
169 /*tp_as_async*/ nullptr,
170 /*tp_repr*/ (reprfunc)Id_repr,
171 /*tp_as_number*/ nullptr,
172 /*tp_as_sequence*/ nullptr,
173 /*tp_as_mapping*/ nullptr,
174 /*tp_hash*/ nullptr,
175 /*tp_call*/ nullptr,
176 /*tp_str*/ nullptr,
177 /*tp_getattro*/ nullptr,
178 /*tp_setattro*/ nullptr,
179 /*tp_as_buffer*/ nullptr,
180 /*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
181 /*tp_doc*/ Id_doc,
182 /*tp_traverse*/ nullptr,
183 /*tp_clear*/ nullptr,
184 /*tp_richcompare*/ (richcmpfunc)Id_RichCompare,
185 /*tp_weaklistoffset*/ 0,
186 /*tp_iter*/ nullptr,
187 /*tp_iternext*/ nullptr,
188 /*tp_methods*/ nullptr,
189 /*tp_members*/ nullptr,
190 /*tp_getset*/ BPy_Id_getseters,
191 /*tp_base*/ nullptr,
192 /*tp_dict*/ nullptr,
193 /*tp_descr_get*/ nullptr,
194 /*tp_descr_set*/ nullptr,
195 /*tp_dictoffset*/ 0,
196 /*tp_init*/ (initproc)Id_init,
197 /*tp_alloc*/ nullptr,
198 /*tp_new*/ PyType_GenericNew,
199};
200
PyObject * PyBool_from_bool(bool b)
static int Id_second_set(BPy_Id *self, PyObject *value, void *)
Definition BPy_Id.cpp:141
PyTypeObject Id_Type
Definition BPy_Id.cpp:160
static int Id_first_set(BPy_Id *self, PyObject *value, void *)
Definition BPy_Id.cpp:118
static void Id_dealloc(BPy_Id *self)
Definition BPy_Id.cpp:73
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:51
static PyObject * Id_repr(BPy_Id *self)
Definition BPy_Id.cpp:79
static PyGetSetDef BPy_Id_getseters[]
Definition BPy_Id.cpp:152
static PyObject * Id_second_get(BPy_Id *self, void *)
Definition BPy_Id.cpp:136
static PyObject * Id_first_get(BPy_Id *self, void *)
Definition BPy_Id.cpp:113
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:85
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