| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2013 Jürgen Riegel <[email protected]> * |
| 5 |
* * |
| 6 |
* This file is part of the FreeCAD CAx development system. * |
| 7 |
* * |
| 8 |
* This library is free software; you can redistribute it and/or * |
| 9 |
* modify it under the terms of the GNU Library General Public * |
| 10 |
* License as published by the Free Software Foundation; either * |
| 11 |
* version 2 of the License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* This library is distributed in the hope that it will be useful, * |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 |
* GNU Library General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Library General Public * |
| 19 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 20 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 21 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 22 |
* * |
| 23 |
***************************************************************************/ |
| 24 |
|
| 25 |
#include <algorithm> |
| 26 |
#include <ranges> |
| 27 |
|
| 28 |
#include "Unit.h" |
| 29 |
|
| 30 |
// generated out of Unit.pyi |
| 31 |
#include "UnitPy.h" |
| 32 |
#include "UnitPy.cpp" |
| 33 |
|
| 34 |
#include "QuantityPy.h" |
| 35 |
|
| 36 |
using namespace Base; |
| 37 |
|
| 38 |
// returns a string which represents the object e.g. when printed in python |
| 39 |
std::string UnitPy::representation() const |
| 40 |
{ |
| 41 |
return getUnitPtr()->representation(); |
| 42 |
} |
| 43 |
|
| 44 |
PyObject* UnitPy::PyMake(PyTypeObject* /*unused*/, PyObject* /*unused*/, PyObject* /*unused*/) |
| 45 |
{ |
| 46 |
// create a new instance of UnitPy and the Twin object |
| 47 |
return new UnitPy(new Unit); |
| 48 |
} |
| 49 |
|
| 50 |
// constructor method |
| 51 |
int UnitPy::PyInit(PyObject* args, PyObject* /*kwd*/) |
| 52 |
{ |
| 53 |
PyObject* object {}; |
| 54 |
Unit* self = getUnitPtr(); |
| 55 |
|
| 56 |
// get quantity |
| 57 |
if (PyArg_ParseTuple(args, "O!", &(QuantityPy::Type), &object)) { |
| 58 |
*self = static_cast<QuantityPy*>(object)->getQuantityPtr()->getUnit(); |
| 59 |
return 0; |
| 60 |
} |
| 61 |
PyErr_Clear(); // set by PyArg_ParseTuple() |
| 62 |
|
| 63 |
// get unit |
| 64 |
if (PyArg_ParseTuple(args, "O!", &(UnitPy::Type), &object)) { |
| 65 |
*self = *(static_cast<UnitPy*>(object)->getUnitPtr()); |
| 66 |
return 0; |
| 67 |
} |
| 68 |
PyErr_Clear(); // set by PyArg_ParseTuple() |
| 69 |
|
| 70 |
// get string |
| 71 |
char* string {}; |
| 72 |
if (PyArg_ParseTuple(args, "et", "utf-8", &string)) { |
| 73 |
std::string str(string); |
| 74 |
PyMem_Free(string); |
| 75 |
try { |
| 76 |
*self = Quantity::parse(str).getUnit(); |
| 77 |
return 0; |
| 78 |
} |
| 79 |
catch (const ParserError& e) { |
| 80 |
PyErr_SetString(PyExc_ValueError, e.what()); |
| 81 |
return -1; |
| 82 |
} |
| 83 |
} |
| 84 |
PyErr_Clear(); // set by PyArg_ParseTuple() |
| 85 |
|
| 86 |
int i1 {0}; |
| 87 |
int i2 {0}; |
| 88 |
int i3 {0}; |
| 89 |
int i4 {0}; |
| 90 |
int i5 {0}; |
| 91 |
int i6 {0}; |
| 92 |
int i7 {0}; |
| 93 |
int i8 {0}; |
| 94 |
if (PyArg_ParseTuple(args, "|iiiiiiii", &i1, &i2, &i3, &i4, &i5, &i6, &i7, &i8)) { |
| 95 |
try { |
| 96 |
*self = Unit(i1, i2, i3, i4, i5, i6, i7, i8); |
| 97 |
return 0; |
| 98 |
} |
| 99 |
catch (const OverflowError& e) { |
| 100 |
PyErr_SetString(PyExc_OverflowError, e.what()); |
| 101 |
return -1; |
| 102 |
} |
| 103 |
catch (const UnderflowError& e) { |
| 104 |
PyErr_SetString(PyExc_OverflowError, e.what()); |
| 105 |
return -1; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
PyErr_SetString(PyExc_TypeError, "Either string, (float,8 ints), Unit() or Quantity()"); |
| 110 |
return -1; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
PyObject* UnitPy::number_add_handler(PyObject* self, PyObject* other) |
| 115 |
{ |
| 116 |
if (!PyObject_TypeCheck(self, &(UnitPy::Type))) { |
| 117 |
PyErr_SetString(PyExc_TypeError, "First arg must be Unit"); |
| 118 |
return nullptr; |
| 119 |
} |
| 120 |
if (!PyObject_TypeCheck(other, &(UnitPy::Type))) { |
| 121 |
PyErr_SetString(PyExc_TypeError, "Second arg must be Unit"); |
| 122 |
return nullptr; |
| 123 |
} |
| 124 |
Unit* a = static_cast<UnitPy*>(self)->getUnitPtr(); |
| 125 |
Unit* b = static_cast<UnitPy*>(other)->getUnitPtr(); |
| 126 |
|
| 127 |
if (*a != *b) { |
| 128 |
PyErr_SetString(PyExc_TypeError, "Units not matching!"); |
| 129 |
return nullptr; |
| 130 |
} |
| 131 |
|
| 132 |
return new UnitPy(new Unit(*a)); |
| 133 |
} |
| 134 |
|
| 135 |
PyObject* UnitPy::number_subtract_handler(PyObject* self, PyObject* other) |
| 136 |
{ |
| 137 |
if (!PyObject_TypeCheck(self, &(UnitPy::Type))) { |
| 138 |
PyErr_SetString(PyExc_TypeError, "First arg must be Unit"); |
| 139 |
return nullptr; |
| 140 |
} |
| 141 |
if (!PyObject_TypeCheck(other, &(UnitPy::Type))) { |
| 142 |
PyErr_SetString(PyExc_TypeError, "Second arg must be Unit"); |
| 143 |
return nullptr; |
| 144 |
} |
| 145 |
Unit* a = static_cast<UnitPy*>(self)->getUnitPtr(); |
| 146 |
Unit* b = static_cast<UnitPy*>(other)->getUnitPtr(); |
| 147 |
|
| 148 |
if (*a != *b) { |
| 149 |
PyErr_SetString(PyExc_TypeError, "Units not matching!"); |
| 150 |
return nullptr; |
| 151 |
} |
| 152 |
|
| 153 |
return new UnitPy(new Unit(*a)); |
| 154 |
} |
| 155 |
|
| 156 |
PyObject* UnitPy::number_multiply_handler(PyObject* self, PyObject* other) |
| 157 |
{ |
| 158 |
if (!PyObject_TypeCheck(self, &(UnitPy::Type))) { |
| 159 |
PyErr_SetString(PyExc_TypeError, "First arg must be Unit"); |
| 160 |
return nullptr; |
| 161 |
} |
| 162 |
|
| 163 |
if (PyObject_TypeCheck(other, &(UnitPy::Type))) { |
| 164 |
Unit* a = static_cast<UnitPy*>(self)->getUnitPtr(); |
| 165 |
Unit* b = static_cast<UnitPy*>(other)->getUnitPtr(); |
| 166 |
|
| 167 |
return new UnitPy(new Unit((*a) * (*b))); |
| 168 |
} |
| 169 |
PyErr_SetString(PyExc_TypeError, "A Unit can only be multiplied by a Unit"); |
| 170 |
return nullptr; |
| 171 |
} |
| 172 |
|
| 173 |
PyObject* UnitPy::richCompare(PyObject* v, PyObject* w, int op) |
| 174 |
{ |
| 175 |
if (PyObject_TypeCheck(v, &(UnitPy::Type)) && PyObject_TypeCheck(w, &(UnitPy::Type))) { |
| 176 |
const Unit* u1 = static_cast<UnitPy*>(v)->getUnitPtr(); |
| 177 |
const Unit* u2 = static_cast<UnitPy*>(w)->getUnitPtr(); |
| 178 |
|
| 179 |
PyObject* res = nullptr; |
| 180 |
if (op != Py_EQ && op != Py_NE) { |
| 181 |
PyErr_SetString(PyExc_TypeError, "no ordering relation is defined for Units"); |
| 182 |
return nullptr; |
| 183 |
} |
| 184 |
if (op == Py_EQ) { |
| 185 |
res = (*u1 == *u2) ? Py_True : Py_False; // NOLINT |
| 186 |
Py_INCREF(res); |
| 187 |
return res; |
| 188 |
} |
| 189 |
res = (*u1 != *u2) ? Py_True : Py_False; // NOLINT |
| 190 |
Py_INCREF(res); |
| 191 |
return res; |
| 192 |
} |
| 193 |
// This always returns False |
| 194 |
Py_INCREF(Py_NotImplemented); |
| 195 |
return Py_NotImplemented; |
| 196 |
} |
| 197 |
|
| 198 |
Py::String UnitPy::getType() const |
| 199 |
{ |
| 200 |
return {getUnitPtr()->getTypeString(), "utf-8"}; |
| 201 |
} |
| 202 |
|
| 203 |
Py::Tuple UnitPy::getSignature() const |
| 204 |
{ |
| 205 |
Py::Tuple tuple {unitNumExponents}; |
| 206 |
auto exps = getUnitPtr()->exponents(); |
| 207 |
std::ranges::for_each(exps, [&, pos {0}](auto exp) mutable { |
| 208 |
tuple.setItem(pos++, Py::Long {exp}); |
| 209 |
}); |
| 210 |
return tuple; |
| 211 |
} |
| 212 |
|
| 213 |
PyObject* UnitPy::getCustomAttributes(const char* /*attr*/) const |
| 214 |
{ |
| 215 |
return nullptr; |
| 216 |
} |
| 217 |
|
| 218 |
int UnitPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/) |
| 219 |
{ |
| 220 |
return 0; |
| 221 |
} |
| 222 |
|
| 223 |
PyObject* UnitPy::number_divide_handler(PyObject* /*self*/, PyObject* /*other*/) |
| 224 |
{ |
| 225 |
PyErr_SetString(PyExc_NotImplementedError, "Not implemented"); |
| 226 |
return nullptr; |
| 227 |
} |
| 228 |
|
| 229 |
PyObject* UnitPy::number_remainder_handler(PyObject* /*self*/, PyObject* /*other*/) |
| 230 |
{ |
| 231 |
PyErr_SetString(PyExc_NotImplementedError, "Not implemented"); |
| 232 |
return nullptr; |
| 233 |
} |
| 234 |
|
| 235 |
PyObject* UnitPy::number_divmod_handler(PyObject* /*self*/, PyObject* /*other*/) |
| 236 |
{ |
| 237 |
PyErr_SetString(PyExc_NotImplementedError, "Not implemented"); |
| 238 |
return nullptr; |
| 239 |
} |
| 240 |
|
| 241 |
PyObject* UnitPy::number_power_handler(PyObject* /*self*/, PyObject* /*other*/, PyObject* /*modulo*/) |
| 242 |
{ |
| 243 |
PyErr_SetString(PyExc_NotImplementedError, "Not implemented"); |
| 244 |
return nullptr; |
| 245 |
} |
| 246 |
|
| 247 |
PyObject* UnitPy::number_negative_handler(PyObject* /*self*/) |
| 248 |
{ |
| 249 |
PyErr_SetString(PyExc_NotImplementedError, "Not implemented"); |
| 250 |
return nullptr; |
| 251 |
} |
| 252 |
|
| 253 |
PyObject* UnitPy::number_positive_handler(PyObject* /*self*/) |
| 254 |
{ |
| 255 |
PyErr_SetString(PyExc_NotImplementedError, "Not implemented"); |
| 256 |
return nullptr; |
| 257 |
} |
| 258 |
|
| 259 |
PyObject* UnitPy::number_absolute_handler(PyObject* /*self*/) |
| 260 |
{ |
| 261 |
PyErr_SetString(PyExc_NotImplementedError, "Not implemented"); |
| 262 |
return nullptr; |
| 263 |
} |
| 264 |
|
| 265 |
int UnitPy::number_nonzero_handler(PyObject* /*self*/) |
| 266 |
{ |
| 267 |
return 1; |
| 268 |
} |
| 269 |
|
| 270 |
PyObject* UnitPy::number_invert_handler(PyObject* /*self*/) |
| 271 |
{ |
| 272 |
PyErr_SetString(PyExc_NotImplementedError, "Not implemented"); |
| 273 |
return nullptr; |
| 274 |
} |
| 275 |
|
| 276 |
PyObject* UnitPy::number_lshift_handler(PyObject* /*self*/, PyObject* /*other*/) |
| 277 |
{ |
| 278 |
PyErr_SetString(PyExc_NotImplementedError, "Not implemented"); |
| 279 |
return nullptr; |
| 280 |
} |
| 281 |
|
| 282 |
PyObject* UnitPy::number_rshift_handler(PyObject* /*self*/, PyObject* /*other*/) |
| 283 |
{ |
| 284 |
PyErr_SetString(PyExc_NotImplementedError, "Not implemented"); |
| 285 |
return nullptr; |
| 286 |
} |
| 287 |
|
| 288 |
PyObject* UnitPy::number_and_handler(PyObject* /*self*/, PyObject* /*other*/) |
| 289 |
{ |
| 290 |
PyErr_SetString(PyExc_NotImplementedError, "Not implemented"); |
| 291 |
return nullptr; |
| 292 |
} |
| 293 |
|
| 294 |
PyObject* UnitPy::number_xor_handler(PyObject* /*self*/, PyObject* /*other*/) |
| 295 |
{ |
| 296 |
PyErr_SetString(PyExc_NotImplementedError, "Not implemented"); |
| 297 |
return nullptr; |
| 298 |
} |
| 299 |
|
| 300 |
PyObject* UnitPy::number_or_handler(PyObject* /*self*/, PyObject* /*other*/) |
| 301 |
{ |
| 302 |
PyErr_SetString(PyExc_NotImplementedError, "Not implemented"); |
| 303 |
return nullptr; |
| 304 |
} |
| 305 |
|
| 306 |
PyObject* UnitPy::number_int_handler(PyObject* /*self*/) |
| 307 |
{ |
| 308 |
PyErr_SetString(PyExc_NotImplementedError, "Not implemented"); |
| 309 |
return nullptr; |
| 310 |
} |
| 311 |
|
| 312 |
PyObject* UnitPy::number_float_handler(PyObject* /*self*/) |
| 313 |
{ |
| 314 |
PyErr_SetString(PyExc_NotImplementedError, "Not implemented"); |
| 315 |
return nullptr; |
| 316 |
} |