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 <cstddef>
5#include <cstdint>
6#include <type_traits>
7
8#ifndef M_PI
9#define M_PI 3.14159265358979323846
10#endif
11
12#ifndef M_2PI
13#define M_2PI (2.0 * M_PI)
14#endif
15
16#ifndef M_1G
19constexpr double M_1G = 9.80665;
20#endif
21
22#ifndef DEF2STR
23#define XR_TO_STR(_arg) #_arg
24#define DEF2STR(_arg) XR_TO_STR(_arg)
25#endif
26
27#ifndef UNUSED
30#define UNUSED(_x) ((void)(_x))
31#endif
32
33#ifndef OFFSET_OF
36#define OFFSET_OF(type, member) ((size_t)&((type*)0)->member)
37#endif
38
39#ifndef MEMBER_SIZE_OF
42#define MEMBER_SIZE_OF(type, member) (sizeof(decltype(((type*)0)->member)))
43#endif
44
45#ifndef CONTAINER_OF
48#define CONTAINER_OF(ptr, type, member) \
49 ((type*)((char*)(ptr) - OFFSET_OF(type, member))) // NOLINT
50#endif
51
53static constexpr size_t LIBXR_CACHE_LINE_SIZE = (sizeof(void*) == 8) ? 64 : 32;
54static constexpr size_t LIBXR_ALIGN_SIZE = (sizeof(void*));
55
56namespace LibXR
57{
63enum class ErrorCode : int8_t
64{
65 PENDING = 1,
66 OK = 0,
67 FAILED = -1,
68 INIT_ERR = -2,
69 ARG_ERR = -3,
70 STATE_ERR = -4,
71 SIZE_ERR = -5,
72 CHECK_ERR = -6,
73 NOT_SUPPORT = -7,
74 NOT_FOUND = -8,
75 NO_RESPONSE = -9,
76 NO_MEM = -10,
77 NO_BUFF = -11,
78 TIMEOUT = -12,
79 EMPTY = -13,
80 FULL = -14,
81 BUSY = -15,
82 PTR_NULL = -16,
83 OUT_OF_RANGE = -17
84};
85
91enum class SizeLimitMode : uint8_t
92{
93 EQUAL = 0,
94 LESS = 1,
95 MORE = 2,
96 NONE = 3
97};
98} // namespace LibXR
99
100#ifdef ASSERT
101#undef ASSERT
102#endif
103
104#ifdef ASSERT_FROM_CALLBACK
105#undef ASSERT_FROM_CALLBACK
106#endif
107
108#ifdef LIBXR_DEBUG_BUILD
114#define ASSERT(arg) \
115 do \
116 { \
117 if (!(arg)) \
118 { \
119 libxr_fatal_error(__FILE__, __LINE__, false); \
120 } \
121 } while (0)
122
130#define ASSERT_FROM_CALLBACK(arg, in_isr) \
131 do \
132 { \
133 if (!(arg)) \
134 { \
135 libxr_fatal_error(__FILE__, __LINE__, (in_isr)); \
136 } \
137 } while (0)
138#else
139#define ASSERT(arg) (void(arg), (void)0)
140#define ASSERT_FROM_CALLBACK(arg, in_isr) \
141 do \
142 { \
143 (void)(arg); \
144 (void)(in_isr); \
145 } while (0)
146#endif
147
155extern "C" void libxr_fatal_error(const char* file, uint32_t line, bool in_isr);
156
157namespace LibXR
158{
168template <typename T1, typename T2>
169constexpr auto max(T1 a, T2 b) -> typename std::common_type<T1, T2>::type
170{
171 return (a > b) ? a : b;
172}
173
183template <typename T1, typename T2>
184constexpr auto min(T1 a, T2 b) -> typename std::common_type<T1, T2>::type
185{
186 return (a < b) ? a : b;
187}
188} // namespace LibXR
LibXR 命名空间
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举
Definition libxr_def.hpp:64
@ 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
SizeLimitMode
定义尺寸限制模式
Definition libxr_def.hpp:92
@ LESS
尺寸必须小于 | Size must be less
@ EQUAL
尺寸必须相等 | Size must be equal
@ MORE
尺寸必须大于 | Size must be more
constexpr auto max(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最大值
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值