9#include <QDBusArgument>
43namespace swift::misc::physical_quantities
45 template <
class MU,
class PQ>
51 template <
class MU,
class PQ>
57 template <
class MU,
class PQ>
63 template <
class MU,
class PQ>
66 return m_unit.getSymbol(
true);
69 template <
class MU,
class PQ>
71 : m_value(unit.
isNull() ? 0.0 :
value), m_unit(unit)
73 Q_ASSERT_X(!std::isnan(
value), Q_FUNC_INFO,
"nan value");
74 Q_ASSERT_X(!std::isinf(
value), Q_FUNC_INFO,
"infinity");
77 template <
class MU,
class PQ>
83 template <
class MU,
class PQ>
86 if (
this == &other)
return true;
88 if (this->isNull())
return other.
isNull();
89 if (other.
isNull())
return false;
91 double diff = std::abs(m_value - other.
value(m_unit));
92 return diff <= m_unit.getEpsilon();
95 template <
class MU,
class PQ>
98 m_value += other.
value(m_unit);
102 template <
class MU,
class PQ>
108 template <
class MU,
class PQ>
114 template <
class MU,
class PQ>
117 m_value -= other.
value(m_unit);
121 template <
class MU,
class PQ>
124 return m_unit.isEpsilon(m_value);
127 template <
class MU,
class PQ>
133 template <
class MU,
class PQ>
139 template <
class MU,
class PQ>
142 if (this->
isNull() || qFuzzyIsNull(m_value))
144 return *this->derived();
150 return *this->derived();
153 template <
class MU,
class PQ>
156 if (this->
isNull() || qFuzzyIsNull(m_value))
158 return *this->derived();
164 return *this->derived();
167 template <
class MU,
class PQ>
170 if (this->
isNull() || qFuzzyIsNull(m_value))
172 return *this->derived();
176 return *this->derived();
178 PQ copy(*this->derived());
179 return copy.makePositive();
182 template <
class MU,
class PQ>
185 constexpr double NaN = std::numeric_limits<double>::quiet_NaN();
186 argument << (this->
isNull() ? NaN : this->
value(UnitClass::defaultUnit()));
189 template <
class MU,
class PQ>
193 m_unit = UnitClass::defaultUnit();
194 if (std::isnan(m_value))
200 template <
class MU,
class PQ>
207 template <
class MU,
class PQ>
214 template <
class MU,
class PQ>
217 constexpr double NaN = std::numeric_limits<double>::quiet_NaN();
221 template <
class MU,
class PQ>
225 m_unit = UnitClass::defaultUnit();
226 if (std::isnan(m_value))
232 template <
class MU,
class PQ>
239 template <
class MU,
class PQ>
242 PQ copy = *derived();
247 template <
class MU,
class PQ>
254 template <
class MU,
class PQ>
257 PQ copy = *derived();
262 template <
class MU,
class PQ>
265 PQ copy = *derived();
270 template <
class MU,
class PQ>
273 if (*
this == other)
return false;
283 if (isNull() && other.
isNull())
288 return (m_value < other.
value(m_unit));
291 template <
class MU,
class PQ>
295 if (m_unit == newUnit || this->
isNull())
299 if (newUnit.isNull())
305 m_value = newUnit.convertFrom(m_value, m_unit);
311 template <
class MU,
class PQ>
314 if (m_unit == newUnit || this->
isNull())
319 copy.switchUnit(newUnit);
323 template <
class MU,
class PQ>
326 return m_unit.isNull();
329 template <
class MU,
class PQ>
333 m_unit = MU::nullUnit();
336 template <
class MU,
class PQ>
346 template <
class MU,
class PQ>
355 template <
class MU,
class PQ>
361 template <
class MU,
class PQ>
365 Q_ASSERT_X(!unit.isNull(), Q_FUNC_INFO,
"Cannot convert to null");
370 return unit.makeRoundedQStringWithUnit(this->
value(unit), digits, withGroupSeparator, i18n);
373 template <
class MU,
class PQ>
378 return QStringLiteral(
"null");
383 template <
class MU,
class PQ>
390 m_value = m_unit.roundToEpsilon(m_value);
393 template <
class MU,
class PQ>
396 Q_ASSERT_X(!unit.isNull(), Q_FUNC_INFO,
"Cannot convert to null");
397 return unit.roundValue(this->
value(unit), digits);
400 template <
class MU,
class PQ>
403 Q_ASSERT_X(!unit.isNull(), Q_FUNC_INFO,
"Cannot convert to null");
404 const double v = this->
value(unit);
408 template <
class MU,
class PQ>
414 template <
class MU,
class PQ>
423 return diff <= m_unit.getEpsilon();
426 template <
class MU,
class PQ>
432 template <
class MU,
class PQ>
437 return QStringLiteral(
"null");
443 template <
class MU,
class PQ>
446 Q_ASSERT_X(!unit.isNull(), Q_FUNC_INFO,
"Cannot convert to null");
447 return unit.convertFrom(m_value, m_unit);
450 template <
class MU,
class PQ>
455 return QStringLiteral(
"null");
460 template <
class MU,
class PQ>
471 return pq1 > pq2 ? pq1 : pq2;
474 template <
class MU,
class PQ>
485 return pq1 < pq2 ? pq1 : pq2;
488 template <
class MU,
class PQ>
491 static const PQ n(0, MU::nullUnit());
495 template <
class MU,
class PQ>
503 template <
class MU,
class PQ>
508 json.insert(
"unit",
QJsonValue(m_unit.getSymbol()));
512 template <
class MU,
class PQ>
521 if (
value.isUndefined())
527 m_value =
value.toDouble();
530 template <
class MU,
class PQ>
536 template <
class MU,
class PQ>
538 const MU &defaultUnitIfMissing)
542 const QString v =
value + defaultUnitIfMissing.getSymbol();
551 template <
class MU,
class PQ>
557 template <
class MU,
class PQ>
559 const MU &defaultUnitIfMissing)
564 v =
value + defaultUnitIfMissing.getSymbol();
569 pq.parseFromString(v, mode);
573 template <
class MU,
class PQ>
577 pq.parseFromString(
value, mode);
581 template <
class MU,
class PQ>
602 template <
class MU,
class PQ>
607 (*this) = variant.
value<PQ>();
613 case IndexValue: m_value = variant.
toDouble();
break;
614 case IndexUnit: m_unit = variant.
value<MU>();
break;
615 case IndexValueRounded0DigitsWithUnit:
616 case IndexValueRounded1DigitsWithUnit:
617 case IndexValueRounded2DigitsWithUnit:
618 case IndexValueRounded3DigitsWithUnit:
624 template <
class MU,
class PQ>
629 return compareImpl(*derived(), pq);
641 template <
class MU,
class PQ>
642 int CPhysicalQuantity<MU, PQ>::compareImpl(
const PQ &a,
const PQ &b)
658 template <
class MU,
class PQ>
659 PQ
const *CPhysicalQuantity<MU, PQ>::derived()
const
661 return static_cast<PQ
const *
>(
this);
664 template <
class MU,
class PQ>
665 PQ *CPhysicalQuantity<MU, PQ>::derived()
667 return static_cast<PQ *
>(
this);
Physical unit angle (radians, degrees).
Specialized class for angles (degrees, radian).
Specialized class for frequency (hertz, mega hertz, kilo hertz).
Thrown when a convertFromJson method encounters an unrecoverable error in JSON data.
Non-owning reference to a CPropertyIndex with a subset of its features.
QString toQString(bool i18n=false) const
Cast as QString.
CastType frontCasted() const
First element casted to given type, usually the PropertIndex enum.
bool isMyself() const
Myself index, used with nesting.
Tag type signifying overloaded marshalling methods that preserve data at the expense of size.
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
static CAccelerationUnit defaultUnit()
Default unit.
static U unitFromSymbol(const QString &symbol, bool strict=true)
Unit from symbol.
A physical quantity such as "5m", "20s", "1500ft/s".
double value() const
Value in current unit.
CPhysicalQuantity & operator+=(const CPhysicalQuantity &other)
Plus operator +=.
bool isNegativeWithEpsilonConsidered() const
Value <= 0 epsilon considered.
void setValueSameUnit(double value)
Change value without changing unit.
QString convertToQString(bool i18n=false) const
void parseFromString(const QString &value)
Parse value from string.
void unmarshallFromDbus(const QDBusArgument &argument)
Unmarshall without begin/endStructure, for when composed within another object.
static PQ parsedFromString(const QString &value, CPqString::SeparatorMode mode=CPqString::SeparatorBestGuess)
Parsed from given string.
PQ switchedUnit(const MU &newUnit) const
Return copy with switched unit.
const MU & getUnit() const
Unit.
PQ operator-() const
Unary operator -.
int valueInteger(MU unit) const
As integer value.
PQ operator/(double divide) const
Operator /.
PQ & switchUnit(const MU &newUnit)
Change unit, and convert value to maintain the same quantity.
void setUnitBySymbol(const QString &unitName)
Set unit by string.
size_t getValueHash() const
qHash overload, needed for storing value in a QSet.
bool isInteger() const
Is value an integer.
QJsonObject toJson() const
Cast to JSON object.
bool isNull() const
Is quantity null?
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
static const PQ & minValue(const PQ &pq1, const PQ &pq2)
Minimum of 2 quantities.
int comparePropertyByIndex(CPropertyIndexRef index, const PQ &pq) const
Compare for index.
CPhysicalQuantity & operator-=(const CPhysicalQuantity &other)
Minus operator-=.
double value(MU unit) const
Value in given unit.
void unmarshalFromDataStream(QDataStream &stream)
Unmarshal a value from a QDataStream.
CPhysicalQuantity(double value, MU unit)
Constructor with double.
QString valueRoundedAsString(MU unit, int digits=-1) const
Rounded value in given unit.
void roundToEpsilon()
Round current value in current unit to epsilon.
void addValueSameUnit(double value)
Add to the value in the current unit.
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
void setUnit(const MU &unit)
Simply set unit, do no calclulate conversion.
QString getUnitSymbol() const
Unit.
void substractValueSameUnit(double value)
Substract from the value in the current unit.
CPhysicalQuantity & operator/=(double divide)
Divide operator /=.
void marshalToDataStream(QDataStream &stream) const
Marshal a value to a QDataStream.
int valueInteger() const
As integer value in current unit.
void convertFromJson(const QJsonObject &json)
Assign from JSON object.
void setCurrentUnitValue(double value)
Set value in current unit.
const PQ & makePositive()
Make value always positive.
static const PQ & maxValue(const PQ &pq1, const PQ &pq2)
Maximum of 2 quantities.
static const PQ & null()
NULL PQ.
void marshallToDbus(QDBusArgument &argument) const
Marshall without begin/endStructure, for when composed within another object.
CPhysicalQuantity & operator*=(double multiply)
Multiply operator *=.
QString valueRoundedWithUnit(const MU &unit, int digits=-1, bool withGroupSeparator=false, bool i18n=false) const
Value to QString with the given unit, e.g. "5.00m".
bool isPositiveWithEpsilonConsidered() const
Value >= 0 epsilon considered.
double valueRounded(MU unit, int digits=-1) const
Rounded value in given unit.
PQ abs() const
Absolute value (always >=0).
const PQ & makeNegative()
Make value always negative.
PQ operator*(double multiply) const
Operator *.
bool isZeroEpsilonConsidered() const
Quantity value <= epsilon.
static PQ parse(const QString &value, SeparatorMode mode=SeparatorQtDefault)
Parse into concrete type.
SeparatorMode
Number separators / group separators.
@ SeparatorQtDefault
100000.00 no group separator
int compare(T a, T b)
Compare arithmetic values.
bool is09OrSeparatorOnlyString(const QString &testString)
String with 0-9/separator only.
size_t qHash(const std::string &key, uint seed)
std::string qHash
#define SWIFT_DEFINE_PQ_MIXINS(MU, PQ)
Explicit template definition of mixins for a CPhysicalQuantity subclass.
QJsonObject::iterator insert(QLatin1StringView key, const QJsonValue &value)
bool isUndefined() const const
QString toString() const const
QString number(double n, char format, int precision)
QVariant fromValue(T &&value)
double toDouble(bool *ok) const const
QString toString() const const
#define SWIFT_VERIFY_X(COND, WHERE, WHAT)
A weaker kind of assert.