MAX22200 Driver 0.1.0
API reference
Loading...
Searching...
No Matches
max22200_detail.hpp
Go to the documentation of this file.
1
11#ifndef MAX22200_DETAIL_HPP
12#define MAX22200_DETAIL_HPP
13
14#include <concepts>
15#include <cstdint>
16#include <limits>
17#include <type_traits>
18
19namespace max22200::detail {
20
34template <typename Port, typename Sig>
35 requires std::is_class_v<Port> && std::is_function_v<Sig>
36struct ExactHook;
37
39template <typename Port, typename R, typename... Args>
40 requires std::is_class_v<Port>
41struct ExactHook<Port, R(Args...) noexcept> {
43 static void Take(R (Port::*hook)(Args...) noexcept) noexcept;
45 static void Take(R (Port::*hook)(Args...) const noexcept) noexcept;
46};
47
49template <typename T>
50concept ClassType = std::is_class_v<T>;
51
54template <ClassType>
55inline constexpr bool kAlwaysFalse = false;
56
58template <typename E>
59concept Enumeration = std::is_enum_v<E>;
60
65template <Enumeration E>
66[[nodiscard]] constexpr std::underlying_type_t<E> ToUnderlying(E e) noexcept {
67 return static_cast<std::underlying_type_t<E>>(e);
68}
69
74template <Enumeration E>
75struct EnumLast;
76
81template <Enumeration E>
82[[nodiscard]] constexpr bool IsEnumerator(E e) noexcept {
83 return static_cast<uint32_t>(ToUnderlying(e)) <=
84 static_cast<uint32_t>(ToUnderlying(EnumLast<E>::kValue));
85}
86
92template <typename Reg, uint32_t Shift, uint32_t Width>
93 requires std::unsigned_integral<Reg> && (Width > 0U) && (Width < 32U) &&
94 ((Shift + Width) <= static_cast<uint32_t>(std::numeric_limits<Reg>::digits))
95struct Field {
97 static constexpr uint32_t kMax = (uint32_t{1} << Width) - 1U;
99 static constexpr Reg kMask = static_cast<Reg>(kMax << Shift);
100
102 [[nodiscard]] static constexpr Reg Insert(Reg reg, uint32_t value) noexcept {
103 return static_cast<Reg>((static_cast<uint32_t>(reg) & ~static_cast<uint32_t>(kMask)) |
104 ((value & kMax) << Shift));
105 }
107 [[nodiscard]] static constexpr uint32_t Extract(Reg reg) noexcept {
108 return (static_cast<uint32_t>(reg) >> Shift) & kMax;
109 }
110};
111
116inline void LiteralOutOfRange() noexcept {}
117
124consteval uint8_t operator""_u8(unsigned long long v) noexcept {
125 if (v > 0xFFULL) {
126 LiteralOutOfRange();
127 }
128 return static_cast<uint8_t>(v);
129}
130
137consteval uint32_t operator""_u32(unsigned long long v) noexcept {
138 if (v > 0xFFFF'FFFFULL) {
139 LiteralOutOfRange();
140 }
141 return static_cast<uint32_t>(v);
142}
143
144} // namespace max22200::detail
145
146#endif // MAX22200_DETAIL_HPP