BASIS 2 Driver 0.1.0
API reference
Loading...
Searching...
No Matches
alicat_basis2_detail.hpp
Go to the documentation of this file.
1
11#ifndef ALICAT_BASIS2_DETAIL_HPP
12#define ALICAT_BASIS2_DETAIL_HPP
13
14#include <algorithm>
15#include <array>
16#include <bit>
17#include <concepts>
18#include <cstddef>
19#include <cstdint>
20#include <limits>
21#include <type_traits>
22
23namespace alicat_basis2::detail {
24
38template <typename Port, typename Sig>
39 requires std::is_class_v<Port> && std::is_function_v<Sig>
40struct ExactHook;
41
43template <typename Port, typename R, typename... Args>
44 requires std::is_class_v<Port>
45struct ExactHook<Port, R(Args...) noexcept> {
47 static void Take(R (Port::*hook)(Args...) noexcept) noexcept;
49 static void Take(R (Port::*hook)(Args...) const noexcept) noexcept;
50};
51
53template <typename T>
54concept ClassType = std::is_class_v<T>;
55
58template <ClassType>
59inline constexpr bool kAlwaysFalse = false;
60
62template <typename E>
63concept Enumeration = std::is_enum_v<E>;
64
69template <Enumeration E>
70[[nodiscard]] constexpr std::underlying_type_t<E> ToUnderlying(E e) noexcept {
71 return static_cast<std::underlying_type_t<E>>(e);
72}
73
78template <Enumeration E>
79struct EnumLast;
80
85template <Enumeration E>
86[[nodiscard]] constexpr bool IsEnumerator(E e) noexcept {
87 return static_cast<uint32_t>(ToUnderlying(e)) <=
88 static_cast<uint32_t>(ToUnderlying(EnumLast<E>::kValue));
89}
90
96template <typename Reg, uint32_t Shift, uint32_t Width>
97 requires std::unsigned_integral<Reg> && (Width > 0U) && (Width < 32U) &&
98 ((Shift + Width) <= static_cast<uint32_t>(std::numeric_limits<Reg>::digits))
99struct Field {
101 static constexpr uint32_t kMax = (uint32_t{1} << Width) - 1U;
103 static constexpr Reg kMask = static_cast<Reg>(kMax << Shift);
104
106 [[nodiscard]] static constexpr Reg Insert(Reg reg, uint32_t value) noexcept {
107 return static_cast<Reg>((static_cast<uint32_t>(reg) & ~static_cast<uint32_t>(kMask)) |
108 ((value & kMax) << Shift));
109 }
111 [[nodiscard]] static constexpr uint32_t Extract(Reg reg) noexcept {
112 return (static_cast<uint32_t>(reg) >> Shift) & kMax;
113 }
114};
115
117inline constexpr std::array<char, 10> kDigitChars{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
119inline constexpr std::array<char, 26> kUpperChars{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
120 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
121 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
123inline constexpr std::array<char, 26> kLowerChars{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
124 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
125 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
126
129template <std::size_t N>
130[[nodiscard]] constexpr int32_t PlaceOf(const std::array<char, N>& table, char c) noexcept {
131 const auto* const found = std::find(table.begin(), table.end(), c);
132 return (found == table.end()) ? -1 : static_cast<int32_t>(found - table.begin());
133}
134
136[[nodiscard]] constexpr int32_t DigitValue(char c) noexcept {
137 return PlaceOf(kDigitChars, c);
138}
140[[nodiscard]] constexpr bool IsDigit(char c) noexcept {
141 return DigitValue(c) >= 0;
142}
144[[nodiscard]] constexpr char DigitChar(uint64_t value) noexcept {
145 return kDigitChars[static_cast<std::size_t>(value % 10U)];
146}
148[[nodiscard]] constexpr int32_t UpperIndex(char c) noexcept {
149 return PlaceOf(kUpperChars, c);
150}
152[[nodiscard]] constexpr bool IsUpper(char c) noexcept {
153 return UpperIndex(c) >= 0;
154}
156[[nodiscard]] constexpr char UpperChar(uint32_t index) noexcept {
157 return kUpperChars[static_cast<std::size_t>(index % 26U)];
158}
160[[nodiscard]] constexpr bool IsLower(char c) noexcept {
161 return PlaceOf(kLowerChars, c) >= 0;
162}
164[[nodiscard]] constexpr bool IsAlnum(char c) noexcept {
165 return IsUpper(c) || IsLower(c) || IsDigit(c);
166}
168[[nodiscard]] constexpr uint8_t ByteOf(char c) noexcept {
169 return std::bit_cast<uint8_t>(c);
170}
173[[nodiscard]] constexpr bool SameChar(char first, char second) noexcept {
174 return ByteOf(first) == ByteOf(second);
175}
177[[nodiscard]] constexpr char CharOf(uint8_t byte) noexcept {
178 return std::bit_cast<char>(byte);
179}
181[[nodiscard]] constexpr bool IsPrintableByte(uint8_t byte) noexcept {
182 return (byte >= 0x20U) && (byte <= 0x7EU);
183}
184
185} // namespace alicat_basis2::detail
186
187#endif // ALICAT_BASIS2_DETAIL_HPP