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 
47 class Ratio {
48  Q_DECLARE_TR_FUNCTIONS(Ratio)
49 
50 public:
51  // Constructors / Destructor
52 
56  constexpr Ratio() noexcept : Ratio(0) {}
57 
63  constexpr Ratio(const Ratio& ratio) noexcept : mPpm(ratio.mPpm) {}
64 
70  constexpr explicit Ratio(qint32 ppm) noexcept : mPpm(ppm) {}
71 
75  ~Ratio() = default;
76 
77  // Setters
78 
84  void setRatioPpm(qint32 ppm) noexcept { mPpm = ppm; }
85 
96  void setRatioPercent(qreal percent) noexcept { mPpm = percent * 1e4; }
97 
108  void setRatioNormalized(qreal normalized) noexcept {
109  mPpm = normalized * 1e6;
110  }
111 
122  void setRatioNormalized(const QString& normalized) {
123  mPpm = normalizedStringToPpm(normalized);
124  }
125 
126  // Conversions
127 
133  qint32 toPpm() const noexcept { return mPpm; }
134 
140  qreal toPercent() const noexcept { return (qreal)mPpm / 1e4; }
141 
147  qreal toNormalized() const noexcept { return (qreal)mPpm / 1e6; }
148 
156  QString toNormalizedString() const noexcept;
157 
158  // Static Methods
159 
167  static Ratio fromPercent(qreal percent) noexcept;
168 
176  static Ratio fromNormalized(qreal normalized) noexcept;
177 
195  static Ratio fromNormalized(const QString& normalized);
196 
197  // Static Methods to create often used ratios
198  static Ratio percent0() noexcept { return Ratio(0); }
199  static Ratio percent50() noexcept { return Ratio(500000); }
200  static Ratio percent100() noexcept { return Ratio(1000000); }
201 
202  // Operators
203  Ratio& operator=(const Ratio& rhs) {
204  mPpm = rhs.mPpm;
205  return *this;
206  }
207  Ratio& operator+=(const Ratio& rhs) {
208  mPpm += rhs.mPpm;
209  return *this;
210  }
211  Ratio& operator-=(const Ratio& rhs) {
212  mPpm -= rhs.mPpm;
213  return *this;
214  }
215  Ratio& operator*=(const Ratio& rhs) {
216  mPpm *= rhs.mPpm;
217  return *this;
218  }
219  Ratio& operator*=(qint32 rhs) {
220  mPpm *= rhs;
221  return *this;
222  }
223  Ratio& operator/=(const Ratio& rhs) {
224  mPpm /= rhs.mPpm;
225  return *this;
226  }
227  Ratio& operator/=(qint32 rhs) {
228  mPpm /= rhs;
229  return *this;
230  }
231  Ratio operator+(const Ratio& rhs) const { return Ratio(mPpm + rhs.mPpm); }
232  Ratio operator-() const { return Ratio(-mPpm); }
233  Ratio operator-(const Ratio& rhs) const { return Ratio(mPpm - rhs.mPpm); }
234  Ratio operator*(const Ratio& rhs) const { return Ratio(mPpm * rhs.mPpm); }
235  Ratio operator*(qint32 rhs) const { return Ratio(mPpm * rhs); }
236  Ratio operator/(const Ratio& rhs) const { return Ratio(mPpm / rhs.mPpm); }
237  Ratio operator/(qint32 rhs) const { return Ratio(mPpm / rhs); }
238  Ratio operator%(const Ratio& rhs) const { return Ratio(mPpm % rhs.mPpm); }
239  constexpr bool operator>(const Ratio& rhs) const { return mPpm > rhs.mPpm; }
240  constexpr bool operator>(qint32 rhs) const { return mPpm > rhs; }
241  constexpr bool operator<(const Ratio& rhs) const { return mPpm < rhs.mPpm; }
242  constexpr bool operator<(qint32 rhs) const { return mPpm < rhs; }
243  constexpr bool operator>=(const Ratio& rhs) const { return mPpm >= rhs.mPpm; }
244  constexpr bool operator>=(qint32 rhs) const { return mPpm >= rhs; }
245  constexpr bool operator<=(const Ratio& rhs) const { return mPpm <= rhs.mPpm; }
246  constexpr bool operator<=(qint32 rhs) const { return mPpm <= rhs; }
247  constexpr bool operator==(const Ratio& rhs) const { return mPpm == rhs.mPpm; }
248  constexpr bool operator==(qint32 rhs) const { return mPpm == rhs; }
249  constexpr bool operator!=(const Ratio& rhs) const { return mPpm != rhs.mPpm; }
250  constexpr bool operator!=(qint32 rhs) const { return mPpm != rhs; }
251  explicit operator bool() const { return mPpm != 0; }
252 
253 private:
254  // Private Static Functions
255 
269  static qint32 normalizedStringToPpm(const QString& normalized);
270 
271  // Private Member Variables
272  qint32 mPpm;
273 };
274 
275 /*******************************************************************************
276  * Non-Member Functions
277  ******************************************************************************/
278 
279 template <>
282 }
283 
284 template <>
286  bool throwIfEmpty) {
287  QString str = sexpr.getStringOrToken(throwIfEmpty);
288  return Ratio::fromNormalized(str);
289 }
290 
291 inline QDataStream& operator<<(QDataStream& stream, const Ratio& ratio) {
292  stream << ratio.toNormalizedString();
293  return stream;
294 }
295 
296 inline QDebug operator<<(QDebug stream, const Ratio& ratio) {
297  stream << QString("Ratio(%1%%°)").arg(ratio.toPercent());
298  return stream;
299 }
300 
301 inline uint qHash(const Ratio& key, uint seed = 0) noexcept {
302  return ::qHash(key.toPpm(), seed);
303 }
304 
305 /*******************************************************************************
306  * Class UnsignedRatio
307  ******************************************************************************/
308 
310  template <typename Value, typename Predicate>
311  static constexpr auto verify(Value&& val, const Predicate& p) ->
312  typename std::decay<Value>::type {
313  return p(val) ? std::forward<Value>(val)
314  : (throw RuntimeError(__FILE__, __LINE__,
315  Ratio::tr("Value must be >= 0!")),
316  std::forward<Value>(val));
317  }
318 };
319 
321  constexpr bool operator()(const Ratio& r) const noexcept { return r >= 0; }
322 };
323 
331 using UnsignedRatio =
332  type_safe::constrained_type<Ratio, UnsignedRatioConstraint,
334 
335 template <>
337  return SExpression::createToken(obj->toNormalizedString());
338 }
339 
340 template <>
342  bool throwIfEmpty) {
343  QString str = sexpr.getStringOrToken(throwIfEmpty);
345 }
346 
347 inline QDataStream& operator<<(QDataStream& stream,
348  const UnsignedRatio& ratio) {
349  stream << ratio->toNormalizedString();
350  return stream;
351 }
352 
353 inline QDebug operator<<(QDebug stream, const UnsignedRatio& ratio) {
354  stream << QString("UnsignedRatio(%1%%)").arg(ratio->toPercent());
355  return stream;
356 }
357 
358 inline uint qHash(const UnsignedRatio& key, uint seed = 0) noexcept {
359  return ::qHash(key->toPpm(), seed);
360 }
361 
362 /*******************************************************************************
363  * End of File
364  ******************************************************************************/
365 
366 } // namespace librepcb
367 
368 #endif // LIBREPCB_RATIO_H
~Ratio()=default
Destructor.
constexpr bool operator==(const Ratio &rhs) const
Definition: ratio.h:247
void setRatioNormalized(qreal normalized) noexcept
Set the ratio as a normalized number.
Definition: ratio.h:108
static Ratio percent0() noexcept
Definition: ratio.h:198
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition: attributekey.h:109
Ratio operator*(const Ratio &rhs) const
Definition: ratio.h:234
constexpr bool operator>=(const Ratio &rhs) const
Definition: ratio.h:243
Ratio operator%(const Ratio &rhs) const
Definition: ratio.h:238
Ratio & operator*=(const Ratio &rhs)
Definition: ratio.h:215
Ratio operator/(const Ratio &rhs) const
Definition: ratio.h:236
constexpr bool operator==(qint32 rhs) const
Definition: ratio.h:248
constexpr bool operator>=(qint32 rhs) const
Definition: ratio.h:244
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:96
Ratio operator-() const
Definition: ratio.h:232
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition: ratio.h:311
qreal toPercent() const noexcept
Get the ratio in percent.
Definition: ratio.h:140
Ratio operator/(qint32 rhs) const
Definition: ratio.h:237
uint qHash(const UnsignedRatio &key, uint seed=0) noexcept
Definition: ratio.h:358
const QString & getStringOrToken(bool throwIfEmpty=false) const
Definition: sexpression.cpp:101
SExpression serializeToSExpression(const HAlign &obj)
Definition: alignment.h:79
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:203
HAlign deserializeFromSExpression(const SExpression &sexpr, bool throwIfEmpty)
Definition: alignment.h:93
static SExpression createToken(const QString &token)
Definition: sexpression.cpp:280
constexpr bool operator<=(qint32 rhs) const
Definition: ratio.h:246
type_safe::constrained_type< Ratio, UnsignedRatioConstraint, UnsignedRatioVerifier > UnsignedRatio
Definition: ratio.h:333
qint32 toPpm() const noexcept
Get the ratio in PPM.
Definition: ratio.h:133
Ratio & operator/=(qint32 rhs)
Definition: ratio.h:227
Ratio & operator-=(const Ratio &rhs)
Definition: ratio.h:211
Ratio operator-(const Ratio &rhs) const
Definition: ratio.h:233
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:199
static Ratio percent100() noexcept
Definition: ratio.h:200
Definition: ratio.h:320
constexpr bool operator<(qint32 rhs) const
Definition: ratio.h:242
constexpr Ratio(const Ratio &ratio) noexcept
Copy Constructor.
Definition: ratio.h:63
Ratio operator*(qint32 rhs) const
Definition: ratio.h:235
constexpr bool operator<=(const Ratio &rhs) const
Definition: ratio.h:245
The RuntimeError class.
Definition: exceptions.h:219
constexpr bool operator>(const Ratio &rhs) const
Definition: ratio.h:239
constexpr Ratio(qint32 ppm) noexcept
Constructor with a ratio in PPM.
Definition: ratio.h:70
constexpr bool operator!=(const Ratio &rhs) const
Definition: ratio.h:249
QString toNormalizedString() const noexcept
Get the ratio as a normalized QString.
Definition: ratio.cpp:38
constexpr Ratio() noexcept
Default Constructor.
Definition: ratio.h:56
constexpr bool operator()(const Ratio &r) const noexcept
Definition: ratio.h:321
qreal toNormalized() const noexcept
Get the ratio as a normalized number.
Definition: ratio.h:147
The Ratio class is used to represent a ratio number (e.g. 13.37%)
Definition: ratio.h:47
constexpr bool operator!=(qint32 rhs) const
Definition: ratio.h:250
Definition: ratio.h:309
qint32 mPpm
the ratio in PPM
Definition: ratio.h:272
Ratio & operator*=(qint32 rhs)
Definition: ratio.h:219
constexpr bool operator>(qint32 rhs) const
Definition: ratio.h:240
Ratio & operator+=(const Ratio &rhs)
Definition: ratio.h:207
constexpr bool operator<(const Ratio &rhs) const
Definition: ratio.h:241
void setRatioPpm(qint32 ppm) noexcept
Set the ratio in PPM.
Definition: ratio.h:84
void setRatioNormalized(const QString &normalized)
Set the ratio as a normalized number, represented in a QString.
Definition: ratio.h:122
Ratio operator+(const Ratio &rhs) const
Definition: ratio.h:231
The SExpression class.
Definition: sexpression.h:60
uint qHash(const AttributeKey &key, uint seed=0) noexcept
Definition: attributekey.h:119
Ratio & operator/=(const Ratio &rhs)
Definition: ratio.h:223