Moved operators inside the class, made all functions to be constexpr

This commit is contained in:
Vladyslav Baranovskyi
2024-10-08 21:09:11 +03:00
parent 0def449d13
commit f7a85d60ec

View File

@@ -18,79 +18,61 @@ namespace OpenVulkano::Math
uint64_t timestamp; uint64_t timestamp;
public: public:
explicit Timestamp(uint64_t nanos = 0) : timestamp(nanos) {} explicit constexpr Timestamp(uint64_t nanos = 0) : timestamp(nanos) {}
explicit Timestamp(double seconds) : timestamp(static_cast<uint64_t>(seconds * SECONDS_TO_NANOS)) {} explicit constexpr Timestamp(double seconds) : timestamp(static_cast<uint64_t>(seconds * SECONDS_TO_NANOS)) {}
[[nodiscard]] uint64_t GetNanos() const { return timestamp; } [[nodiscard]] constexpr uint64_t GetNanos() const { return timestamp; }
[[nodiscard]] double GetSeconds() const { return timestamp * NANOS_TO_SECONDS; } [[nodiscard]] constexpr double GetSeconds() const { return timestamp * NANOS_TO_SECONDS; }
Timestamp& operator+=(const Timestamp& rhs) constexpr Timestamp& operator+=(const Timestamp& rhs)
{ {
timestamp += rhs.timestamp; timestamp += rhs.timestamp;
return *this; return *this;
} }
Timestamp& operator-=(const Timestamp& rhs) constexpr Timestamp& operator-=(const Timestamp& rhs)
{ {
timestamp -= rhs.timestamp; timestamp -= rhs.timestamp;
return *this; return *this;
} }
Timestamp& operator =(double time) constexpr Timestamp& operator =(double time)
{ {
timestamp = Timestamp(time).timestamp; timestamp = Timestamp(time).timestamp;
return *this; return *this;
} }
Timestamp& operator =(uint64_t time) constexpr Timestamp& operator =(uint64_t time)
{ {
timestamp = time; timestamp = time;
return *this; return *this;
} }
constexpr inline bool operator==(const Timestamp& rhs) const { return GetNanos() == rhs.GetNanos(); }
constexpr inline bool operator!=(const Timestamp& rhs) const { return !(*this == rhs); }
constexpr inline bool operator<(const Timestamp& rhs) const { return GetNanos() < rhs.GetNanos(); }
constexpr inline bool operator>(const Timestamp& rhs) const { return rhs < *this; }
constexpr inline bool operator>=(const Timestamp& rhs) const { return !(*this < rhs); }
constexpr inline bool operator<=(const Timestamp& rhs) const { return !(rhs < *this); }
constexpr inline Timestamp operator-(const Timestamp& rhs) const
{
return Timestamp(GetNanos() - rhs.GetNanos());
}
constexpr inline Timestamp operator+(const Timestamp& rhs) const
{
return Timestamp(GetNanos() + rhs.GetNanos());
}
constexpr inline double operator/(const Timestamp& rhs) const
{
return GetNanos() / static_cast<double>(rhs.GetNanos());
}
}; };
inline bool operator==(const Timestamp& lhs, const Timestamp& rhs)
{
return lhs.GetNanos() == rhs.GetNanos();
}
inline bool operator!=(const Timestamp& lhs, const Timestamp& rhs)
{
return !(lhs == rhs);
}
inline bool operator<(const Timestamp& lhs, const Timestamp& rhs)
{
return lhs.GetNanos() < rhs.GetNanos();
}
inline bool operator>(const Timestamp& lhs, const Timestamp& rhs)
{
return rhs < lhs;
}
inline bool operator>=(const Timestamp& lhs, const Timestamp& rhs)
{
return !(lhs < rhs);
}
inline bool operator<=(const Timestamp& lhs, const Timestamp& rhs)
{
return !(rhs < lhs);
}
inline Timestamp operator-(const Timestamp& lhs, const Timestamp& rhs)
{
return Timestamp(lhs.GetNanos() - rhs.GetNanos());
}
inline Timestamp operator+(const Timestamp& lhs, const Timestamp& rhs)
{
return Timestamp(lhs.GetNanos() + rhs.GetNanos());
}
inline double operator/(const Timestamp& lhs, const Timestamp& rhs)
{
return lhs.GetNanos() / static_cast<double>(rhs.GetNanos());
}
} }