| 34 | /// @endcode |
| 35 | template<typename Rep, typename Period = fl::ratio<1>> |
| 36 | class duration { |
| 37 | public: |
| 38 | using rep = Rep; |
| 39 | using period = Period; |
| 40 | |
| 41 | /// Default constructor - zero duration |
| 42 | constexpr duration() FL_NOEXCEPT : mCount(0) {} |
| 43 | |
| 44 | /// Explicit constructor from tick count |
| 45 | /// @param count Number of ticks |
| 46 | constexpr explicit duration(const Rep& count) FL_NOEXCEPT : mCount(count) {} |
| 47 | |
| 48 | /// Implicit conversion constructor from compatible duration types |
| 49 | /// @tparam Rep2 Source tick type |
| 50 | /// @tparam Period2 Source tick period |
| 51 | template<typename Rep2, typename Period2> |
| 52 | constexpr duration(const duration<Rep2, Period2>& d) FL_NOEXCEPT |
| 53 | : mCount(duration_cast_impl<Rep, Period, Rep2, Period2>(d.count())) {} |
| 54 | |
| 55 | /// Get the tick count |
| 56 | /// @return Number of ticks stored in this duration |
| 57 | constexpr Rep count() const FL_NOEXCEPT { return mCount; } |
| 58 | |
| 59 | private: |
| 60 | Rep mCount; |
| 61 | |
| 62 | /// Internal duration_cast implementation |
| 63 | template<typename ToRep, typename ToPeriod, typename FromRep, typename FromPeriod> |
| 64 | static constexpr ToRep duration_cast_impl(FromRep count) FL_NOEXCEPT { |
| 65 | // Convert from source period to destination period |
| 66 | // target_count = source_count * (FromPeriod / ToPeriod) |
| 67 | // Using: target = source * FromPeriod::num * ToPeriod::den / (FromPeriod::den * ToPeriod::num) |
| 68 | using ratio = fl::ratio_divide<FromPeriod, ToPeriod>; |
| 69 | return static_cast<ToRep>( |
| 70 | static_cast<FromRep>(count) * static_cast<FromRep>(ratio::num) / static_cast<FromRep>(ratio::den) |
| 71 | ); |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | /// @brief Cast one duration type to another |
| 76 | /// @tparam ToDuration Target duration type |