mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-22 14:55:36 +08:00
Had a bit of spare time so tried out the policy design pattern I've been learning about, applying it to PQ converters.
Some interesting results: * Minor simplification of PQ converters and unit ctors; may not seem like much, but I think it enables possible future simplifications of a similar kind. * CMeasurementPrefix is gone, no longer needed. Now prefixes are applied at the template stage. * Apart from that, PQs public API is unchanged. * Discovered that ft/s^2 conversion factor was wrong: fixed.
This commit is contained in:
@@ -15,15 +15,9 @@ namespace BlackMiscTest
|
||||
*/
|
||||
int CSamplesPhysicalQuantities::samples()
|
||||
{
|
||||
|
||||
// cases which must not work
|
||||
// CMeasurementUnit mu; //must not work
|
||||
// CLengthUnit du1(CAngleUnit::rad());
|
||||
|
||||
CMeasurementPrefix pf1 = CMeasurementPrefix::h();
|
||||
CMeasurementPrefix pf2 = CMeasurementPrefix::M();
|
||||
qDebug() << pf1 << pf2 << (1.0 * pf1.toDouble());
|
||||
|
||||
CLengthUnit lu1(CLengthUnit::cm());
|
||||
CLengthUnit lu2(CLengthUnit::ft());
|
||||
QString lu1s = lu1.toQString(true);
|
||||
|
||||
@@ -13,71 +13,10 @@ namespace BlackMisc
|
||||
namespace PhysicalQuantities
|
||||
{
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// --- Prefix ------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
/*
|
||||
* Equal?
|
||||
*/
|
||||
bool CMeasurementPrefix::operator ==(const CMeasurementPrefix &other) const
|
||||
{
|
||||
if (this == &other) return true;
|
||||
return this->m_factor == other.m_factor && this->m_name == other.m_name;
|
||||
}
|
||||
|
||||
/*
|
||||
* Not equal
|
||||
*/
|
||||
bool CMeasurementPrefix::operator !=(const CMeasurementPrefix &other) const
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// --- Measurement unit --------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
/*
|
||||
* Constructor
|
||||
*/
|
||||
CMeasurementUnit::CMeasurementUnit(const QString &name, const QString &symbol, double factor, int displayDigits, double epsilon) :
|
||||
m_name(name), m_symbol(symbol), m_epsilon(epsilon), m_displayDigits(displayDigits), m_converter(new LinearConverter(factor))
|
||||
{
|
||||
}
|
||||
|
||||
/*
|
||||
* Constructor
|
||||
*/
|
||||
CMeasurementUnit::CMeasurementUnit(const QString &name, const QString &symbol, double factor, double offset, int displayDigits, double epsilon) :
|
||||
m_name(name), m_symbol(symbol), m_epsilon(epsilon), m_displayDigits(displayDigits), m_converter(new AffineConverter(factor, offset))
|
||||
{
|
||||
}
|
||||
|
||||
/*
|
||||
* Constructor
|
||||
*/
|
||||
CMeasurementUnit::CMeasurementUnit(const QString &name, const QString &symbol, Converter *converter, int displayDigits, double epsilon) :
|
||||
m_name(name), m_symbol(symbol), m_epsilon(epsilon), m_displayDigits(displayDigits), m_converter(converter)
|
||||
{
|
||||
}
|
||||
|
||||
/*
|
||||
* Constructor
|
||||
*/
|
||||
CMeasurementUnit::CMeasurementUnit(const QString &name, const QString &symbol, const CMeasurementUnit &base, const CMeasurementPrefix &prefix, int displayDigits, double epsilon) :
|
||||
m_name(name), m_symbol(symbol), m_epsilon(epsilon), m_displayDigits(displayDigits), m_converter(base.m_converter->clone(prefix))
|
||||
{
|
||||
}
|
||||
|
||||
/*
|
||||
* Copy constructor
|
||||
*/
|
||||
CMeasurementUnit::CMeasurementUnit(const CMeasurementUnit &other) :
|
||||
m_name(other.m_name), m_symbol(other.m_symbol), m_epsilon(other.m_epsilon), m_displayDigits(other.m_displayDigits), m_converter(other.m_converter)
|
||||
{
|
||||
}
|
||||
|
||||
/*
|
||||
* Equal operator
|
||||
*/
|
||||
|
||||
@@ -22,232 +22,6 @@ namespace BlackMisc
|
||||
namespace PhysicalQuantities
|
||||
{
|
||||
|
||||
/*!
|
||||
* \brief Typical prefixes (multipliers) such as kilo, mega, hecto.
|
||||
* See <a href="http://www.poynton.com/notes/units/index.html">here</a> for an overview.
|
||||
* Use the static values such as CMeasurementPrefix::k() to specify values.
|
||||
*/
|
||||
class CMeasurementPrefix : public CStreamable
|
||||
{
|
||||
private:
|
||||
QString m_name; //!< name, e.g. "kilo"
|
||||
QString m_symbol; //!< prefix, e.g. "k" for kilo
|
||||
double m_factor; //!< factor, e.g. 1000 for kilo 1/100 for centi
|
||||
|
||||
/*!
|
||||
* Constructor by parameters
|
||||
* \brief CMeasurementMultiplier
|
||||
* \param name
|
||||
* \param prefixName
|
||||
* \param factor
|
||||
*/
|
||||
CMeasurementPrefix(const QString &name, const QString &symbol, double factor) :
|
||||
m_name(name), m_symbol(symbol), m_factor(factor) {}
|
||||
|
||||
protected:
|
||||
/*!
|
||||
* \brief Name as string
|
||||
* \param i18n
|
||||
* \return
|
||||
*/
|
||||
virtual QString convertToQString(bool /* i18n */ = false) const
|
||||
{
|
||||
return this->m_name;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Stream to DBus
|
||||
* \param argument
|
||||
*/
|
||||
virtual void marshallToDbus(QDBusArgument &argument) const
|
||||
{
|
||||
argument << this->m_name;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Stream from DBus
|
||||
* \param argument
|
||||
*/
|
||||
virtual void unmarshallFromDbus(const QDBusArgument &argument)
|
||||
{
|
||||
QString name;
|
||||
argument >> name;
|
||||
(*this) = CMeasurementPrefix::fromPrefixName(name);
|
||||
}
|
||||
|
||||
public:
|
||||
/*!
|
||||
* \brief Equal operator ==
|
||||
* \param other
|
||||
* \return
|
||||
*/
|
||||
bool operator == (const CMeasurementPrefix &other) const;
|
||||
|
||||
/*!
|
||||
* \brief Unequal operator !=
|
||||
* \param other
|
||||
* \return
|
||||
*/
|
||||
bool operator != (const CMeasurementPrefix &other) const;
|
||||
|
||||
/*!
|
||||
* \brief Factor, e.g.1000 for "kilo"
|
||||
* \return
|
||||
*/
|
||||
double getFactor() const
|
||||
{
|
||||
return this->m_factor;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Factor
|
||||
* \return
|
||||
*/
|
||||
double toDouble() const
|
||||
{
|
||||
return this->getFactor();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Name, e.g. "kilo"
|
||||
* \return
|
||||
*/
|
||||
QString getName(bool i18n = false) const
|
||||
{
|
||||
return i18n ? QCoreApplication::translate("CMeasurementPrefix", this->m_name.toStdString().c_str()) : this->m_name;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Prefix, e.g. "k" for "kilo"
|
||||
* \return
|
||||
*/
|
||||
QString getSymbol(bool i18n = false) const
|
||||
{
|
||||
return i18n ? QCoreApplication::translate("CMeasurementPrefix", this->m_symbol.toStdString().c_str()) : this->m_symbol;
|
||||
}
|
||||
|
||||
// --- static units, always use these for initialization
|
||||
// --- Remark: Static initialization in C++ is random, this is why no static members
|
||||
// --- are used
|
||||
|
||||
/*!
|
||||
* \brief Unit "None"
|
||||
* \return
|
||||
*/
|
||||
static const CMeasurementPrefix &None()
|
||||
{
|
||||
static CMeasurementPrefix none("", "", 0.0);
|
||||
return none;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Unit "One"
|
||||
* \return
|
||||
*/
|
||||
static const CMeasurementPrefix &One()
|
||||
{
|
||||
static CMeasurementPrefix one(QT_TR_NOOP("one"), "", 1.0);
|
||||
return one;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Unit "mega"
|
||||
* \return
|
||||
*/
|
||||
static const CMeasurementPrefix &M()
|
||||
{
|
||||
static CMeasurementPrefix mega(QT_TR_NOOP("mega"), "M", 1E6);
|
||||
return mega;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Unit "kilo"
|
||||
* \return
|
||||
*/
|
||||
static const CMeasurementPrefix &k()
|
||||
{
|
||||
static CMeasurementPrefix kilo(QT_TR_NOOP("kilo"), "k", 1000.0);
|
||||
return kilo;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Unit "giga"
|
||||
* \return
|
||||
*/
|
||||
static const CMeasurementPrefix &G()
|
||||
{
|
||||
static CMeasurementPrefix giga(QT_TR_NOOP("giga"), "G", 1E9);
|
||||
return giga;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Unit "hecto"
|
||||
* \return
|
||||
*/
|
||||
static const CMeasurementPrefix &h()
|
||||
{
|
||||
static CMeasurementPrefix hecto(QT_TR_NOOP("hecto"), "h", 100.0);
|
||||
return hecto;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Unit "centi"
|
||||
* \return
|
||||
*/
|
||||
static const CMeasurementPrefix &c()
|
||||
{
|
||||
static CMeasurementPrefix centi(QT_TR_NOOP("centi"), "c", 0.01);
|
||||
return centi;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Unit "milli"
|
||||
* \return
|
||||
*/
|
||||
static const CMeasurementPrefix &m()
|
||||
{
|
||||
static CMeasurementPrefix milli(QT_TR_NOOP("milli"), "m", 1E-03);
|
||||
return milli;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief All prefixes
|
||||
* \return
|
||||
*/
|
||||
static const QList<CMeasurementPrefix> &prefixes()
|
||||
{
|
||||
static QList<CMeasurementPrefix> prefixes;
|
||||
if (prefixes.isEmpty())
|
||||
{
|
||||
prefixes.append(CMeasurementPrefix::c());
|
||||
prefixes.append(CMeasurementPrefix::G());
|
||||
prefixes.append(CMeasurementPrefix::h());
|
||||
prefixes.append(CMeasurementPrefix::k());
|
||||
prefixes.append(CMeasurementPrefix::M());
|
||||
prefixes.append(CMeasurementPrefix::m());
|
||||
prefixes.append(CMeasurementPrefix::None());
|
||||
prefixes.append(CMeasurementPrefix::One());
|
||||
}
|
||||
return prefixes;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Prefix from name
|
||||
* \param prefixName must be valid!
|
||||
* \return
|
||||
*/
|
||||
static const CMeasurementPrefix &fromPrefixName(const QString &prefixName)
|
||||
{
|
||||
const QList<CMeasurementPrefix> &prefixes = CMeasurementPrefix::prefixes();
|
||||
for (int i = 0; i < prefixes.size(); ++i) {
|
||||
if (prefixes.at(i).getName() == prefixName) return (prefixes.at(i));
|
||||
}
|
||||
qFatal("Illegal unit name");
|
||||
return CMeasurementPrefix::None(); // just suppress "not all control paths return a value"
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------------
|
||||
// --- Unit
|
||||
// ---------------------------------------------------------------------------------
|
||||
@@ -280,98 +54,117 @@ protected:
|
||||
* \return
|
||||
*/
|
||||
virtual double fromDefault(double factor) const = 0;
|
||||
/*!
|
||||
* Make a copy of this object with a different prefix.
|
||||
* \param prefix
|
||||
* \return
|
||||
*/
|
||||
virtual Converter *clone(const CMeasurementPrefix &prefix) const = 0;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Concrete strategy pattern for converting unit that does nothing.
|
||||
*/
|
||||
struct IdentityConverter : public Converter
|
||||
{
|
||||
virtual double toDefault(double factor) const { return factor; }
|
||||
virtual double fromDefault(double factor) const { return factor; }
|
||||
};
|
||||
|
||||
/*!
|
||||
* Concrete strategy pattern for converting unit with linear conversion.
|
||||
* \tparam Policy a policy class with static method factor() returning double
|
||||
*/
|
||||
class LinearConverter : public Converter
|
||||
template <class Policy>
|
||||
struct LinearConverter : public Converter
|
||||
{
|
||||
double m_factor;
|
||||
public:
|
||||
/*!
|
||||
* Constructor
|
||||
* \param factor
|
||||
*/
|
||||
LinearConverter(double factor) : m_factor(factor) {}
|
||||
virtual double toDefault(double factor) const { return factor * m_factor; }
|
||||
virtual double fromDefault(double factor) const { return factor / m_factor; }
|
||||
virtual Converter *clone(const CMeasurementPrefix &prefix) const { auto ret = new LinearConverter(*this); ret->m_factor *= prefix.getFactor(); return ret; }
|
||||
virtual double toDefault(double factor) const { return factor * Policy::factor(); }
|
||||
virtual double fromDefault(double factor) const { return factor / Policy::factor(); }
|
||||
};
|
||||
|
||||
/*!
|
||||
* Concrete strategy pattern for converting unit with affine conversion.
|
||||
* Concrete strategy pattern for converting unit with offset linear conversion.
|
||||
* \tparam Policy a policy class with static methods factor() and offset() returning double
|
||||
*/
|
||||
class AffineConverter : public Converter
|
||||
template <class Policy>
|
||||
struct AffineConverter : public Converter
|
||||
{
|
||||
double m_factor;
|
||||
double m_offset;
|
||||
public:
|
||||
/*!
|
||||
* Constructor
|
||||
* \param factor
|
||||
* \param offset
|
||||
*/
|
||||
AffineConverter(double factor, double offset) : m_factor(factor), m_offset(offset) {}
|
||||
virtual double toDefault(double factor) const { return (factor - m_offset) * m_factor; }
|
||||
virtual double fromDefault(double factor) const { return factor / m_factor + m_offset; }
|
||||
virtual Converter *clone(const CMeasurementPrefix &prefix) const { auto ret = new AffineConverter(*this); ret->m_factor *= prefix.getFactor(); return ret; }
|
||||
virtual double toDefault(double factor) const { return (factor - Policy::offset()) * Policy::factor(); }
|
||||
virtual double fromDefault(double factor) const { return factor / Policy::factor() + Policy::offset(); }
|
||||
};
|
||||
|
||||
/*!
|
||||
* Concrete strategy pattern for converting unit with subdivision conversion.
|
||||
* Concrete strategy pattern for converting unit with one subdivision conversion.
|
||||
* \tparam FactorPolicy a policy class with static method factor() returning double
|
||||
* \tparam SubdivPolicy a policy class with static methods fraction() and subfactor() returning double
|
||||
*/
|
||||
template <int Num, int Den>
|
||||
class SubdivisionConverter : public Converter
|
||||
template <class FactorPolicy, class SubdivPolicy>
|
||||
struct SubdivisionConverter : public Converter
|
||||
{
|
||||
double m_factor;
|
||||
public:
|
||||
/*!
|
||||
* Constructor
|
||||
*/
|
||||
SubdivisionConverter(double factor = 1) : m_factor(factor) {}
|
||||
virtual double toDefault(double factor) const { using BlackMisc::Math::CMath;
|
||||
double ret = CMath::trunc(factor); factor = CMath::fract(factor) * Den;
|
||||
ret += factor / Num;
|
||||
return ret * m_factor; }
|
||||
double part2 = CMath::fract(factor) * SubdivPolicy::fraction();
|
||||
factor = CMath::trunc(factor) + part2 / SubdivPolicy::subfactor();
|
||||
return factor * FactorPolicy::factor(); }
|
||||
virtual double fromDefault(double factor) const { using BlackMisc::Math::CMath;
|
||||
factor /= m_factor;
|
||||
double ret = CMath::trunc(factor); factor = CMath::fract(factor) * Num;
|
||||
return ret += factor / Den; }
|
||||
virtual Converter *clone(const CMeasurementPrefix &) const { qFatal("Not implemented"); return 0; }
|
||||
factor /= FactorPolicy::factor();
|
||||
double part2 = CMath::fract(factor) * SubdivPolicy::subfactor();
|
||||
return CMath::trunc(factor) + part2 / SubdivPolicy::fraction(); }
|
||||
};
|
||||
|
||||
/*!
|
||||
* Concrete strategy pattern for converting unit with subdivision conversion.
|
||||
* Concrete strategy pattern for converting unit with two subdivision conversions.
|
||||
* \tparam FactorPolicy a policy class with static method factor() returning double
|
||||
* \tparam SubdivPolicy a policy class with static methods fraction() and subfactor() returning double
|
||||
*/
|
||||
template <int Num1, int Den1, int Num2, int Den2>
|
||||
class SubdivisionConverter2 : public Converter
|
||||
template <class FactorPolicy, class SubdivPolicy>
|
||||
struct SubdivisionConverter2 : public Converter
|
||||
{
|
||||
double m_factor;
|
||||
public:
|
||||
/*!
|
||||
* Constructor
|
||||
*/
|
||||
SubdivisionConverter2(double factor = 1) : m_factor(factor) {}
|
||||
virtual double toDefault(double factor) const { using BlackMisc::Math::CMath;
|
||||
double ret = CMath::trunc(factor); factor = CMath::fract(factor) * Den1;
|
||||
ret += CMath::trunc(factor) / Num1; factor = CMath::fract(factor) * Den2;
|
||||
ret += factor / (Num1 * Num2);
|
||||
return ret * m_factor; }
|
||||
double part2 = CMath::fract(factor) * SubdivPolicy::fraction();
|
||||
double part3 = CMath::fract(part2) * SubdivPolicy::fraction();
|
||||
factor = CMath::trunc(factor) + (CMath::trunc(part2) + part3 / SubdivPolicy::subfactor()) / SubdivPolicy::subfactor();
|
||||
return factor * FactorPolicy::factor(); }
|
||||
virtual double fromDefault(double factor) const { using BlackMisc::Math::CMath;
|
||||
factor /= m_factor;
|
||||
double ret = CMath::trunc(factor); factor = CMath::fract(factor) * Num1;
|
||||
ret += CMath::trunc(factor) / Den1; factor = CMath::fract(factor) * Num2;
|
||||
return ret += factor / (Den1 * Den2); }
|
||||
virtual Converter *clone(const CMeasurementPrefix &) const { qFatal("Not implemented"); return 0; }
|
||||
factor /= FactorPolicy::factor();
|
||||
double part2 = CMath::fract(factor) * SubdivPolicy::subfactor();
|
||||
double part3 = CMath::fract(part2) * SubdivPolicy::subfactor();
|
||||
return CMath::trunc(factor) + (CMath::trunc(part2) + part3 / SubdivPolicy::fraction()) / SubdivPolicy::fraction(); }
|
||||
};
|
||||
|
||||
//! \{
|
||||
//! Metapolicy that can be used to modify template parameters of converters
|
||||
struct One {
|
||||
static double factor() { return 1; } //!< factor \return
|
||||
};
|
||||
template <class Policy>
|
||||
struct Two {
|
||||
static double factor() { return Policy::factor() * 2.0; } //!< factor \return
|
||||
};
|
||||
template <class Policy>
|
||||
struct Milli {
|
||||
static double factor() { return Policy::factor() / 1000.0; } //!< factor \return
|
||||
};
|
||||
template <class Policy>
|
||||
struct Centi {
|
||||
static double factor() { return Policy::factor() / 100.0; } //!< factor \return
|
||||
};
|
||||
template <class Policy>
|
||||
struct Hecto {
|
||||
static double factor() { return Policy::factor() * 100.0; } //!< factor \return
|
||||
};
|
||||
template <class Policy>
|
||||
struct Kilo {
|
||||
static double factor() { return Policy::factor() * 1000.0; } //!< factor \return
|
||||
};
|
||||
template <class Policy>
|
||||
struct Mega {
|
||||
static double factor() { return Policy::factor() * 1e+6; } //!< factor \return
|
||||
};
|
||||
template <class Policy>
|
||||
struct Giga {
|
||||
static double factor() { return Policy::factor() * 1e+9; } //!< factor \return
|
||||
};
|
||||
template <int Subfactor>
|
||||
struct InEachHundred {
|
||||
static double fraction() { return 100.0f; } //!< fraction \return
|
||||
static double subfactor() { return float(Subfactor); } //!< subfactor \return
|
||||
};
|
||||
//! \}
|
||||
|
||||
private:
|
||||
QString m_name; //!< name, e.g. "meter"
|
||||
QString m_symbol; //!< unit name, e.g. "m"
|
||||
@@ -380,51 +173,25 @@ private:
|
||||
QSharedDataPointer<Converter> m_converter; //!< strategy pattern allows an arbitrary conversion method as per object
|
||||
|
||||
protected:
|
||||
/*!
|
||||
* Construct a unit with linear conversion
|
||||
* \param name
|
||||
* \param symbol
|
||||
* \param factor
|
||||
* \param displayDigits
|
||||
* \param epsilon
|
||||
*/
|
||||
CMeasurementUnit(const QString &name, const QString &symbol, double factor, int displayDigits, double epsilon);
|
||||
|
||||
/*!
|
||||
* Construct a unit with affine conversion
|
||||
* \param name
|
||||
* \param symbol
|
||||
* \param factor
|
||||
* \param offset
|
||||
* \param displayDigits
|
||||
* \param epsilon
|
||||
*/
|
||||
CMeasurementUnit(const QString &name, const QString &symbol, double factor, double offset, int displayDigits, double epsilon);
|
||||
|
||||
/*!
|
||||
* Construct a unit with custom conversion
|
||||
* \param name
|
||||
* \param symbol
|
||||
* \param converter
|
||||
* \param displayDigits
|
||||
* \param epsilon
|
||||
*/
|
||||
CMeasurementUnit(const QString &name, const QString &symbol, Converter *converter, int displayDigits, double epsilon);
|
||||
|
||||
/*!
|
||||
* Construct from base unit and prefix
|
||||
* \param base
|
||||
* \param prefix
|
||||
* \param displayDigits
|
||||
* \param epsilon
|
||||
*/
|
||||
CMeasurementUnit(const QString &name, const QString &symbol, const CMeasurementUnit &base, const CMeasurementPrefix &prefix, int displayDigits = 2, double epsilon = 1E-10);
|
||||
template <class Converter>
|
||||
CMeasurementUnit(const QString &name, const QString &symbol, const Converter &, int displayDigits, double epsilon)
|
||||
: m_name(name), m_symbol(symbol), m_epsilon(epsilon), m_displayDigits(displayDigits), m_converter(new Converter)
|
||||
{}
|
||||
|
||||
/*!
|
||||
* \brief Copy constructor
|
||||
* \param other
|
||||
*/
|
||||
CMeasurementUnit(const CMeasurementUnit &other);
|
||||
CMeasurementUnit(const CMeasurementUnit &other)
|
||||
: m_name(other.m_name), m_symbol(other.m_symbol), m_epsilon(other.m_epsilon), m_displayDigits(other.m_displayDigits), m_converter(other.m_converter)
|
||||
{}
|
||||
|
||||
/*!
|
||||
* \brief String for streaming operators is full name
|
||||
@@ -588,7 +355,12 @@ public:
|
||||
*/
|
||||
static CMeasurementUnit &None()
|
||||
{
|
||||
static CMeasurementUnit none("none", "", 0.0, 0, 0);
|
||||
struct NilConverter : public Converter
|
||||
{
|
||||
virtual double toDefault(double) const { return 0.0; }
|
||||
virtual double fromDefault(double) const { return 0.0; }
|
||||
};
|
||||
static CMeasurementUnit none("none", "", NilConverter(), 0, 0);
|
||||
return none;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -30,28 +30,16 @@ namespace PhysicalQuantities
|
||||
class CLengthUnit : public CMeasurementUnit
|
||||
{
|
||||
private:
|
||||
/*!
|
||||
* \brief Constructor length unit
|
||||
* \param name
|
||||
* \param symbol
|
||||
* \param factor
|
||||
* \param displayDigits
|
||||
* \param epsilon
|
||||
*/
|
||||
CLengthUnit(const QString &name, const QString &symbol, double factor, int displayDigits = 2, double epsilon = 1E-9) :
|
||||
CMeasurementUnit(name, symbol, factor, displayDigits, epsilon) {}
|
||||
template <class Converter>
|
||||
CLengthUnit(const QString &name, const QString &symbol, const Converter &converter, int displayDigits = 2, double epsilon = 1E-9) :
|
||||
CMeasurementUnit(name, symbol, converter, displayDigits, epsilon)
|
||||
{}
|
||||
|
||||
/*!
|
||||
* \brief Constructor length unit
|
||||
* \param name
|
||||
* \param symbol
|
||||
* \param prefix
|
||||
* \param base
|
||||
* \param displayDigits
|
||||
* \param epsilon
|
||||
*/
|
||||
CLengthUnit(const QString &name, const QString &symbol, const CMeasurementPrefix &prefix, const CLengthUnit &base, int displayDigits = 2, double epsilon = 1E-9) :
|
||||
CMeasurementUnit(name, symbol, base, prefix, displayDigits, epsilon) {}
|
||||
struct NauticalMilesToMeters { static double factor() { return 1852.0; } };
|
||||
struct FeetToMeters { static double factor() { return 0.3048; } };
|
||||
struct MilesToMeters { static double factor() { return 1609.344; } };
|
||||
struct StatuteMilesToMeters { static double factor() { return 1609.3472; } };
|
||||
typedef One MetersToMeters;
|
||||
|
||||
public:
|
||||
/*!
|
||||
@@ -59,12 +47,6 @@ public:
|
||||
*/
|
||||
CLengthUnit() : CMeasurementUnit(defaultUnit()) {}
|
||||
|
||||
/*!
|
||||
* \brief Copy constructor
|
||||
* \param other
|
||||
*/
|
||||
CLengthUnit(const CLengthUnit &other) : CMeasurementUnit(other) {}
|
||||
|
||||
/*!
|
||||
* \brief Default unit
|
||||
* \return
|
||||
@@ -77,7 +59,7 @@ public:
|
||||
*/
|
||||
static const CLengthUnit &m()
|
||||
{
|
||||
static CLengthUnit m(QT_TRANSLATE_NOOP("CMeasurementUnit", "meter"), "m", 1);
|
||||
static CLengthUnit m(QT_TRANSLATE_NOOP("CMeasurementUnit", "meter"), "m", IdentityConverter());
|
||||
return m;
|
||||
}
|
||||
|
||||
@@ -87,7 +69,7 @@ public:
|
||||
*/
|
||||
static const CLengthUnit &NM()
|
||||
{
|
||||
static CLengthUnit NM(QT_TRANSLATE_NOOP("CMeasurementUnit", "nautical mile"), "NM", 1000.0 * 1.85200, 3);
|
||||
static CLengthUnit NM(QT_TRANSLATE_NOOP("CMeasurementUnit", "nautical mile"), "NM", LinearConverter<NauticalMilesToMeters>(), 3);
|
||||
return NM;
|
||||
}
|
||||
|
||||
@@ -97,7 +79,7 @@ public:
|
||||
*/
|
||||
static const CLengthUnit &ft()
|
||||
{
|
||||
static CLengthUnit ft(QT_TRANSLATE_NOOP("CMeasurementUnit", "foot"), "ft", 0.3048, 0);
|
||||
static CLengthUnit ft(QT_TRANSLATE_NOOP("CMeasurementUnit", "foot"), "ft", LinearConverter<FeetToMeters>(), 0);
|
||||
return ft;
|
||||
}
|
||||
|
||||
@@ -107,7 +89,7 @@ public:
|
||||
*/
|
||||
static const CLengthUnit &km()
|
||||
{
|
||||
static CLengthUnit km(QT_TRANSLATE_NOOP("CMeasurementUnit", "kilometer"), "km", CMeasurementPrefix::k(), m(), 3);
|
||||
static CLengthUnit km(QT_TRANSLATE_NOOP("CMeasurementUnit", "kilometer"), "km", LinearConverter<Kilo<MetersToMeters>>(), 3);
|
||||
return km;
|
||||
}
|
||||
|
||||
@@ -117,7 +99,7 @@ public:
|
||||
*/
|
||||
static const CLengthUnit &cm()
|
||||
{
|
||||
static CLengthUnit cm(QT_TRANSLATE_NOOP("CMeasurementUnit", "centimeter"), "cm", CMeasurementPrefix::c(), m(), 1);
|
||||
static CLengthUnit cm(QT_TRANSLATE_NOOP("CMeasurementUnit", "centimeter"), "cm", LinearConverter<Centi<MetersToMeters>>(), 1);
|
||||
return cm;
|
||||
}
|
||||
|
||||
@@ -127,7 +109,7 @@ public:
|
||||
*/
|
||||
static const CLengthUnit &mi()
|
||||
{
|
||||
static CLengthUnit mi(QT_TRANSLATE_NOOP("CMeasurementUnit", "mile"), "mi", 1609.344, 3);
|
||||
static CLengthUnit mi(QT_TRANSLATE_NOOP("CMeasurementUnit", "mile"), "mi", LinearConverter<MilesToMeters>(), 3);
|
||||
return mi;
|
||||
}
|
||||
|
||||
@@ -137,7 +119,7 @@ public:
|
||||
*/
|
||||
static const CLengthUnit &SM()
|
||||
{
|
||||
static CLengthUnit sm(QT_TRANSLATE_NOOP("CMeasurementUnit", "statute mile"), "SM", 1609.3472, 3);
|
||||
static CLengthUnit sm(QT_TRANSLATE_NOOP("CMeasurementUnit", "statute mile"), "SM", LinearConverter<StatuteMilesToMeters>(), 3);
|
||||
return sm;
|
||||
}
|
||||
|
||||
@@ -180,27 +162,13 @@ protected:
|
||||
class CAngleUnit : public CMeasurementUnit
|
||||
{
|
||||
private:
|
||||
/*!
|
||||
* \brief Constructor angle units: Radian, degree
|
||||
* \param name
|
||||
* \param symbol
|
||||
* \param factor
|
||||
* \param displayDigits
|
||||
* \param epsilon
|
||||
*/
|
||||
CAngleUnit(const QString &name, const QString &symbol, double factor, int displayDigits = 2, double epsilon = 1E-9) :
|
||||
CMeasurementUnit(name, symbol, factor, displayDigits, epsilon) {}
|
||||
template <class Converter>
|
||||
CAngleUnit(const QString &name, const QString &symbol, const Converter &converter, int displayDigits = 2, double epsilon = 1E-9) :
|
||||
CMeasurementUnit(name, symbol, converter, displayDigits, epsilon)
|
||||
{}
|
||||
|
||||
/*!
|
||||
* \brief Constructor angle units: Sexagesimal
|
||||
* \param name
|
||||
* \param symbol
|
||||
* \param converter
|
||||
* \param displayDigits
|
||||
* \param epsilon
|
||||
*/
|
||||
CAngleUnit(const QString &name, const QString &symbol, Converter *converter, int displayDigits = 2, double epsilon = 1E-9) :
|
||||
CMeasurementUnit(name, symbol, converter, displayDigits, epsilon) {}
|
||||
struct RadiansToDegrees { static double factor() { return 180.0 / M_PI; } };
|
||||
typedef One DegreesToDegrees;
|
||||
|
||||
public:
|
||||
/*!
|
||||
@@ -208,12 +176,6 @@ public:
|
||||
*/
|
||||
CAngleUnit() : CMeasurementUnit(defaultUnit()) {}
|
||||
|
||||
/*!
|
||||
* \brief Copy constructor
|
||||
* \param other
|
||||
*/
|
||||
CAngleUnit(const CAngleUnit &other) : CMeasurementUnit(other) {}
|
||||
|
||||
/*!
|
||||
* \brief Default unit
|
||||
* \return
|
||||
@@ -235,7 +197,7 @@ public:
|
||||
*/
|
||||
static const CAngleUnit &rad()
|
||||
{
|
||||
static CAngleUnit rad(QT_TRANSLATE_NOOP("CMeasurementUnit", "radian"), "rad", 180.0 / M_PI);
|
||||
static CAngleUnit rad(QT_TRANSLATE_NOOP("CMeasurementUnit", "radian"), "rad", LinearConverter<RadiansToDegrees>());
|
||||
return rad;
|
||||
}
|
||||
|
||||
@@ -245,7 +207,7 @@ public:
|
||||
*/
|
||||
static const CAngleUnit °()
|
||||
{
|
||||
static CAngleUnit deg(QT_TRANSLATE_NOOP("CMeasurementUnit", "degree"), QT_TRANSLATE_NOOP("CMeasurementUnit", "deg"), 1);
|
||||
static CAngleUnit deg(QT_TRANSLATE_NOOP("CMeasurementUnit", "degree"), QT_TRANSLATE_NOOP("CMeasurementUnit", "deg"), IdentityConverter());
|
||||
return deg;
|
||||
}
|
||||
|
||||
@@ -255,7 +217,7 @@ public:
|
||||
*/
|
||||
static const CAngleUnit &sexagesimalDeg()
|
||||
{
|
||||
static CAngleUnit deg(QT_TRANSLATE_NOOP("CMeasurementUnit", "degree, minute, second"), "DMS", new SubdivisionConverter2<60, 100, 60, 100>, 4);
|
||||
static CAngleUnit deg(QT_TRANSLATE_NOOP("CMeasurementUnit", "degree, minute, second"), "DMS", SubdivisionConverter2<DegreesToDegrees, InEachHundred<60>>(), 4);
|
||||
return deg;
|
||||
(void)QT_TRANSLATE_NOOP("CMeasurementUnit", "%L1 %L2 %L3");
|
||||
(void)QT_TRANSLATE_NOOP("CMeasurementUnit", "-%L1 %L2 %L3");
|
||||
@@ -267,7 +229,7 @@ public:
|
||||
*/
|
||||
static const CAngleUnit &sexagesimalDegMin()
|
||||
{
|
||||
static CAngleUnit deg(QT_TRANSLATE_NOOP("CMeasurementUnit", "degree, minute"), "MinDec", new SubdivisionConverter<60, 100>, 4);
|
||||
static CAngleUnit deg(QT_TRANSLATE_NOOP("CMeasurementUnit", "degree, minute"), "MinDec", SubdivisionConverter<DegreesToDegrees, InEachHundred<60>>(), 4);
|
||||
return deg;
|
||||
(void)QT_TRANSLATE_NOOP("CMeasurementUnit", "%L1 %L2");
|
||||
(void)QT_TRANSLATE_NOOP("CMeasurementUnit", "-%L1 %L2");
|
||||
@@ -309,28 +271,12 @@ protected:
|
||||
class CFrequencyUnit : public CMeasurementUnit
|
||||
{
|
||||
private:
|
||||
/*!
|
||||
* Constructor frequency unit
|
||||
* \param name
|
||||
* \param symbol
|
||||
* \param factor
|
||||
* \param displayDigits
|
||||
* \param epsilon
|
||||
*/
|
||||
CFrequencyUnit(const QString &name, const QString &symbol, double factor, int displayDigits = 2, double epsilon = 1E-9) :
|
||||
CMeasurementUnit(name, symbol, factor, displayDigits, epsilon) {}
|
||||
template <class Converter>
|
||||
CFrequencyUnit(const QString &name, const QString &symbol, const Converter &converter, int displayDigits = 2, double epsilon = 1E-9) :
|
||||
CMeasurementUnit(name, symbol, converter, displayDigits, epsilon)
|
||||
{}
|
||||
|
||||
/*!
|
||||
* Constructor frequency unit
|
||||
* \param name
|
||||
* \param symbol
|
||||
* \param prefix
|
||||
* \param base
|
||||
* \param displayDigits
|
||||
* \param epsilon
|
||||
*/
|
||||
CFrequencyUnit(const QString &name, const QString &symbol, const CMeasurementPrefix &prefix, const CFrequencyUnit &base, int displayDigits = 2, double epsilon = 1E-9) :
|
||||
CMeasurementUnit(name, symbol, base, prefix, displayDigits, epsilon) {}
|
||||
typedef One HertzToHertz;
|
||||
|
||||
public:
|
||||
/*!
|
||||
@@ -338,12 +284,6 @@ public:
|
||||
*/
|
||||
CFrequencyUnit() : CMeasurementUnit(defaultUnit()) {}
|
||||
|
||||
/*!
|
||||
* \brief Copy constructor
|
||||
* \param other
|
||||
*/
|
||||
CFrequencyUnit(const CFrequencyUnit &other) : CMeasurementUnit(other) {}
|
||||
|
||||
/*!
|
||||
* \brief Default unit
|
||||
* \return
|
||||
@@ -356,7 +296,7 @@ public:
|
||||
*/
|
||||
static const CFrequencyUnit &Hz()
|
||||
{
|
||||
static CFrequencyUnit Hz(QT_TRANSLATE_NOOP("CMeasurementUnit", "hertz"), "Hz", 1);
|
||||
static CFrequencyUnit Hz(QT_TRANSLATE_NOOP("CMeasurementUnit", "hertz"), "Hz", IdentityConverter());
|
||||
return Hz;
|
||||
}
|
||||
|
||||
@@ -366,7 +306,7 @@ public:
|
||||
*/
|
||||
static const CFrequencyUnit &kHz()
|
||||
{
|
||||
static CFrequencyUnit kHz(QT_TRANSLATE_NOOP("CMeasurementUnit", "kilohertz"), "kHz", CMeasurementPrefix::k(), Hz(), 1);
|
||||
static CFrequencyUnit kHz(QT_TRANSLATE_NOOP("CMeasurementUnit", "kilohertz"), "kHz", LinearConverter<Kilo<HertzToHertz>>(), 1);
|
||||
return kHz;
|
||||
}
|
||||
|
||||
@@ -376,7 +316,7 @@ public:
|
||||
*/
|
||||
static const CFrequencyUnit &MHz()
|
||||
{
|
||||
static CFrequencyUnit MHz(QT_TRANSLATE_NOOP("CMeasurementUnit", "megahertz"), "MHz", CMeasurementPrefix::M(), Hz(), 2);
|
||||
static CFrequencyUnit MHz(QT_TRANSLATE_NOOP("CMeasurementUnit", "megahertz"), "MHz", LinearConverter<Mega<HertzToHertz>>(), 2);
|
||||
return MHz;
|
||||
}
|
||||
|
||||
@@ -386,7 +326,7 @@ public:
|
||||
*/
|
||||
static const CFrequencyUnit &GHz()
|
||||
{
|
||||
static CFrequencyUnit GHz(QT_TRANSLATE_NOOP("CMeasurementUnit", "gigahertz"), "GHz", CMeasurementPrefix::G(), Hz(), 2);
|
||||
static CFrequencyUnit GHz(QT_TRANSLATE_NOOP("CMeasurementUnit", "gigahertz"), "GHz", LinearConverter<Giga<HertzToHertz>>(), 2);
|
||||
return GHz;
|
||||
}
|
||||
|
||||
@@ -426,28 +366,13 @@ protected:
|
||||
class CMassUnit : public CMeasurementUnit
|
||||
{
|
||||
private:
|
||||
/*!
|
||||
* \brief Constructor mass units
|
||||
* \param name
|
||||
* \param symbol
|
||||
* \param factor
|
||||
* \param displayDigits
|
||||
* \param epsilon
|
||||
*/
|
||||
CMassUnit(const QString &name, const QString &symbol, double factor, int displayDigits = 2, double epsilon = 1E-9) :
|
||||
CMeasurementUnit(name, symbol, factor, displayDigits, epsilon) {}
|
||||
template <class Converter>
|
||||
CMassUnit(const QString &name, const QString &symbol, const Converter &converter, int displayDigits = 2, double epsilon = 1E-9) :
|
||||
CMeasurementUnit(name, symbol, converter, displayDigits, epsilon)
|
||||
{}
|
||||
|
||||
/*!
|
||||
* \brief Constructor mass units
|
||||
* \param name
|
||||
* \param symbol
|
||||
* \param prefix
|
||||
* \param base
|
||||
* \param displayDigits
|
||||
* \param epsilon
|
||||
*/
|
||||
CMassUnit(const QString &name, const QString &symbol, const CMeasurementPrefix &prefix, const CMassUnit &base, int displayDigits = 2, double epsilon = 1E-9) :
|
||||
CMeasurementUnit(name, symbol, base, prefix, displayDigits, epsilon) {}
|
||||
typedef Milli<One> GramsToKilograms;
|
||||
struct PoundsToKilograms { static double factor() { return 0.45359237; } };
|
||||
|
||||
public:
|
||||
/*!
|
||||
@@ -455,12 +380,6 @@ public:
|
||||
*/
|
||||
CMassUnit() : CMeasurementUnit(defaultUnit()) {}
|
||||
|
||||
/*!
|
||||
* \brief Copy constructor
|
||||
* \param other
|
||||
*/
|
||||
CMassUnit(const CMassUnit &other) : CMeasurementUnit(other) {}
|
||||
|
||||
/*!
|
||||
* \brief Default unit
|
||||
* \return
|
||||
@@ -473,7 +392,7 @@ public:
|
||||
*/
|
||||
static const CMassUnit &kg()
|
||||
{
|
||||
static CMassUnit kg(QT_TRANSLATE_NOOP("CMeasurementUnit", "kilogram"), "kg", CMeasurementPrefix::k(), g(), 1);
|
||||
static CMassUnit kg(QT_TRANSLATE_NOOP("CMeasurementUnit", "kilogram"), "kg", IdentityConverter(), 1);
|
||||
return kg;
|
||||
}
|
||||
|
||||
@@ -483,7 +402,7 @@ public:
|
||||
*/
|
||||
static const CMassUnit &g()
|
||||
{
|
||||
static CMassUnit g(QT_TRANSLATE_NOOP("CMeasurementUnit", "gram"), "g", 0.001, 0);
|
||||
static CMassUnit g(QT_TRANSLATE_NOOP("CMeasurementUnit", "gram"), "g", LinearConverter<GramsToKilograms>(), 0);
|
||||
return g;
|
||||
}
|
||||
|
||||
@@ -493,7 +412,7 @@ public:
|
||||
*/
|
||||
static const CMassUnit &tonne()
|
||||
{
|
||||
static CMassUnit t(QT_TRANSLATE_NOOP("CMeasurementUnit", "tonne"), "t", 1000.0, 3);
|
||||
static CMassUnit t(QT_TRANSLATE_NOOP("CMeasurementUnit", "tonne"), "t", LinearConverter<Mega<GramsToKilograms>>(), 3);
|
||||
return t;
|
||||
}
|
||||
|
||||
@@ -503,7 +422,7 @@ public:
|
||||
*/
|
||||
static const CMassUnit &shortTon()
|
||||
{
|
||||
static CMassUnit ton(QT_TRANSLATE_NOOP("CMeasurementUnit", "short ton"), "ton", 907.18474, 3);
|
||||
static CMassUnit ton(QT_TRANSLATE_NOOP("CMeasurementUnit", "short ton"), "ton", LinearConverter<Two<Kilo<PoundsToKilograms>>>(), 3);
|
||||
return ton;
|
||||
}
|
||||
|
||||
@@ -513,7 +432,7 @@ public:
|
||||
*/
|
||||
static const CMassUnit &lb()
|
||||
{
|
||||
static CMassUnit lbs(QT_TRANSLATE_NOOP("CMeasurementUnit", "pound"), "lb", 0.45359237, 1);
|
||||
static CMassUnit lbs(QT_TRANSLATE_NOOP("CMeasurementUnit", "pound"), "lb", LinearConverter<PoundsToKilograms>(), 1);
|
||||
return lbs;
|
||||
}
|
||||
|
||||
@@ -554,28 +473,15 @@ protected:
|
||||
class CPressureUnit : public CMeasurementUnit
|
||||
{
|
||||
private:
|
||||
/*!
|
||||
* \brief Pressure unit constructor
|
||||
* \param name
|
||||
* \param symbol
|
||||
* \param factor
|
||||
* \param displayDigits
|
||||
* \param epsilon
|
||||
*/
|
||||
CPressureUnit(const QString &name, const QString &symbol, double factor, int displayDigits = 2, double epsilon = 1E-9) :
|
||||
CMeasurementUnit(name, symbol, factor, displayDigits, epsilon) {}
|
||||
template <class Converter>
|
||||
CPressureUnit(const QString &name, const QString &symbol, const Converter &converter, int displayDigits = 2, double epsilon = 1E-9) :
|
||||
CMeasurementUnit(name, symbol, converter, displayDigits, epsilon)
|
||||
{}
|
||||
|
||||
/*!
|
||||
* \brief Pressure unit constructor
|
||||
* \param name
|
||||
* \param symbol
|
||||
* \param prefix
|
||||
* \param base
|
||||
* \param displayDigits
|
||||
* \param epsilon
|
||||
*/
|
||||
CPressureUnit(const QString &name, const QString &symbol, const CMeasurementPrefix &prefix, const CPressureUnit &base, int displayDigits = 2, double epsilon = 1E-9) :
|
||||
CMeasurementUnit(name, symbol, base, prefix, displayDigits, epsilon) {}
|
||||
typedef Centi<One> PascalsToHectopascals;
|
||||
struct PsiToHectopascals { static double factor() { return 68.948; } };
|
||||
struct InchesToHectopascals { static double factor() { return 33.86389; } };
|
||||
struct MillimetersToHectopascals { static double factor() { return 860.142806; } };
|
||||
|
||||
public:
|
||||
/*!
|
||||
@@ -583,12 +489,6 @@ public:
|
||||
*/
|
||||
CPressureUnit() : CMeasurementUnit(defaultUnit()) {}
|
||||
|
||||
/*!
|
||||
* \brief Copy constructor
|
||||
* \param other
|
||||
*/
|
||||
CPressureUnit(const CPressureUnit &other) : CMeasurementUnit(other) {}
|
||||
|
||||
/*!
|
||||
* \brief Default unit
|
||||
* \return
|
||||
@@ -601,7 +501,7 @@ public:
|
||||
*/
|
||||
static const CPressureUnit &Pa()
|
||||
{
|
||||
static CPressureUnit Pa(QT_TRANSLATE_NOOP("CMeasurementUnit", "pascal"), "Pa", 0.01);
|
||||
static CPressureUnit Pa(QT_TRANSLATE_NOOP("CMeasurementUnit", "pascal"), "Pa", LinearConverter<PascalsToHectopascals>());
|
||||
return Pa;
|
||||
}
|
||||
|
||||
@@ -611,7 +511,7 @@ public:
|
||||
*/
|
||||
static const CPressureUnit &hPa()
|
||||
{
|
||||
static CPressureUnit hPa(QT_TRANSLATE_NOOP("CMeasurementUnit", "hectopascal"), "hPa", CMeasurementPrefix::h(), Pa());
|
||||
static CPressureUnit hPa(QT_TRANSLATE_NOOP("CMeasurementUnit", "hectopascal"), "hPa", IdentityConverter());
|
||||
return hPa;
|
||||
}
|
||||
|
||||
@@ -621,7 +521,7 @@ public:
|
||||
*/
|
||||
static const CPressureUnit &psi()
|
||||
{
|
||||
static CPressureUnit psi(QT_TRANSLATE_NOOP("CMeasurementUnit", "pound per square inch"), "psi", 68.948, 2);
|
||||
static CPressureUnit psi(QT_TRANSLATE_NOOP("CMeasurementUnit", "pound per square inch"), "psi", LinearConverter<PsiToHectopascals>(), 2);
|
||||
return psi;
|
||||
}
|
||||
|
||||
@@ -631,7 +531,7 @@ public:
|
||||
*/
|
||||
static const CPressureUnit &bar()
|
||||
{
|
||||
static CPressureUnit bar(QT_TRANSLATE_NOOP("CMeasurementUnit", "bar"), "bar", 1000, 1);
|
||||
static CPressureUnit bar(QT_TRANSLATE_NOOP("CMeasurementUnit", "bar"), "bar", LinearConverter<Kilo<One>>(), 1);
|
||||
return bar;
|
||||
}
|
||||
|
||||
@@ -641,7 +541,7 @@ public:
|
||||
*/
|
||||
static const CPressureUnit &mbar()
|
||||
{
|
||||
static CPressureUnit mbar(QT_TRANSLATE_NOOP("CMeasurementUnit", "millibar"), "mbar", CMeasurementPrefix::m(), bar(), 1);
|
||||
static CPressureUnit mbar(QT_TRANSLATE_NOOP("CMeasurementUnit", "millibar"), "mbar", IdentityConverter(), 1);
|
||||
return mbar;
|
||||
}
|
||||
|
||||
@@ -651,7 +551,7 @@ public:
|
||||
*/
|
||||
static const CPressureUnit &inHg()
|
||||
{
|
||||
static CPressureUnit inhg(QT_TRANSLATE_NOOP("CMeasurementUnit", "inch of mercury"), "inHg", 33.86389);
|
||||
static CPressureUnit inhg(QT_TRANSLATE_NOOP("CMeasurementUnit", "inch of mercury"), "inHg", LinearConverter<InchesToHectopascals>());
|
||||
return inhg;
|
||||
}
|
||||
|
||||
@@ -661,7 +561,7 @@ public:
|
||||
*/
|
||||
static const CPressureUnit &mmHg()
|
||||
{
|
||||
static CPressureUnit mmhg(QT_TRANSLATE_NOOP("CMeasurementUnit", "millimeter of mercury"), "mmHg", 860.142806);
|
||||
static CPressureUnit mmhg(QT_TRANSLATE_NOOP("CMeasurementUnit", "millimeter of mercury"), "mmHg", LinearConverter<MillimetersToHectopascals>());
|
||||
return mmhg;
|
||||
}
|
||||
|
||||
@@ -703,17 +603,15 @@ protected:
|
||||
class CTemperatureUnit : public CMeasurementUnit
|
||||
{
|
||||
private:
|
||||
/*!
|
||||
* Constructor temperature unit
|
||||
* \param name
|
||||
* \param symbol
|
||||
* \param factor
|
||||
* \param offset
|
||||
* \param displayDigits
|
||||
* \param epsilon
|
||||
*/
|
||||
CTemperatureUnit(const QString &name, const QString &symbol, double factor, double offset, int displayDigits = 2, double epsilon = 1E-9) :
|
||||
CMeasurementUnit(name, symbol, factor, offset, displayDigits, epsilon) {}
|
||||
template <class Converter>
|
||||
CTemperatureUnit(const QString &name, const QString &symbol, const Converter &converter, int displayDigits = 2, double epsilon = 1E-9) :
|
||||
CMeasurementUnit(name, symbol, converter, displayDigits, epsilon)
|
||||
{}
|
||||
|
||||
struct KelvinToCentigrade { static double factor() { return 1.0; }
|
||||
static double offset() { return 273.15; } };
|
||||
struct FahrenheitToCentigrade { static double factor() { return 5.0 / 9.0; }
|
||||
static double offset() { return 32.0; } };
|
||||
|
||||
public:
|
||||
/*!
|
||||
@@ -721,12 +619,6 @@ public:
|
||||
*/
|
||||
CTemperatureUnit() : CMeasurementUnit(defaultUnit()) {}
|
||||
|
||||
/*!
|
||||
* \brief Copy constructor
|
||||
* \param other
|
||||
*/
|
||||
CTemperatureUnit(const CTemperatureUnit &other) : CMeasurementUnit(other) {}
|
||||
|
||||
/*!
|
||||
* \brief Default unit
|
||||
* \return
|
||||
@@ -739,7 +631,7 @@ public:
|
||||
*/
|
||||
static const CTemperatureUnit &K()
|
||||
{
|
||||
static CTemperatureUnit K(QT_TRANSLATE_NOOP("CMeasurementUnit", "Kelvin"), "K", 1, 273.15);
|
||||
static CTemperatureUnit K(QT_TRANSLATE_NOOP("CMeasurementUnit", "Kelvin"), "K", AffineConverter<KelvinToCentigrade>());
|
||||
return K;
|
||||
}
|
||||
|
||||
@@ -749,7 +641,7 @@ public:
|
||||
*/
|
||||
static const CTemperatureUnit &C()
|
||||
{
|
||||
static CTemperatureUnit C(QT_TRANSLATE_NOOP("CMeasurementUnit", "centigrade"), QT_TRANSLATE_NOOP("CMeasurementUnit", "C"), 1, 0);
|
||||
static CTemperatureUnit C(QT_TRANSLATE_NOOP("CMeasurementUnit", "centigrade"), QT_TRANSLATE_NOOP("CMeasurementUnit", "C"), IdentityConverter());
|
||||
return C;
|
||||
}
|
||||
|
||||
@@ -759,7 +651,7 @@ public:
|
||||
*/
|
||||
static const CTemperatureUnit &F()
|
||||
{
|
||||
static CTemperatureUnit F(QT_TRANSLATE_NOOP("CMeasurementUnit", "Fahrenheit"), QT_TRANSLATE_NOOP("CMeasurementUnit", "F"), 5.0 / 9.0, 32);
|
||||
static CTemperatureUnit F(QT_TRANSLATE_NOOP("CMeasurementUnit", "Fahrenheit"), QT_TRANSLATE_NOOP("CMeasurementUnit", "F"), AffineConverter<FahrenheitToCentigrade>());
|
||||
return F;
|
||||
}
|
||||
|
||||
@@ -798,16 +690,15 @@ protected:
|
||||
class CSpeedUnit : public CMeasurementUnit
|
||||
{
|
||||
private:
|
||||
/*!
|
||||
* \brief Speed unit constructor
|
||||
* \param name
|
||||
* \param symbol
|
||||
* \param factor
|
||||
* \param displayDigits
|
||||
* \param epsilon
|
||||
*/
|
||||
CSpeedUnit(const QString &name, const QString &symbol, double factor, int displayDigits = 2, double epsilon = 1E-9) :
|
||||
CMeasurementUnit(name, symbol, factor, displayDigits, epsilon) {}
|
||||
template <class Converter>
|
||||
CSpeedUnit(const QString &name, const QString &symbol, const Converter &converter, int displayDigits = 2, double epsilon = 1E-9) :
|
||||
CMeasurementUnit(name, symbol, converter, displayDigits, epsilon)
|
||||
{}
|
||||
|
||||
struct KnotsToMps { static double factor() { return 1852.0 / 3600.0; } };
|
||||
struct KphToMps { static double factor() { return 1.0 / 3.6; } };
|
||||
struct FtPerSecToMps { static double factor() { return 0.3048 ; } };
|
||||
struct FtPerMinToMps { static double factor() { return 0.3048 / 60.0; } };
|
||||
|
||||
public:
|
||||
/*!
|
||||
@@ -815,12 +706,6 @@ public:
|
||||
*/
|
||||
CSpeedUnit() : CMeasurementUnit(defaultUnit()) {}
|
||||
|
||||
/*!
|
||||
* Constructor, allows to implement methods in base class
|
||||
* \param other
|
||||
*/
|
||||
CSpeedUnit(const CSpeedUnit &other) : CMeasurementUnit(other) {}
|
||||
|
||||
/*!
|
||||
* Default unit
|
||||
* \return
|
||||
@@ -833,7 +718,7 @@ public:
|
||||
*/
|
||||
static const CSpeedUnit &m_s()
|
||||
{
|
||||
static CSpeedUnit ms(QT_TRANSLATE_NOOP("CMeasurementUnit", "meter per second"), "m/s", 1);
|
||||
static CSpeedUnit ms(QT_TRANSLATE_NOOP("CMeasurementUnit", "meter per second"), "m/s", IdentityConverter());
|
||||
return ms;
|
||||
}
|
||||
|
||||
@@ -843,7 +728,7 @@ public:
|
||||
*/
|
||||
static const CSpeedUnit &kts()
|
||||
{
|
||||
static CSpeedUnit kts(QT_TRANSLATE_NOOP("CMeasurementUnit", "knot"), "kts", 1852.0 / 3600.0, 1);
|
||||
static CSpeedUnit kts(QT_TRANSLATE_NOOP("CMeasurementUnit", "knot"), "kts", LinearConverter<KnotsToMps>(), 1);
|
||||
return kts;
|
||||
}
|
||||
|
||||
@@ -853,7 +738,7 @@ public:
|
||||
*/
|
||||
static const CSpeedUnit &NM_h()
|
||||
{
|
||||
static CSpeedUnit NMh(QT_TRANSLATE_NOOP("CMeasurementUnit", "nautical mile per hour"), "NM/h", 1852.0 / 3600.0, 1);
|
||||
static CSpeedUnit NMh(QT_TRANSLATE_NOOP("CMeasurementUnit", "nautical mile per hour"), "NM/h", LinearConverter<KnotsToMps>(), 1);
|
||||
return NMh;
|
||||
}
|
||||
|
||||
@@ -863,7 +748,7 @@ public:
|
||||
*/
|
||||
static const CSpeedUnit &ft_s()
|
||||
{
|
||||
static CSpeedUnit fts(QT_TRANSLATE_NOOP("CMeasurementUnit", "foot per second"), "ft/s", 0.3048, 0);
|
||||
static CSpeedUnit fts(QT_TRANSLATE_NOOP("CMeasurementUnit", "foot per second"), "ft/s", LinearConverter<FtPerSecToMps>(), 0);
|
||||
return fts;
|
||||
}
|
||||
|
||||
@@ -873,7 +758,7 @@ public:
|
||||
*/
|
||||
static const CSpeedUnit &ft_min()
|
||||
{
|
||||
static CSpeedUnit ftmin(QT_TRANSLATE_NOOP("CMeasurementUnit", "foot per minute"), "ft/min", 0.3048 / 60.0, 0);
|
||||
static CSpeedUnit ftmin(QT_TRANSLATE_NOOP("CMeasurementUnit", "foot per minute"), "ft/min", LinearConverter<FtPerMinToMps>(), 0);
|
||||
return ftmin;
|
||||
}
|
||||
|
||||
@@ -883,7 +768,7 @@ public:
|
||||
*/
|
||||
static const CSpeedUnit &km_h()
|
||||
{
|
||||
static CSpeedUnit kmh(QT_TRANSLATE_NOOP("CMeasurementUnit", "kilometer per hour"), "km/h", 1.0 / 3.6, 1);
|
||||
static CSpeedUnit kmh(QT_TRANSLATE_NOOP("CMeasurementUnit", "kilometer per hour"), "km/h", LinearConverter<KphToMps>(), 1);
|
||||
return kmh;
|
||||
}
|
||||
|
||||
@@ -925,39 +810,15 @@ protected:
|
||||
class CTimeUnit : public CMeasurementUnit
|
||||
{
|
||||
private:
|
||||
/*!
|
||||
* \brief Time unit constructor
|
||||
* \param name
|
||||
* \param symbol
|
||||
* \param factor
|
||||
* \param displayDigits
|
||||
* \param epsilon
|
||||
*/
|
||||
CTimeUnit(const QString &name, const QString &symbol, double factor, int displayDigits = 2, double epsilon = 1E-9) :
|
||||
CMeasurementUnit(name, symbol, factor, displayDigits, epsilon) {}
|
||||
template <class Converter>
|
||||
CTimeUnit(const QString &name, const QString &symbol, const Converter &converter, int displayDigits = 2, double epsilon = 1E-9) :
|
||||
CMeasurementUnit(name, symbol, converter, displayDigits, epsilon)
|
||||
{}
|
||||
|
||||
/*!
|
||||
* \brief Time unit constructor
|
||||
* \param name
|
||||
* \param symbol
|
||||
* \param prefix
|
||||
* \param base
|
||||
* \param displayDigits
|
||||
* \param epsilon
|
||||
*/
|
||||
CTimeUnit(const QString &name, const QString &symbol, const CMeasurementPrefix &prefix, const CTimeUnit &base, int displayDigits = 2, double epsilon = 1E-9) :
|
||||
CMeasurementUnit(name, symbol, base, prefix, displayDigits, epsilon) {}
|
||||
|
||||
/*!
|
||||
* \brief Time unit constructor
|
||||
* \param name
|
||||
* \param symbol
|
||||
* \param converter
|
||||
* \param displayDigits
|
||||
* \param epsilon
|
||||
*/
|
||||
CTimeUnit(const QString &name, const QString &symbol, Converter *converter, int displayDigits = 2, double epsilon = 1E-9) :
|
||||
CMeasurementUnit(name, symbol, converter, displayDigits, epsilon) {}
|
||||
typedef One SecondsToSeconds;
|
||||
struct DaysToSeconds { static double factor() { return 60.0 * 60.0 * 24.0; } };
|
||||
struct HoursToSeconds { static double factor() { return 60.0 * 60.0; } };
|
||||
struct MinutesToSeconds { static double factor() { return 60.0; } };
|
||||
|
||||
public:
|
||||
/*!
|
||||
@@ -965,12 +826,6 @@ public:
|
||||
*/
|
||||
CTimeUnit() : CMeasurementUnit(defaultUnit()) {}
|
||||
|
||||
/*!
|
||||
* Constructor, allows to implement methods in base class
|
||||
* \param other
|
||||
*/
|
||||
CTimeUnit(const CTimeUnit &other) : CMeasurementUnit(other) {}
|
||||
|
||||
/*!
|
||||
* Default unit
|
||||
* \return
|
||||
@@ -992,7 +847,7 @@ public:
|
||||
*/
|
||||
static const CTimeUnit &s()
|
||||
{
|
||||
static CTimeUnit s(QT_TRANSLATE_NOOP("CMeasurementUnit", "second"), "s", 1, 1);
|
||||
static CTimeUnit s(QT_TRANSLATE_NOOP("CMeasurementUnit", "second"), "s", IdentityConverter(), 1);
|
||||
return s;
|
||||
}
|
||||
|
||||
@@ -1002,7 +857,7 @@ public:
|
||||
*/
|
||||
static const CTimeUnit &ms()
|
||||
{
|
||||
static CTimeUnit ms(QT_TRANSLATE_NOOP("CMeasurementUnit", "millisecond"), "ms", CMeasurementPrefix::m(), s(), 0);
|
||||
static CTimeUnit ms(QT_TRANSLATE_NOOP("CMeasurementUnit", "millisecond"), "ms", LinearConverter<Milli<SecondsToSeconds>>(), 0);
|
||||
return ms;
|
||||
}
|
||||
|
||||
@@ -1012,7 +867,7 @@ public:
|
||||
*/
|
||||
static const CTimeUnit &h()
|
||||
{
|
||||
static CTimeUnit h(QT_TRANSLATE_NOOP("CMeasurementUnit", "hour"), "h", 3600, 1);
|
||||
static CTimeUnit h(QT_TRANSLATE_NOOP("CMeasurementUnit", "hour"), "h", LinearConverter<HoursToSeconds>(), 1);
|
||||
return h;
|
||||
}
|
||||
|
||||
@@ -1022,7 +877,7 @@ public:
|
||||
*/
|
||||
static const CTimeUnit &min()
|
||||
{
|
||||
static CTimeUnit min(QT_TRANSLATE_NOOP("CMeasurementUnit", "minute"), "min", 60, 2);
|
||||
static CTimeUnit min(QT_TRANSLATE_NOOP("CMeasurementUnit", "minute"), "min", LinearConverter<MinutesToSeconds>(), 2);
|
||||
return min;
|
||||
}
|
||||
|
||||
@@ -1032,7 +887,7 @@ public:
|
||||
*/
|
||||
static const CTimeUnit &d()
|
||||
{
|
||||
static CTimeUnit day(QT_TRANSLATE_NOOP("CMeasurementUnit", "day"), "d", 3600 * 24, 1);
|
||||
static CTimeUnit day(QT_TRANSLATE_NOOP("CMeasurementUnit", "day"), "d", LinearConverter<DaysToSeconds>(), 1);
|
||||
return day;
|
||||
}
|
||||
|
||||
@@ -1041,7 +896,7 @@ public:
|
||||
*/
|
||||
static const CTimeUnit &hms()
|
||||
{
|
||||
static CTimeUnit hms(QT_TRANSLATE_NOOP("CMeasurementUnit", "hour, minute, second"), "hms", new SubdivisionConverter2<60, 100, 60, 100>(3600), 4);
|
||||
static CTimeUnit hms(QT_TRANSLATE_NOOP("CMeasurementUnit", "hour, minute, second"), "hms", SubdivisionConverter2<HoursToSeconds, InEachHundred<60>>(), 4);
|
||||
return hms;
|
||||
(void)QT_TRANSLATE_NOOP("CMeasurementUnit", "%L1h%L2m%L3s");
|
||||
(void)QT_TRANSLATE_NOOP("CMeasurementUnit", "%-L1h%L2m%L3s");
|
||||
@@ -1052,7 +907,7 @@ public:
|
||||
*/
|
||||
static const CTimeUnit &hrmin()
|
||||
{
|
||||
static CTimeUnit hrmin(QT_TRANSLATE_NOOP("CMeasurementUnit", "hour, minute"), "hm", new SubdivisionConverter<60, 100>(3600), 3);
|
||||
static CTimeUnit hrmin(QT_TRANSLATE_NOOP("CMeasurementUnit", "hour, minute"), "hm", SubdivisionConverter<HoursToSeconds, InEachHundred<60>>(), 3);
|
||||
return hrmin;
|
||||
(void)QT_TRANSLATE_NOOP("CMeasurementUnit", "%L1h%L2m");
|
||||
(void)QT_TRANSLATE_NOOP("CMeasurementUnit", "-%L1h%L2m");
|
||||
@@ -1063,7 +918,7 @@ public:
|
||||
*/
|
||||
static const CTimeUnit &minsec()
|
||||
{
|
||||
static CTimeUnit minsec(QT_TRANSLATE_NOOP("CMeasurementUnit", "minute, second"), "minsec", new SubdivisionConverter<60, 100>(60), 2);
|
||||
static CTimeUnit minsec(QT_TRANSLATE_NOOP("CMeasurementUnit", "minute, second"), "minsec", SubdivisionConverter<MinutesToSeconds, InEachHundred<60>>(), 2);
|
||||
return minsec;
|
||||
(void)QT_TRANSLATE_NOOP("CMeasurementUnit", "%L1m%L2s");
|
||||
(void)QT_TRANSLATE_NOOP("CMeasurementUnit", "-%L1m%L2s");
|
||||
@@ -1106,16 +961,12 @@ protected:
|
||||
class CAccelerationUnit : public CMeasurementUnit
|
||||
{
|
||||
private:
|
||||
/*!
|
||||
* \brief Acceleration unit constructor
|
||||
* \param name
|
||||
* \param symbol
|
||||
* \param factor
|
||||
* \param displayDigits
|
||||
* \param epsilon
|
||||
*/
|
||||
CAccelerationUnit(const QString &name, const QString &symbol, double factor, int displayDigits = 2, double epsilon = 1E-9) :
|
||||
CMeasurementUnit(name, symbol, factor, displayDigits, epsilon) {}
|
||||
template <class Converter>
|
||||
CAccelerationUnit(const QString &name, const QString &symbol, const Converter &converter, int displayDigits = 2, double epsilon = 1E-9) :
|
||||
CMeasurementUnit(name, symbol, converter, displayDigits, epsilon)
|
||||
{}
|
||||
|
||||
struct FeetToMeters { static double factor() { return 0.3048; } };
|
||||
|
||||
public:
|
||||
/*!
|
||||
@@ -1123,12 +974,6 @@ public:
|
||||
*/
|
||||
CAccelerationUnit() : CMeasurementUnit(defaultUnit()) {}
|
||||
|
||||
/*!
|
||||
* Copy constructor
|
||||
* \param other
|
||||
*/
|
||||
CAccelerationUnit(const CAccelerationUnit &other) : CMeasurementUnit(other) {}
|
||||
|
||||
/*!
|
||||
* Default unit
|
||||
* \return
|
||||
@@ -1141,7 +986,7 @@ public:
|
||||
*/
|
||||
static const CAccelerationUnit &m_s2()
|
||||
{
|
||||
static CAccelerationUnit ms2(QT_TRANSLATE_NOOP("CMeasurementUnit", "meter per second per second"), QT_TRANSLATE_NOOP("CMeasurementUnit", "m/s^2"), 1, 1);
|
||||
static CAccelerationUnit ms2(QT_TRANSLATE_NOOP("CMeasurementUnit", "meter per second per second"), QT_TRANSLATE_NOOP("CMeasurementUnit", "m/s^2"), IdentityConverter(), 1);
|
||||
return ms2;
|
||||
}
|
||||
|
||||
@@ -1151,7 +996,7 @@ public:
|
||||
*/
|
||||
static const CAccelerationUnit &ft_s2()
|
||||
{
|
||||
static CAccelerationUnit fts2(QT_TRANSLATE_NOOP("CMeasurementUnit", "foot per second per second"), QT_TRANSLATE_NOOP("CMeasurementUnit", "ft/s^2"), 3.28084, 0);
|
||||
static CAccelerationUnit fts2(QT_TRANSLATE_NOOP("CMeasurementUnit", "foot per second per second"), QT_TRANSLATE_NOOP("CMeasurementUnit", "ft/s^2"), LinearConverter<FeetToMeters>(), 0);
|
||||
return fts2;
|
||||
}
|
||||
|
||||
|
||||
@@ -81,7 +81,7 @@ void CTestPhysicalQuantities::frequencyTests()
|
||||
QVERIFY2(f1.valueRounded(CFrequencyUnit::kHz(), 2) == 1000, "Mega is 1000kHz");
|
||||
QVERIFY2(f1.value() == 1 , "1MHz");
|
||||
QVERIFY2(f1.value(CFrequencyUnit::defaultUnit()) == 1000000 , "1E6 Hz");
|
||||
CFrequency f2(CMeasurementPrefix::M().toDouble(), CFrequencyUnit::Hz()) ; // 1 Megahertz
|
||||
CFrequency f2(1e+6, CFrequencyUnit::Hz()) ; // 1 Megahertz
|
||||
QVERIFY2(f1 == f2 , "MHz is 1E6 Hz");
|
||||
}
|
||||
|
||||
@@ -173,7 +173,7 @@ void CTestPhysicalQuantities::accelerationTests()
|
||||
CAcceleration a2(a1);
|
||||
a1.switchUnit(CAccelerationUnit::ft_s2());
|
||||
QVERIFY2(a1 == a2, "Accelerations should be similar");
|
||||
QVERIFY2(BlackMisc::Math::CMath::round(a1.value() * ftFactor, 6) == a2.valueRounded(6),
|
||||
QVERIFY2(BlackMisc::Math::CMath::round(a2.value() * ftFactor, 6) == a1.valueRounded(6),
|
||||
"Numerical values should be equal");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user