LibrePCB Developers Documentation
Loading...
Searching...
No Matches
ratio.h
Go to the documentation of this file.
1/*
2 * LibrePCB - Professional EDA for everyone!
3 * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4 * https://librepcb.org/
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef LIBREPCB_CORE_RATIO_H
21#define LIBREPCB_CORE_RATIO_H
22
23/*******************************************************************************
24 * Includes
25 ******************************************************************************/
26#include "../exceptions.h"
27
28#include <type_safe/constrained_type.hpp>
29
30#include <QtCore>
31
32/*******************************************************************************
33 * Namespace / Forward Declarations
34 ******************************************************************************/
35namespace librepcb {
36
37/*******************************************************************************
38 * Class Ratio
39 ******************************************************************************/
40
44class Ratio {
45 Q_DECLARE_TR_FUNCTIONS(Ratio)
46
47public:
48 // Constructors / Destructor
49
53 constexpr Ratio() noexcept : Ratio(0) {}
54
60 constexpr Ratio(const Ratio& ratio) noexcept : mPpm(ratio.mPpm) {}
61
67 constexpr explicit Ratio(qint32 ppm) noexcept : mPpm(ppm) {}
68
72 ~Ratio() = default;
73
74 // Setters
75
81 void setRatioPpm(qint32 ppm) noexcept { mPpm = ppm; }
82
88 void setRatioPercent(int percent) noexcept { mPpm = percent * 1e4; }
89
98 void setRatioPercent(qreal percent) noexcept { mPpm = percent * 1e4; }
99
111 void setRatioNormalized(qreal normalized) noexcept {
112 mPpm = normalized * 1e6;
113 }
114
125 void setRatioNormalized(const QString& normalized) {
126 mPpm = normalizedStringToPpm(normalized);
127 }
128
129 // Conversions
130
136 qint32 toPpm() const noexcept { return mPpm; }
137
143 qreal toPercent() const noexcept { return (qreal)mPpm / 1e4; }
144
150 qreal toNormalized() const noexcept { return (qreal)mPpm / 1e6; }
151
159 QString toNormalizedString() const noexcept;
160
161 // Static Methods
162
170 static Ratio fromPercent(int percent) noexcept;
171
179 static Ratio fromPercent(qreal percent) noexcept;
180
188 static Ratio fromNormalized(qreal normalized) noexcept;
189
207 static Ratio fromNormalized(const QString& normalized);
208
209 // Operators
210 Ratio& operator=(const Ratio& rhs) {
211 mPpm = rhs.mPpm;
212 return *this;
213 }
214 Ratio& operator+=(const Ratio& rhs) {
215 mPpm += rhs.mPpm;
216 return *this;
217 }
218 Ratio& operator-=(const Ratio& rhs) {
219 mPpm -= rhs.mPpm;
220 return *this;
221 }
222 Ratio& operator*=(const Ratio& rhs) {
223 mPpm *= rhs.mPpm;
224 return *this;
225 }
226 Ratio& operator*=(qint32 rhs) {
227 mPpm *= rhs;
228 return *this;
229 }
230 Ratio& operator/=(const Ratio& rhs) {
231 mPpm /= rhs.mPpm;
232 return *this;
233 }
234 Ratio& operator/=(qint32 rhs) {
235 mPpm /= rhs;
236 return *this;
237 }
238 Ratio operator+(const Ratio& rhs) const { return Ratio(mPpm + rhs.mPpm); }
239 Ratio operator-() const { return Ratio(-mPpm); }
240 Ratio operator-(const Ratio& rhs) const { return Ratio(mPpm - rhs.mPpm); }
241 Ratio operator*(const Ratio& rhs) const { return Ratio(mPpm * rhs.mPpm); }
242 Ratio operator*(qint32 rhs) const { return Ratio(mPpm * rhs); }
243 Ratio operator/(const Ratio& rhs) const { return Ratio(mPpm / rhs.mPpm); }
244 Ratio operator/(qint32 rhs) const { return Ratio(mPpm / rhs); }
245 Ratio operator%(const Ratio& rhs) const { return Ratio(mPpm % rhs.mPpm); }
246 constexpr bool operator>(const Ratio& rhs) const { return mPpm > rhs.mPpm; }
247 constexpr bool operator>(qint32 rhs) const { return mPpm > rhs; }
248 constexpr bool operator<(const Ratio& rhs) const { return mPpm < rhs.mPpm; }
249 constexpr bool operator<(qint32 rhs) const { return mPpm < rhs; }
250 constexpr bool operator>=(const Ratio& rhs) const { return mPpm >= rhs.mPpm; }
251 constexpr bool operator>=(qint32 rhs) const { return mPpm >= rhs; }
252 constexpr bool operator<=(const Ratio& rhs) const { return mPpm <= rhs.mPpm; }
253 constexpr bool operator<=(qint32 rhs) const { return mPpm <= rhs; }
254 constexpr bool operator==(const Ratio& rhs) const { return mPpm == rhs.mPpm; }
255 constexpr bool operator==(qint32 rhs) const { return mPpm == rhs; }
256 constexpr bool operator!=(const Ratio& rhs) const { return mPpm != rhs.mPpm; }
257 constexpr bool operator!=(qint32 rhs) const { return mPpm != rhs; }
258 explicit operator bool() const { return mPpm != 0; }
259
260private:
261 // Private Static Functions
262
276 static qint32 normalizedStringToPpm(const QString& normalized);
277
278 // Private Member Variables
279 qint32 mPpm;
280};
281
282/*******************************************************************************
283 * Non-Member Functions
284 ******************************************************************************/
285
286inline QDataStream& operator<<(QDataStream& stream, const Ratio& ratio) {
287 stream << ratio.toNormalizedString();
288 return stream;
289}
290
291inline QDebug operator<<(QDebug stream, const Ratio& ratio) {
292 stream << QString("Ratio(%1%%°)").arg(ratio.toPercent());
293 return stream;
294}
295
296inline std::size_t qHash(const Ratio& key, std::size_t seed = 0) noexcept {
297 return ::qHash(key.toPpm(), seed);
298}
299
300/*******************************************************************************
301 * Class UnsignedRatio
302 ******************************************************************************/
303
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)
309 : (throw RuntimeError(__FILE__, __LINE__,
310 Ratio::tr("Value must be >= 0!")),
311 std::forward<Value>(val));
312 }
313};
314
316 constexpr bool operator()(const Ratio& r) const noexcept { return r >= 0; }
317};
318
327 type_safe::constrained_type<Ratio, UnsignedRatioConstraint,
329
330inline QDataStream& operator<<(QDataStream& stream,
331 const UnsignedRatio& ratio) {
332 stream << ratio->toNormalizedString();
333 return stream;
334}
335
336inline QDebug operator<<(QDebug stream, const UnsignedRatio& ratio) {
337 stream << QString("UnsignedRatio(%1%%)").arg(ratio->toPercent());
338 return stream;
339}
340
341inline std::size_t qHash(const UnsignedRatio& key,
342 std::size_t seed = 0) noexcept {
343 return ::qHash(key->toPpm(), seed);
344}
345
346/*******************************************************************************
347 * Class UnsignedLimitedRatio
348 ******************************************************************************/
349
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)
355 : (throw RuntimeError(__FILE__, __LINE__,
356 Ratio::tr("Value must be 0..1!")),
357 std::forward<Value>(val));
358 }
359};
360
362 constexpr bool operator()(const Ratio& r) const noexcept {
363 return (r >= 0) && (r <= Ratio::fromPercent(100));
364 }
365};
366
375 type_safe::constrained_type<Ratio, UnsignedLimitedRatioConstraint,
377
378inline QDataStream& operator<<(QDataStream& stream,
379 const UnsignedLimitedRatio& ratio) {
380 stream << ratio->toNormalizedString();
381 return stream;
382}
383
384inline QDebug operator<<(QDebug stream, const UnsignedLimitedRatio& ratio) {
385 stream << QString("UnsignedLimitedRatio(%1%%)").arg(ratio->toPercent());
386 return stream;
387}
388
389inline std::size_t qHash(const UnsignedLimitedRatio& key,
390 std::size_t seed = 0) noexcept {
391 return ::qHash(key->toPpm(), seed);
392}
393
394/*******************************************************************************
395 * End of File
396 ******************************************************************************/
397
398} // namespace librepcb
399
400#endif
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
Definition ratio.h:315
constexpr bool operator()(const Ratio &r) const noexcept
Definition ratio.h:316
Definition ratio.h:304
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition ratio.h:306