AT25040B Driver 0.1.0
API reference
Loading...
Searching...
No Matches
at25040b_detail.hpp
Go to the documentation of this file.
1
11#ifndef AT25040B_DETAIL_HPP
12#define AT25040B_DETAIL_HPP
13
14#include <concepts>
15#include <cstdint>
16#include <limits>
17#include <type_traits>
18
19namespace at25040b::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
123inline void LiteralOutOfRange() noexcept {}
124
126consteval uint8_t operator""_u8(unsigned long long value) noexcept { // NOLINT(google-runtime-int)
127 if (value > static_cast<unsigned long long>(std::numeric_limits<uint8_t>::max())) {
128 LiteralOutOfRange();
129 }
130 return static_cast<uint8_t>(value);
131}
132
134consteval uint16_t operator""_u16(unsigned long long value) noexcept { // NOLINT(google-runtime-int)
135 if (value > static_cast<unsigned long long>(std::numeric_limits<uint16_t>::max())) {
136 LiteralOutOfRange();
137 }
138 return static_cast<uint16_t>(value);
139}
140
142consteval uint32_t operator""_u32(unsigned long long value) noexcept { // NOLINT(google-runtime-int)
143 if (value > static_cast<unsigned long long>(std::numeric_limits<uint32_t>::max())) {
144 LiteralOutOfRange();
145 }
146 return static_cast<uint32_t>(value);
147}
148
151static_assert(0xFF_u8 == std::numeric_limits<uint8_t>::max() && 0xFFFF_u16 == 0xFFFFU &&
152 0xFFFF'FFFF_u32 == std::numeric_limits<uint32_t>::max());
153
154} // namespace at25040b::detail
155
156#endif // AT25040B_DETAIL_HPP