| 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 <array> |
| 26 |
#include <cmath> |
| 27 |
#include <limits> |
| 28 |
#include <numbers> |
| 29 |
#include <sstream> |
| 30 |
#include <string> |
| 31 |
|
| 32 |
#include <fmt/format.h> |
| 33 |
|
| 34 |
#include "Exception.h" |
| 35 |
#include "Quantity.h" |
| 36 |
#include "Tools.h" |
| 37 |
#include "UnitsApi.h" |
| 38 |
#include "UnitsConvData.h" |
| 39 |
#include "UnitsSchema.h" |
| 40 |
|
| 41 |
/** \defgroup Units Units system |
| 42 |
\ingroup BASE |
| 43 |
\brief The quantities and units system enables FreeCAD to work transparently with many different |
| 44 |
units |
| 45 |
*/ |
| 46 |
|
| 47 |
// suppress annoying warnings from generated source files |
| 48 |
#ifdef _MSC_VER |
| 49 |
# pragma warning(disable : 4003) |
| 50 |
# pragma warning(disable : 4018) |
| 51 |
# pragma warning(disable : 4065) |
| 52 |
# pragma warning(disable : 4273) |
| 53 |
# pragma warning(disable : 4335) // disable MAC file format warning on VC |
| 54 |
#endif |
| 55 |
|
| 56 |
using Base::Quantity; |
| 57 |
using Base::QuantityFormat; |
| 58 |
using Base::UnitsSchema; |
| 59 |
|
| 60 |
QuantityFormat::QuantityFormat() |
| 61 |
: option(OmitGroupSeparator | RejectGroupSeparator) |
| 62 |
, format(Fixed) |
| 63 |
, _precision(-1) |
| 64 |
, _denominator(-1) |
| 65 |
{} |
| 66 |
|
| 67 |
QuantityFormat::QuantityFormat(QuantityFormat::NumberFormat format, int decimals) |
| 68 |
: option(OmitGroupSeparator | RejectGroupSeparator) |
| 69 |
, format(format) |
| 70 |
, _precision(decimals) |
| 71 |
, _denominator(-1) |
| 72 |
{} |
| 73 |
|
| 74 |
int QuantityFormat::getPrecision() const |
| 75 |
{ |
| 76 |
return _precision < 0 ? UnitsApi::getDecimals() : _precision; |
| 77 |
} |
| 78 |
|
| 79 |
int QuantityFormat::getDenominator() const |
| 80 |
{ |
| 81 |
return _denominator < 0 ? UnitsApi::getDenominator() : _denominator; |
| 82 |
} |
| 83 |
|
| 84 |
// ---------------------------------------------------------------------------- |
| 85 |
|
| 86 |
Quantity::Quantity() |
| 87 |
: myValue {0.0} |
| 88 |
{} |
| 89 |
|
| 90 |
Quantity::Quantity(double value, const Unit& unit) |
| 91 |
: myValue {value} |
| 92 |
, myUnit {unit} |
| 93 |
{} |
| 94 |
|
| 95 |
Quantity::Quantity(double value, const std::string& unit) |
| 96 |
{ |
| 97 |
if (unit.empty()) { |
| 98 |
myValue = value; |
| 99 |
myUnit = Unit(); |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
try { |
| 104 |
auto tmpQty = parse(unit); |
| 105 |
myValue = value * tmpQty.getValue(); |
| 106 |
myUnit = tmpQty.getUnit(); |
| 107 |
} |
| 108 |
catch (const Base::ParserError&) { |
| 109 |
myValue = 0.0; |
| 110 |
myUnit = Unit(); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
double Quantity::getValueAs(const Quantity& other) const |
| 115 |
{ |
| 116 |
return myValue / other.getValue(); |
| 117 |
} |
| 118 |
|
| 119 |
bool Quantity::operator==(const Quantity& that) const |
| 120 |
{ |
| 121 |
return (myValue == that.myValue) && (myUnit == that.myUnit); |
| 122 |
} |
| 123 |
|
| 124 |
bool Quantity::operator!=(const Quantity& that) const |
| 125 |
{ |
| 126 |
return !(*this == that); |
| 127 |
} |
| 128 |
|
| 129 |
bool Quantity::operator<(const Quantity& that) const |
| 130 |
{ |
| 131 |
if (myUnit != that.myUnit) { |
| 132 |
throw Base::UnitsMismatchError( |
| 133 |
"Quantity::operator <(): quantities need to have same unit to compare" |
| 134 |
); |
| 135 |
} |
| 136 |
|
| 137 |
return (myValue < that.myValue); |
| 138 |
} |
| 139 |
|
| 140 |
bool Quantity::operator>(const Quantity& that) const |
| 141 |
{ |
| 142 |
if (myUnit != that.myUnit) { |
| 143 |
throw Base::UnitsMismatchError( |
| 144 |
"Quantity::operator >(): quantities need to have same unit to compare" |
| 145 |
); |
| 146 |
} |
| 147 |
|
| 148 |
return (myValue > that.myValue); |
| 149 |
} |
| 150 |
|
| 151 |
bool Quantity::operator<=(const Quantity& that) const |
| 152 |
{ |
| 153 |
if (myUnit != that.myUnit) { |
| 154 |
throw Base::UnitsMismatchError( |
| 155 |
"Quantity::operator <=(): quantities need to have same unit to compare" |
| 156 |
); |
| 157 |
} |
| 158 |
|
| 159 |
return (myValue <= that.myValue); |
| 160 |
} |
| 161 |
|
| 162 |
bool Quantity::operator>=(const Quantity& that) const |
| 163 |
{ |
| 164 |
if (myUnit != that.myUnit) { |
| 165 |
throw Base::UnitsMismatchError( |
| 166 |
"Quantity::operator >=(): quantities need to have same unit to compare" |
| 167 |
); |
| 168 |
} |
| 169 |
|
| 170 |
return (myValue >= that.myValue); |
| 171 |
} |
| 172 |
|
| 173 |
Quantity Quantity::operator*(const Quantity& other) const |
| 174 |
{ |
| 175 |
return Quantity(myValue * other.myValue, myUnit * other.myUnit); |
| 176 |
} |
| 177 |
|
| 178 |
Quantity Quantity::operator*(double factor) const |
| 179 |
{ |
| 180 |
return Quantity(myValue * factor, myUnit); |
| 181 |
} |
| 182 |
|
| 183 |
Quantity Quantity::operator/(const Quantity& other) const |
| 184 |
{ |
| 185 |
return Quantity(myValue / other.myValue, myUnit / other.myUnit); |
| 186 |
} |
| 187 |
|
| 188 |
Quantity Quantity::operator/(double factor) const |
| 189 |
{ |
| 190 |
return Quantity(myValue / factor, myUnit); |
| 191 |
} |
| 192 |
|
| 193 |
Quantity Quantity::pow(const Quantity& other) const |
| 194 |
{ |
| 195 |
if (!other.isDimensionless()) { |
| 196 |
throw Base::UnitsMismatchError("Quantity::pow(): exponent must not have a unit"); |
| 197 |
} |
| 198 |
|
| 199 |
return Quantity( |
| 200 |
std::pow(myValue, other.myValue), |
| 201 |
myUnit.pow(static_cast<signed char>(other.myValue)) |
| 202 |
); |
| 203 |
} |
| 204 |
|
| 205 |
Quantity Quantity::pow(double exp) const |
| 206 |
{ |
| 207 |
return Quantity(std::pow(myValue, exp), myUnit.pow(exp)); |
| 208 |
} |
| 209 |
|
| 210 |
Quantity Quantity::operator+(const Quantity& other) const |
| 211 |
{ |
| 212 |
if (myUnit != other.myUnit) { |
| 213 |
throw Base::UnitsMismatchError("Quantity::operator +(): Unit mismatch in plus operation"); |
| 214 |
} |
| 215 |
|
| 216 |
return Quantity(myValue + other.myValue, myUnit); |
| 217 |
} |
| 218 |
|
| 219 |
Quantity& Quantity::operator+=(const Quantity& other) |
| 220 |
{ |
| 221 |
if (myUnit != other.myUnit) { |
| 222 |
throw Base::UnitsMismatchError("Quantity::operator +=(): Unit mismatch in plus operation"); |
| 223 |
} |
| 224 |
|
| 225 |
myValue += other.myValue; |
| 226 |
|
| 227 |
return *this; |
| 228 |
} |
| 229 |
|
| 230 |
Quantity Quantity::operator-(const Quantity& other) const |
| 231 |
{ |
| 232 |
if (myUnit != other.myUnit) { |
| 233 |
throw Base::UnitsMismatchError("Quantity::operator -(): Unit mismatch in minus operation"); |
| 234 |
} |
| 235 |
|
| 236 |
return Quantity(myValue - other.myValue, myUnit); |
| 237 |
} |
| 238 |
|
| 239 |
Quantity& Quantity::operator-=(const Quantity& other) |
| 240 |
{ |
| 241 |
if (myUnit != other.myUnit) { |
| 242 |
throw Base::UnitsMismatchError("Quantity::operator -=(): Unit mismatch in minus operation"); |
| 243 |
} |
| 244 |
|
| 245 |
myValue -= other.myValue; |
| 246 |
|
| 247 |
return *this; |
| 248 |
} |
| 249 |
|
| 250 |
Quantity Quantity::operator-() const |
| 251 |
{ |
| 252 |
return Quantity(-myValue, myUnit); |
| 253 |
} |
| 254 |
|
| 255 |
std::string Quantity::toString(const QuantityFormat& format) const |
| 256 |
{ |
| 257 |
return fmt::format("'{} {}'", toNumber(format), myUnit.getString()); |
| 258 |
} |
| 259 |
|
| 260 |
std::string Quantity::toNumber(const QuantityFormat& format) const |
| 261 |
{ |
| 262 |
std::stringstream ss; |
| 263 |
|
| 264 |
switch (format.format) { |
| 265 |
case QuantityFormat::Fixed: |
| 266 |
ss << std::fixed; |
| 267 |
break; |
| 268 |
case QuantityFormat::Scientific: |
| 269 |
ss << std::scientific; |
| 270 |
break; |
| 271 |
default: |
| 272 |
break; |
| 273 |
} |
| 274 |
ss << std::setprecision(format.getPrecision()) << myValue; |
| 275 |
|
| 276 |
return ss.str(); |
| 277 |
} |
| 278 |
|
| 279 |
std::string Quantity::getUserString() const |
| 280 |
{ |
| 281 |
double dummy1 {}; // to satisfy GCC |
| 282 |
std::string dummy2 {}; |
| 283 |
return getUserString(dummy1, dummy2); |
| 284 |
} |
| 285 |
|
| 286 |
std::string Quantity::getUserString(double& factor, std::string& unitString) const |
| 287 |
{ |
| 288 |
return Base::UnitsApi::schemaTranslate(*this, factor, unitString); |
| 289 |
} |
| 290 |
|
| 291 |
std::string Quantity::getUserString(UnitsSchema* schema, double& factor, std::string& unitString) const |
| 292 |
{ |
| 293 |
return schema->translate(*this, factor, unitString); |
| 294 |
} |
| 295 |
|
| 296 |
std::string Quantity::getSafeUserString() const |
| 297 |
{ |
| 298 |
auto userStr = getUserString(); |
| 299 |
if (myValue != 0.0 && parse(userStr).getValue() == 0) { |
| 300 |
auto unitStr = getUnit().getString(); |
| 301 |
userStr = fmt::format("{}{}{}", myValue, unitStr.empty() ? "" : " ", unitStr); |
| 302 |
} |
| 303 |
|
| 304 |
return Tools::escapeQuotesFromString(userStr); |
| 305 |
} |
| 306 |
|
| 307 |
/// true if unit equals to 1, therefore quantity has no dimension |
| 308 |
bool Quantity::isDimensionless() const |
| 309 |
{ |
| 310 |
return myUnit == Unit::One; |
| 311 |
} |
| 312 |
|
| 313 |
/// true if it has a specific unit or no dimension. |
| 314 |
bool Quantity::isDimensionlessOrUnit(const Unit& unit) const |
| 315 |
{ |
| 316 |
return isDimensionless() || myUnit == unit; |
| 317 |
} |
| 318 |
|
| 319 |
// true if it has a number with or without a unit |
| 320 |
bool Quantity::isValid() const |
| 321 |
{ |
| 322 |
return !std::isnan(myValue); |
| 323 |
} |
| 324 |
|
| 325 |
void Quantity::setInvalid() |
| 326 |
{ |
| 327 |
myValue = std::numeric_limits<double>::quiet_NaN(); |
| 328 |
} |
| 329 |
|
| 330 |
// === Predefined types ===================================================== |
| 331 |
// clang-format off |
| 332 |
using namespace Base::UnitsConvData; |
| 333 |
|
| 334 |
const Quantity Quantity::NanoMetre ( 1.0e-6 , Unit::Length ); |
| 335 |
const Quantity Quantity::MicroMetre ( 1.0e-3 , Unit::Length ); |
| 336 |
const Quantity Quantity::MilliMetre ( 1.0 , Unit::Length ); |
| 337 |
const Quantity Quantity::CentiMetre ( 10.0 , Unit::Length ); |
| 338 |
const Quantity Quantity::DeciMetre ( 100.0 , Unit::Length ); |
| 339 |
const Quantity Quantity::Metre ( 1.0e3 , Unit::Length ); |
| 340 |
const Quantity Quantity::KiloMetre ( 1.0e6 , Unit::Length ); |
| 341 |
|
| 342 |
const Quantity Quantity::MilliLiter ( 1000.0 , Unit::Volume ); |
| 343 |
const Quantity Quantity::Liter ( 1.0e6 , Unit::Volume ); |
| 344 |
|
| 345 |
const Quantity Quantity::Hertz ( 1.0 , Unit::Frequency ); |
| 346 |
const Quantity Quantity::KiloHertz ( 1.0e3 , Unit::Frequency ); |
| 347 |
const Quantity Quantity::MegaHertz ( 1.0e6 , Unit::Frequency ); |
| 348 |
const Quantity Quantity::GigaHertz ( 1.0e9 , Unit::Frequency ); |
| 349 |
const Quantity Quantity::TeraHertz ( 1.0e12 , Unit::Frequency ); |
| 350 |
|
| 351 |
const Quantity Quantity::MicroGram ( 1.0e-9 , Unit::Mass ); |
| 352 |
const Quantity Quantity::MilliGram ( 1.0e-6 , Unit::Mass ); |
| 353 |
const Quantity Quantity::Gram ( 1.0e-3 , Unit::Mass ); |
| 354 |
const Quantity Quantity::KiloGram ( 1.0 , Unit::Mass ); |
| 355 |
const Quantity Quantity::Ton ( 1.0e3 , Unit::Mass ); |
| 356 |
|
| 357 |
const Quantity Quantity::Second ( 1.0 , Unit::TimeSpan ); |
| 358 |
const Quantity Quantity::Minute ( 60.0 , Unit::TimeSpan ); |
| 359 |
const Quantity Quantity::Hour ( 3600.0 , Unit::TimeSpan ); |
| 360 |
|
| 361 |
const Quantity Quantity::Ampere ( 1.0 , Unit::ElectricCurrent ); |
| 362 |
const Quantity Quantity::MilliAmpere ( 0.001 , Unit::ElectricCurrent ); |
| 363 |
const Quantity Quantity::KiloAmpere ( 1000.0 , Unit::ElectricCurrent ); |
| 364 |
const Quantity Quantity::MegaAmpere ( 1.0e6 , Unit::ElectricCurrent ); |
| 365 |
|
| 366 |
const Quantity Quantity::Kelvin ( 1.0 , Unit::Temperature ); |
| 367 |
const Quantity Quantity::MilliKelvin ( 0.001 , Unit::Temperature ); |
| 368 |
const Quantity Quantity::MicroKelvin ( 0.000001 , Unit::Temperature ); |
| 369 |
|
| 370 |
const Quantity Quantity::MilliMole ( 0.001 , Unit::AmountOfSubstance ); |
| 371 |
const Quantity Quantity::Mole ( 1.0 , Unit::AmountOfSubstance ); |
| 372 |
|
| 373 |
const Quantity Quantity::Candela ( 1.0 , Unit::LuminousIntensity ); |
| 374 |
|
| 375 |
const Quantity Quantity::Inch ( in , Unit::Length ); |
| 376 |
const Quantity Quantity::Foot ( ft , Unit::Length ); |
| 377 |
const Quantity Quantity::Thou ( in / 1000 , Unit::Length ); |
| 378 |
const Quantity Quantity::Yard ( yd , Unit::Length ); |
| 379 |
const Quantity Quantity::Mile ( mi , Unit::Length ); |
| 380 |
|
| 381 |
const Quantity Quantity::MilePerHour ( mi / 3600 , Unit::Velocity ); |
| 382 |
|
| 383 |
const Quantity Quantity::SquareFoot ( ft * ft , Unit::Area ); |
| 384 |
const Quantity Quantity::CubicFoot ( ft * ft * ft , Unit::Volume ); |
| 385 |
|
| 386 |
const Quantity Quantity::Pound ( lb , Unit::Mass ); |
| 387 |
const Quantity Quantity::Ounce ( lb / 16 , Unit::Mass ); |
| 388 |
const Quantity Quantity::Stone ( lb * 14 , Unit::Mass ); |
| 389 |
const Quantity Quantity::Hundredweights ( lb * 112 , Unit::Mass ); |
| 390 |
|
| 391 |
const Quantity Quantity::PoundForce ( 1000 * lbf , Unit::Force ); |
| 392 |
|
| 393 |
const Quantity Quantity::Newton ( 1000.0 , Unit::Force ); // Newton (kg*m/s^2) |
| 394 |
const Quantity Quantity::MilliNewton ( 1.0 , Unit::Force ); |
| 395 |
const Quantity Quantity::KiloNewton ( 1e+6 , Unit::Force ); |
| 396 |
const Quantity Quantity::MegaNewton ( 1e+9 , Unit::Force ); |
| 397 |
|
| 398 |
const Quantity Quantity::NewtonPerMeter ( 1.00 , Unit::Stiffness ); // Newton per meter (N/m or kg/s^2) |
| 399 |
const Quantity Quantity::MilliNewtonPerMeter ( 1e-3 , Unit::Stiffness ); |
| 400 |
const Quantity Quantity::KiloNewtonPerMeter ( 1e3 , Unit::Stiffness ); |
| 401 |
const Quantity Quantity::MegaNewtonPerMeter ( 1e6 , Unit::Stiffness ); |
| 402 |
|
| 403 |
const Quantity Quantity::Pascal ( 0.001 , Unit::Pressure ); // Pascal (kg/m/s^2 or N/m^2) |
| 404 |
const Quantity Quantity::KiloPascal ( 1.00 , Unit::Pressure ); |
| 405 |
const Quantity Quantity::MegaPascal ( 1000.0 , Unit::Pressure ); |
| 406 |
const Quantity Quantity::GigaPascal ( 1e+6 , Unit::Pressure ); |
| 407 |
|
| 408 |
const Quantity Quantity::MilliBar ( 0.1 , Unit::Pressure ); |
| 409 |
const Quantity Quantity::Bar ( 100.0 , Unit::Pressure ); // 1 bar = 100 kPa |
| 410 |
|
| 411 |
const Quantity Quantity::Torr ( 101.325 / 760.0 , Unit::Pressure ); // Torr is a defined fraction of Pascal (kg/m/s^2 or N/m^2) |
| 412 |
const Quantity Quantity::mTorr ( 101.325 / 760.0 / 1e3 , Unit::Pressure ); // Torr is a defined fraction of Pascal (kg/m/s^2 or N/m^2) |
| 413 |
const Quantity Quantity::yTorr ( 101.325 / 760.0 / 1e6 , Unit::Pressure ); // Torr is a defined fraction of Pascal (kg/m/s^2 or N/m^2) |
| 414 |
|
| 415 |
const Quantity Quantity::PSI ( psi , Unit::Pressure ); |
| 416 |
const Quantity Quantity::KSI ( psi * 1000 , Unit::Pressure ); |
| 417 |
const Quantity Quantity::MPSI ( psi * 1000000 , Unit::Pressure ); |
| 418 |
|
| 419 |
const Quantity Quantity::Watt ( 1e+6 , Unit::Power ); // Watt (kg*m^2/s^3) |
| 420 |
const Quantity Quantity::MilliWatt ( 1e+3 , Unit::Power ); |
| 421 |
const Quantity Quantity::KiloWatt ( 1e+9 , Unit::Power ); |
| 422 |
const Quantity Quantity::VoltAmpere ( 1e+6 , Unit::Power ); // VoltAmpere (kg*m^2/s^3) |
| 423 |
|
| 424 |
const Quantity Quantity::Volt ( 1e+6 , Unit::ElectricPotential ); // Volt (kg*m^2/A/s^3) |
| 425 |
const Quantity Quantity::MilliVolt ( 1e+3 , Unit::ElectricPotential ); |
| 426 |
const Quantity Quantity::KiloVolt ( 1e+9 , Unit::ElectricPotential ); |
| 427 |
|
| 428 |
const Quantity Quantity::MegaSiemens ( 1.0 , Unit::ElectricalConductance ); |
| 429 |
const Quantity Quantity::KiloSiemens ( 1e-3 , Unit::ElectricalConductance ); |
| 430 |
const Quantity Quantity::Siemens ( 1e-6 , Unit::ElectricalConductance ); // Siemens (A^2*s^3/kg/m^2) |
| 431 |
const Quantity Quantity::MilliSiemens ( 1e-9 , Unit::ElectricalConductance ); |
| 432 |
const Quantity Quantity::MicroSiemens ( 1e-12 , Unit::ElectricalConductance ); |
| 433 |
|
| 434 |
const Quantity Quantity::Ohm ( 1e+6 , Unit::ElectricalResistance ); // Ohm (kg*m^2/A^2/s^3) |
| 435 |
const Quantity Quantity::KiloOhm ( 1e+9 , Unit::ElectricalResistance ); |
| 436 |
const Quantity Quantity::MegaOhm ( 1e+12 , Unit::ElectricalResistance ); |
| 437 |
|
| 438 |
const Quantity Quantity::Coulomb ( 1.0 , Unit::ElectricCharge ); // Coulomb (A*s) |
| 439 |
|
| 440 |
const Quantity Quantity::Tesla ( 1.0 , Unit::MagneticFluxDensity ); // Tesla (kg/s^2/A) |
| 441 |
const Quantity Quantity::Gauss ( 1e-4 , Unit::MagneticFluxDensity ); // 1 G = 1e-4 T |
| 442 |
|
| 443 |
const Quantity Quantity::Weber ( 1e6 , Unit::MagneticFlux ); // Weber (kg*m^2/s^2/A) |
| 444 |
|
| 445 |
const Quantity Quantity::PicoFarad ( 1e-18 , Unit::ElectricalCapacitance ); |
| 446 |
const Quantity Quantity::NanoFarad ( 1e-15 , Unit::ElectricalCapacitance ); |
| 447 |
const Quantity Quantity::MicroFarad ( 1e-12 , Unit::ElectricalCapacitance ); |
| 448 |
const Quantity Quantity::MilliFarad ( 1e-9 , Unit::ElectricalCapacitance ); |
| 449 |
const Quantity Quantity::Farad ( 1e-6 , Unit::ElectricalCapacitance ); // Farad (s^4*A^2/m^2/kg) |
| 450 |
|
| 451 |
const Quantity Quantity::NanoHenry ( 1e-3 , Unit::ElectricalInductance ); |
| 452 |
const Quantity Quantity::MicroHenry ( 1.0 , Unit::ElectricalInductance ); |
| 453 |
const Quantity Quantity::MilliHenry ( 1e+3 , Unit::ElectricalInductance ); |
| 454 |
const Quantity Quantity::Henry ( 1e+6 , Unit::ElectricalInductance ); // Henry (kg*m^2/s^2/A^2) |
| 455 |
|
| 456 |
const Quantity Quantity::Joule ( 1e+6 , Unit::Work ); // Joule (kg*m^2/s^2) |
| 457 |
const Quantity Quantity::MilliJoule ( 1e+3 , Unit::Work ); |
| 458 |
const Quantity Quantity::KiloJoule ( 1e+9 , Unit::Work ); |
| 459 |
const Quantity Quantity::VoltAmpereSecond ( 1e+6 , Unit::Work ); // Joule (kg*m^2/s^2) |
| 460 |
const Quantity Quantity::WattSecond ( 1e+6 , Unit::Work ); // Joule (kg*m^2/s^2) |
| 461 |
const Quantity Quantity::KiloWattHour ( 3.6e+12 , Unit::Work ); // 1 kWh = 3.6e6 J |
| 462 |
const Quantity Quantity::ElectronVolt ( 1.602176634e-13 , Unit::Work ); // 1 eV = 1.602176634e-19 J |
| 463 |
const Quantity Quantity::KiloElectronVolt ( 1.602176634e-10 , Unit::Work ); |
| 464 |
const Quantity Quantity::MegaElectronVolt ( 1.602176634e-7 , Unit::Work ); |
| 465 |
const Quantity Quantity::Calorie ( 4.1868e+6 , Unit::Work ); // 1 cal = 4.1868 J |
| 466 |
const Quantity Quantity::KiloCalorie ( 4.1868e+9 , Unit::Work ); |
| 467 |
const Quantity Quantity::NewtonMeter ( 1e+6 , Unit::Moment ); // Joule (kg*m^2/s^2) |
| 468 |
|
| 469 |
const Quantity Quantity::KMH ( 1e+6 / 3600 , Unit::Velocity ); // km/h |
| 470 |
const Quantity Quantity::MPH ( mi / 3600 , Unit::Velocity ); // Mile/h |
| 471 |
|
| 472 |
const Quantity Quantity::AngMinute ( 1.0 / 60.0 , Unit::Angle ); // angular minute |
| 473 |
const Quantity Quantity::AngSecond ( 1.0 / 3600.0 , Unit::Angle ); // angular second |
| 474 |
const Quantity Quantity::Degree ( 1.0 , Unit::Angle ); // degree (internal standard angle) |
| 475 |
const Quantity Quantity::Radian ( 180 / std::numbers::pi, Unit::Angle ); // radian |
| 476 |
const Quantity Quantity::Gon ( 360.0 / 400.0 , Unit::Angle ); // gon |
| 477 |
// clang-format on |
| 478 |
|
| 479 |
// === Parser & Scanner stuff =============================================== |
| 480 |
|
| 481 |
// include the Scanner and the Parser for the 'Quantity's |
| 482 |
|
| 483 |
// NOLINTNEXTLINE |
| 484 |
Quantity QuantResult; |
| 485 |
|
| 486 |
/* helper function for tuning number strings with groups in a locale agnostic way... */ |
| 487 |
// NOLINTBEGIN |
| 488 |
double num_change(char* yytext, char dez_delim, char grp_delim) |
| 489 |
{ |
| 490 |
double ret_val {}; |
| 491 |
const int num = 40; |
| 492 |
std::array<char, num> temp {}; |
| 493 |
int iter = 0; |
| 494 |
for (char* ch = yytext; *ch != '\0'; ch++) { |
| 495 |
// skip group delimiter |
| 496 |
if (*ch == grp_delim) { |
| 497 |
continue; |
| 498 |
} |
| 499 |
// check for a dez delimiter other then dot |
| 500 |
if (*ch == dez_delim && dez_delim != '.') { |
| 501 |
temp[iter++] = '.'; |
| 502 |
} |
| 503 |
else { |
| 504 |
temp[iter++] = *ch; |
| 505 |
} |
| 506 |
// check buffer overflow |
| 507 |
if (iter >= num) { |
| 508 |
return 0.0; |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
temp[iter] = '\0'; |
| 513 |
|
| 514 |
ret_val = atof(temp.data()); |
| 515 |
return ret_val; |
| 516 |
} |
| 517 |
// NOLINTEND |
| 518 |
|
| 519 |
#if defined(__clang__) |
| 520 |
# pragma clang diagnostic push |
| 521 |
# pragma clang diagnostic ignored "-Wmissing-noreturn" |
| 522 |
#endif |
| 523 |
|
| 524 |
// error func |
| 525 |
void Quantity_yyerror(const char* errorinfo) |
| 526 |
{ |
| 527 |
throw Base::ParserError(errorinfo); |
| 528 |
} |
| 529 |
|
| 530 |
#if defined(__clang__) |
| 531 |
# pragma clang diagnostic pop |
| 532 |
#endif |
| 533 |
|
| 534 |
|
| 535 |
#if defined(__clang__) |
| 536 |
# pragma clang diagnostic push |
| 537 |
# pragma clang diagnostic ignored "-Wsign-compare" |
| 538 |
# pragma clang diagnostic ignored "-Wunneeded-internal-declaration" |
| 539 |
#elif defined(__GNUC__) |
| 540 |
# pragma GCC diagnostic push |
| 541 |
# pragma GCC diagnostic ignored "-Wsign-compare" |
| 542 |
# pragma GCC diagnostic ignored "-Wfree-nonheap-object" |
| 543 |
#endif |
| 544 |
|
| 545 |
namespace QuantityParser |
| 546 |
{ |
| 547 |
|
| 548 |
// NOLINTNEXTLINE |
| 549 |
#define YYINITDEPTH 20 |
| 550 |
// show parser the lexer method |
| 551 |
#define yylex QuantityLexer |
| 552 |
int QuantityLexer(); |
| 553 |
|
| 554 |
// Parser, defined in Quantity.y |
| 555 |
// NOLINTNEXTLINE |
| 556 |
#include "Quantity.tab.c" |
| 557 |
|
| 558 |
#ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 559 |
// Scanner, defined in Quantity.l |
| 560 |
// NOLINTNEXTLINE |
| 561 |
# include "Quantity.lex.c" |
| 562 |
#endif // DOXYGEN_SHOULD_SKIP_THIS |
| 563 |
|
| 564 |
class StringBufferCleaner |
| 565 |
{ |
| 566 |
public: |
| 567 |
explicit StringBufferCleaner(YY_BUFFER_STATE buffer) |
| 568 |
: my_string_buffer {buffer} |
| 569 |
{} |
| 570 |
~StringBufferCleaner() |
| 571 |
{ |
| 572 |
// free the scan buffer |
| 573 |
yy_delete_buffer(my_string_buffer); |
| 574 |
} |
| 575 |
|
| 576 |
StringBufferCleaner(const StringBufferCleaner&) = delete; |
| 577 |
StringBufferCleaner(StringBufferCleaner&&) = delete; |
| 578 |
StringBufferCleaner& operator=(const StringBufferCleaner&) = delete; |
| 579 |
StringBufferCleaner& operator=(StringBufferCleaner&&) = delete; |
| 580 |
|
| 581 |
private: |
| 582 |
YY_BUFFER_STATE my_string_buffer; |
| 583 |
}; |
| 584 |
|
| 585 |
} // namespace QuantityParser |
| 586 |
|
| 587 |
#if defined(__clang__) |
| 588 |
# pragma clang diagnostic pop |
| 589 |
#elif defined(__GNUC__) |
| 590 |
# pragma GCC diagnostic pop |
| 591 |
#endif |
| 592 |
|
| 593 |
Quantity Quantity::parse(const std::string& string) |
| 594 |
{ |
| 595 |
// parse from buffer |
| 596 |
QuantityParser::YY_BUFFER_STATE my_string_buffer = QuantityParser::yy_scan_string(string.c_str()); |
| 597 |
QuantityParser::StringBufferCleaner cleaner(my_string_buffer); |
| 598 |
// set the global return variables |
| 599 |
QuantResult = Quantity(std::numeric_limits<double>::min()); |
| 600 |
// run the parser |
| 601 |
QuantityParser::yyparse(); |
| 602 |
|
| 603 |
return QuantResult; |
| 604 |
} |