DYNAMIXEL Driver 0.1.0
API reference
Loading...
Searching...
No Matches
dynamixel_detail.hpp
Go to the documentation of this file.
1
11#ifndef DYNAMIXEL_DETAIL_HPP
12#define DYNAMIXEL_DETAIL_HPP
13
14#include <algorithm>
15#include <array>
16#include <concepts>
17#include <cstddef>
18#include <cstdint>
19#include <limits>
20#include <type_traits>
21
22namespace dynamixel::detail {
23
28void LiteralOutOfRange() noexcept;
29
31inline constexpr std::uint64_t kNotADigit = 99U;
32
34inline constexpr std::array<char, 16> kLowerDigits{'0', '1', '2', '3', '4', '5', '6', '7',
35 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
37inline constexpr std::array<char, 16> kUpperDigits{'0', '1', '2', '3', '4', '5', '6', '7',
38 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
40inline constexpr std::array<char, 2> kHexMarks{'x', 'X'};
42inline constexpr std::array<char, 2> kBinMarks{'b', 'B'};
43
51consteval std::uint64_t LiteralDigit(char c) noexcept {
52 const auto* const lower = std::find(kLowerDigits.begin(), kLowerDigits.end(), c);
53 if (lower != kLowerDigits.end()) {
54 return static_cast<std::uint64_t>(lower - kLowerDigits.begin());
55 }
56 const auto* const upper = std::find(kUpperDigits.begin(), kUpperDigits.end(), c);
57 if (upper != kUpperDigits.end()) {
58 return static_cast<std::uint64_t>(upper - kUpperDigits.begin());
59 }
60 return kNotADigit;
61}
62
68consteval std::uint64_t LiteralPrefixBase(char c) noexcept {
69 if (std::find(kHexMarks.begin(), kHexMarks.end(), c) != kHexMarks.end()) {
70 return 16U;
71 }
72 if (std::find(kBinMarks.begin(), kBinMarks.end(), c) != kBinMarks.end()) {
73 return 2U;
74 }
75 return 0U;
76}
77
85template <char... Digits>
86consteval std::uint64_t RawLiteralValue(std::uint64_t maximum) noexcept {
87 constexpr std::array<char, sizeof...(Digits)> kText{Digits...};
88 std::uint64_t base = 10U;
89 std::size_t first = 0U;
90 if ((kText.size() > 1U) && (LiteralDigit(kText[0]) == 0U)) {
91 base = LiteralPrefixBase(kText[1]);
92 first = 2U;
93 if (base == 0U) {
94 base = 8U; // no prefix: the leading 0 only marks octal
95 first = 1U;
96 }
97 }
98 std::uint64_t value = 0U;
99 for (std::size_t i = first; i < kText.size(); ++i) {
100 const std::uint64_t digit = LiteralDigit(kText[i]);
101 if (digit == kNotADigit) {
102 continue; // a digit separator: the compiler has accepted the literal, so nothing else is left
103 }
104 // Checked before the multiplication, so a long literal cannot wrap round to a small value.
105 if ((digit >= base) || (value > ((maximum - digit) / base))) {
106 LiteralOutOfRange();
107 }
108 value = (value * base) + digit;
109 }
110 return value;
111}
112
113} // namespace dynamixel::detail
114
115namespace dynamixel::inline literals {
118
125template <char... Digits>
126consteval std::uint8_t operator""_u8() noexcept {
127 return static_cast<std::uint8_t>(
128 detail::RawLiteralValue<Digits...>(std::numeric_limits<std::uint8_t>::max()));
129}
130
136template <char... Digits>
137consteval std::uint16_t operator""_u16() noexcept {
138 return static_cast<std::uint16_t>(
139 detail::RawLiteralValue<Digits...>(std::numeric_limits<std::uint16_t>::max()));
140}
141
148template <char... Digits>
149consteval std::uint32_t operator""_u32() noexcept {
150 return static_cast<std::uint32_t>(
151 detail::RawLiteralValue<Digits...>(std::numeric_limits<std::uint32_t>::max()));
152}
153
154} // namespace dynamixel::inline literals
155
156namespace dynamixel::detail {
157
171template <typename Port, typename Sig>
172 requires std::is_class_v<Port> && std::is_function_v<Sig>
173struct ExactHook;
174
176template <typename Port, typename R, typename... Args>
177 requires std::is_class_v<Port>
178struct ExactHook<Port, R(Args...) noexcept> {
180 static void Take(R (Port::*hook)(Args...) noexcept) noexcept;
182 static void Take(R (Port::*hook)(Args...) const noexcept) noexcept;
183};
184
186template <typename T>
187concept ClassType = std::is_class_v<T>;
188
191template <ClassType>
192inline constexpr bool kAlwaysFalse = false;
193
195template <typename E>
196concept Enumeration = std::is_enum_v<E>;
197
202template <Enumeration E>
203[[nodiscard]] constexpr std::underlying_type_t<E> ToUnderlying(E e) noexcept {
204 return static_cast<std::underlying_type_t<E>>(e);
205}
206
211template <Enumeration E>
212struct EnumLast;
213
218template <Enumeration E>
219[[nodiscard]] constexpr bool IsEnumerator(E e) noexcept {
220 return static_cast<uint32_t>(ToUnderlying(e)) <=
221 static_cast<uint32_t>(ToUnderlying(EnumLast<E>::kValue));
222}
223
231[[nodiscard]] constexpr bool InRange(uint32_t value, uint32_t low, uint32_t high) noexcept {
232 return (value >= low) && (value <= high);
233}
234
240template <typename Reg, uint32_t Shift, uint32_t Width>
241 requires std::unsigned_integral<Reg> && (Width > 0U) && (Width < 32U) &&
242 ((Shift + Width) <= static_cast<uint32_t>(std::numeric_limits<Reg>::digits))
243struct Field {
245 static constexpr uint32_t kMax = (uint32_t{1} << Width) - 1U;
247 static constexpr Reg kMask = static_cast<Reg>(kMax << Shift);
248
250 [[nodiscard]] static constexpr Reg Insert(Reg reg, uint32_t value) noexcept {
251 return static_cast<Reg>((static_cast<uint32_t>(reg) & ~static_cast<uint32_t>(kMask)) |
252 ((value & kMax) << Shift));
253 }
255 [[nodiscard]] static constexpr uint32_t Extract(Reg reg) noexcept {
256 return (static_cast<uint32_t>(reg) >> Shift) & kMax;
257 }
258};
259
260} // namespace dynamixel::detail
261
262#endif // DYNAMIXEL_DETAIL_HPP
Reg
Family-neutral register names.
Definition dynamixel_tables.hpp:67