libxr  1.0
Want to be the best embedded framework
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
17#define UNUSED(_x) ((void)(_x))
18#endif
19
20#ifndef LIBXR_SINGLE_CORE
21#define LIBXR_SINGLE_CORE 0
22#endif
23
24#if defined(_MSC_VER)
25#define LIBXR_NOINLINE __declspec(noinline)
26#define LIBXR_PACKED_BEGIN __pragma(pack(push, 1))
27#define LIBXR_PACKED_END __pragma(pack(pop))
28#define LIBXR_PACKED
29#elif defined(__clang__) || defined(__GNUC__)
30#define LIBXR_NOINLINE __attribute__((noinline))
31#define LIBXR_PACKED_BEGIN _Pragma("pack(push, 1)")
32#define LIBXR_PACKED_END _Pragma("pack(pop)")
33#define LIBXR_PACKED __attribute__((packed))
34#else
35#warning "LibXR compiler compatibility macros fallback to no-op on unknown compiler"
36#define LIBXR_NOINLINE
37#define LIBXR_PACKED_BEGIN
38#define LIBXR_PACKED_END
39#define LIBXR_PACKED
40#endif
41
42namespace LibXR
43{
45inline constexpr double PI = 3.14159265358979323846;
46
48inline constexpr double TWO_PI = 2.0 * PI;
49
51inline constexpr double STANDARD_GRAVITY = 9.80665;
52
55inline constexpr size_t HW_CACHE_LINE_SIZE = (sizeof(void*) == 8) ? 64 : 32;
56
59#if LIBXR_SINGLE_CORE
60inline constexpr size_t CONCURRENCY_ALIGNMENT = sizeof(size_t);
61#else
62inline constexpr size_t CONCURRENCY_ALIGNMENT = HW_CACHE_LINE_SIZE;
63#endif
64
66inline constexpr size_t CACHE_LINE_SIZE = HW_CACHE_LINE_SIZE;
67
69inline constexpr size_t ALIGN_SIZE = sizeof(void*);
70
75template <typename OwnerType, typename MemberType>
76concept MemberObjectPointer = std::is_member_object_pointer_v<MemberType OwnerType::*>;
77
82template <typename LeftType, typename RightType>
83concept CommonOrdered = std::common_with<LeftType, RightType> &&
84 requires(const LeftType& left, const RightType& right) {
85 { left < right } -> std::convertible_to<bool>;
86 { left > right } -> std::convertible_to<bool>;
87 };
88
96template <typename OwnerType, typename MemberType>
98[[nodiscard]] inline size_t OffsetOf(MemberType OwnerType::* member) noexcept
99{
100 return reinterpret_cast<size_t>(
101 &(reinterpret_cast<const volatile OwnerType*>(0)->*member));
102}
103
112template <typename OwnerType, typename MemberType>
113 requires MemberObjectPointer<OwnerType, MemberType>
114[[nodiscard]] inline OwnerType* ContainerOf(MemberType* ptr,
115 MemberType OwnerType::* member) noexcept
116{
117 return reinterpret_cast<OwnerType*>(reinterpret_cast<std::byte*>(ptr) -
118 OffsetOf(member));
119}
120
121template <typename OwnerType, typename MemberType>
122 requires MemberObjectPointer<OwnerType, MemberType>
123[[nodiscard]] inline const OwnerType* ContainerOf(const MemberType* ptr,
124 MemberType OwnerType::* member) noexcept
125{
126 return reinterpret_cast<const OwnerType*>(reinterpret_cast<const std::byte*>(ptr) -
127 OffsetOf(member));
128}
129
135enum class ErrorCode : int8_t
136{
137 PENDING = 1,
138 OK = 0,
139 FAILED = -1,
140 INIT_ERR = -2,
141 ARG_ERR = -3,
142 STATE_ERR = -4,
143 SIZE_ERR = -5,
144 CHECK_ERR = -6,
145 NOT_SUPPORT = -7,
146 NOT_FOUND = -8,
147 NO_RESPONSE = -9,
148 NO_MEM = -10,
149 NO_BUFF = -11,
150 TIMEOUT = -12,
151 EMPTY = -13,
152 FULL = -14,
153 BUSY = -15,
154 PTR_NULL = -16,
155 OUT_OF_RANGE = -17
156};
157
163enum class SizeLimitMode : uint8_t
164{
165 EQUAL = 0,
166 LESS = 1,
167 MORE = 2,
168 NONE = 3
169};
170
181[[nodiscard]] constexpr bool SizeLimitCheck(SizeLimitMode mode, size_t limit,
182 size_t size) noexcept
183{
184 switch (mode)
185 {
187 return limit == size;
189 return limit >= size;
191 return limit <= size;
193 return true;
194 }
195 return false;
196}
197} // namespace LibXR
198
199#ifdef DEV_ASSERT
200#undef DEV_ASSERT
201#endif
202
203#ifdef DEV_ASSERT_FROM_CALLBACK
204#undef DEV_ASSERT_FROM_CALLBACK
205#endif
206
207#ifdef REQUIRE
208#undef REQUIRE
209#endif
210
211#ifdef REQUIRE_FROM_CALLBACK
212#undef REQUIRE_FROM_CALLBACK
213#endif
214
215#ifdef ASSERT
216#undef ASSERT
217#endif
218
219#ifdef ASSERT_FROM_CALLBACK
220#undef ASSERT_FROM_CALLBACK
221#endif
222
228#ifdef LIBXR_DEBUG_BUILD
229#define ASSERT(arg) \
230 do \
231 { \
232 if (!(arg)) \
233 { \
234 libxr_fatal_error(__FILE__, __LINE__, false); \
235 } \
236 } while (0)
237
245#define ASSERT_FROM_CALLBACK(arg, in_isr) \
246 do \
247 { \
248 if (!(arg)) \
249 { \
250 libxr_fatal_error(__FILE__, __LINE__, (in_isr)); \
251 } \
252 } while (0)
253#else
254#define ASSERT(arg) (void(arg), (void)0)
255#define ASSERT_FROM_CALLBACK(arg, in_isr) \
256 do \
257 { \
258 (void)(arg); \
259 (void)(in_isr); \
260 } while (0)
261#endif
262
268#ifdef LIBXR_DEV_ASSERT_BUILD
269#define DEV_ASSERT(arg) \
270 do \
271 { \
272 if (!(arg)) \
273 { \
274 libxr_fatal_error(__FILE__, __LINE__, false); \
275 } \
276 } while (0)
277
285#define DEV_ASSERT_FROM_CALLBACK(arg, in_isr) \
286 do \
287 { \
288 if (!(arg)) \
289 { \
290 libxr_fatal_error(__FILE__, __LINE__, (in_isr)); \
291 } \
292 } while (0)
293#else
294#define DEV_ASSERT(arg) (void(arg), (void)0)
295#define DEV_ASSERT_FROM_CALLBACK(arg, in_isr) \
296 do \
297 { \
298 (void)(arg); \
299 (void)(in_isr); \
300 } while (0)
301#endif
302
308#define REQUIRE(arg) \
309 do \
310 { \
311 if (!(arg)) \
312 { \
313 libxr_fatal_error(__FILE__, __LINE__, false); \
314 } \
315 } while (0)
316
324#define REQUIRE_FROM_CALLBACK(arg, in_isr) \
325 do \
326 { \
327 if (!(arg)) \
328 { \
329 libxr_fatal_error(__FILE__, __LINE__, (in_isr)); \
330 } \
331 } while (0)
332
340extern "C" void libxr_fatal_error(const char* file, uint32_t line, bool in_isr);
341
342namespace LibXR
343{
353template <typename LeftType, typename RightType>
354 requires CommonOrdered<LeftType, RightType>
355constexpr auto max(LeftType a, RightType b) -> std::common_type_t<LeftType, RightType>
356{
357 return (a > b) ? a : b;
358}
359
369template <typename LeftType, typename RightType>
370 requires CommonOrdered<LeftType, RightType>
371constexpr auto min(LeftType a, RightType b) -> std::common_type_t<LeftType, RightType>
372{
373 return (a < b) ? a : b;
374}
375} // namespace LibXR
具有公共类型且可比较大小的类型对
Definition libxr_def.hpp:83
指向非静态数据成员的成员指针
Definition libxr_def.hpp:76
LibXR 命名空间
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举
@ 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:51
SizeLimitMode
定义尺寸限制模式
@ 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:62
constexpr auto min(LeftType a, RightType b) -> std::common_type_t< LeftType, RightType >
计算两个数的最小值
constexpr size_t CACHE_LINE_SIZE
兼容旧代码的缓存行别名 / Backward-compatible cache-line alias for existing code
Definition libxr_def.hpp:66
constexpr double TWO_PI
2PI 常量 / 2PI constant
Definition libxr_def.hpp:48
constexpr size_t HW_CACHE_LINE_SIZE
真实硬件缓存行大小(用于 DMA / cache coherency) / Hardware cache-line size used for DMA/cache-coherency boundarie...
Definition libxr_def.hpp:55
constexpr bool SizeLimitCheck(SizeLimitMode mode, size_t limit, size_t size) noexcept
尺寸约束的纯判断函数
size_t OffsetOf(MemberType OwnerType::*member) noexcept
计算成员在宿主对象中的偏移量
Definition libxr_def.hpp:98
OwnerType * ContainerOf(MemberType *ptr, MemberType OwnerType::*member) noexcept
通过成员指针恢复其所属对象指针
constexpr size_t ALIGN_SIZE
平台自然对齐大小 / Native platform alignment size
Definition libxr_def.hpp:69
constexpr auto max(LeftType a, RightType b) -> std::common_type_t< LeftType, RightType >
计算两个数的最大值
constexpr double PI
PI 常量 / PI constant.
Definition libxr_def.hpp:45