avcpp  2.0
Wrapper for the FFmpeg that simplify usage from C++ projects.
rational.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <utility>
4 #include <iostream>
5 #include <memory>
6 #include <type_traits>
7 
8 #include "ffmpeg.h"
9 #include "avutils.h"
10 
11 #if AVCPP_USE_SPACESHIP_OPERATOR
12 # include <compare>
13 #endif
14 
15 namespace av
16 {
17 
18 #define AV_TIME_BASE_Q_CPP AVRational{1, AV_TIME_BASE}
19 
20 enum
21 {
23 };
24 
25 class Rational
26 {
27 public:
28  Rational() noexcept;
29  Rational(int numerator, int denominator = 1) noexcept;
30  Rational(const AVRational &value) noexcept;
31  Rational(double value, int maxPrecision = RationalMaxPrecision) noexcept;
32 
33  AVRational& getValue() noexcept { return m_value; }
34  const AVRational& getValue() const noexcept { return m_value; }
35  void setValue(const AVRational &newValue) noexcept { m_value = newValue; }
36 
37  void setNumerator(int numerator) noexcept { m_value.num = numerator; }
38  void setDenominator(int denominator) noexcept { m_value.den = denominator; }
39 
40  int getNumerator() const noexcept{ return m_value.num; }
41  int getDenominator() const noexcept { return m_value.den; }
42  double getDouble() const noexcept { return (double)m_value.num / (double)m_value.den; }
43 
44  static Rational fromDouble(double value, int maxPrecision = RationalMaxPrecision) noexcept;
45 
46  int64_t rescale(int64_t srcValue, const Rational &dstBase) const noexcept;
47 
48  void dump() const noexcept;
49 
50  Rational& operator= (const AVRational &value) noexcept;
51  Rational& operator= (double value) noexcept;
52 
53  bool operator== (const Rational &other) const noexcept;
54 #if AVCPP_USE_SPACESHIP_OPERATOR
55  std::strong_ordering operator<=>(const Rational &other) const noexcept
56  {
57  if (auto const cmp = threewaycmp(other); cmp == 0) {
58  return std::strong_ordering::equal;
59  } else if (cmp < 0) {
60  return std::strong_ordering::less;
61  } else {
62  return std::strong_ordering::greater;
63  }
64  }
65 #else
66  bool operator!= (const Rational &other) const noexcept {
67  return threewaycmp(other) != 0;
68  }
69  bool operator<(const Rational &other) const noexcept {
70  return threewaycmp(other) < 0;
71  }
72  bool operator>(const Rational &other) const noexcept {
73  return threewaycmp(other) > 0;
74  }
75  bool operator<=(const Rational &other) const noexcept {
76  return (*this < other) || (*this == other);
77  }
78  bool operator>=(const Rational &other) const noexcept {
79  return (*this > other) || (*this == other);
80  }
81 #endif
82 
83  Rational operator+ (const Rational &value) const noexcept;
84  Rational operator- (const Rational &value) const noexcept;
85  Rational operator* (const Rational &value) const noexcept;
86  Rational operator/ (const Rational &value) const noexcept;
87 
88  double operator() () const noexcept;
89 
90  operator const AVRational&() const noexcept
91  {
92  return m_value;
93  }
94 
95 private:
96  int threewaycmp(const Rational &other) const noexcept;
97 
98 private:
99  AVRational m_value;
100 };
101 
102 
103 template<typename T>
104 auto operator/ (T num, Rational value) ->
105  std::enable_if_t<std::is_floating_point_v<T> || std::is_integral_v<T>, Rational>
106 {
107  return Rational{num, 1} / value;
108 }
109 
110 template<typename T>
111 auto operator/ (Rational value, T num) ->
112  std::enable_if_t<std::is_floating_point_v<T> || std::is_integral_v<T>, Rational>
113 {
114  return value / Rational{num, 1};
115 }
116 
117 inline std::ostream& operator<< (std::ostream &stream, const Rational &value)
118 {
119  stream << value.getNumerator() << '/' << value.getDenominator();
120  return stream;
121 
122 }
123 
124 inline std::istream& operator>> (std::istream &stream, Rational &value)
125 {
126  char ch;
127  AVRational temp;
128 
129  try
130  {
131  stream >> temp.num >> ch >> temp.den;
132  if (ch == '/')
133  {
134  value.setNumerator(temp.num);
135  value.setDenominator(temp.den);
136  }
137  }
138  catch (const std::exception &e)
139  {}
140 
141  return stream;
142 }
143 
144 
145 } // ::av
146 
147 
148 #ifdef __cpp_lib_format
149 #include <format>
150 // std::format
151 template <typename CharT>
152 struct std::formatter<av::Rational, CharT>
153 {
154  template<typename ParseContext>
155  constexpr auto parse(ParseContext& ctx)
156  {
157  return ctx.begin();
158  }
159  template<typename ParseContext>
160  auto format(const av::Rational& value, ParseContext& ctx) const
161  {
162  return std::format_to(ctx.out(), "{}/{}", value.getNumerator(), value.getDenominator());
163  }
164 };
165 #endif
Definition: rational.h:26
Rational operator-(const Rational &value) const noexcept
Definition: rational.cpp:64
bool operator>(const Rational &other) const noexcept
Definition: rational.h:72
bool operator<(const Rational &other) const noexcept
Definition: rational.h:69
double getDouble() const noexcept
Definition: rational.h:42
Rational operator*(const Rational &value) const noexcept
Definition: rational.cpp:70
void setNumerator(int numerator) noexcept
Definition: rational.h:37
Rational() noexcept
Definition: rational.cpp:10
static Rational fromDouble(double value, int maxPrecision=RationalMaxPrecision) noexcept
Definition: rational.cpp:30
void dump() const noexcept
Definition: rational.cpp:41
int getDenominator() const noexcept
Definition: rational.h:41
int64_t rescale(int64_t srcValue, const Rational &dstBase) const noexcept
Definition: rational.cpp:36
void setValue(const AVRational &newValue) noexcept
Definition: rational.h:35
const AVRational & getValue() const noexcept
Definition: rational.h:34
AVRational & getValue() noexcept
Definition: rational.h:33
bool operator<=(const Rational &other) const noexcept
Definition: rational.h:75
bool operator>=(const Rational &other) const noexcept
Definition: rational.h:78
int getNumerator() const noexcept
Definition: rational.h:40
bool operator!=(const Rational &other) const noexcept
Definition: rational.h:66
Rational operator+(const Rational &value) const noexcept
Definition: rational.cpp:58
double operator()() const noexcept
Definition: rational.cpp:82
void setDenominator(int denominator) noexcept
Definition: rational.h:38
Rational operator/(const Rational &value) const noexcept
Definition: rational.cpp:76
Definition: audioresampler.cpp:8
std::ostream & operator<<(std::ostream &stream, const Rational &value)
Definition: rational.h:117
std::istream & operator>>(std::istream &stream, Rational &value)
Definition: rational.h:124
@ RationalMaxPrecision
Definition: rational.h:22
auto operator/(T num, Rational value) -> std::enable_if_t< std::is_floating_point_v< T >||std::is_integral_v< T >, Rational >
Definition: rational.h:104