libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
libxr_def.hpp
1#pragma once
2
3#include <cmath>
4#include <concepts>
5#include <cstddef>
6#include <cstdint>
7#include <type_traits>
8
9#ifndef DEF2STR
10#define XR_TO_STR(_arg) #_arg
11#define DEF2STR(_arg) XR_TO_STR(_arg)
12#endif
13
14#ifndef UNUSED
16#define UNUSED(_x) ((void)(_x))
17#endif
18
19#ifndef LIBXR_SINGLE_CORE
20#define LIBXR_SINGLE_CORE 0
21#endif
22
23#if defined(_MSC_VER)
24#define LIBXR_NOINLINE __declspec(noinline)
25#define LIBXR_PACKED_BEGIN __pragma(pack(push, 1))
26#define LIBXR_PACKED_END __pragma(pack(pop))
27#define LIBXR_PACKED
28#elif defined(__clang__) || defined(__GNUC__)
29#define LIBXR_NOINLINE __attribute__((noinline))
30#define LIBXR_PACKED_BEGIN _Pragma("pack(push, 1)")
31#define LIBXR_PACKED_END _Pragma("pack(pop)")
32#define LIBXR_PACKED __attribute__((packed))
33#else
34#warning "LibXR compiler compatibility macros fallback to no-op on unknown compiler"
35#define LIBXR_NOINLINE
36#define LIBXR_PACKED_BEGIN
37#define LIBXR_PACKED_END
38#define LIBXR_PACKED
39#endif
40
41namespace LibXR
42{
44inline constexpr double PI = 3.14159265358979323846;
45
47inline constexpr double TWO_PI = 2.0 * PI;
48
50inline constexpr double STANDARD_GRAVITY = 9.80665;
51
54inline constexpr size_t HW_CACHE_LINE_SIZE = (sizeof(void*) == 8) ? 64 : 32;
55
58#if LIBXR_SINGLE_CORE
59inline constexpr size_t CONCURRENCY_ALIGNMENT = sizeof(size_t);
60#else
61inline constexpr size_t CONCURRENCY_ALIGNMENT = HW_CACHE_LINE_SIZE;
62#endif
63
65inline constexpr size_t CACHE_LINE_SIZE = HW_CACHE_LINE_SIZE;
66
68inline constexpr size_t ALIGN_SIZE = sizeof(void*);
69
73template <typename OwnerType, typename MemberType>
74concept MemberObjectPointer = std::is_member_object_pointer_v<MemberType OwnerType::*>;
75
79template <typename LeftType, typename RightType>
80concept CommonOrdered = std::common_with<LeftType, RightType> &&
81 requires(const LeftType& left, const RightType& right) {
82 { left < right } -> std::convertible_to<bool>;
83 { left > right } -> std::convertible_to<bool>;
84 };
85
93template <typename OwnerType, typename MemberType>
95[[nodiscard]] inline size_t OffsetOf(MemberType OwnerType::* member) noexcept
96{
97 return reinterpret_cast<size_t>(
98 &(reinterpret_cast<const volatile OwnerType*>(0)->*member));
99}
100
109template <typename OwnerType, typename MemberType>
110 requires MemberObjectPointer<OwnerType, MemberType>
111[[nodiscard]] inline OwnerType* ContainerOf(MemberType* ptr,
112 MemberType OwnerType::* member) noexcept
113{
114 return reinterpret_cast<OwnerType*>(reinterpret_cast<std::byte*>(ptr) -
115 OffsetOf(member));
116}
117
118template <typename OwnerType, typename MemberType>
119 requires MemberObjectPointer<OwnerType, MemberType>
120[[nodiscard]] inline const OwnerType* ContainerOf(const MemberType* ptr,
121 MemberType OwnerType::* member) noexcept
122{
123 return reinterpret_cast<const OwnerType*>(reinterpret_cast<const std::byte*>(ptr) -
124 OffsetOf(member));
125}
126
131enum class ErrorCode : int8_t
132{
133 PENDING = 1,
134 OK = 0,
135 FAILED = -1,
136 INIT_ERR = -2,
137 ARG_ERR = -3,
138 STATE_ERR = -4,
139 SIZE_ERR = -5,
140 CHECK_ERR = -6,
141 NOT_SUPPORT = -7,
142 NOT_FOUND = -8,
143 NO_RESPONSE = -9,
144 NO_MEM = -10,
145 NO_BUFF = -11,
146 TIMEOUT = -12,
147 EMPTY = -13,
148 FULL = -14,
149 BUSY = -15,
150 PTR_NULL = -16,
151 OUT_OF_RANGE = -17
152};
153
158enum class SizeLimitMode : uint8_t
159{
160 EQUAL = 0,
161 LESS = 1,
162 MORE = 2,
163 NONE = 3
164};
165
175[[nodiscard]] constexpr bool SizeLimitCheck(SizeLimitMode mode, size_t limit,
176 size_t size) noexcept
177{
178 switch (mode)
179 {
181 return limit == size;
183 return limit >= size;
185 return limit <= size;
187 return true;
188 }
189 return false;
190}
191} // namespace LibXR
192
193#ifdef DEV_ASSERT
194#undef DEV_ASSERT
195#endif
196
197#ifdef DEV_ASSERT_FROM_CALLBACK
198#undef DEV_ASSERT_FROM_CALLBACK
199#endif
200
201#ifdef REQUIRE
202#undef REQUIRE
203#endif
204
205#ifdef REQUIRE_FROM_CALLBACK
206#undef REQUIRE_FROM_CALLBACK
207#endif
208
209#ifdef ASSERT
210#undef ASSERT
211#endif
212
213#ifdef ASSERT_FROM_CALLBACK
214#undef ASSERT_FROM_CALLBACK
215#endif
216
224#ifdef LIBXR_DEBUG_BUILD
225#define ASSERT(arg) \
226 do \
227 { \
228 if (!(arg)) \
229 { \
230 libxr_fatal_error(__FILE__, __LINE__, false); \
231 } \
232 } while (0)
233
241#define ASSERT_FROM_CALLBACK(arg, in_isr) \
242 do \
243 { \
244 if (!(arg)) \
245 { \
246 libxr_fatal_error(__FILE__, __LINE__, (in_isr)); \
247 } \
248 } while (0)
249#else
250#define ASSERT(arg) (void(arg), (void)0)
251#define ASSERT_FROM_CALLBACK(arg, in_isr) \
252 do \
253 { \
254 (void)(arg); \
255 (void)(in_isr); \
256 } while (0)
257#endif
258
266#ifdef LIBXR_DEV_ASSERT_BUILD
267#define DEV_ASSERT(arg) \
268 do \
269 { \
270 if (!(arg)) \
271 { \
272 libxr_fatal_error(__FILE__, __LINE__, false); \
273 } \
274 } while (0)
275
283#define DEV_ASSERT_FROM_CALLBACK(arg, in_isr) \
284 do \
285 { \
286 if (!(arg)) \
287 { \
288 libxr_fatal_error(__FILE__, __LINE__, (in_isr)); \
289 } \
290 } while (0)
291#else
292#define DEV_ASSERT(arg) ((void)0)
293#define DEV_ASSERT_FROM_CALLBACK(arg, in_isr) ((void)0)
294#endif
295
302#define REQUIRE(arg) \
303 do \
304 { \
305 if (!(arg)) \
306 { \
307 libxr_fatal_error(__FILE__, __LINE__, false); \
308 } \
309 } while (0)
310
318#define REQUIRE_FROM_CALLBACK(arg, in_isr) \
319 do \
320 { \
321 if (!(arg)) \
322 { \
323 libxr_fatal_error(__FILE__, __LINE__, (in_isr)); \
324 } \
325 } while (0)
326
333extern "C" void libxr_fatal_error(const char* file, uint32_t line, bool in_isr);
334
335namespace LibXR
336{
345template <typename LeftType, typename RightType>
346 requires CommonOrdered<LeftType, RightType>
347constexpr auto max(LeftType a, RightType b) -> std::common_type_t<LeftType, RightType>
348{
349 return (a > b) ? a : b;
350}
351
360template <typename LeftType, typename RightType>
361 requires CommonOrdered<LeftType, RightType>
362constexpr auto min(LeftType a, RightType b) -> std::common_type_t<LeftType, RightType>
363{
364 return (a < b) ? a : b;
365}
366} // namespace LibXR
具有公共类型且可比较大小的类型对 / Type pair with a common type and ordering
Definition libxr_def.hpp:80
指向非静态数据成员的成员指针 / Pointer to a non-static data member
Definition libxr_def.hpp:74
LibXR 命名空间 / LibXR namespace.
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举 / Defines an enumeration for error codes
@ TIMEOUT
超时 / Timeout
@ SIZE_ERR
尺寸错误 / Size error
@ CHECK_ERR
校验错误 / Check error
@ INIT_ERR
初始化错误 / Initialization error
@ OUT_OF_RANGE
超出范围 / Out of range
@ STATE_ERR
状态错误 / State error
@ BUSY
忙碌 / Busy
@ NOT_FOUND
未找到 / Not found
@ NO_MEM
内存不足 / Insufficient memory
@ NO_RESPONSE
无响应 / No response
@ PTR_NULL
空指针 / Null pointer
@ NO_BUFF
缓冲区不足 / Insufficient buffer
@ NOT_SUPPORT
不支持 / Not supported
@ FAILED
操作失败 / Operation failed
@ EMPTY
为空 / Empty
@ FULL
已满 / Full
@ PENDING
等待中 / Pending
@ OK
操作成功 / Operation successful
@ ARG_ERR
参数错误 / Argument error
constexpr double STANDARD_GRAVITY
标准重力加速度(m/s²) / Standard gravitational acceleration (m/s²)
Definition libxr_def.hpp:50
SizeLimitMode
定义尺寸限制模式 / Defines size limit modes
@ LESS
尺寸必须小于等于 / Size must be less than or equal
@ EQUAL
尺寸必须相等 / Size must be equal
@ NONE
无限制 / No restriction
@ MORE
尺寸必须大于等于 / Size must be greater than or equal
constexpr size_t CONCURRENCY_ALIGNMENT
并发结构对齐粒度(用于降低多核伪共享) / Alignment policy used by concurrency-oriented structures
Definition libxr_def.hpp:61
constexpr auto min(LeftType a, RightType b) -> std::common_type_t< LeftType, RightType >
计算两个数的最小值 / Computes the minimum of two numbers
constexpr size_t CACHE_LINE_SIZE
兼容旧代码的缓存行别名 / Backward-compatible cache-line alias for existing code
Definition libxr_def.hpp:65
constexpr double TWO_PI
2PI 常量 / 2PI constant
Definition libxr_def.hpp:47
constexpr size_t HW_CACHE_LINE_SIZE
真实硬件缓存行大小(用于 DMA / cache coherency) / Hardware cache-line size used for DMA/cache-coherency boundarie...
Definition libxr_def.hpp:54
constexpr bool SizeLimitCheck(SizeLimitMode mode, size_t limit, size_t size) noexcept
尺寸约束的纯判断函数 / Pure predicate for size-limit comparisons
size_t OffsetOf(MemberType OwnerType::*member) noexcept
计算成员在宿主对象中的偏移量 Computes the offset of a member within the owning object
Definition libxr_def.hpp:95
OwnerType * ContainerOf(MemberType *ptr, MemberType OwnerType::*member) noexcept
通过成员指针恢复其所属对象指针 Recover the owning object pointer from a member pointer
constexpr size_t ALIGN_SIZE
平台自然对齐大小 / Native platform alignment size
Definition libxr_def.hpp:68
constexpr auto max(LeftType a, RightType b) -> std::common_type_t< LeftType, RightType >
计算两个数的最大值 / Computes the maximum of two numbers
constexpr double PI
PI 常量 / PI constant.
Definition libxr_def.hpp:44