diff --git a/include/structures/Time.hpp b/include/structures/Time.hpp index e7ea9ef..aaf5dfa 100644 --- a/include/structures/Time.hpp +++ b/include/structures/Time.hpp @@ -15,15 +15,48 @@ public: Int32 AsMilliseconds() const; Int64 AsMicroseconds() const; - template Duration AsChrono() const; + template> std::chrono::duration AsChrono() const; std::chrono::seconds AsChronoSeconds() const; std::chrono::milliseconds AsChronoMilliseconds() const; std::chrono::microseconds AsChronoMicroseconds() const; + template friend Time Create(const Rep& duration); + template friend Time Create(const std::chrono::duration& duration); friend Time Seconds(float seconds); friend Time Milliseconds(Int32 milliseconds); friend Time Microseconds(Int64 microseconds); + friend bool operator==(const Time& left, const Time& right); + friend bool operator!=(const Time& left, const Time& right); + friend bool operator<(const Time& left, const Time& right); + friend bool operator>(const Time& left, const Time& right); + friend bool operator<=(const Time& left, const Time& right); + friend bool operator>=(const Time& left, const Time& right); + + friend Time operator-(const Time& right); + + friend Time operator+(const Time& left, const Time& right); + friend Time& operator+=(Time& left, const Time& right); + + friend Time operator-(const Time& left, const Time& right); + friend Time& operator-=(Time& left, const Time& right); + + friend Time operator*(const Time& left, float right); + friend Time operator*(const Time& left, Int64 right); + friend Time operator*(float left, const Time& right); + friend Time operator*(Int64 left, const Time& right); + friend Time& operator*=(Time& left, float right); + friend Time& operator*=(Time& left, Int64 right); + + friend Time operator/(const Time& left, float right); + friend Time operator/(const Time& left, Int64 right); + friend Time& operator/=(Time& left, float right); + friend Time& operator/=(Time& left, Int64 right); + friend float operator/(const Time& left, const Time& right); + + friend Time operator%(const Time& left, const Time& right); + friend Time& operator%=(Time& left, const Time& right); + private: std::chrono::duration microseconds; }; @@ -35,10 +68,26 @@ inline Rep Time::AsValue() const return std::chrono::duration_cast>(microseconds).count(); } -template -inline Duration Time::AsChrono() const +template +inline std::chrono::duration Time::AsChrono() const { - return std::chrono::duration_cast(microseconds); + return std::chrono::duration_cast>(microseconds); +} + +template> +inline Time Create(const Rep& duration) +{ + Time newTime; + newTime.microseconds = std::chrono::duration_cast>(std::chrono::duration(duration)); + return newTime; +} + +template> +inline Time Create(const std::chrono::duration& duration) +{ + Time newTime; + newTime.microseconds = std::chrono::duration_cast>(duration); + return newTime; } SDLU_END \ No newline at end of file diff --git a/src/structures/Time.cpp b/src/structures/Time.cpp index f20d88e..def608a 100644 --- a/src/structures/Time.cpp +++ b/src/structures/Time.cpp @@ -25,38 +25,159 @@ Int64 Time::AsMicroseconds() const std::chrono::seconds Time::AsChronoSeconds() const { - return AsChrono(); + return AsChrono>(); } std::chrono::milliseconds Time::AsChronoMilliseconds() const { - return AsChrono(); + return AsChrono(); } std::chrono::microseconds Time::AsChronoMicroseconds() const { - return AsChrono(); + return AsChrono(); } Time Seconds(float seconds) { - Time newTime; - newTime.microseconds = std::chrono::duration_cast>(std::chrono::duration(seconds)); - return newTime; + return Create(seconds); } Time Milliseconds(Int32 milliseconds) { - Time newTime; - newTime.microseconds = std::chrono::duration_cast>(std::chrono::duration(milliseconds)); - return newTime; + return Create(milliseconds); } Time Microseconds(Int64 microseconds) { - Time newTime; - newTime.microseconds = std::chrono::duration(microseconds); - return newTime; + return Create(microseconds); +} + +bool operator==(const Time& left, const Time& right) +{ + return (left.microseconds == right.microseconds); +} + +bool operator!=(const Time& left, const Time& right) +{ + return (left.microseconds != right.microseconds); +} + +bool operator<(const Time& left, const Time& right) +{ + return (left.microseconds < right.microseconds); +} + +bool operator>(const Time& left, const Time& right) +{ + return (left.microseconds > right.microseconds); +} + +bool operator<=(const Time& left, const Time& right) +{ + return (left.microseconds <= right.microseconds); +} + +bool operator>=(const Time& left, const Time& right) +{ + return (left.microseconds >= right.microseconds); +} + +Time operator-(const Time& right) +{ + return Create(-right.microseconds); +} + +Time operator+(const Time& left, const Time& right) +{ + return Create(left.microseconds + right.microseconds); +} + +Time& operator+=(Time& left, const Time& right) +{ + left = left + right; + return left; +} + +Time operator-(const Time& left, const Time& right) +{ + return Create(left.microseconds - right.microseconds); +} + +Time& operator-=(Time& left, const Time& right) +{ + left = left - right; + return left; +} + +Time operator*(const Time& left, float right) +{ + return Create(left.microseconds * right); +} + +Time operator*(const Time& left, Int64 right) +{ + return Create(left.microseconds * right); +} + +Time operator*(float left, const Time& right) +{ + return (right * left); +} + +Time operator*(Int64 left, const Time& right) +{ + return (right * left); +} + +Time& operator*=(Time& left, float right) +{ + left = left * right; + return left; +} + +Time& operator*=(Time& left, Int64 right) +{ + left = left * right; + return left; +} + +Time operator/(const Time& left, float right) +{ + return Create(left.microseconds / right); +} + +Time operator/(const Time& left, Int64 right) +{ + return Create(left.microseconds / right); +} + +Time& operator/=(Time& left, float right) +{ + left = left / right; + return left; +} + +Time& operator/=(Time& left, Int64 right) +{ + left = left / right; + return left; +} + +float operator/(const Time& left, const Time& right) +{ + return ((float)(left.AsMicroseconds()) / (float)(right.AsMicroseconds())); +} + +Time operator%(const Time& left, const Time& right) +{ + return Create(left.microseconds % right.microseconds); +} + +Time& operator%=(Time& left, const Time& right) +{ + left = left % right; + return left; } SDLU_END \ No newline at end of file