avcpp 3.1.0
Wrapper for the FFmpeg that simplify usage from C++ projects.
Loading...
Searching...
No Matches
rational.h
Go to the documentation of this file.
1#pragma once
2
9
10#include <utility>
11#include <iostream>
12#include <memory>
13#include <type_traits>
14
15#include "ffmpeg.h"
16#include "avutils.h"
17
18#if AVCPP_USE_SPACESHIP_OPERATOR
19# include <compare>
20#endif
21
22namespace av
23{
24
30#define AV_TIME_BASE_Q_CPP AVRational{1, AV_TIME_BASE}
31
32enum
33{
34 RationalMaxPrecision = 5
35};
36
49{
50public:
54 Rational() noexcept;
61 Rational(int numerator, int denominator = 1) noexcept;
67 Rational(const AVRational &value) noexcept;
74 Rational(double value, int maxPrecision = RationalMaxPrecision) noexcept;
75
81 AVRational& getValue() noexcept { return m_value; }
87 const AVRational& getValue() const noexcept { return m_value; }
93 void setValue(const AVRational &newValue) noexcept { m_value = newValue; }
94
100 void setNumerator(int numerator) noexcept { m_value.num = numerator; }
106 void setDenominator(int denominator) noexcept { m_value.den = denominator; }
107
113 int getNumerator() const noexcept{ return m_value.num; }
119 int getDenominator() const noexcept { return m_value.den; }
125 double getDouble() const noexcept { return (double)m_value.num / (double)m_value.den; }
126
135 static Rational fromDouble(double value, int maxPrecision = RationalMaxPrecision) noexcept;
136
145 int64_t rescale(int64_t srcValue, const Rational &dstBase) const noexcept;
146
150 void dump() const noexcept;
151
159 Rational& operator= (const AVRational &value) noexcept;
167 Rational& operator= (double value) noexcept;
168
176 bool operator== (const Rational &other) const noexcept;
177#if AVCPP_USE_SPACESHIP_OPERATOR
185 std::strong_ordering operator<=>(const Rational &other) const noexcept
186 {
187 if (auto const cmp = threewaycmp(other); cmp == 0) {
188 return std::strong_ordering::equal;
189 } else if (cmp < 0) {
190 return std::strong_ordering::less;
191 } else {
192 return std::strong_ordering::greater;
193 }
194 }
195#else
203 bool operator!= (const Rational &other) const noexcept {
204 return threewaycmp(other) != 0;
205 }
213 bool operator<(const Rational &other) const noexcept {
214 return threewaycmp(other) < 0;
215 }
223 bool operator>(const Rational &other) const noexcept {
224 return threewaycmp(other) > 0;
225 }
233 bool operator<=(const Rational &other) const noexcept {
234 return (*this < other) || (*this == other);
235 }
243 bool operator>=(const Rational &other) const noexcept {
244 return (*this > other) || (*this == other);
245 }
246#endif
247
255 Rational operator+ (const Rational &value) const noexcept;
263 Rational operator- (const Rational &value) const noexcept;
271 Rational operator* (const Rational &value) const noexcept;
279 Rational operator/ (const Rational &value) const noexcept;
280
286 double operator() () const noexcept;
287
293 operator const AVRational&() const noexcept
294 {
295 return m_value;
296 }
297
298private:
299 int threewaycmp(const Rational &other) const noexcept;
300
301private:
302 AVRational m_value;
303};
304
305
306template<typename T>
317auto operator/ (T num, Rational value) ->
318 std::enable_if_t<std::is_floating_point_v<T> || std::is_integral_v<T>, Rational>
319{
320 return Rational{num, 1} / value;
321}
322
323template<typename T>
334auto operator/ (Rational value, T num) ->
335 std::enable_if_t<std::is_floating_point_v<T> || std::is_integral_v<T>, Rational>
336{
337 return value / Rational{num, 1};
338}
339
348inline std::ostream& operator<< (std::ostream &stream, const Rational &value)
349{
350 stream << value.getNumerator() << '/' << value.getDenominator();
351 return stream;
352
353}
354
363inline std::istream& operator>> (std::istream &stream, Rational &value)
364{
365 char ch;
366 AVRational temp;
367
368 try
369 {
370 stream >> temp.num >> ch >> temp.den;
371 if (ch == '/')
372 {
373 value.setNumerator(temp.num);
374 value.setDenominator(temp.den);
375 }
376 }
377 catch (const std::exception &e)
378 {}
379
380 return stream;
381}
382
383
384} // ::av
385
386
387#ifdef __cpp_lib_format
388#include <format>
389// std::format
390template <typename CharT>
391struct std::formatter<av::Rational, CharT>
392{
393 template<typename ParseContext>
394 constexpr auto parse(ParseContext& ctx)
395 {
396 return ctx.begin();
397 }
398 template<typename ParseContext>
399 auto format(const av::Rational& value, ParseContext& ctx) const
400 {
401 return std::format_to(ctx.out(), "{}/{}", value.getNumerator(), value.getDenominator());
402 }
403};
404#endif
Common helpers: comparisons, value lookup, non-copyable base, string utilities.
Rational number: a numerator and a denominator.
Definition rational.h:49
Rational operator-(const Rational &value) const noexcept
Subtract another rational.
const AVRational & getValue() const noexcept
The underlying FFmpeg value.
Definition rational.h:87
double getDouble() const noexcept
The value as a floating point number.
Definition rational.h:125
Rational operator*(const Rational &value) const noexcept
Multiply by another rational.
void setNumerator(int numerator) noexcept
Set the numerator.
Definition rational.h:100
AVRational & getValue() noexcept
Mutable access to the underlying FFmpeg value.
Definition rational.h:81
Rational() noexcept
Create zero, in the sense of 0/1.
static Rational fromDouble(double value, int maxPrecision=RationalMaxPrecision) noexcept
Approximate a floating point value as a rational.
void dump() const noexcept
Print the value through the FFmpeg log system.
int getDenominator() const noexcept
The denominator.
Definition rational.h:119
std::strong_ordering operator<=>(const Rational &other) const noexcept
Order two rationals by value.
Definition rational.h:185
int64_t rescale(int64_t srcValue, const Rational &dstBase) const noexcept
Rescale a value from this base to another.
void setValue(const AVRational &newValue) noexcept
Replace the value.
Definition rational.h:93
int getNumerator() const noexcept
The numerator.
Definition rational.h:113
Rational operator+(const Rational &value) const noexcept
Add another rational.
double operator()() const noexcept
The value as a floating point number.
void setDenominator(int denominator) noexcept
Set the denominator.
Definition rational.h:106
Rational operator/(const Rational &value) const noexcept
Divide by another rational.
FFmpeg types re-exported for the public API and small value wrappers.
Root namespace of the library.
Definition audioresampler.h:19
bool operator<(const Timestamp &left, const Timestamp &right) noexcept
Compare two timestamps, taking their time bases into account.
Definition timestamp.h:270
std::istream & operator>>(std::istream &stream, Rational &value)
Read a rational written as "numerator/denominator".
Definition rational.h:363
bool operator>(const Timestamp &left, const Timestamp &right) noexcept
Compare two timestamps, taking their time bases into account.
Definition timestamp.h:254
bool operator<=(const Timestamp &left, const Timestamp &right) noexcept
Compare two timestamps.
Definition timestamp.h:330
bool operator>=(const Timestamp &left, const Timestamp &right) noexcept
Compare two timestamps.
Definition timestamp.h:315
bool operator!=(const Timestamp &left, const Timestamp &right) noexcept
Compare two timestamps.
Definition timestamp.h:301
std::ostream & operator<<(std::ostream &stream, const Rational &value)
Print the rational as "numerator/denominator".
Definition rational.h:348
auto operator/(T num, Rational value) -> std::enable_if_t< std::is_floating_point_v< T >||std::is_integral_v< T >, Rational >
Divide a number by a rational.
Definition rational.h:317