smooth_feedback
Control and estimation on Lie groups
Loading...
Searching...
No Matches
time.hpp
1// Copyright (C) 2022 Petter Nilsson. MIT License.
2
3#pragma once
4
5#include <chrono>
6#include <concepts>
7
8namespace smooth::feedback {
9
13template<typename T>
15
16// clang-format off
17
24template<typename T>
25concept Time = requires(T t1, T t2, double t_dbl)
26{
28 {time_trait<T>::plus(t1, t_dbl)}->std::convertible_to<T>;
29
31 {time_trait<T>::minus(t2, t1)}->std::convertible_to<double>;
32};
33
34// clang-format on
35
39template<std::floating_point T>
40struct time_trait<T>
41{
42 // \cond
43 static constexpr T plus(T t, double t_dbl) { return t + static_cast<T>(t_dbl); }
44
45 static constexpr double minus(T t2, T t1) { return static_cast<double>(t2 - t1); }
46 // \endcond
47};
48
52template<typename Clock, typename Duration>
53struct time_trait<std::chrono::time_point<Clock, Duration>>
54{
55 // \cond
56 using T = std::chrono::time_point<Clock, Duration>;
57
58 static constexpr T plus(T t, double t_dbl)
59 {
60 return t + std::chrono::duration_cast<Duration>(std::chrono::duration<double>(t_dbl));
61 }
62
63 static constexpr double minus(T t2, T t1)
64 {
65 return std::chrono::duration_cast<std::chrono::duration<double>>(t2 - t1).count();
66 }
67 // \endcond
68};
69
73template<typename Rep, typename Ratio>
74struct time_trait<std::chrono::duration<Rep, Ratio>>
75{
76 // \cond
77 using T = std::chrono::duration<Rep, Ratio>;
78
79 static constexpr T plus(T t, double t_dbl)
80 {
81 return t + std::chrono::duration_cast<T>(std::chrono::duration<double>(t_dbl));
82 }
83
84 static constexpr double minus(T t2, T t1)
85 {
86 return std::chrono::duration_cast<std::chrono::duration<double>>(t2 - t1).count();
87 }
88 // \endcond
89};
90
91} // namespace smooth::feedback
A Time type supports right-addition with a double, and subtraction of two time types should be conver...
Definition: time.hpp:25
Trait class to specify Time operations.
Definition: time.hpp:14