LibrePCB Developers Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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_RATIO_H
21 #define LIBREPCB_RATIO_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "../fileio/sexpression.h"
27 
28 #include <type_safe/constrained_type.hpp>
29 
30 #include <QtCore>
31 
32 /*******************************************************************************
33  * Namespace / Forward Declarations
34  ******************************************************************************/
35 namespace librepcb {
36 
37 /*******************************************************************************
38  * Class Ratio
39  ******************************************************************************/
40 
44 class Ratio {
45  Q_DECLARE_TR_FUNCTIONS(Ratio)
46 
47 public:
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 
93  void setRatioPercent(qreal percent) noexcept { mPpm = percent * 1e4; }
94 
105  void setRatioNormalized(qreal normalized) noexcept {
106  mPpm = normalized * 1e6;
107  }
108 
119  void setRatioNormalized(const QString& normalized) {
120  mPpm = normalizedStringToPpm(normalized);
121  }
122 
123  // Conversions
124 
130  qint32 toPpm() const noexcept { return mPpm; }
131 
137  qreal toPercent() const noexcept { return (qreal)mPpm / 1e4; }
138 
144  qreal toNormalized() const noexcept { return (qreal)mPpm / 1e6; }
145 
153  QString toNormalizedString() const noexcept;
154 
155  // Static Methods
156 
164  static Ratio fromPercent(qreal percent) noexcept;
165 
173  static Ratio fromNormalized(qreal normalized) noexcept;
174 
192  static Ratio fromNormalized(const QString& normalized);
193 
194  // Static Methods to create often used ratios
195  static Ratio percent0() noexcept { return Ratio(0); }
196  static Ratio percent50() noexcept { return Ratio(500000); }
197  static Ratio percent100() noexcept { return Ratio(1000000); }
198 
199  // Operators
200  Ratio& operator=(const Ratio& rhs) {
201  mPpm = rhs.mPpm;
202  return *this;
203  }
204  Ratio& operator+=(const Ratio& rhs) {
205  mPpm += rhs.mPpm;
206  return *this;
207  }
208  Ratio& operator-=(const Ratio& rhs) {
209  mPpm -= rhs.mPpm;
210  return *this;
211  }
212  Ratio& operator*=(const Ratio& rhs) {
213  mPpm *= rhs.mPpm;
214  return *this;
215  }
216  Ratio& operator*=(qint32 rhs) {
217  mPpm *= rhs;
218  return *this;
219  }
220  Ratio& operator/=(const Ratio& rhs) {
221  mPpm /= rhs.mPpm;
222  return *this;
223  }
224  Ratio& operator/=(qint32 rhs) {
225  mPpm /= rhs;
226  return *this;
227  }
228  Ratio operator+(const Ratio& rhs) const { return Ratio(mPpm + rhs.mPpm); }
229  Ratio operator-() const { return Ratio(-mPpm); }
230  Ratio operator-(const Ratio& rhs) const { return Ratio(mPpm - rhs.mPpm); }
231  Ratio operator*(const Ratio& rhs) const { return Ratio(mPpm * rhs.mPpm); }
232  Ratio operator*(qint32 rhs) const { return Ratio(mPpm * rhs); }
233  Ratio operator/(const Ratio& rhs) const { return Ratio(mPpm / rhs.mPpm); }
234  Ratio operator/(qint32 rhs) const { return Ratio(mPpm / rhs); }
235  Ratio operator%(const Ratio& rhs) const { return Ratio(mPpm % rhs.mPpm); }
236  constexpr bool operator>(const Ratio& rhs) const { return mPpm > rhs.mPpm; }
237  constexpr bool operator>(qint32 rhs) const { return mPpm > rhs; }
238  constexpr bool operator<(const Ratio& rhs) const { return mPpm < rhs.mPpm; }
239  constexpr bool operator<(qint32 rhs) const { return mPpm < rhs; }
240  constexpr bool operator>=(const Ratio& rhs) const { return mPpm >= rhs.mPpm; }
241  constexpr bool operator>=(qint32 rhs) const { return mPpm >= rhs; }
242  constexpr bool operator<=(const Ratio& rhs) const { return mPpm <= rhs.mPpm; }
243  constexpr bool operator<=(qint32 rhs) const { return mPpm <= rhs; }
244  constexpr bool operator==(const Ratio& rhs) const { return mPpm == rhs.mPpm; }
245  constexpr bool operator==(qint32 rhs) const { return mPpm == rhs; }
246  constexpr bool operator!=(const Ratio& rhs) const { return mPpm != rhs.mPpm; }
247  constexpr bool operator!=(qint32 rhs) const { return mPpm != rhs; }
248  explicit operator bool() const { return mPpm != 0; }
249 
250 private:
251  // Private Static Functions
252 
266  static qint32 normalizedStringToPpm(const QString& normalized);
267 
268  // Private Member Variables
269  qint32 mPpm;
270 };
271 
272 /*******************************************************************************
273  * Non-Member Functions
274  ******************************************************************************/
275 
276 template <>
279 }
280 
281 template <>
283  bool throwIfEmpty) {
284  QString str = sexpr.getStringOrToken(throwIfEmpty);
285  return Ratio::fromNormalized(str);
286 }
287 
288 inline QDataStream& operator<<(QDataStream& stream, const Ratio& ratio) {
289  stream << ratio.toNormalizedString();
290  return stream;
291 }
292 
293 inline QDebug operator<<(QDebug stream, const Ratio& ratio) {
294  stream << QString("Ratio(%1%%°)").arg(ratio.toPercent());
295  return stream;
296 }
297 
298 inline uint qHash(const Ratio& key, uint seed = 0) noexcept {
299  return ::qHash(key.toPpm(), seed);
300 }
301 
302 /*******************************************************************************
303  * Class UnsignedRatio
304  ******************************************************************************/
305 
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)
311  : (throw RuntimeError(__FILE__, __LINE__,
312  Ratio::tr("Value must be >= 0!")),
313  std::forward<Value>(val));
314  }
315 };
316 
318  constexpr bool operator()(const Ratio& r) const noexcept { return r >= 0; }
319 };
320 
328 using UnsignedRatio =
329  type_safe::constrained_type<Ratio, UnsignedRatioConstraint,
331 
332 template <>
334  return SExpression::createToken(obj->toNormalizedString());
335 }
336 
337 template <>
339  bool throwIfEmpty) {
340  QString str = sexpr.getStringOrToken(throwIfEmpty);
342 }
343 
344 inline QDataStream& operator<<(QDataStream& stream,
345  const UnsignedRatio& ratio) {
346  stream << ratio->toNormalizedString();
347  return stream;
348 }
349 
350 inline QDebug operator<<(QDebug stream, const UnsignedRatio& ratio) {
351  stream << QString("UnsignedRatio(%1%%)").arg(ratio->toPercent());
352  return stream;
353 }
354 
355 inline uint qHash(const UnsignedRatio& key, uint seed = 0) noexcept {
356  return ::qHash(key->toPpm(), seed);
357 }
358 
359 /*******************************************************************************
360  * End of File
361  ******************************************************************************/
362 
363 } // namespace librepcb
364 
365 #endif // LIBREPCB_RATIO_H
~Ratio()=default
Destructor.
constexpr bool operator==(const Ratio &rhs) const
Definition: ratio.h:244
void setRatioNormalized(qreal normalized) noexcept
Set the ratio as a normalized number.
Definition: ratio.h:105
static Ratio percent0() noexcept
Definition: ratio.h:195
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition: attributekey.h:109
Ratio operator*(const Ratio &rhs) const
Definition: ratio.h:231
constexpr bool operator>=(const Ratio &rhs) const
Definition: ratio.h:240
Ratio operator%(const Ratio &rhs) const
Definition: ratio.h:235
Ratio & operator*=(const Ratio &rhs)
Definition: ratio.h:212
Ratio operator/(const Ratio &rhs) const
Definition: ratio.h:233
constexpr bool operator==(qint32 rhs) const
Definition: ratio.h:245
constexpr bool operator>=(qint32 rhs) const
Definition: ratio.h:241
static Ratio fromNormalized(qreal normalized) noexcept
Get a Ratio object with a specific ratio.
Definition: ratio.cpp:50
void setRatioPercent(qreal percent) noexcept
Set the ratio in percent.
Definition: ratio.h:93
Ratio operator-() const
Definition: ratio.h:229
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition: ratio.h:308
qreal toPercent() const noexcept
Get the ratio in percent.
Definition: ratio.h:137
Ratio operator/(qint32 rhs) const
Definition: ratio.h:234
uint qHash(const UnsignedRatio &key, uint seed=0) noexcept
Definition: ratio.h:355
const QString & getStringOrToken(bool throwIfEmpty=false) const
Definition: sexpression.cpp:101
SExpression serializeToSExpression(const HAlign &obj)
Definition: alignment.h:76
static Ratio fromPercent(qreal percent) noexcept
Get a Ratio object with a specific ratio.
Definition: ratio.cpp:44
Ratio & operator=(const Ratio &rhs)
Definition: ratio.h:200
HAlign deserializeFromSExpression(const SExpression &sexpr, bool throwIfEmpty)
Definition: alignment.h:90
static SExpression createToken(const QString &token)
Definition: sexpression.cpp:280
constexpr bool operator<=(qint32 rhs) const
Definition: ratio.h:243
type_safe::constrained_type< Ratio, UnsignedRatioConstraint, UnsignedRatioVerifier > UnsignedRatio
Definition: ratio.h:330
qint32 toPpm() const noexcept
Get the ratio in PPM.
Definition: ratio.h:130
Ratio & operator/=(qint32 rhs)
Definition: ratio.h:224
Ratio & operator-=(const Ratio &rhs)
Definition: ratio.h:208
Ratio operator-(const Ratio &rhs) const
Definition: ratio.h:230
static qint32 normalizedStringToPpm(const QString &normalized)
Convert a normalized ratio from a QString to an integer (in PPM)
Definition: ratio.cpp:64
static Ratio percent50() noexcept
Definition: ratio.h:196
static Ratio percent100() noexcept
Definition: ratio.h:197
Definition: ratio.h:317
constexpr bool operator<(qint32 rhs) const
Definition: ratio.h:239
constexpr Ratio(const Ratio &ratio) noexcept
Copy Constructor.
Definition: ratio.h:60
Ratio operator*(qint32 rhs) const
Definition: ratio.h:232
constexpr bool operator<=(const Ratio &rhs) const
Definition: ratio.h:242
The RuntimeError class.
Definition: exceptions.h:219
constexpr bool operator>(const Ratio &rhs) const
Definition: ratio.h:236
constexpr Ratio(qint32 ppm) noexcept
Constructor with a ratio in PPM.
Definition: ratio.h:67
constexpr bool operator!=(const Ratio &rhs) const
Definition: ratio.h:246
QString toNormalizedString() const noexcept
Get the ratio as a normalized QString.
Definition: ratio.cpp:38
constexpr Ratio() noexcept
Default Constructor.
Definition: ratio.h:53
constexpr bool operator()(const Ratio &r) const noexcept
Definition: ratio.h:318
qreal toNormalized() const noexcept
Get the ratio as a normalized number.
Definition: ratio.h:144
The Ratio class is used to represent a ratio number (e.g. 13.37%)
Definition: ratio.h:44
constexpr bool operator!=(qint32 rhs) const
Definition: ratio.h:247
Definition: ratio.h:306
qint32 mPpm
the ratio in PPM
Definition: ratio.h:269
Ratio & operator*=(qint32 rhs)
Definition: ratio.h:216
constexpr bool operator>(qint32 rhs) const
Definition: ratio.h:237
Ratio & operator+=(const Ratio &rhs)
Definition: ratio.h:204
constexpr bool operator<(const Ratio &rhs) const
Definition: ratio.h:238
void setRatioPpm(qint32 ppm) noexcept
Set the ratio in PPM.
Definition: ratio.h:81
void setRatioNormalized(const QString &normalized)
Set the ratio as a normalized number, represented in a QString.
Definition: ratio.h:119
Ratio operator+(const Ratio &rhs) const
Definition: ratio.h:228
The SExpression class.
Definition: sexpression.h:57
uint qHash(const AttributeKey &key, uint seed=0) noexcept
Definition: attributekey.h:119
Ratio & operator/=(const Ratio &rhs)
Definition: ratio.h:220