Files
oak-editor/app/common/timerange.cpp
T
lightyear15 96074d3f3a timerange quick refactor
std::swap can saves some lines of code,
and less code means less to maintain
2019-11-08 22:02:55 +01:00

67 lines
982 B
C++

#include "timerange.h"
#include <utility>
TimeRange::TimeRange(const rational &in, const rational &out) :
in_(in),
out_(out)
{
normalize();
}
const rational &TimeRange::in() const
{
return in_;
}
const rational &TimeRange::out() const
{
return out_;
}
const rational &TimeRange::length() const
{
return length_;
}
void TimeRange::set_in(const rational &in)
{
in_ = in;
normalize();
}
void TimeRange::set_out(const rational &out)
{
out_ = out;
normalize();
}
void TimeRange::set_range(const rational &in, const rational &out)
{
in_ = in;
out_ = out;
normalize();
}
bool TimeRange::operator<(const TimeRange &r) const
{
return (in() + out()) < (r.in() + r.out());
}
bool TimeRange::operator>(const TimeRange &r) const
{
return (in() + out()) > (r.in() + r.out());
}
void TimeRange::normalize()
{
// If `out` is earlier than `in`, swap them
if (out_ < in_)
{
std::swap(out_, in_);
}
// Calculate length
length_ = out_ - in_;
}