| 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 |
|
| 26 |
#ifndef BASE_Quantity_H |
| 27 |
#define BASE_Quantity_H |
| 28 |
|
| 29 |
#include "Unit.h" |
| 30 |
#include <string> |
| 31 |
|
| 32 |
namespace Base |
| 33 |
{ |
| 34 |
class UnitsSchema; |
| 35 |
|
| 36 |
struct BaseExport QuantityFormat |
| 37 |
{ |
| 38 |
enum NumberOption |
| 39 |
{ |
| 40 |
None = 0x00, |
| 41 |
OmitGroupSeparator = 0x01, |
| 42 |
RejectGroupSeparator = 0x02 |
| 43 |
}; |
| 44 |
enum NumberFormat |
| 45 |
{ |
| 46 |
Default = 0, |
| 47 |
Fixed = 1, |
| 48 |
Scientific = 2 |
| 49 |
}; |
| 50 |
|
| 51 |
using NumberOptions = int; |
| 52 |
NumberOptions option; |
| 53 |
NumberFormat format; |
| 54 |
|
| 55 |
int getPrecision() const; |
| 56 |
inline void setPrecision(int precision) |
| 57 |
{ |
| 58 |
_precision = precision; |
| 59 |
} |
| 60 |
|
| 61 |
int getDenominator() const; |
| 62 |
inline void setDenominator(int denominator) |
| 63 |
{ |
| 64 |
_denominator = denominator; |
| 65 |
} |
| 66 |
|
| 67 |
QuantityFormat(); |
| 68 |
explicit QuantityFormat(NumberFormat format, int decimals = -1); |
| 69 |
inline char toFormat() const |
| 70 |
{ |
| 71 |
switch (format) { |
| 72 |
case Fixed: |
| 73 |
return 'f'; |
| 74 |
case Scientific: |
| 75 |
return 'e'; |
| 76 |
default: |
| 77 |
return 'g'; |
| 78 |
} |
| 79 |
} |
| 80 |
static inline NumberFormat toFormat(char ch, bool* ok = nullptr) |
| 81 |
{ |
| 82 |
if (ok) { |
| 83 |
*ok = true; |
| 84 |
} |
| 85 |
switch (ch) { |
| 86 |
case 'f': |
| 87 |
return Fixed; |
| 88 |
case 'e': |
| 89 |
return Scientific; |
| 90 |
case 'g': |
| 91 |
return Default; |
| 92 |
default: |
| 93 |
if (ok) { |
| 94 |
*ok = false; |
| 95 |
} |
| 96 |
return Default; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
private: |
| 101 |
int _precision, _denominator; |
| 102 |
}; |
| 103 |
|
| 104 |
/** |
| 105 |
* The Quantity class. |
| 106 |
*/ |
| 107 |
class BaseExport Quantity |
| 108 |
{ |
| 109 |
public: |
| 110 |
/// default constructor |
| 111 |
Quantity(); |
| 112 |
Quantity(const Quantity&) = default; |
| 113 |
Quantity(Quantity&&) = default; |
| 114 |
explicit Quantity(double value, const Unit& unit = Unit()); |
| 115 |
explicit Quantity(double value, const std::string& unit); |
| 116 |
/// Destruction |
| 117 |
~Quantity() = default; |
| 118 |
|
| 119 |
/** Operators. */ |
| 120 |
//@{ |
| 121 |
Quantity operator*(const Quantity& other) const; |
| 122 |
Quantity operator*(double factor) const; |
| 123 |
Quantity operator+(const Quantity& other) const; |
| 124 |
Quantity& operator+=(const Quantity& other); |
| 125 |
Quantity operator-(const Quantity& other) const; |
| 126 |
Quantity& operator-=(const Quantity& other); |
| 127 |
Quantity operator-() const; |
| 128 |
Quantity operator/(const Quantity& other) const; |
| 129 |
Quantity operator/(double factor) const; |
| 130 |
bool operator==(const Quantity&) const; |
| 131 |
bool operator!=(const Quantity&) const; |
| 132 |
bool operator<(const Quantity&) const; |
| 133 |
bool operator>(const Quantity&) const; |
| 134 |
bool operator<=(const Quantity&) const; |
| 135 |
bool operator>=(const Quantity&) const; |
| 136 |
Quantity& operator=(const Quantity&) = default; |
| 137 |
Quantity& operator=(Quantity&&) = default; |
| 138 |
Quantity pow(const Quantity&) const; |
| 139 |
Quantity pow(double) const; |
| 140 |
//@} |
| 141 |
|
| 142 |
const QuantityFormat& getFormat() const |
| 143 |
{ |
| 144 |
return myFormat; |
| 145 |
} |
| 146 |
void setFormat(const QuantityFormat& fmt) |
| 147 |
{ |
| 148 |
myFormat = fmt; |
| 149 |
} |
| 150 |
|
| 151 |
std::string toString(const QuantityFormat& format = QuantityFormat(QuantityFormat::Default)) const; |
| 152 |
|
| 153 |
std::string toNumber(const QuantityFormat& format = QuantityFormat(QuantityFormat::Default)) const; |
| 154 |
|
| 155 |
std::string getUserString() const; |
| 156 |
/// transfer to user preferred unit/potence |
| 157 |
std::string getUserString(double& factor, std::string& unitString) const; |
| 158 |
std::string getUserString(UnitsSchema* schema, double& factor, std::string& unitString) const; |
| 159 |
std::string getSafeUserString() const; |
| 160 |
|
| 161 |
static Quantity parse(const std::string& string); |
| 162 |
|
| 163 |
/// returns the unit of the quantity |
| 164 |
const Unit& getUnit() const |
| 165 |
{ |
| 166 |
return myUnit; |
| 167 |
} |
| 168 |
/// set the unit of the quantity |
| 169 |
void setUnit(const Unit& un) |
| 170 |
{ |
| 171 |
myUnit = un; |
| 172 |
} |
| 173 |
/// get the Value of the quantity |
| 174 |
double getValue() const |
| 175 |
{ |
| 176 |
return myValue; |
| 177 |
} |
| 178 |
/// set the value of the quantity |
| 179 |
void setValue(double val) |
| 180 |
{ |
| 181 |
myValue = val; |
| 182 |
} |
| 183 |
/** get the Value in a special unit given as quantity. |
| 184 |
* One can use one of the predifeined quantity units in this class |
| 185 |
*/ |
| 186 |
double getValueAs(const Quantity&) const; |
| 187 |
|
| 188 |
|
| 189 |
/// true if it has no unit |
| 190 |
bool isDimensionless() const; |
| 191 |
/// true if it has a specific unit or no dimension. |
| 192 |
bool isDimensionlessOrUnit(const Unit& unit) const; |
| 193 |
/// true if it has a number with or without a unit |
| 194 |
bool isValid() const; |
| 195 |
/// sets the quantity invalid |
| 196 |
void setInvalid(); |
| 197 |
|
| 198 |
/** Predefined Unit types. */ |
| 199 |
//@{ |
| 200 |
static const Quantity NanoMetre; |
| 201 |
static const Quantity MicroMetre; |
| 202 |
static const Quantity CentiMetre; |
| 203 |
static const Quantity DeciMetre; |
| 204 |
static const Quantity Metre; |
| 205 |
static const Quantity MilliMetre; |
| 206 |
static const Quantity KiloMetre; |
| 207 |
|
| 208 |
static const Quantity Liter; |
| 209 |
static const Quantity MilliLiter; |
| 210 |
|
| 211 |
static const Quantity Hertz; |
| 212 |
static const Quantity KiloHertz; |
| 213 |
static const Quantity MegaHertz; |
| 214 |
static const Quantity GigaHertz; |
| 215 |
static const Quantity TeraHertz; |
| 216 |
|
| 217 |
static const Quantity MicroGram; |
| 218 |
static const Quantity MilliGram; |
| 219 |
static const Quantity Gram; |
| 220 |
static const Quantity KiloGram; |
| 221 |
static const Quantity Ton; |
| 222 |
|
| 223 |
static const Quantity Second; |
| 224 |
static const Quantity Minute; |
| 225 |
static const Quantity Hour; |
| 226 |
|
| 227 |
static const Quantity Ampere; |
| 228 |
static const Quantity MilliAmpere; |
| 229 |
static const Quantity KiloAmpere; |
| 230 |
static const Quantity MegaAmpere; |
| 231 |
|
| 232 |
static const Quantity Kelvin; |
| 233 |
static const Quantity MilliKelvin; |
| 234 |
static const Quantity MicroKelvin; |
| 235 |
|
| 236 |
static const Quantity MilliMole; |
| 237 |
static const Quantity Mole; |
| 238 |
|
| 239 |
static const Quantity Candela; |
| 240 |
|
| 241 |
static const Quantity Inch; |
| 242 |
static const Quantity Foot; |
| 243 |
static const Quantity Thou; |
| 244 |
static const Quantity Yard; |
| 245 |
static const Quantity Mile; |
| 246 |
|
| 247 |
static const Quantity MilePerHour; |
| 248 |
|
| 249 |
static const Quantity Pound; |
| 250 |
static const Quantity Ounce; |
| 251 |
static const Quantity Stone; |
| 252 |
static const Quantity Hundredweights; |
| 253 |
|
| 254 |
static const Quantity SquareFoot; |
| 255 |
static const Quantity CubicFoot; |
| 256 |
|
| 257 |
static const Quantity PoundForce; |
| 258 |
|
| 259 |
static const Quantity Newton; |
| 260 |
static const Quantity MilliNewton; |
| 261 |
static const Quantity KiloNewton; |
| 262 |
static const Quantity MegaNewton; |
| 263 |
|
| 264 |
static const Quantity NewtonPerMeter; |
| 265 |
static const Quantity MilliNewtonPerMeter; |
| 266 |
static const Quantity KiloNewtonPerMeter; |
| 267 |
static const Quantity MegaNewtonPerMeter; |
| 268 |
|
| 269 |
static const Quantity Pascal; |
| 270 |
static const Quantity KiloPascal; |
| 271 |
static const Quantity MegaPascal; |
| 272 |
static const Quantity GigaPascal; |
| 273 |
|
| 274 |
static const Quantity Bar; |
| 275 |
static const Quantity MilliBar; |
| 276 |
|
| 277 |
static const Quantity Torr; |
| 278 |
static const Quantity mTorr; |
| 279 |
static const Quantity yTorr; |
| 280 |
|
| 281 |
static const Quantity PSI; |
| 282 |
static const Quantity KSI; |
| 283 |
static const Quantity MPSI; |
| 284 |
|
| 285 |
static const Quantity Watt; |
| 286 |
static const Quantity MilliWatt; |
| 287 |
static const Quantity KiloWatt; |
| 288 |
static const Quantity VoltAmpere; |
| 289 |
|
| 290 |
static const Quantity Volt; |
| 291 |
static const Quantity MilliVolt; |
| 292 |
static const Quantity KiloVolt; |
| 293 |
|
| 294 |
static const Quantity MegaSiemens; |
| 295 |
static const Quantity KiloSiemens; |
| 296 |
static const Quantity Siemens; |
| 297 |
static const Quantity MilliSiemens; |
| 298 |
static const Quantity MicroSiemens; |
| 299 |
|
| 300 |
static const Quantity Ohm; |
| 301 |
static const Quantity KiloOhm; |
| 302 |
static const Quantity MegaOhm; |
| 303 |
|
| 304 |
static const Quantity Coulomb; |
| 305 |
|
| 306 |
static const Quantity Tesla; |
| 307 |
static const Quantity Gauss; |
| 308 |
|
| 309 |
static const Quantity Weber; |
| 310 |
|
| 311 |
static const Quantity Farad; |
| 312 |
static const Quantity MilliFarad; |
| 313 |
static const Quantity MicroFarad; |
| 314 |
static const Quantity NanoFarad; |
| 315 |
static const Quantity PicoFarad; |
| 316 |
|
| 317 |
static const Quantity Henry; |
| 318 |
static const Quantity MilliHenry; |
| 319 |
static const Quantity MicroHenry; |
| 320 |
static const Quantity NanoHenry; |
| 321 |
|
| 322 |
static const Quantity Joule; |
| 323 |
static const Quantity MilliJoule; |
| 324 |
static const Quantity KiloJoule; |
| 325 |
static const Quantity NewtonMeter; |
| 326 |
static const Quantity VoltAmpereSecond; |
| 327 |
static const Quantity WattSecond; |
| 328 |
static const Quantity KiloWattHour; |
| 329 |
static const Quantity ElectronVolt; |
| 330 |
static const Quantity KiloElectronVolt; |
| 331 |
static const Quantity MegaElectronVolt; |
| 332 |
static const Quantity Calorie; |
| 333 |
static const Quantity KiloCalorie; |
| 334 |
|
| 335 |
static const Quantity KMH; |
| 336 |
static const Quantity MPH; |
| 337 |
|
| 338 |
static const Quantity Degree; |
| 339 |
static const Quantity Radian; |
| 340 |
static const Quantity Gon; |
| 341 |
static const Quantity AngMinute; |
| 342 |
static const Quantity AngSecond; |
| 343 |
//@} |
| 344 |
|
| 345 |
private: |
| 346 |
double myValue; |
| 347 |
Unit myUnit; |
| 348 |
QuantityFormat myFormat; |
| 349 |
}; |
| 350 |
|
| 351 |
} // namespace Base |
| 352 |
|
| 353 |
#endif // BASE_Quantity_H |