SFM_FLOW_METER Driver 0.1.0
API reference
Loading...
Searching...
No Matches
sfm_flow_meter_detail.hpp
Go to the documentation of this file.
1
13#ifndef SFM_FLOW_METER_DETAIL_HPP
14#define SFM_FLOW_METER_DETAIL_HPP
15
16#include <concepts>
17#include <cstdint>
18#include <limits>
19#include <type_traits>
20
21namespace sfm::detail {
22
28void LiteralOutOfRange() noexcept;
29
37consteval std::uint8_t operator""_u8(unsigned long long v) noexcept {
38 if (v > 0xFFULL) {
39 LiteralOutOfRange();
40 }
41 return static_cast<std::uint8_t>(v);
42}
43
50consteval std::uint16_t operator""_u16(unsigned long long v) noexcept {
51 if (v > 0xFFFFULL) {
52 LiteralOutOfRange();
53 }
54 return static_cast<std::uint16_t>(v);
55}
56
64consteval std::uint32_t operator""_u32(unsigned long long v) noexcept {
65 if (v > 0xFFFF'FFFFULL) {
66 LiteralOutOfRange();
67 }
68 return static_cast<std::uint32_t>(v);
69}
70
86template <typename Port, typename Sig>
87 requires std::is_class_v<Port> && std::is_function_v<Sig>
88struct ExactHook;
89
91template <typename Port, typename R, typename... Args>
92 requires std::is_class_v<Port>
93struct ExactHook<Port, R(Args...) noexcept> {
95 static void Take(R (Port::*hook)(Args...) noexcept) noexcept;
97 static void Take(R (Port::*hook)(Args...) const noexcept) noexcept;
98};
99
102template <typename T>
103concept ClassType = std::is_class_v<T>;
104
107template <ClassType>
108inline constexpr bool kAlwaysFalse = false;
109
112template <typename E>
113concept Enumeration = std::is_enum_v<E>;
114
119template <Enumeration E>
120[[nodiscard]] constexpr std::underlying_type_t<E> ToUnderlying(E e) noexcept {
121 return static_cast<std::underlying_type_t<E>>(e);
122}
123
129template <Enumeration E>
130struct EnumLast;
131
137template <Enumeration E>
138[[nodiscard]] constexpr bool IsEnumerator(E e) noexcept {
139 return static_cast<uint32_t>(ToUnderlying(e)) <=
140 static_cast<uint32_t>(ToUnderlying(EnumLast<E>::kValue));
141}
142
148template <typename Reg, uint32_t Shift, uint32_t Width>
149 requires std::unsigned_integral<Reg> && (Width > 0U) && (Width < 32U) &&
150 ((Shift + Width) <= static_cast<uint32_t>(std::numeric_limits<Reg>::digits))
151struct Field {
153 static constexpr uint32_t kMax = (uint32_t{1} << Width) - 1U;
155 static constexpr Reg kMask = static_cast<Reg>(kMax << Shift);
156
159 [[nodiscard]] static constexpr Reg Insert(Reg reg, uint32_t value) noexcept {
160 return static_cast<Reg>((static_cast<uint32_t>(reg) & ~static_cast<uint32_t>(kMask)) |
161 ((value & kMax) << Shift));
162 }
164 [[nodiscard]] static constexpr uint32_t Extract(Reg reg) noexcept {
165 return (static_cast<uint32_t>(reg) >> Shift) & kMax;
166 }
167};
168
169} // namespace sfm::detail
170
171#endif // SFM_FLOW_METER_DETAIL_HPP