⏱️ golxzn::os::chrono ⏱️ 1.2.4
Chrono for golxzn's projects.
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1/**
2 * @file golxzn/os/chrono/utils.hpp
3 * @author Ruslan Golovinskii (golxzn@gmail.com)
4 * @brief Useful functions and aliases for golxzn::os::chrono
5 * @date 2023-10-24
6 *
7 * @copyright Copyright (c) 2023
8 */
9
10#pragma once
11
12#include <chrono>
13#include <utility>
14#include <type_traits>
15
16namespace golxzn::os::chrono::utils {
17
18template<class T>
19using floating_point_t = std::enable_if_t<std::is_floating_point_v<T>, T>;
20
21/**
22 * @brief Default clock type
23 */
24using default_base_clock = std::conditional_t<std::chrono::high_resolution_clock::is_steady,
25 std::chrono::high_resolution_clock,
26 std::chrono::steady_clock
27>;
28
29template<class Clock, class Minimum = std::micro>
30struct enough_resolution : std::bool_constant<std::ratio_less_equal_v<typename Clock::period, Minimum>> {};
31
32template<class Clock>
33static constexpr bool enough_resolution_v{ enough_resolution<Clock>::value };
34
35/**
36 * @brief Time difference between two time points
37 * @ingroup Chrono utilities
38 * @tparam T return type
39 * @param current current time (usually std::chrono::time_point)
40 * @param last last time (usually std::chrono::time_point)
41 * @return constexpr T
42 * @see golxzn::os::chrono::utils::base_clock
43 */
44template<class T, class Other>
45[[nodiscard]] constexpr auto difference(const Other &current, const Other &last) noexcept
46 -> std::conditional_t<std::is_constructible_v<T, std::chrono::microseconds>, T, void> {
47 return T{ std::chrono::duration_cast<std::chrono::microseconds>(current - last) };
48}
49
50} // namespace golxzn::os::chrono::utils
constexpr auto difference(const Other &current, const Other &last) noexcept -> std::conditional_t< std::is_constructible_v< T, std::chrono::microseconds >, T, void >
Time difference between two time points.
Definition utils.hpp:45
std::conditional_t< std::chrono::high_resolution_clock::is_steady, std::chrono::high_resolution_clock, std::chrono::steady_clock > default_base_clock
Default clock type.
Definition utils.hpp:27