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 #ifdef 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 #ifdef AVCPP_USE_SPACESHIP_OPERATOR
55  std::strong_ordering operator<=>(const Rational &other) const noexcept
56  {
57  switch (threewaycmp(other)) {
58  case -1:
59  return std::strong_ordering::less;
60  case 0:
61  return std::strong_ordering::equal;
62  case 1:
63  return std::strong_ordering::greater;
64  }
65  return std::strong_ordering::equal; // make a compiler happy
66  }
67 #else
68  bool operator!= (const Rational &other) const noexcept {
69  return threewaycmp(other) != 0;
70  }
71  bool operator<(const Rational &other) const noexcept {
72  return threewaycmp(other) < 0;
73  }
74  bool operator>(const Rational &other) const noexcept {
75  return threewaycmp(other) > 0;
76  }
77  bool operator<=(const Rational &other) const noexcept {
78  return (*this < other) || (*this == other);
79  }
80  bool operator>=(const Rational &other) const noexcept {
81  return (*this > other) || (*this == other);
82  }
83 #endif
84 
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  Rational operator/ (const Rational &value) const noexcept;
89 
90  double operator() () const noexcept;
91 
92  operator const AVRational&() const noexcept
93  {
94  return m_value;
95  }
96 
97 private:
98  int threewaycmp(const Rational &other) const noexcept;
99 
100 private:
101  AVRational m_value;
102 };
103 
104 
105 template<typename T>
106 auto operator/ (T num, Rational value) ->
107  std::enable_if_t<std::is_floating_point_v<T> || std::is_integral_v<T>, Rational>
108 {
109  return Rational{num, 1} / value;
110 }
111 
112 template<typename T>
113 auto operator/ (Rational value, T num) ->
114  std::enable_if_t<std::is_floating_point_v<T> || std::is_integral_v<T>, Rational>
115 {
116  return value / Rational{num, 1};
117 }
118 
119 inline std::ostream& operator<< (std::ostream &stream, const Rational &value)
120 {
121  stream << value.getNumerator() << '/' << value.getDenominator();
122  return stream;
123 
124 }
125 
126 inline std::istream& operator>> (std::istream &stream, Rational &value)
127 {
128  char ch;
129  AVRational temp;
130 
131  try
132  {
133  stream >> temp.num >> ch >> temp.den;
134  if (ch == '/')
135  {
136  value.setNumerator(temp.num);
137  value.setDenominator(temp.den);
138  }
139  }
140  catch (const std::exception &e)
141  {}
142 
143  return stream;
144 }
145 
146 
147 } // ::av
148 
149 
150 #ifdef __cpp_lib_format
151 #include <format>
152 // std::format
153 template <typename CharT>
154 struct std::formatter<av::Rational, CharT>
155 {
156  template<typename ParseContext>
157  constexpr auto parse(ParseContext& ctx)
158  {
159  return ctx.begin();
160  }
161  template<typename ParseContext>
162  auto format(const av::Rational& value, ParseContext& ctx) const
163  {
164  return std::format_to(ctx.out(), "{}/{}", value.getNumerator(), value.getDenominator());
165  }
166 };
167 #endif
av::Rational
Definition: rational.h:25
ffmpeg.h
av::Rational::getValue
AVRational & getValue() noexcept
Definition: rational.h:33
av::Rational::operator<=
bool operator<=(const Rational &other) const noexcept
Definition: rational.h:77
av::RationalMaxPrecision
@ RationalMaxPrecision
Definition: rational.h:22
av::Rational::getNumerator
int getNumerator() const noexcept
Definition: rational.h:40
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:74
av::Rational::Rational
Rational() noexcept
Definition: rational.cpp:10
av::Rational::getDenominator
int getDenominator() const noexcept
Definition: rational.h:41
av::Rational::operator-
Rational operator-(const Rational &value) const noexcept
Definition: rational.cpp:64
avutils.h
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:119
av::Rational::operator>=
bool operator>=(const Rational &other) const noexcept
Definition: rational.h:80
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:106
av::Rational::dump
void dump() const noexcept
Definition: rational.cpp:41
av::Rational::setNumerator
void setNumerator(int numerator) noexcept
Definition: rational.h:37
av::Rational::operator()
double operator()() const noexcept
Definition: rational.cpp:82
av::Rational::getValue
const AVRational & getValue() const noexcept
Definition: rational.h:34
av
Definition: audioresampler.cpp:8
av::Rational::operator<
bool operator<(const Rational &other) const noexcept
Definition: rational.h:71
av::Rational::getDouble
double getDouble() const noexcept
Definition: rational.h:42
av::Rational::setValue
void setValue(const AVRational &newValue) noexcept
Definition: rational.h:35
av::Rational::operator!=
bool operator!=(const Rational &other) const noexcept
Definition: rational.h:68
av::Rational::setDenominator
void setDenominator(int denominator) noexcept
Definition: rational.h:38
av::operator>>
std::istream & operator>>(std::istream &stream, Rational &value)
Definition: rational.h:126
av::Rational::operator*
Rational operator*(const Rational &value) const noexcept
Definition: rational.cpp:70