From cbc60a3f449d1cee8d0efb224d50dcfba7f4f534 Mon Sep 17 00:00:00 2001 From: wmayer Date: Tue, 25 Jan 2022 00:26:47 +0100 Subject: [PATCH] Base: clean-up code in UnitsApi --- src/Base/Unit.h | 1 + src/Base/UnitsApi.cpp | 66 ++---------------- src/Base/UnitsApi.h | 27 +++----- src/Base/UnitsApiPy.cpp | 103 +++++----------------------- src/Mod/Robot/App/WaypointPyImp.cpp | 4 +- 5 files changed, 32 insertions(+), 169 deletions(-) diff --git a/src/Base/Unit.h b/src/Base/Unit.h index 665813ef80..d1db414334 100644 --- a/src/Base/Unit.h +++ b/src/Base/Unit.h @@ -31,6 +31,7 @@ #endif #include #include +#include namespace Base { diff --git a/src/Base/UnitsApi.cpp b/src/Base/UnitsApi.cpp index 5bdff83351..82bfb6fd24 100644 --- a/src/Base/UnitsApi.cpp +++ b/src/Base/UnitsApi.cpp @@ -52,35 +52,12 @@ using namespace Base; - -//const QString UnitsApi::getQuantityName(QuantityType t) -//{ -// // check limits -// assert(t<9); -// // returns -// return QString::fromLatin1(QuantityNames[t]); -//} // === static attributes ================================================ -double UnitsApi::defaultFactor = 1.0; UnitsSchemaPtr UnitsApi::UserPrefSystem(new UnitsSchemaInternal()); -UnitSystem UnitsApi::actSystem = UnitSystem::SI1; +UnitSystem UnitsApi::currentSystem = UnitSystem::SI1; -//double UnitsApi::UserPrefFactor [50]; -//QString UnitsApi::UserPrefUnit [50]; -int UnitsApi::UserPrefDecimals = 2; - -UnitsApi::UnitsApi(const char* /*filter*/) -{ -} - -UnitsApi::UnitsApi(const std::string& /*filter*/) -{ -} - -UnitsApi::~UnitsApi() -{ -} +int UnitsApi::UserPrefDecimals = 2; const char* UnitsApi::getDescription(UnitSystem system) { @@ -143,12 +120,12 @@ void UnitsApi::setSchema(UnitSystem s) } UserPrefSystem = createSchema(s); - actSystem = s; + currentSystem = s; // for wrong value fall back to standard schema if (!UserPrefSystem) { UserPrefSystem = std::make_unique(); - actSystem = UnitSystem::SI1; + currentSystem = UnitSystem::SI1; } UserPrefSystem->setSchemaUnits(); // if necessary a unit schema can change the constants in Quantity (e.g. mi=1.8km rather then 1.6km). @@ -172,19 +149,6 @@ QString UnitsApi::toNumber(double d, const QuantityFormat& f) return number; } -//double UnitsApi::translateUnit(const char* str) -//{ -// bool temp; -// return parse(str,temp ); -//} -// -//double UnitsApi::translateUnit(const QString & str) -//{ -// bool temp; -// return parse(str.toUtf8() ,temp); -//} -// - // === static translation methods ========================================== QString UnitsApi::schemaTranslate(const Base::Quantity& quant, double &factor, QString &unitString) @@ -192,26 +156,7 @@ QString UnitsApi::schemaTranslate(const Base::Quantity& quant, double &factor, Q return UserPrefSystem->schemaTranslate(quant,factor,unitString); } - -//QString UnitsApi::toStrWithUserPrefs(QuantityType t,double Value) -//{ -// return UserPrefSystem->toStrWithUserPrefs(t,Value); -// //double UnitValue = Value/UserPrefFactor[t]; -// //return QString::fromLatin1("%1 %2").arg(UnitValue).arg(UserPrefUnit[t]); -//} -// -//void UnitsApi::toStrWithUserPrefs(QuantityType t,double Value,QString &outValue,QString &outUnit) -//{ -// UserPrefSystem->toStrWithUserPrefs(t,Value,outValue,outUnit); -//} -// -//PyObject *UnitsApi::toPyWithUserPrefs(QuantityType t,double Value) -//{ -// return PyFloat_FromDouble(Value * UserPrefFactor[t]); -//} -// - -double UnitsApi::toDbl(PyObject *ArgObj, const Base::Unit &u) +double UnitsApi::toDouble(PyObject *ArgObj, const Base::Unit &u) { if (PyUnicode_Check(ArgObj)) { QString str = QString::fromUtf8(PyUnicode_AsUTF8(ArgObj)); @@ -263,4 +208,3 @@ int UnitsApi::getDecimals() { return UserPrefDecimals; } - diff --git a/src/Base/UnitsApi.h b/src/Base/UnitsApi.h index 594878d7e7..ac1452d1b2 100644 --- a/src/Base/UnitsApi.h +++ b/src/Base/UnitsApi.h @@ -42,11 +42,6 @@ class BaseExport UnitsApi { public: - /** Constructs a UnitsApi object. */ - UnitsApi(const char* filter); - UnitsApi(const std::string& filter); - virtual ~UnitsApi(); - /** set Schema * set the UnitsSchema of the Application * this a represented by a class of type UnitSchema which @@ -55,7 +50,9 @@ public: */ static void setSchema(UnitSystem s); /// return the active schema - static UnitSystem getSchema(void){return actSystem;} + static UnitSystem getSchema() { + return currentSystem; + } /// Returns a brief description of a schema static const char* getDescription(UnitSystem); @@ -83,16 +80,14 @@ public: static QString toNumber(double d, const QuantityFormat& f = QuantityFormat(QuantityFormat::Default)); /// generate a value for a quantity with default user preferred system - static double toDbl(PyObject *ArgObj,const Base::Unit &u=Base::Unit()); + static double toDouble(PyObject* args, const Base::Unit& u = Base::Unit()); /// generate a value for a quantity with default user preferred system - static Quantity toQuantity(PyObject *ArgObj,const Base::Unit &u=Base::Unit()); + static Quantity toQuantity(PyObject* args, const Base::Unit& u = Base::Unit()); // set the number of decimals static void setDecimals(int); // get the number of decimals static int getDecimals(); - /// set the application defaults - //static void setDefaults(void); //@} //double Result; @@ -100,24 +95,18 @@ public: // Python interface static PyMethodDef Methods[]; - static double defaultFactor; - /// return an instance of the given enum value static UnitsSchemaPtr createSchema(UnitSystem s); protected: // not used at the moment static UnitsSchemaPtr UserPrefSystem; - static UnitSystem actSystem; + static UnitSystem currentSystem; /// number of decimals for floats static int UserPrefDecimals; - // do the real work - //static double parse(const char*,bool &UsedUnit); - -protected: // the python API wrapper methods - //static PyObject *sTranslateUnit (PyObject *self,PyObject *args); - //static PyObject *sGetWithPrefs (PyObject *self,PyObject *args); +protected: + // the python API wrapper methods static PyObject *sParseQuantity (PyObject *self,PyObject *args); static PyObject *sListSchemas (PyObject *self,PyObject *args); static PyObject *sGetSchema (PyObject *self,PyObject *args); diff --git a/src/Base/UnitsApiPy.cpp b/src/Base/UnitsApiPy.cpp index 103a7b9c1b..fe7702d665 100644 --- a/src/Base/UnitsApiPy.cpp +++ b/src/Base/UnitsApiPy.cpp @@ -41,34 +41,9 @@ using namespace Base; //************************************************************************** // Python stuff of UnitsApi -// UnitsApi Methods // Methods structure +// UnitsApi Methods PyMethodDef UnitsApi::Methods[] = { - //{"translateUnit", (PyCFunction) UnitsApi::sTranslateUnit, METH_VARARGS, - // "translateUnit(string) -> double\n\n" - // "calculate a mathematical expression with units to a number. \n" - // "can be used for simple unit translation like: \n" - // " translateUnit('10m')\n" - // " or for more complex espressions:\n" - // " translateUnit('sin(pi)/50.0 m/s^2')\n" - //}, - //{"getWithPrefs", (PyCFunction) UnitsApi::sGetWithPrefs, METH_VARARGS, - // "getWithPrefs(type,[string|float|int]) -> double\n\n" - // "Translation to internal regarding user prefs \n" - // " That means if no unit is issued the user prefs are in \n" - // " charge. If one unit is used the user prefs get ignored\n" - // " type can be: \n" - // " Length \n" - // " Area \n" - // " Volume \n" - // " Angle \n" - // " TimeSpan \n" - // " Velocity \n" - // " Acceleration \n" - // " Mass \n" - // " Temperature \n" - - //}, - {"parseQuantity", (PyCFunction) UnitsApi::sParseQuantity, METH_VARARGS, + {"parseQuantity", UnitsApi::sParseQuantity, METH_VARARGS, "parseQuantity(string) -> Base.Quantity()\n\n" "calculate a mathematical expression with units to a quantity object. \n" "can be used for simple unit translation like: \n" @@ -76,77 +51,31 @@ PyMethodDef UnitsApi::Methods[] = { "or for more complex espressions:\n" "parseQuantity('sin(pi)/50.0 m/s^2')\n" }, - {"listSchemas", (PyCFunction) UnitsApi::sListSchemas, METH_VARARGS, + {"listSchemas", UnitsApi::sListSchemas, METH_VARARGS, "listSchemas() -> a tuple of schemas\n\n" "listSchemas(int) -> description of the given schema\n\n" }, - {"getSchema", (PyCFunction) UnitsApi::sGetSchema, METH_VARARGS, + {"getSchema", UnitsApi::sGetSchema, METH_VARARGS, "getSchema() -> int\n\n" "The int is the position of the tuple returned by listSchemas" }, - {"setSchema", (PyCFunction) UnitsApi::sSetSchema, METH_VARARGS, + {"setSchema", UnitsApi::sSetSchema, METH_VARARGS, "setSchema(int) -> None\n\n" "Sets the current schema to the given number, if possible" }, - {"schemaTranslate", (PyCFunction) UnitsApi::sSchemaTranslate, METH_VARARGS, + {"schemaTranslate", UnitsApi::sSchemaTranslate, METH_VARARGS, "schemaTranslate(Quantity, int) -> tuple\n\n" "Translate a quantity to a given schema" }, - {NULL, NULL, 0, NULL} /* Sentinel */ + {nullptr, nullptr, 0, nullptr} /* Sentinel */ }; -//PyObject* UnitsApi::sTranslateUnit(PyObject * /*self*/, PyObject *args) -//{ -// char *pstr; -// if (!PyArg_ParseTuple(args, "s", &pstr)) // convert args: Python->C -// return NULL; // NULL triggers exception -// try { -// return Py::new_reference_to(Py::Object(Py::Float(UnitsApi::translateUnit(pstr)))); -// } -// catch (const Base::Exception& e) { -// PyErr_Format(PyExc_IOError, "invalid unit expression %s: %s\n", pstr, e.what()); -// return 0L; -// } -// catch (const std::exception& e) { -// PyErr_Format(PyExc_IOError, "invalid unit expression %s: %s\n", pstr, e.what()); -// return 0L; -// } -//} -// -//PyObject* UnitsApi::sGetWithPrefs(PyObject * /*self*/, PyObject *args) -//{ -// char *type; -// PyObject *obj; -// if (!PyArg_ParseTuple(args, "sO", &type,&obj)) // convert args: Python->C -// return NULL; // NULL triggers exception -// try { -// QuantityType t; -// if(strcmp("Length",type)==0) -// t = Length; -// else{ -// PyErr_Format(PyExc_IOError, "invalid quantity type: %s!", type); -// return 0L; -// } -// -// double result = toDblWithUserPrefs(t,obj); -// return Py::new_reference_to(Py::Object(Py::Float(result))); -// } -// catch (const Base::Exception&) { -// PyErr_Format(PyExc_IOError, "invalid unit expression \n"); -// return 0L; -// } -// catch (const std::exception&) { -// PyErr_Format(PyExc_IOError, "invalid unit expression \n"); -// return 0L; -// } -//} - PyObject* UnitsApi::sParseQuantity(PyObject * /*self*/, PyObject *args) { char *pstr; - if (!PyArg_ParseTuple(args, "et", "utf-8", &pstr)) // convert args: Python->C - return NULL; // NULL triggers exception + if (!PyArg_ParseTuple(args, "et", "utf-8", &pstr)) + return nullptr; Quantity rtn; QString qstr = QString::fromUtf8(pstr); @@ -180,22 +109,22 @@ PyObject* UnitsApi::sListSchemas(PyObject * /*self*/, PyObject *args) int num = static_cast(UnitSystem::NumUnitSystemTypes); if (index < 0 || index >= num) { PyErr_SetString(PyExc_ValueError, "invalid schema value"); - return 0; + return nullptr; } return Py_BuildValue("s", UnitsApi::getDescription(static_cast(index))); } PyErr_SetString(PyExc_TypeError, "int or empty argument list expected"); - return 0; + return nullptr; } PyObject* UnitsApi::sGetSchema(PyObject * /*self*/, PyObject *args) { if (!PyArg_ParseTuple(args, "")) - return NULL; + return nullptr; - return Py_BuildValue("i", static_cast(actSystem)); + return Py_BuildValue("i", static_cast(currentSystem)); } PyObject* UnitsApi::sSetSchema(PyObject * /*self*/, PyObject *args) @@ -206,7 +135,7 @@ PyObject* UnitsApi::sSetSchema(PyObject * /*self*/, PyObject *args) int num = static_cast(UnitSystem::NumUnitSystemTypes); if (index < 0 || index >= num) { PyErr_SetString(PyExc_ValueError, "invalid schema value"); - return 0; + return nullptr; } setSchema((UnitSystem)index); } @@ -218,7 +147,7 @@ PyObject* UnitsApi::sSchemaTranslate(PyObject * /*self*/, PyObject *args) PyObject* q; int index; if (!PyArg_ParseTuple(args, "O!i", &(QuantityPy::Type), &q, &index)) - return 0; + return nullptr; Quantity quant; quant = *static_cast(q)->getQuantityPtr(); @@ -226,7 +155,7 @@ PyObject* UnitsApi::sSchemaTranslate(PyObject * /*self*/, PyObject *args) std::unique_ptr schema(createSchema(static_cast(index))); if (!schema.get()) { PyErr_SetString(PyExc_ValueError, "invalid schema value"); - return 0; + return nullptr; } double factor; diff --git a/src/Mod/Robot/App/WaypointPyImp.cpp b/src/Mod/Robot/App/WaypointPyImp.cpp index cfb17f8a79..4fd590cdba 100644 --- a/src/Mod/Robot/App/WaypointPyImp.cpp +++ b/src/Mod/Robot/App/WaypointPyImp.cpp @@ -129,14 +129,14 @@ int WaypointPy::PyInit(PyObject* args, PyObject* kwd) getWaypointPtr()->Velocity = 0; } else - getWaypointPtr()->Velocity = Base::UnitsApi::toDbl(vel,Base::Unit::Velocity); + getWaypointPtr()->Velocity = Base::UnitsApi::toDouble(vel,Base::Unit::Velocity); getWaypointPtr()->Cont = cont?true:false; getWaypointPtr()->Tool = tool; getWaypointPtr()->Base = base; if(acc == 0) getWaypointPtr()->Accelaration = 100; else - getWaypointPtr()->Accelaration = Base::UnitsApi::toDbl(acc,Base::Unit::Acceleration); + getWaypointPtr()->Accelaration = Base::UnitsApi::toDouble(acc,Base::Unit::Acceleration); return 0; }