libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
libxr_def.hpp
1#pragma once
2
3#include <concepts>
4#include <cmath>
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#if defined(_MSC_VER)
21#define LIBXR_FORCE_OPTIMIZE_O3
22#define LIBXR_FORCE_OPTIMIZE_OS
23#define LIBXR_NOINLINE __declspec(noinline)
24#define LIBXR_PACKED_BEGIN __pragma(pack(push, 1))
25#define LIBXR_PACKED_END __pragma(pack(pop))
26#define LIBXR_PACKED
27#elif defined(__clang__) || defined(__GNUC__)
28#define LIBXR_FORCE_OPTIMIZE_O3 __attribute__((optimize("O3")))
29#define LIBXR_FORCE_OPTIMIZE_OS __attribute__((optimize("Os")))
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_FORCE_OPTIMIZE_O3
37#define LIBXR_FORCE_OPTIMIZE_OS
38#define LIBXR_NOINLINE
39#define LIBXR_PACKED_BEGIN
40#define LIBXR_PACKED_END
41#define LIBXR_PACKED
42#endif
43
44namespace LibXR
45{
47inline constexpr double PI = 3.14159265358979323846;
48
50inline constexpr double TWO_PI = 2.0 * PI;
51
53inline constexpr double STANDARD_GRAVITY = 9.80665;
54
56inline constexpr size_t CACHE_LINE_SIZE = (sizeof(void*) == 8) ? 64 : 32;
57
59inline constexpr size_t ALIGN_SIZE = sizeof(void*);
60
65template <typename OwnerType, typename MemberType>
66concept MemberObjectPointer = std::is_member_object_pointer_v<MemberType OwnerType::*>;
67
72template <typename LeftType, typename RightType>
73concept CommonOrdered = std::common_with<LeftType, RightType> &&
74 requires(const LeftType& left, const RightType& right)
75{
76 { left < right } -> std::convertible_to<bool>;
77 { left > right } -> std::convertible_to<bool>;
78};
79
86template <typename OwnerType, typename MemberType>
88[[nodiscard]] inline size_t OffsetOf(MemberType OwnerType::*member) noexcept
89{
90 return reinterpret_cast<size_t>(
91 &(reinterpret_cast<const volatile OwnerType*>(0)->*member));
92}
93
101template <typename OwnerType, typename MemberType>
102requires MemberObjectPointer<OwnerType, MemberType>
103[[nodiscard]] inline OwnerType* ContainerOf(MemberType* ptr,
104 MemberType OwnerType::*member) noexcept
105{
106 return reinterpret_cast<OwnerType*>(reinterpret_cast<std::byte*>(ptr) -
107 OffsetOf(member));
108}
109
110template <typename OwnerType, typename MemberType>
111requires MemberObjectPointer<OwnerType, MemberType>
112[[nodiscard]] inline const OwnerType* ContainerOf(const MemberType* ptr,
113 MemberType OwnerType::*member) noexcept
114{
115 return reinterpret_cast<const OwnerType*>(reinterpret_cast<const std::byte*>(ptr) -
116 OffsetOf(member));
117}
118
124enum class ErrorCode : int8_t
125{
126 PENDING = 1,
127 OK = 0,
128 FAILED = -1,
129 INIT_ERR = -2,
130 ARG_ERR = -3,
131 STATE_ERR = -4,
132 SIZE_ERR = -5,
133 CHECK_ERR = -6,
134 NOT_SUPPORT = -7,
135 NOT_FOUND = -8,
136 NO_RESPONSE = -9,
137 NO_MEM = -10,
138 NO_BUFF = -11,
139 TIMEOUT = -12,
140 EMPTY = -13,
141 FULL = -14,
142 BUSY = -15,
143 PTR_NULL = -16,
144 OUT_OF_RANGE = -17
145};
146
152enum class SizeLimitMode : uint8_t
153{
154 EQUAL = 0,
155 LESS = 1,
156 MORE = 2,
157 NONE = 3
158};
159
170[[nodiscard]] constexpr bool SizeLimitCheck(SizeLimitMode mode, size_t limit,
171 size_t size) noexcept
172{
173 switch (mode)
174 {
176 return limit == size;
178 return limit >= size;
180 return limit <= size;
182 return true;
183 }
184 return false;
185}
186} // namespace LibXR
187
188#ifdef DEV_ASSERT
189#undef DEV_ASSERT
190#endif
191
192#ifdef DEV_ASSERT_FROM_CALLBACK
193#undef DEV_ASSERT_FROM_CALLBACK
194#endif
195
196#ifdef REQUIRE
197#undef REQUIRE
198#endif
199
200#ifdef REQUIRE_FROM_CALLBACK
201#undef REQUIRE_FROM_CALLBACK
202#endif
203
204#ifdef ASSERT
205#undef ASSERT
206#endif
207
208#ifdef ASSERT_FROM_CALLBACK
209#undef ASSERT_FROM_CALLBACK
210#endif
211
217#ifdef LIBXR_DEBUG_BUILD
218#define ASSERT(arg) \
219 do \
220 { \
221 if (!(arg)) \
222 { \
223 libxr_fatal_error(__FILE__, __LINE__, false); \
224 } \
225 } while (0)
226
234#define ASSERT_FROM_CALLBACK(arg, in_isr) \
235 do \
236 { \
237 if (!(arg)) \
238 { \
239 libxr_fatal_error(__FILE__, __LINE__, (in_isr)); \
240 } \
241 } while (0)
242#else
243#define ASSERT(arg) (void(arg), (void)0)
244#define ASSERT_FROM_CALLBACK(arg, in_isr) \
245 do \
246 { \
247 (void)(arg); \
248 (void)(in_isr); \
249 } while (0)
250#endif
251
257#ifdef LIBXR_DEV_ASSERT_BUILD
258#define DEV_ASSERT(arg) \
259 do \
260 { \
261 if (!(arg)) \
262 { \
263 libxr_fatal_error(__FILE__, __LINE__, false); \
264 } \
265 } while (0)
266
274#define DEV_ASSERT_FROM_CALLBACK(arg, in_isr) \
275 do \
276 { \
277 if (!(arg)) \
278 { \
279 libxr_fatal_error(__FILE__, __LINE__, (in_isr)); \
280 } \
281 } while (0)
282#else
283#define DEV_ASSERT(arg) (void(arg), (void)0)
284#define DEV_ASSERT_FROM_CALLBACK(arg, in_isr) \
285 do \
286 { \
287 (void)(arg); \
288 (void)(in_isr); \
289 } while (0)
290#endif
291
297#define REQUIRE(arg) \
298 do \
299 { \
300 if (!(arg)) \
301 { \
302 libxr_fatal_error(__FILE__, __LINE__, false); \
303 } \
304 } while (0)
305
313#define REQUIRE_FROM_CALLBACK(arg, in_isr) \
314 do \
315 { \
316 if (!(arg)) \
317 { \
318 libxr_fatal_error(__FILE__, __LINE__, (in_isr)); \
319 } \
320 } while (0)
321
329extern "C" void libxr_fatal_error(const char* file, uint32_t line, bool in_isr);
330
331namespace LibXR
332{
342template <typename LeftType, typename RightType>
343requires CommonOrdered<LeftType, RightType>
344constexpr auto max(LeftType a, RightType b) -> std::common_type_t<LeftType, RightType>
345{
346 return (a > b) ? a : b;
347}
348
358template <typename LeftType, typename RightType>
359requires CommonOrdered<LeftType, RightType>
360constexpr auto min(LeftType a, RightType b) -> std::common_type_t<LeftType, RightType>
361{
362 return (a < b) ? a : b;
363}
364} // namespace LibXR
具有公共类型且可比较大小的类型对
Definition libxr_def.hpp:73
指向非静态数据成员的成员指针
Definition libxr_def.hpp:66
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:53
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 auto min(LeftType a, RightType b) -> std::common_type_t< LeftType, RightType >
计算两个数的最小值
constexpr size_t CACHE_LINE_SIZE
缓存行大小 / Cache line size
Definition libxr_def.hpp:56
constexpr double TWO_PI
2PI 常量 / 2PI constant
Definition libxr_def.hpp:50
constexpr bool SizeLimitCheck(SizeLimitMode mode, size_t limit, size_t size) noexcept
尺寸约束的纯判断函数
size_t OffsetOf(MemberType OwnerType::*member) noexcept
计算成员在宿主对象中的偏移量
Definition libxr_def.hpp:88
OwnerType * ContainerOf(MemberType *ptr, MemberType OwnerType::*member) noexcept
通过成员指针恢复其所属对象指针
constexpr size_t ALIGN_SIZE
平台自然对齐大小 / Native platform alignment size
Definition libxr_def.hpp:59
constexpr auto max(LeftType a, RightType b) -> std::common_type_t< LeftType, RightType >
计算两个数的最大值
constexpr double PI
PI 常量 / PI constant.
Definition libxr_def.hpp:47