swift
physicalquantity.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #ifndef SWIFT_MISC_PQ_PHYSICALQUANTITY_H
7 #define SWIFT_MISC_PQ_PHYSICALQUANTITY_H
8 
9 #include <QDBusArgument>
10 #include <QJsonObject>
11 #include <QString>
12 #include <QtGlobal>
13 
15 #include "misc/mixin/mixindbus.h"
16 #include "misc/mixin/mixinicon.h"
17 #include "misc/mixin/mixinindex.h"
18 #include "misc/mixin/mixinjson.h"
20 #include "misc/mixin/mixinstring.h"
21 #include "misc/pq/pqstring.h"
22 #include "misc/pq/units.h"
23 #include "misc/propertyindexref.h"
24 #include "misc/swiftmiscexport.h"
25 
26 namespace swift::misc::physical_quantities
27 {
28  template <class MU, class PQ>
29  class CPhysicalQuantity;
30 }
31 
33 #define SWIFT_TEMPLATE_PQ_MIXINS(MU, PQ, Extern, Export) \
34  namespace swift::misc::physical_quantities \
35  { \
36  class PQ; \
37  } \
38  namespace swift::misc::private_ns \
39  { \
40  Extern template struct Export CValueObjectMetaInfo<physical_quantities::PQ>; \
41  Extern template struct Export MetaTypeHelper<physical_quantities::PQ>; \
42  } \
43  namespace swift::misc::mixin \
44  { \
45  Extern template class Export \
46  DBusOperators<physical_quantities::CPhysicalQuantity<physical_quantities::MU, physical_quantities::PQ>>; \
47  Extern template class Export DataStreamOperators< \
48  physical_quantities::CPhysicalQuantity<physical_quantities::MU, physical_quantities::PQ>>; \
49  Extern template class Export \
50  JsonOperators<physical_quantities::CPhysicalQuantity<physical_quantities::MU, physical_quantities::PQ>>; \
51  Extern template class Export Index<physical_quantities::PQ>; \
52  Extern template class Export MetaType<physical_quantities::PQ>; \
53  Extern template class Export String<physical_quantities::PQ>; \
54  Extern template class Export \
55  Icon<physical_quantities::CPhysicalQuantity<physical_quantities::MU, physical_quantities::PQ>>; \
56  }
58 
69 #if defined(Q_OS_WIN) && defined(Q_CC_GNU)
70 # define SWIFT_DECLARE_PQ_MIXINS(MU, PQ)
71 # define SWIFT_DEFINE_PQ_MIXINS(MU, PQ)
72 #elif defined(Q_OS_WIN) && defined(Q_CC_CLANG)
73 # define SWIFT_DECLARE_PQ_MIXINS(MU, PQ) SWIFT_TEMPLATE_PQ_MIXINS(MU, PQ, extern, )
74 # define SWIFT_DEFINE_PQ_MIXINS(MU, PQ) SWIFT_TEMPLATE_PQ_MIXINS(MU, PQ, , SWIFT_MISC_EXPORT)
75 #else
76 # define SWIFT_DECLARE_PQ_MIXINS(MU, PQ) SWIFT_TEMPLATE_PQ_MIXINS(MU, PQ, extern, )
77 # define SWIFT_DEFINE_PQ_MIXINS(MU, PQ) SWIFT_TEMPLATE_PQ_MIXINS(MU, PQ, , )
78 #endif
79 
80 SWIFT_DECLARE_PQ_MIXINS(CAngleUnit, CAngle)
81 SWIFT_DECLARE_PQ_MIXINS(CLengthUnit, CLength)
82 SWIFT_DECLARE_PQ_MIXINS(CPressureUnit, CPressure)
83 SWIFT_DECLARE_PQ_MIXINS(CFrequencyUnit, CFrequency)
84 SWIFT_DECLARE_PQ_MIXINS(CMassUnit, CMass)
85 SWIFT_DECLARE_PQ_MIXINS(CTemperatureUnit, CTemperature)
86 SWIFT_DECLARE_PQ_MIXINS(CSpeedUnit, CSpeed)
87 SWIFT_DECLARE_PQ_MIXINS(CTimeUnit, CTime)
88 SWIFT_DECLARE_PQ_MIXINS(CAccelerationUnit, CAcceleration)
89 
90 namespace swift::misc::physical_quantities
91 {
92  class CAngle;
93  class CLength;
94  class CPressure;
95  class CFrequency;
96  class CMass;
97  class CTemperature;
98  class CSpeed;
99  class CTime;
100  class CAcceleration;
101 
105  template <class MU, class PQ>
107  public mixin::DBusOperators<CPhysicalQuantity<MU, PQ>>,
108  public mixin::DataStreamOperators<CPhysicalQuantity<MU, PQ>>,
109  public mixin::JsonOperators<CPhysicalQuantity<MU, PQ>>,
110  public mixin::Index<PQ>,
111  public mixin::MetaType<PQ>,
112  public mixin::String<PQ>,
113  public mixin::Icon<CPhysicalQuantity<MU, PQ>>
114  {
116  friend int compare(const PQ &a, const PQ &b) { return compareImpl(a, b); }
117 
118  public:
121  {
122  IndexUnit = CPropertyIndexRef::GlobalIndexCPhysicalQuantity,
123  IndexValue,
124  IndexValueRounded0DigitsWithUnit,
125  IndexValueRounded1DigitsWithUnit,
126  IndexValueRounded2DigitsWithUnit,
127  IndexValueRounded3DigitsWithUnit,
128  IndexValueRounded6DigitsWithUnit
129  };
130 
132  const MU &getUnit() const;
133 
136  void setUnit(const MU &unit);
137 
139  void setUnitBySymbol(const QString &unitName);
140 
142  QString getUnitSymbol() const;
143 
145  PQ &switchUnit(const MU &newUnit);
146 
148  PQ switchedUnit(const MU &newUnit) const;
149 
151  bool isNull() const;
152 
154  void setNull();
155 
157  double value(MU unit) const;
158 
160  double value() const;
161 
163  void setCurrentUnitValue(double value);
164 
166  int valueInteger(MU unit) const;
167 
169  QString valueIntegerAsString(MU unit) const { return QString::number(this->valueInteger(unit)); }
170 
172  int valueInteger() const;
173 
175  QString valueIntegerAsString() const { return QString::number(this->valueInteger()); }
176 
178  bool isInteger() const;
179 
182  double valueRounded(MU unit, int digits = -1) const;
183 
186  double valueRounded(int digits = -1) const;
187 
190  QString valueRoundedAsString(MU unit, int digits = -1) const;
191 
194  QString valueRoundedWithUnit(const MU &unit, int digits = -1, bool withGroupSeparator = false,
195  bool i18n = false) const;
196 
199  QString valueRoundedWithUnit(int digits = -1, bool withGroupSeparator = false, bool i18n = false) const;
200 
203  void roundToEpsilon();
204 
206  void setValueSameUnit(double value);
207 
209  void addValueSameUnit(double value);
210 
212  void substractValueSameUnit(double value);
213 
215  CPhysicalQuantity &operator*=(double multiply);
216 
218  CPhysicalQuantity &operator/=(double divide);
219 
221  PQ operator*(double multiply) const;
222 
224  friend PQ operator*(double factor, const PQ &other) { return other * factor; }
225 
227  PQ operator/(double divide) const;
228 
230  PQ operator-() const;
231 
233  friend bool operator==(const CPhysicalQuantity &a, const CPhysicalQuantity &b) { return a.equals(b); }
234 
236  friend bool operator!=(const CPhysicalQuantity &a, const CPhysicalQuantity &b) { return !a.equals(b); }
237 
240 
243 
245  friend bool operator>(const CPhysicalQuantity &a, const CPhysicalQuantity &b) { return b.lessThan(a); }
246 
248  friend bool operator<(const CPhysicalQuantity &a, const CPhysicalQuantity &b) { return a.lessThan(b); }
249 
251  friend bool operator<=(const CPhysicalQuantity &a, const CPhysicalQuantity &b) { return !b.lessThan(a); }
252 
254  friend bool operator>=(const CPhysicalQuantity &a, const CPhysicalQuantity &b) { return !a.lessThan(b); }
255 
257  friend PQ operator+(const PQ &a, const PQ &b)
258  {
259  PQ copy(a);
260  copy += b;
261  return copy;
262  }
263 
265  friend PQ operator-(const PQ &a, const PQ &b)
266  {
267  PQ copy(a);
268  copy -= b;
269  return copy;
270  }
271 
273  bool isZeroEpsilonConsidered() const;
274 
276  bool isPositiveWithEpsilonConsidered() const;
277 
279  bool isNegativeWithEpsilonConsidered() const;
280 
282  const PQ &makePositive();
283 
285  const PQ &makeNegative();
286 
288  PQ abs() const;
289 
291  void marshallToDbus(QDBusArgument &argument) const;
292 
294  void unmarshallFromDbus(const QDBusArgument &argument);
295 
297  void marshallToDbus(QDBusArgument &argument, LosslessTag) const;
298 
300  void unmarshallFromDbus(const QDBusArgument &argument, LosslessTag);
301 
303  void marshalToDataStream(QDataStream &stream) const;
304 
306  void unmarshalFromDataStream(QDataStream &stream);
307 
309  size_t getValueHash() const;
310 
312  friend size_t qHash(const PQ &pq) { return pq.getValueHash(); } // clazy:exclude=qhash-namespace
313 
315  QJsonObject toJson() const;
316 
318  void convertFromJson(const QJsonObject &json);
319 
321  QVariant propertyByIndex(CPropertyIndexRef index) const;
322 
324  void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant);
325 
327  int comparePropertyByIndex(CPropertyIndexRef index, const PQ &pq) const;
328 
330  QString convertToQString(bool i18n = false) const;
331 
333  void parseFromString(const QString &value);
334 
336  void parseFromString(const QString &value, CPqString::SeparatorMode mode);
337 
339  void parseFromString(const QString &value, CPqString::SeparatorMode mode, const MU &defaultUnitIfMissing);
340 
342  int compare(const PQ &other) const { return compareImpl(*this->derived(), other); }
343 
345  static const PQ &maxValue(const PQ &pq1, const PQ &pq2);
346 
348  static const PQ &minValue(const PQ &pq1, const PQ &pq2);
349 
351  static int compare(const PQ &a, const PQ &b) { return compareImpl(a, b); }
352 
354  static const PQ &null();
355 
358 
360  static PQ parsedFromString(const QString &value, CPqString::SeparatorMode mode, const MU &defaultUnitIfMissing);
361 
362  protected:
364  CPhysicalQuantity(double value, MU unit);
365 
367  CPhysicalQuantity(const QString &unitString);
368 
369  private:
370  double m_value;
371  MU m_unit;
372 
374  using UnitClass = MU;
375 
377  static int compareImpl(const PQ &, const PQ &);
378 
380  bool equals(const CPhysicalQuantity &other) const;
381 
383  bool lessThan(const CPhysicalQuantity &other) const;
384 
386  PQ const *derived() const;
387 
389  PQ *derived();
390  };
391 
393  extern template class SWIFT_MISC_EXPORT_DECLARE_TEMPLATE CPhysicalQuantity<CLengthUnit, CLength>;
394  extern template class SWIFT_MISC_EXPORT_DECLARE_TEMPLATE CPhysicalQuantity<CPressureUnit, CPressure>;
395  extern template class SWIFT_MISC_EXPORT_DECLARE_TEMPLATE CPhysicalQuantity<CFrequencyUnit, CFrequency>;
396  extern template class SWIFT_MISC_EXPORT_DECLARE_TEMPLATE CPhysicalQuantity<CMassUnit, CMass>;
397  extern template class SWIFT_MISC_EXPORT_DECLARE_TEMPLATE CPhysicalQuantity<CTemperatureUnit, CTemperature>;
398  extern template class SWIFT_MISC_EXPORT_DECLARE_TEMPLATE CPhysicalQuantity<CSpeedUnit, CSpeed>;
399  extern template class SWIFT_MISC_EXPORT_DECLARE_TEMPLATE CPhysicalQuantity<CAngleUnit, CAngle>;
400  extern template class SWIFT_MISC_EXPORT_DECLARE_TEMPLATE CPhysicalQuantity<CTimeUnit, CTime>;
401  extern template class SWIFT_MISC_EXPORT_DECLARE_TEMPLATE CPhysicalQuantity<CAccelerationUnit, CAcceleration>;
403 } // namespace swift::misc::physical_quantities
404 
405 #endif // SWIFT_MISC_PQ_PHYSICALQUANTITY_H
Non-owning reference to a CPropertyIndex with a subset of its features.
Tag type signifying overloaded marshalling methods that preserve data at the expense of size.
Definition: mixindbus.h:26
CRTP class template which will generate marshalling operators for a derived class with its own marsha...
Definition: mixindbus.h:39
CRTP class template to generate non-member QDataStream streaming operators.
CRTP class template from which a derived class can inherit icon-related functions.
Definition: mixinicon.h:28
CRTP class template from which a derived class can inherit property indexing functions.
Definition: mixinindex.h:40
CRTP class template which will generate marshalling operators for a derived class with its own marsha...
Definition: mixinjson.h:37
CRTP class template from which a derived class can inherit common methods dealing with the metatype o...
Definition: mixinmetatype.h:29
CRTP class template from which a derived class can inherit string streaming operations.
Definition: mixinstring.h:31
A physical quantity such as "5m", "20s", "1500ft/s".
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
Cast as QString.
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.
friend PQ operator-(const PQ &a, const PQ &b)
Minus operator -.
PQ & switchUnit(const MU &newUnit)
Change unit, and convert value to maintain the same quantity.
QString valueIntegerAsString() const
As integer value in current unit.
void setUnitBySymbol(const QString &unitName)
Set unit by string.
friend PQ operator+(const PQ &a, const PQ &b)
Plus operator +.
size_t getValueHash() const
qHash overload, needed for storing value in a QSet.
friend bool operator>=(const CPhysicalQuantity &a, const CPhysicalQuantity &b)
Greater equal operator >=.
QJsonObject toJson() const
Cast to JSON object.
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-=.
void unmarshalFromDataStream(QDataStream &stream)
Unmarshal a value from a QDataStream.
CPhysicalQuantity(double value, MU unit)
Constructor with double.
QString valueIntegerAsString(MU unit) const
As integer value.
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.
friend PQ operator*(double factor, const PQ &other)
Operator to support commutative multiplication.
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
void setUnit(const MU &unit)
Simply set unit, do no calclulate conversion.
friend int compare(const PQ &a, const PQ &b)
Return negative, zero, or positive if a is less than, equal to, or greater than b.
void substractValueSameUnit(double value)
Substract from the value in the current unit.
CPhysicalQuantity & operator/=(double divide)
Divide operator /=.
friend bool operator<=(const CPhysicalQuantity &a, const CPhysicalQuantity &b)
Less equal operator <=.
static int compare(const PQ &a, const PQ &b)
Implementation of compare.
void marshalToDataStream(QDataStream &stream) const
Marshal a value to a QDataStream.
friend bool operator<(const CPhysicalQuantity &a, const CPhysicalQuantity &b)
Less operator <.
friend size_t qHash(const PQ &pq)
qHash overload, needed for storing value in a QSet.
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.
friend bool operator>(const CPhysicalQuantity &a, const CPhysicalQuantity &b)
Greater operator >
static const PQ & maxValue(const PQ &pq1, const PQ &pq2)
Maximum of 2 quantities.
void marshallToDbus(QDBusArgument &argument) const
Marshall without begin/endStructure, for when composed within another object.
friend bool operator==(const CPhysicalQuantity &a, const CPhysicalQuantity &b)
Equal operator ==.
CPhysicalQuantity & operator*=(double multiply)
Multiply operator *=.
int compare(const PQ &other) const
Compare with other PQ.
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.
const PQ & makeNegative()
Make value always negative.
friend bool operator!=(const CPhysicalQuantity &a, const CPhysicalQuantity &b)
Not equal operator !=.
PQ operator*(double multiply) const
Operator *.
bool isZeroEpsilonConsidered() const
Quantity value <= epsilon.
SeparatorMode
Number separators / group separators.
Definition: pqstring.h:34
#define SWIFT_DECLARE_PQ_MIXINS(MU, PQ)
Explicit template declaration of mixins for a CPhysicalQuantity subclass to be placed near the top of...
#define SWIFT_MISC_EXPORT_DECLARE_TEMPLATE
Export explicit template declaration.