Verovio
Source code documentation
fraction.h
1 // Name: fraction.h
3 // Author: Laurent Pugin
4 // Created: 2024
5 // Copyright (c) Authors and others. All rights reserved.
7 
8 #ifndef __VRV_FRACTION_H__
9 #define __VRV_FRACTION_H__
10 
11 #include "vrv.h"
12 
13 namespace vrv {
14 
15 //----------------------------------------------------------------------------
16 // Fraction
17 //----------------------------------------------------------------------------
18 
19 class Fraction {
20 
21 public:
22  // Constructors - make them explicit to avoid type conversion
23  explicit Fraction(int num = 0) : m_numerator(num), m_denominator(1) {}
24  explicit Fraction(int num, int denom);
25  explicit Fraction(data_DURATION duration);
26 
27  // Enable implicit conversion constructor for `int`
28  template <typename T, typename = std::enable_if_t<std::is_same_v<T, int>>>
29  Fraction(T num) : m_numerator(num), m_denominator(1)
30  {
31  }
32 
34  Fraction operator+(const Fraction &other) const;
36  Fraction operator-(const Fraction &other) const;
38  Fraction operator*(const Fraction &other) const;
40  Fraction operator/(const Fraction &other) const;
42  Fraction operator%(const Fraction &other) const;
43 
45  bool operator==(const Fraction &other) const;
47  std::strong_ordering operator<=>(const Fraction &other) const;
48 
50  int GetNumerator() const { return m_numerator; }
51  int GetDenominator() const { return m_denominator; }
52 
54  double ToDouble() const;
55 
57  std::string ToString() const;
58 
60  std::pair<data_DURATION, Fraction> ToDur() const;
61 
62  //----------------//
63  // Static methods //
64  //----------------//
65 
67  static void Reduce(int &numerator, int &denominator);
68 
69 private:
71  void Reduce();
72 
73 public:
74  //
75 private:
76  int m_numerator;
77  int m_denominator;
78 };
79 
80 } // namespace vrv
81 
82 #endif
vrv::Fraction::ToDur
std::pair< data_DURATION, Fraction > ToDur() const
Convert to data_DURATION and the remaining Fraction.
vrv::Fraction::ToDouble
double ToDouble() const
Convert fraction to a double.
vrv::Fraction::operator-
Fraction operator-(const Fraction &other) const
Subtraction operator.
vrv::Fraction
Definition: fraction.h:19
vrv::Fraction::GetNumerator
int GetNumerator() const
Getters.
Definition: fraction.h:50
vrv::Fraction::operator%
Fraction operator%(const Fraction &other) const
Modulo operator.
vrv::Fraction::operator==
bool operator==(const Fraction &other) const
Equality operator.
vrv::Fraction::ToString
std::string ToString() const
Convert fraction to a string.
vrv::Fraction::operator+
Fraction operator+(const Fraction &other) const
Addition operator.
vrv::Fraction::operator/
Fraction operator/(const Fraction &other) const
Division operator.
vrv::Fraction::operator*
Fraction operator*(const Fraction &other) const
Multiplication operator.