20#ifndef LIBREPCB_CORE_RATIO_H
21#define LIBREPCB_CORE_RATIO_H
26#include "../exceptions.h"
28#include <type_safe/constrained_type.hpp>
45 Q_DECLARE_TR_FUNCTIONS(
Ratio)
67 constexpr explicit Ratio(qint32 ppm) noexcept :
mPpm(ppm) {}
112 mPpm = normalized * 1e6;
258 explicit operator bool()
const {
return mPpm != 0; }
292 stream << QString(
"Ratio(%1%%°)").arg(ratio.
toPercent());
296inline std::size_t
qHash(
const Ratio& key, std::size_t seed = 0) noexcept {
297 return ::qHash(key.
toPpm(), seed);
305 template <
typename Value,
typename Predicate>
306 static constexpr auto verify(Value&& val,
const Predicate& p) ->
307 typename std::decay<Value>::type {
308 return p(val) ? std::forward<Value>(val)
310 Ratio::tr(
"Value must be >= 0!")),
311 std::forward<Value>(val));
332 stream << ratio->toNormalizedString();
337 stream << QString(
"UnsignedRatio(%1%%)").arg(ratio->toPercent());
342 std::size_t seed = 0) noexcept {
343 return ::qHash(key->toPpm(), seed);
351 template <
typename Value,
typename Predicate>
352 static constexpr auto verify(Value&& val,
const Predicate& p) ->
353 typename std::decay<Value>::type {
354 return p(val) ? std::forward<Value>(val)
356 Ratio::tr(
"Value must be 0..1!")),
357 std::forward<Value>(val));
380 stream << ratio->toNormalizedString();
385 stream << QString(
"UnsignedLimitedRatio(%1%%)").arg(ratio->toPercent());
390 std::size_t seed = 0) noexcept {
391 return ::qHash(key->toPpm(), seed);
The Ratio class is used to represent a ratio number (e.g. 13.37%)
Definition ratio.h:44
Ratio & operator+=(const Ratio &rhs)
Definition ratio.h:214
constexpr bool operator<(const Ratio &rhs) const
Definition ratio.h:248
constexpr bool operator>(qint32 rhs) const
Definition ratio.h:247
static Ratio fromPercent(int percent) noexcept
Get a Ratio object with a specific ratio.
Definition ratio.cpp:45
constexpr bool operator==(qint32 rhs) const
Definition ratio.h:255
constexpr bool operator>=(qint32 rhs) const
Definition ratio.h:251
Ratio operator+(const Ratio &rhs) const
Definition ratio.h:238
Ratio & operator/=(qint32 rhs)
Definition ratio.h:234
Ratio operator/(qint32 rhs) const
Definition ratio.h:244
qint32 mPpm
the ratio in PPM
Definition ratio.h:279
Ratio & operator-=(const Ratio &rhs)
Definition ratio.h:218
qreal toPercent() const noexcept
Get the ratio in percent.
Definition ratio.h:143
Ratio & operator*=(const Ratio &rhs)
Definition ratio.h:222
void setRatioNormalized(qreal normalized) noexcept
Set the ratio as a normalized number.
Definition ratio.h:111
constexpr bool operator!=(qint32 rhs) const
Definition ratio.h:257
static qint32 normalizedStringToPpm(const QString &normalized)
Convert a normalized ratio from a QString to an integer (in PPM)
Definition ratio.cpp:71
qint32 toPpm() const noexcept
Get the ratio in PPM.
Definition ratio.h:136
constexpr bool operator!=(const Ratio &rhs) const
Definition ratio.h:256
constexpr bool operator>(const Ratio &rhs) const
Definition ratio.h:246
void setRatioPpm(qint32 ppm) noexcept
Set the ratio in PPM.
Definition ratio.h:81
constexpr Ratio(qint32 ppm) noexcept
Constructor with a ratio in PPM.
Definition ratio.h:67
constexpr bool operator<(qint32 rhs) const
Definition ratio.h:249
Ratio operator*(const Ratio &rhs) const
Definition ratio.h:241
constexpr bool operator<=(qint32 rhs) const
Definition ratio.h:253
void setRatioNormalized(const QString &normalized)
Set the ratio as a normalized number, represented in a QString.
Definition ratio.h:125
static Ratio fromNormalized(qreal normalized) noexcept
Get a Ratio object with a specific ratio.
Definition ratio.cpp:57
constexpr bool operator<=(const Ratio &rhs) const
Definition ratio.h:252
constexpr Ratio() noexcept
Default Constructor.
Definition ratio.h:53
constexpr bool operator==(const Ratio &rhs) const
Definition ratio.h:254
qreal toNormalized() const noexcept
Get the ratio as a normalized number.
Definition ratio.h:150
constexpr Ratio(const Ratio &ratio) noexcept
Copy Constructor.
Definition ratio.h:60
void setRatioPercent(int percent) noexcept
Set the ratio in percent.
Definition ratio.h:88
Ratio operator/(const Ratio &rhs) const
Definition ratio.h:243
QString toNormalizedString() const noexcept
Get the ratio as a normalized QString.
Definition ratio.cpp:39
Ratio operator%(const Ratio &rhs) const
Definition ratio.h:245
Ratio & operator*=(qint32 rhs)
Definition ratio.h:226
constexpr bool operator>=(const Ratio &rhs) const
Definition ratio.h:250
~Ratio()=default
Destructor.
Ratio operator-() const
Definition ratio.h:239
Ratio operator-(const Ratio &rhs) const
Definition ratio.h:240
Ratio & operator/=(const Ratio &rhs)
Definition ratio.h:230
void setRatioPercent(qreal percent) noexcept
Set the ratio in percent.
Definition ratio.h:98
Ratio operator*(qint32 rhs) const
Definition ratio.h:242
The RuntimeError class.
Definition exceptions.h:218
Definition occmodel.cpp:76
std::size_t qHash(const AttributeKey &key, std::size_t seed=0) noexcept
Definition attributekey.h:118
type_safe::constrained_type< Ratio, UnsignedRatioConstraint, UnsignedRatioVerifier > UnsignedRatio
Definition ratio.h:328
type_safe::constrained_type< Ratio, UnsignedLimitedRatioConstraint, UnsignedLimitedRatioVerifier > UnsignedLimitedRatio
Definition ratio.h:376
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition attributekey.h:108
constexpr bool operator()(const Ratio &r) const noexcept
Definition ratio.h:362
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition ratio.h:352
constexpr bool operator()(const Ratio &r) const noexcept
Definition ratio.h:316
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition ratio.h:306