From 168ca35851699c652d2ebe78583263020a816364 Mon Sep 17 00:00:00 2001 From: jriegel Date: Tue, 10 Sep 2013 18:25:27 +0200 Subject: [PATCH] implementing Quantity further --- src/Base/Quantity.h | 3 +- src/Base/QuantityPy.xml | 9 ++-- src/Base/QuantityPyImp.cpp | 88 ++++++++++++++++++++++++++++++++++++-- src/Base/UnitPy.xml | 1 + src/Base/UnitPyImp.cpp | 59 +++++++++++++++++++++++++ 5 files changed, 152 insertions(+), 8 deletions(-) diff --git a/src/Base/Quantity.h b/src/Base/Quantity.h index 8f5646cd9..232435bf6 100644 --- a/src/Base/Quantity.h +++ b/src/Base/Quantity.h @@ -53,11 +53,12 @@ public: Quantity pow(const Quantity&)const; //@} + static Quantity parse(const char* buffer); + protected: double _Value; Unit _Unit; - static Quantity parse(const char* buffer); }; } // namespace Base diff --git a/src/Base/QuantityPy.xml b/src/Base/QuantityPy.xml index 5f12089fe..52adff129 100644 --- a/src/Base/QuantityPy.xml +++ b/src/Base/QuantityPy.xml @@ -8,6 +8,7 @@ Include="Base/Quantity.h" FatherInclude="Base/PyObjectBase.h" Namespace="Base" + NumberProtocol="true" Constructor="true" Delete="true" FatherNamespace="Base"> @@ -25,22 +26,22 @@ Quantity(string) -- arbitrary mixture of numbers and chars defining a Quantity Quantity - + - multiply two quantities + sets the quantity to the power - Vector to the Base position of the Quantity + Numeric Value of the Quantity (in internal system mm,kg,s) - Direction vector of the Quantity + Unit of the Quantity diff --git a/src/Base/QuantityPyImp.cpp b/src/Base/QuantityPyImp.cpp index 26240ce6d..3deecf491 100644 --- a/src/Base/QuantityPyImp.cpp +++ b/src/Base/QuantityPyImp.cpp @@ -2,6 +2,7 @@ #include "PreCompiled.h" #include "Base/Quantity.h" +#include "Base/Vector3D.h" // inclusion of the generated files (generated out of QuantityPy.xml) #include "QuantityPy.h" @@ -22,20 +23,101 @@ PyObject *QuantityPy::PyMake(struct _typeobject *, PyObject *, PyObject *) // P } // constructor method -int QuantityPy::PyInit(PyObject* /*args*/, PyObject* /*kwd*/) +int QuantityPy::PyInit(PyObject* args, PyObject* kwd) { - return 0; + Quantity *self = getQuantityPtr(); + + double f = DOUBLE_MAX; + + if (PyArg_ParseTuple(args, "|d", &f)) { + if(f!=DOUBLE_MAX) + *self = Quantity(f); + return 0; + } + PyErr_Clear(); // set by PyArg_ParseTuple() + + PyObject *object; + + if (PyArg_ParseTuple(args,"O!",&(Base::QuantityPy::Type), &object)) { + // Note: must be static_cast, not reinterpret_cast + *self = *(static_cast(object)->getQuantityPtr()); + return 0; + } + PyErr_Clear(); // set by PyArg_ParseTuple() + const char* string; + if (PyArg_ParseTuple(args,"s", &string)) { + + + + return -1; + + } + + PyErr_SetString(PyExc_TypeError, "Either three floats, tuple or Vector expected"); + return -1; } -PyObject* QuantityPy::multiply(PyObject * /*args*/) +PyObject* QuantityPy::pow(PyObject * args) { PyErr_SetString(PyExc_NotImplementedError, "Not yet implemented"); return 0; } +PyObject* QuantityPy::number_add_handler(PyObject *self, PyObject *other) +{ + if (!PyObject_TypeCheck(self, &(QuantityPy::Type))) { + PyErr_SetString(PyExc_TypeError, "First arg must be Quantity"); + return 0; + } + if (!PyObject_TypeCheck(other, &(QuantityPy::Type))) { + PyErr_SetString(PyExc_TypeError, "Second arg must be Quantity"); + return 0; + } + Base::Quantity *a = static_cast(self)->getQuantityPtr(); + Base::Quantity *b = static_cast(other)->getQuantityPtr(); + return new QuantityPy(new Quantity(*a+*b) ); +} +PyObject* QuantityPy::number_subtract_handler(PyObject *self, PyObject *other) +{ + if (!PyObject_TypeCheck(self, &(QuantityPy::Type))) { + PyErr_SetString(PyExc_TypeError, "First arg must be Quantity"); + return 0; + } + if (!PyObject_TypeCheck(other, &(QuantityPy::Type))) { + PyErr_SetString(PyExc_TypeError, "Second arg must be Quantity"); + return 0; + } + Base::Quantity *a = static_cast(self)->getQuantityPtr(); + Base::Quantity *b = static_cast(other)->getQuantityPtr(); + return new QuantityPy(new Quantity(*a-*b) ); +} + +PyObject* QuantityPy::number_multiply_handler(PyObject *self, PyObject *other) +{ + if (!PyObject_TypeCheck(self, &(QuantityPy::Type))) { + PyErr_SetString(PyExc_TypeError, "First arg must be Quantity"); + return 0; + } + + if (PyObject_TypeCheck(other, &(QuantityPy::Type))) { + Base::Quantity *a = static_cast(self) ->getQuantityPtr(); + Base::Quantity *b = static_cast(other)->getQuantityPtr(); + + return new QuantityPy(new Quantity(*a * *b) ); + } + else if (PyFloat_Check(other)) { + Base::Quantity *a = static_cast(self) ->getQuantityPtr(); + double b = PyFloat_AsDouble(other); + return new QuantityPy(new Quantity(*a*b) ); + } + else { + PyErr_SetString(PyExc_TypeError, "A Quantity can only be multiplied by Quantity or number"); + return 0; + } +} Py::Float QuantityPy::getValue(void) const diff --git a/src/Base/UnitPy.xml b/src/Base/UnitPy.xml index 5ba7fcc65..60ad8e41b 100644 --- a/src/Base/UnitPy.xml +++ b/src/Base/UnitPy.xml @@ -8,6 +8,7 @@ Include="Base/Unit.h" FatherInclude="Base/PyObjectBase.h" Namespace="Base" + NumberProtocol="true" Constructor="true" Delete="true" FatherNamespace="Base"> diff --git a/src/Base/UnitPyImp.cpp b/src/Base/UnitPyImp.cpp index aaabc2cf8..52b40b6b4 100644 --- a/src/Base/UnitPyImp.cpp +++ b/src/Base/UnitPyImp.cpp @@ -41,7 +41,66 @@ PyObject* UnitPy::getType(PyObject * /*args*/) } +PyObject* UnitPy::number_add_handler(PyObject *self, PyObject *other) +{ + if (!PyObject_TypeCheck(self, &(UnitPy::Type))) { + PyErr_SetString(PyExc_TypeError, "First arg must be Unit"); + return 0; + } + if (!PyObject_TypeCheck(other, &(UnitPy::Type))) { + PyErr_SetString(PyExc_TypeError, "Second arg must be Unit"); + return 0; + } + Base::Unit *a = static_cast(self)->getUnitPtr(); + Base::Unit *b = static_cast(other)->getUnitPtr(); + if (*a != *b) { + PyErr_SetString(PyExc_TypeError, "Units not matching!"); + return 0; + } + + return new UnitPy(new Unit(*a)); +} + +PyObject* UnitPy::number_subtract_handler(PyObject *self, PyObject *other) +{ + if (!PyObject_TypeCheck(self, &(UnitPy::Type))) { + PyErr_SetString(PyExc_TypeError, "First arg must be Unit"); + return 0; + } + if (!PyObject_TypeCheck(other, &(UnitPy::Type))) { + PyErr_SetString(PyExc_TypeError, "Second arg must be Unit"); + return 0; + } + Base::Unit *a = static_cast(self)->getUnitPtr(); + Base::Unit *b = static_cast(other)->getUnitPtr(); + + if (*a != *b) { + PyErr_SetString(PyExc_TypeError, "Units not matching!"); + return 0; + } + + return new UnitPy(new Unit(*a)); +} + +PyObject* UnitPy::number_multiply_handler(PyObject *self, PyObject *other) +{ + if (!PyObject_TypeCheck(self, &(UnitPy::Type))) { + PyErr_SetString(PyExc_TypeError, "First arg must be Unit"); + return 0; + } + + if (PyObject_TypeCheck(other, &(UnitPy::Type))) { + Base::Unit *a = static_cast(self) ->getUnitPtr(); + Base::Unit *b = static_cast(other)->getUnitPtr(); + + return new UnitPy(new Unit( (*a) * (*b) ) ); + } + else { + PyErr_SetString(PyExc_TypeError, "A Unit can only be multiplied by a Unit"); + return 0; + } +} Py::Object UnitPy::getDimensions(void) const