CANOPEN_CLIENT Driver 0.1.0
API reference
Loading...
Searching...
No Matches
canopen_client_detail.hpp
Go to the documentation of this file.
1
11#ifndef CANOPEN_CLIENT_DETAIL_HPP
12#define CANOPEN_CLIENT_DETAIL_HPP
13
14#include <array>
15#include <concepts>
16#include <cstddef>
17#include <cstdint>
18#include <limits>
19#include <type_traits>
20
21namespace canopen_client::detail {
22
27void LiteralOutOfRange() noexcept;
28
35consteval std::uint8_t operator""_u8(unsigned long long v) noexcept {
36 if (v > 0xFFULL) {
37 LiteralOutOfRange();
38 }
39 return static_cast<std::uint8_t>(v);
40}
41
47consteval std::uint16_t operator""_u16(unsigned long long v) noexcept {
48 if (v > 0xFFFFULL) {
49 LiteralOutOfRange();
50 }
51 return static_cast<std::uint16_t>(v);
52}
53
60consteval std::uint32_t operator""_u32(unsigned long long v) noexcept {
61 if (v > 0xFFFF'FFFFULL) {
62 LiteralOutOfRange();
63 }
64 return static_cast<std::uint32_t>(v);
65}
66
72consteval std::size_t operator""_z(unsigned long long v) noexcept {
73 if (v > static_cast<unsigned long long>(std::numeric_limits<std::size_t>::max())) {
74 LiteralOutOfRange();
75 }
76 return static_cast<std::size_t>(v);
77}
78
92template <typename Port, typename Sig>
93 requires std::is_class_v<Port> && std::is_function_v<Sig>
94struct ExactHook;
95
97template <typename Port, typename R, typename... Args>
98 requires std::is_class_v<Port>
99struct ExactHook<Port, R(Args...) noexcept> {
101 static void Take(R (Port::*hook)(Args...) noexcept) noexcept;
103 static void Take(R (Port::*hook)(Args...) const noexcept) noexcept;
104};
105
107template <typename T>
108concept ClassType = std::is_class_v<T>;
109
112template <ClassType>
113inline constexpr bool kAlwaysFalse = false;
114
116template <typename E>
117concept Enumeration = std::is_enum_v<E>;
118
123template <Enumeration E>
124[[nodiscard]] constexpr std::underlying_type_t<E> ToUnderlying(E e) noexcept {
125 return static_cast<std::underlying_type_t<E>>(e);
126}
127
132template <Enumeration E>
133struct EnumLast;
134
139template <Enumeration E>
140[[nodiscard]] constexpr bool IsEnumerator(E e) noexcept {
141 return static_cast<uint32_t>(ToUnderlying(e)) <=
142 static_cast<uint32_t>(ToUnderlying(EnumLast<E>::kValue));
143}
144
150template <typename Reg, uint32_t Shift, uint32_t Width>
151 requires std::unsigned_integral<Reg> && (Width > 0U) && (Width < 32U) &&
152 ((Shift + Width) <= static_cast<uint32_t>(std::numeric_limits<Reg>::digits))
153struct Field {
155 static constexpr uint32_t kMax = (uint32_t{1} << Width) - 1U;
157 static constexpr Reg kMask = static_cast<Reg>(kMax << Shift);
158
160 [[nodiscard]] static constexpr Reg Insert(Reg reg, uint32_t value) noexcept {
161 return static_cast<Reg>((static_cast<uint32_t>(reg) & ~static_cast<uint32_t>(kMask)) |
162 ((value & kMax) << Shift));
163 }
165 [[nodiscard]] static constexpr uint32_t Extract(Reg reg) noexcept {
166 return (static_cast<uint32_t>(reg) >> Shift) & kMax;
167 }
168};
169
176template <std::unsigned_integral T>
177[[nodiscard]] constexpr std::uint32_t Widen(T v) noexcept {
178 return static_cast<std::uint32_t>(v);
179}
180
185constexpr void Bump(std::uint32_t& counter) noexcept {
186 if (counter != std::numeric_limits<std::uint32_t>::max()) {
187 ++counter;
188 }
189}
190
197[[nodiscard]] constexpr bool TestBit(std::uint32_t value, std::uint32_t bit) noexcept {
198 return ((value >> (bit & 31U)) & 1U) != 0U;
199}
200
207[[nodiscard]] constexpr std::uint16_t Le16(std::uint8_t lo, std::uint8_t hi) noexcept {
208 return static_cast<std::uint16_t>(Widen(lo) | (Widen(hi) << 8U));
209}
210
217[[nodiscard]] constexpr std::uint32_t LoadLe32At4(
218 const std::array<std::uint8_t, 8>& bytes) noexcept {
219 return Widen(bytes[4]) | (Widen(bytes[5]) << 8U) | (Widen(bytes[6]) << 16U) |
220 (Widen(bytes[7]) << 24U);
221}
222
228constexpr void StoreLe32At4(std::array<std::uint8_t, 8>& bytes, std::uint32_t value) noexcept {
229 bytes[4] = static_cast<std::uint8_t>(value & 0xFFU);
230 bytes[5] = static_cast<std::uint8_t>((value >> 8U) & 0xFFU);
231 bytes[6] = static_cast<std::uint8_t>((value >> 16U) & 0xFFU);
232 bytes[7] = static_cast<std::uint8_t>((value >> 24U) & 0xFFU);
233}
234
235} // namespace canopen_client::detail
236
237#endif // CANOPEN_CLIENT_DETAIL_HPP