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 #if __cplusplus > 201703L
9 #include <compare>
10 #endif
11 
12 #include "ffmpeg.h"
13 
14 namespace av
15 {
16 
17 #define AV_TIME_BASE_Q_CPP AVRational{1, AV_TIME_BASE}
18 
19 enum
20 {
22 };
23 
24 class Rational
25 {
26 public:
27  Rational() noexcept;
28  Rational(int numerator, int denominator = 1) noexcept;
29  Rational(const AVRational &value) noexcept;
30  Rational(double value, int maxPrecision = RationalMaxPrecision) noexcept;
31 
32  AVRational& getValue() noexcept { return m_value; }
33  const AVRational& getValue() const noexcept { return m_value; }
34  void setValue(const AVRational &newValue) noexcept { m_value = newValue; }
35 
36  void setNumerator(int numerator) noexcept { m_value.num = numerator; }
37  void setDenominator(int denominator) noexcept { m_value.den = denominator; }
38 
39  int getNumerator() const noexcept{ return m_value.num; }
40  int getDenominator() const noexcept { return m_value.den; }
41  double getDouble() const noexcept { return (double)m_value.num / (double)m_value.den; }
42 
43  static Rational fromDouble(double value, int maxPrecision = RationalMaxPrecision) noexcept;
44 
45  int64_t rescale(int64_t srcValue, const Rational &dstBase) const noexcept;
46 
47  void dump() const noexcept;
48 
49  Rational& operator= (const AVRational &value) noexcept;
50  Rational& operator= (double value) noexcept;
51 
52  bool operator== (const Rational &other) const noexcept;
53 #if __cplusplus > 201703L
54  std::strong_ordering operator<=>(const Rational &other) const noexcept
55  {
56  switch (threewaycmp(other)) {
57  case -1:
58  return std::strong_ordering::less;
59  case 0:
60  return std::strong_ordering::equal;
61  case 1:
62  return std::strong_ordering::greater;
63  }
64  return std::strong_ordering::equal; // make a compiler happy
65  }
66 #else
67  bool operator!= (const Rational &other) const noexcept {
68  return threewaycmp(other) != 0;
69  }
70  bool operator<(const Rational &other) const noexcept {
71  return threewaycmp(other) < 0;
72  }
73  bool operator>(const Rational &other) const noexcept {
74  return threewaycmp(other) > 0;
75  }
76  bool operator<=(const Rational &other) const noexcept {
77  return (*this < other) || (*this == other);
78  }
79  bool operator>=(const Rational &other) const noexcept {
80  return (*this > other) || (*this == other);
81  }
82 #endif
83 
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  Rational operator/ (const Rational &value) const noexcept;
88 
89  double operator() () const noexcept;
90 
91  operator const AVRational&() const noexcept
92  {
93  return m_value;
94  }
95 
96 private:
97  int threewaycmp(const Rational &other) const noexcept;
98 
99 private:
100  AVRational m_value;
101 };
102 
103 
104 template<typename T>
105 auto operator/ (T num, Rational value) ->
106  std::enable_if_t<std::is_floating_point_v<T> || std::is_integral_v<T>, Rational>
107 {
108  return Rational{num, 1} / value;
109 }
110 
111 template<typename T>
112 auto operator/ (Rational value, T num) ->
113  std::enable_if_t<std::is_floating_point_v<T> || std::is_integral_v<T>, Rational>
114 {
115  return value / Rational{num, 1};
116 }
117 
118 inline std::ostream& operator<< (std::ostream &stream, const Rational &value)
119 {
120  stream << value.getNumerator() << '/' << value.getDenominator();
121  return stream;
122 
123 }
124 
125 inline std::istream& operator>> (std::istream &stream, Rational &value)
126 {
127  char ch;
128  AVRational temp;
129 
130  try
131  {
132  stream >> temp.num >> ch >> temp.den;
133  if (ch == '/')
134  {
135  value.setNumerator(temp.num);
136  value.setDenominator(temp.den);
137  }
138  }
139  catch (const std::exception &e)
140  {}
141 
142  return stream;
143 }
144 
145 
146 } // ::av
av::Rational
Definition: rational.h:24
ffmpeg.h
av::Rational::getValue
AVRational & getValue() noexcept
Definition: rational.h:32
av::Rational::operator<=
bool operator<=(const Rational &other) const noexcept
Definition: rational.h:76
av::RationalMaxPrecision
@ RationalMaxPrecision
Definition: rational.h:21
av::Rational::getNumerator
int getNumerator() const noexcept
Definition: rational.h:39
av::Rational::rescale
int64_t rescale(int64_t srcValue, const Rational &dstBase) const noexcept
Definition: rational.cpp:36
av::Rational::operator/
Rational operator/(const Rational &value) const noexcept
Definition: rational.cpp:76
av::Rational::operator>
bool operator>(const Rational &other) const noexcept
Definition: rational.h:73
av::Rational::Rational
Rational() noexcept
Definition: rational.cpp:10
av::Rational::getDenominator
int getDenominator() const noexcept
Definition: rational.h:40
av::Rational::operator-
Rational operator-(const Rational &value) const noexcept
Definition: rational.cpp:64
av::Rational::operator+
Rational operator+(const Rational &value) const noexcept
Definition: rational.cpp:58
av::operator<<
std::ostream & operator<<(std::ostream &stream, const Rational &value)
Definition: rational.h:118
av::Rational::operator>=
bool operator>=(const Rational &other) const noexcept
Definition: rational.h:79
av::Rational::fromDouble
static Rational fromDouble(double value, int maxPrecision=RationalMaxPrecision) noexcept
Definition: rational.cpp:30
av::operator/
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:105
av::Rational::dump
void dump() const noexcept
Definition: rational.cpp:41
av::Rational::setNumerator
void setNumerator(int numerator) noexcept
Definition: rational.h:36
av::Rational::operator()
double operator()() const noexcept
Definition: rational.cpp:82
av::Rational::getValue
const AVRational & getValue() const noexcept
Definition: rational.h:33
av
Definition: audioresampler.cpp:8
av::Rational::operator<
bool operator<(const Rational &other) const noexcept
Definition: rational.h:70
av::Rational::getDouble
double getDouble() const noexcept
Definition: rational.h:41
av::Rational::setValue
void setValue(const AVRational &newValue) noexcept
Definition: rational.h:34
av::Rational::operator!=
bool operator!=(const Rational &other) const noexcept
Definition: rational.h:67
av::Rational::setDenominator
void setDenominator(int denominator) noexcept
Definition: rational.h:37
av::operator>>
std::istream & operator>>(std::istream &stream, Rational &value)
Definition: rational.h:125
av::Rational::operator*
Rational operator*(const Rational &value) const noexcept
Definition: rational.cpp:70