From 7c5ce9d2568cb2a2575a1d52e60e3d2342b86f3d Mon Sep 17 00:00:00 2001 From: bofdahof <172177156+bofdahof@users.noreply.github.com> Date: Wed, 22 Jan 2025 09:33:00 +1000 Subject: [PATCH] Base: cleanup Quantity class --- src/Base/Quantity.cpp | 28 ++++++++++++++++++---------- src/Base/Quantity.h | 8 ++------ 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/Base/Quantity.cpp b/src/Base/Quantity.cpp index e5102dbb16..73ecaacf6f 100644 --- a/src/Base/Quantity.cpp +++ b/src/Base/Quantity.cpp @@ -26,14 +26,16 @@ #include #include #include +#include #endif #include -#include #include "Exception.h" #include "Quantity.h" +#include "Tools.h" #include "UnitsApi.h" +#include "UnitsSchema.h" /** \defgroup Units Units system \ingroup BASE @@ -50,7 +52,9 @@ #pragma warning(disable : 4335) // disable MAC file format warning on VC #endif -using namespace Base; +using Base::Quantity; +using Base::QuantityFormat; +using Base::UnitsSchema; // ====== Static attributes ========================= // NOLINTNEXTLINE @@ -237,6 +241,13 @@ Quantity Quantity::operator-() const return Quantity(-(this->myValue), this->myUnit); } +std::string Quantity::getUserString() const +{ + double dummy1 {}; // to satisfy GCC + std::string dummy2 {}; + return getUserString(dummy1, dummy2); +} + std::string Quantity::getUserString(double& factor, std::string& unitString) const { return Base::UnitsApi::schemaTranslate(*this, factor, unitString); @@ -250,15 +261,12 @@ Quantity::getUserString(UnitsSchema* schema, double& factor, std::string& unitSt std::string Quantity::getSafeUserString() const { - auto ret = getUserString(); - if (this->myValue) { - auto feedbackQty = parse(ret); - auto feedbackVal = feedbackQty.getValue(); - if (feedbackVal == 0) { - ret = fmt::format("{} {}", this->myValue, this->getUnit().getString()); - } + auto userStr = getUserString(); + if (myValue != 0.0 && parse(userStr).getValue() == 0) { + userStr = fmt::format("{} {}", myValue, getUnit().getString()); } - return Base::Tools::escapeQuotesFromString(ret); + + return Tools::escapeQuotesFromString(userStr); } /// true if it has a number without a unit diff --git a/src/Base/Quantity.h b/src/Base/Quantity.h index ccfc33cb96..ed0f947ac9 100644 --- a/src/Base/Quantity.h +++ b/src/Base/Quantity.h @@ -156,14 +156,10 @@ public: { myFormat = fmt; } + + std::string getUserString() const; /// transfer to user preferred unit/potence std::string getUserString(double& factor, std::string& unitString) const; - std::string getUserString() const - { // to satisfy GCC - double dummy1 {}; - std::string dummy2 {}; - return getUserString(dummy1, dummy2); - } std::string getUserString(UnitsSchema* schema, double& factor, std::string& unitString) const; std::string getSafeUserString() const;