Skip to content

Commit

Permalink
Fix usage av::Rational with C++20 and newer applications
Browse files Browse the repository at this point in the history
  • Loading branch information
h4tr3d committed Sep 23, 2023
1 parent e9f7182 commit 3849ca6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 54 deletions.
53 changes: 5 additions & 48 deletions src/rational.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,66 +84,23 @@ double Rational::operator ()() const noexcept
return av_q2d(m_value);
}

bool Rational::operator ==(const Rational &other) const noexcept
int Rational::threewaycmp(const Rational &other) const noexcept
{
// HACK: in this case av_cmp_q() return INT_MIN;
if (m_value.den == 0 &&
m_value.num == 0 &&
other.getDenominator() == 0 &&
other.getNumerator() == 0)
{
return true;
return 0;
}

return (av_cmp_q(m_value, other.getValue()) == 0 ? true : false);
}

#if __cplusplus > 201703L

std::strong_ordering Rational::operator<=>(const Rational &other) const noexcept
{
// HACK: in this case av_cmp_q() return INT_MIN;
if (m_value.den == 0 &&
m_value.num == 0 &&
other.getDenominator() == 0 &&
other.getNumerator() == 0)
{
return std::strong_ordering::equal;
}
auto const result = av_cmp_q(m_value, other.getValue());
return result == 0 ? std::strong_ordering::equal :
result == -1 ? std::strong_ordering::less : std::strong_ordering::greater;
}

#else

bool Rational::operator!=(const Rational &other) const noexcept
{
return !(*this == other);
}

bool Rational::operator <(const Rational &other) const noexcept
{
return (av_cmp_q(m_value, other.getValue()) == -1 ? true : false);
}

bool Rational::operator>(const Rational &other) const noexcept
{
return (av_cmp_q(m_value, other.getValue()) == 1 ? true : false);
}

bool Rational::operator<=(const Rational &other) const noexcept
{
auto const result = av_cmp_q(m_value, other.getValue());
return *this == other || result == -1;
return result == 0 ? 0 : result == -1 ? -1 : 1;
}

bool Rational::operator>=(const Rational &other) const noexcept
bool Rational::operator ==(const Rational &other) const noexcept
{
auto const result = av_cmp_q(m_value, other.getValue());
return *this == other || result == 1;
return threewaycmp(other) == 0;
}

#endif

} // ::av
36 changes: 30 additions & 6 deletions src/rational.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,34 @@ class Rational

bool operator== (const Rational &other) const noexcept;
#if __cplusplus > 201703L
std::strong_ordering operator<=>(const Rational &other) const noexcept;
std::strong_ordering operator<=>(const Rational &other) const noexcept
{
switch (threewaycmp(other)) {
case -1:
return std::strong_ordering::less;
case 0:
return std::strong_ordering::equal;
case 1:
return std::strong_ordering::greater;
}
return std::strong_ordering::equal; // make a compiler happy
}
#else
bool operator!= (const Rational &other) const noexcept;
bool operator< (const Rational &other) const noexcept;
bool operator> (const Rational &other) const noexcept;
bool operator<= (const Rational &other) const noexcept;
bool operator>= (const Rational &other) const noexcept;
bool operator!= (const Rational &other) const noexcept {
return threewaycmp(other) != 0;
}
bool operator<(const Rational &other) const noexcept {
return threewaycmp(other) < 0;
}
bool operator>(const Rational &other) const noexcept {
return threewaycmp(other) > 0;
}
bool operator<=(const Rational &other) const noexcept {
return (*this < other) || (*this == other);
}
bool operator>=(const Rational &other) const noexcept {
return (*this > other) || (*this == other);
}
#endif

Rational operator+ (const Rational &value) const noexcept;
Expand All @@ -71,6 +92,9 @@ class Rational
return m_value;
}

private:
int threewaycmp(const Rational &other) const noexcept;

private:
AVRational m_value;
};
Expand Down

0 comments on commit 3849ca6

Please sign in to comment.