tin  1.5.9
ticks.h
Go to the documentation of this file.
1 // Copyright (c) 2026 Tinverse LLC. All rights reserved.
2 // SPDX-License-Identifier: LicenseRef-Tinverse-Commercial
3 
12 
13 #pragma once
14 
15 #include <chrono>
16 #include <cstdint>
17 #include <ratio>
18 #include <type_traits>
19 
20 #ifndef TSM_TICK_REP
21 #define TSM_TICK_REP std::uint32_t
22 #endif
23 
24 #ifndef TSM_TICK_PERIOD
25 #define TSM_TICK_PERIOD std::ratio<1>
26 #endif
27 
28 namespace tsm {
29 
36 
43 
44 static_assert(std::is_integral_v<tick_rep> && std::is_unsigned_v<tick_rep>,
45  "tsm: TSM_TICK_REP must name an unsigned integral type");
46 static_assert(tick_period::num > 0 && tick_period::den > 0,
47  "tsm: TSM_TICK_PERIOD must name a positive std::ratio");
48 
50 using tick_duration = std::chrono::duration<tick_rep, tick_period>;
51 
53 struct tick_count
54 {
55  using rep = tick_rep;
58 
60 
61  constexpr tick_count() noexcept = default;
62 
63  explicit constexpr tick_count(tick_duration duration) noexcept
64  : value(duration)
65  {
66  }
67 
68  explicit constexpr tick_count(tick_rep count) noexcept
70  {
71  }
72 
73  [[nodiscard]] constexpr tick_rep count() const noexcept
74  {
75  return value.count();
76  }
77 };
78 
79 [[nodiscard]] consteval tick_count operator""_ticks(unsigned long long value)
80 {
81  return tick_count{ tick_duration{ static_cast<tick_rep>(value) } };
82 }
83 
84 [[nodiscard]] constexpr tick_count
85 ticks(tick_rep value) noexcept
86 {
87  return tick_count{ value };
88 }
89 
90 template<typename Rep, typename Period>
91 [[nodiscard]] constexpr tick_count
92 ticks(std::chrono::duration<Rep, Period> value) noexcept
93 {
94  return tick_count{ std::chrono::duration_cast<tick_duration>(value) };
95 }
96 
97 } // namespace tsm
Definition: bare_metal.h:20
constexpr tick_count ticks(tick_rep value) noexcept
Definition: ticks.h:85
TSM_TICK_PERIOD tick_period
Definition: ticks.h:42
std::chrono::duration< tick_rep, tick_period > tick_duration
Chrono duration type used for semantic scheduler ticks.
Definition: ticks.h:50
TSM_TICK_REP tick_rep
Definition: ticks.h:35
Strong value type for semantic scheduler ticks.
Definition: ticks.h:54
constexpr tick_count() noexcept=default
constexpr tick_rep count() const noexcept
Definition: ticks.h:73
tick_duration value
Definition: ticks.h:59
constexpr tick_count(tick_rep count) noexcept
Definition: ticks.h:68
tick_period period
Definition: ticks.h:56
tick_duration value_type
Definition: ticks.h:57
tick_rep rep
Definition: ticks.h:55
#define TSM_TICK_REP
Definition: ticks.h:21
#define TSM_TICK_PERIOD
Definition: ticks.h:25