FDO2 Driver 0.1.0
API reference
Loading...
Searching...
No Matches
fdo2_detail.hpp
Go to the documentation of this file.
1
11#ifndef FDO2_DETAIL_HPP
12#define FDO2_DETAIL_HPP
13
14#include <concepts>
15#include <cstdint>
16#include <limits>
17#include <type_traits>
18
19namespace fdo2::detail {
20
25void LiteralOutOfRange() noexcept;
26
33consteval std::uint8_t operator""_u8(unsigned long long v) noexcept {
34 if (v > 0xFFULL) {
35 LiteralOutOfRange();
36 }
37 return static_cast<std::uint8_t>(v);
38}
39
45consteval std::uint16_t operator""_u16(unsigned long long v) noexcept {
46 if (v > 0xFFFFULL) {
47 LiteralOutOfRange();
48 }
49 return static_cast<std::uint16_t>(v);
50}
51
58consteval std::uint32_t operator""_u32(unsigned long long v) noexcept {
59 if (v > 0xFFFF'FFFFULL) {
60 LiteralOutOfRange();
61 }
62 return static_cast<std::uint32_t>(v);
63}
64
78template <typename Port, typename Sig>
79 requires std::is_class_v<Port> && std::is_function_v<Sig>
80struct ExactHook;
81
83template <typename Port, typename R, typename... Args>
84 requires std::is_class_v<Port>
85struct ExactHook<Port, R(Args...) noexcept> {
87 static void Take(R (Port::*hook)(Args...) noexcept) noexcept;
89 static void Take(R (Port::*hook)(Args...) const noexcept) noexcept;
90};
91
93template <typename T>
94concept ClassType = std::is_class_v<T>;
95
98template <ClassType>
99inline constexpr bool kAlwaysFalse = false;
100
102template <typename E>
103concept Enumeration = std::is_enum_v<E>;
104
109template <Enumeration E>
110[[nodiscard]] constexpr std::underlying_type_t<E> ToUnderlying(E e) noexcept {
111 return static_cast<std::underlying_type_t<E>>(e);
112}
113
118template <Enumeration E>
119struct EnumLast;
120
125template <Enumeration E>
126[[nodiscard]] constexpr bool IsEnumerator(E e) noexcept {
127 return static_cast<uint32_t>(ToUnderlying(e)) <=
128 static_cast<uint32_t>(ToUnderlying(EnumLast<E>::kValue));
129}
130
136template <typename Reg, uint32_t Shift, uint32_t Width>
137 requires std::unsigned_integral<Reg> && (Width > 0U) && (Width < 32U) &&
138 ((Shift + Width) <= static_cast<uint32_t>(std::numeric_limits<Reg>::digits))
139struct Field {
141 static constexpr uint32_t kMax = (uint32_t{1} << Width) - 1U;
143 static constexpr Reg kMask = static_cast<Reg>(kMax << Shift);
144
146 [[nodiscard]] static constexpr Reg Insert(Reg reg, uint32_t value) noexcept {
147 return static_cast<Reg>((static_cast<uint32_t>(reg) & ~static_cast<uint32_t>(kMask)) |
148 ((value & kMax) << Shift));
149 }
151 [[nodiscard]] static constexpr uint32_t Extract(Reg reg) noexcept {
152 return (static_cast<uint32_t>(reg) >> Shift) & kMax;
153 }
154};
155
156} // namespace fdo2::detail
157
158#endif // FDO2_DETAIL_HPP