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