20#ifndef LIBREPCB_CORE_RATIO_H
21#define LIBREPCB_CORE_RATIO_H
26#include "../exceptions.h"
27#include "../qtcompat.h"
29#include <type_safe/constrained_type.hpp>
46 Q_DECLARE_TR_FUNCTIONS(
Ratio)
68 constexpr explicit Ratio(qint32 ppm) noexcept :
mPpm(ppm) {}
113 mPpm = normalized * 1e6;
259 explicit operator bool()
const {
return mPpm != 0; }
293 stream << QString(
"Ratio(%1%%°)").arg(ratio.
toPercent());
307 template <
typename Value,
typename Predicate>
308 static constexpr auto verify(Value&& val,
const Predicate& p) ->
309 typename std::decay<Value>::type {
310 return p(val) ? std::forward<Value>(val)
312 Ratio::tr(
"Value must be >= 0!")),
313 std::forward<Value>(val));
334 stream << ratio->toNormalizedString();
339 stream << QString(
"UnsignedRatio(%1%%)").arg(ratio->toPercent());
353 template <
typename Value,
typename Predicate>
354 static constexpr auto verify(Value&& val,
const Predicate& p) ->
355 typename std::decay<Value>::type {
356 return p(val) ? std::forward<Value>(val)
358 Ratio::tr(
"Value must be 0..1!")),
359 std::forward<Value>(val));
382 stream << ratio->toNormalizedString();
387 stream << QString(
"UnsignedLimitedRatio(%1%%)").arg(ratio->toPercent());
uint Hash
Return type of Qt's qHash() function.
Definition: qtcompat.h:58
The Ratio class is used to represent a ratio number (e.g. 13.37%)
Definition: ratio.h:45
Ratio & operator+=(const Ratio &rhs)
Definition: ratio.h:215
constexpr bool operator<(const Ratio &rhs) const
Definition: ratio.h:249
constexpr bool operator>(qint32 rhs) const
Definition: ratio.h:248
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:256
constexpr bool operator>=(qint32 rhs) const
Definition: ratio.h:252
Ratio operator+(const Ratio &rhs) const
Definition: ratio.h:239
Ratio & operator/=(qint32 rhs)
Definition: ratio.h:235
Ratio operator/(qint32 rhs) const
Definition: ratio.h:245
qint32 mPpm
the ratio in PPM
Definition: ratio.h:280
Ratio & operator-=(const Ratio &rhs)
Definition: ratio.h:219
qreal toPercent() const noexcept
Get the ratio in percent.
Definition: ratio.h:144
Ratio & operator*=(const Ratio &rhs)
Definition: ratio.h:223
void setRatioNormalized(qreal normalized) noexcept
Set the ratio as a normalized number.
Definition: ratio.h:112
constexpr bool operator!=(qint32 rhs) const
Definition: ratio.h:258
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:137
constexpr bool operator!=(const Ratio &rhs) const
Definition: ratio.h:257
constexpr bool operator>(const Ratio &rhs) const
Definition: ratio.h:247
void setRatioPpm(qint32 ppm) noexcept
Set the ratio in PPM.
Definition: ratio.h:82
constexpr Ratio(qint32 ppm) noexcept
Constructor with a ratio in PPM.
Definition: ratio.h:68
constexpr bool operator<(qint32 rhs) const
Definition: ratio.h:250
Ratio operator*(const Ratio &rhs) const
Definition: ratio.h:242
constexpr bool operator<=(qint32 rhs) const
Definition: ratio.h:254
void setRatioNormalized(const QString &normalized)
Set the ratio as a normalized number, represented in a QString.
Definition: ratio.h:126
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:253
constexpr Ratio() noexcept
Default Constructor.
Definition: ratio.h:54
constexpr bool operator==(const Ratio &rhs) const
Definition: ratio.h:255
qreal toNormalized() const noexcept
Get the ratio as a normalized number.
Definition: ratio.h:151
constexpr Ratio(const Ratio &ratio) noexcept
Copy Constructor.
Definition: ratio.h:61
void setRatioPercent(int percent) noexcept
Set the ratio in percent.
Definition: ratio.h:89
Ratio operator/(const Ratio &rhs) const
Definition: ratio.h:244
QString toNormalizedString() const noexcept
Get the ratio as a normalized QString.
Definition: ratio.cpp:39
Ratio operator%(const Ratio &rhs) const
Definition: ratio.h:246
Ratio & operator*=(qint32 rhs)
Definition: ratio.h:227
constexpr bool operator>=(const Ratio &rhs) const
Definition: ratio.h:251
~Ratio()=default
Destructor.
Ratio operator-() const
Definition: ratio.h:240
Ratio operator-(const Ratio &rhs) const
Definition: ratio.h:241
Ratio & operator/=(const Ratio &rhs)
Definition: ratio.h:231
void setRatioPercent(qreal percent) noexcept
Set the ratio in percent.
Definition: ratio.h:99
Ratio operator*(qint32 rhs) const
Definition: ratio.h:243
The RuntimeError class.
Definition: exceptions.h:218
Definition: occmodel.cpp:77
QtCompat::Hash qHash(const AttributeKey &key, QtCompat::Hash seed=0) noexcept
Definition: attributekey.h:119
type_safe::constrained_type< Ratio, UnsignedRatioConstraint, UnsignedRatioVerifier > UnsignedRatio
Definition: ratio.h:330
type_safe::constrained_type< Ratio, UnsignedLimitedRatioConstraint, UnsignedLimitedRatioVerifier > UnsignedLimitedRatio
Definition: ratio.h:378
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition: attributekey.h:109
QtCompat::Hash qHash(const UnsignedLimitedRatio &key, QtCompat::Hash seed=0) noexcept
Definition: ratio.h:391
constexpr bool operator()(const Ratio &r) const noexcept
Definition: ratio.h:364
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition: ratio.h:354
constexpr bool operator()(const Ratio &r) const noexcept
Definition: ratio.h:318
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition: ratio.h:308