tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
duration_cast.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2021 Tobias Hienzsch
3
4#ifndef TETL_CHRONO_DURATION_CAST_HPP
5#define TETL_CHRONO_DURATION_CAST_HPP
6
7#include <etl/_chrono/duration.hpp>
8#include <etl/_chrono/treat_as_floating_point.hpp>
9#include <etl/_type_traits/bool_constant.hpp>
10#include <etl/_type_traits/common_type.hpp>
11#include <etl/_type_traits/is_arithmetic.hpp>
12
13namespace etl::chrono {
14
15namespace detail {
16template <typename T>
17struct is_duration : etl::false_type { };
18
19template <typename Rep, typename Period>
20struct is_duration<etl::chrono::duration<Rep, Period>> : etl::true_type { };
21
22template <typename T>
23inline constexpr auto is_duration_v = is_duration<T>::value;
24
25template <typename ToDuration, typename CF, typename CR, bool NumIsOne = false, bool DenIsOne = false>
26struct duration_cast_impl {
27 template <typename Rep, typename Period>
28 [[nodiscard]] static constexpr auto cast(
29 duration<Rep, Period> const& duration
30 ) noexcept(is_arithmetic_v<Rep> and is_arithmetic_v<typename ToDuration::rep>) -> ToDuration
31 {
32 using to_rep = typename ToDuration::rep;
33 return ToDuration(
34 static_cast<to_rep>(static_cast<CR>(duration.count()) * static_cast<CR>(CF::num) / static_cast<CR>(CF::den))
35 );
36 }
37};
38
39template <typename ToDuration, typename CF, typename CR>
40struct duration_cast_impl<ToDuration, CF, CR, true, false> {
41 template <typename Rep, typename Period>
42 [[nodiscard]] static constexpr auto cast(
43 duration<Rep, Period> const& duration
44 ) noexcept(is_arithmetic_v<Rep> and is_arithmetic_v<typename ToDuration::rep>) -> ToDuration
45 {
46 using to_rep = typename ToDuration::rep;
47 return ToDuration(static_cast<to_rep>(static_cast<CR>(duration.count()) / static_cast<CR>(CF::den)));
48 }
49};
50
51template <typename ToDuration, typename CF, typename CR>
52struct duration_cast_impl<ToDuration, CF, CR, false, true> {
53 template <typename Rep, typename Period>
54 [[nodiscard]] static constexpr auto cast(
55 duration<Rep, Period> const& duration
56 ) noexcept(is_arithmetic_v<Rep> and is_arithmetic_v<typename ToDuration::rep>) -> ToDuration
57 {
58 using to_rep = typename ToDuration::rep;
59 return ToDuration(static_cast<to_rep>(static_cast<CR>(duration.count()) * static_cast<CR>(CF::num)));
60 }
61};
62
63template <typename ToDuration, typename CF, typename CR>
64struct duration_cast_impl<ToDuration, CF, CR, true, true> {
65 template <typename Rep, typename Period>
66 [[nodiscard]] static constexpr auto cast(
67 duration<Rep, Period> const& duration
68 ) noexcept(is_arithmetic_v<Rep> and is_arithmetic_v<typename ToDuration::rep>) -> ToDuration
69 {
70 using to_rep = typename ToDuration::rep;
71 return ToDuration(static_cast<to_rep>(duration.count()));
72 }
73};
74
75} // namespace detail
76
77/// \brief Converts a duration to a duration of different type ToDur.
78/// \relates duration
79/// \ingroup chrono
80template <typename ToDur, typename Rep, typename Period>
81 requires(detail::is_duration_v<ToDur>)
82[[nodiscard]] constexpr auto duration_cast(
83 duration<Rep, Period> const& duration
84) noexcept(is_arithmetic_v<Rep> and is_arithmetic_v<typename ToDur::rep>) -> ToDur
85{
86 using detail::duration_cast_impl;
87 using cf = ratio_divide<Period, typename ToDur::period>;
88 using cr = common_type_t<typename ToDur::rep, Rep, intmax_t>;
89 using impl = duration_cast_impl<ToDur, cf, cr, cf::num == 1, cf::den == 1>;
90 return impl::cast(duration);
91}
92
93} // namespace etl::chrono
94
95#endif // TETL_CHRONO_DURATION_CAST_HPP
constexpr auto duration_cast(duration< Rep, Period > const &duration) noexcept(is_arithmetic_v< Rep > and is_arithmetic_v< typename ToDur::rep >) -> ToDur
Converts a duration to a duration of different type ToDur.
Definition duration_cast.hpp:82
Definition abs.hpp:12
Definition adjacent_find.hpp:9
Class template etl::chrono::duration represents a time interval.
Definition duration.hpp:32