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 <cstdint>
5#include <libxr_mem.hpp>
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
61enum class ErrorCode : int8_t
62{
63 PENDING = 1,
64 OK = 0,
65 FAILED = -1,
66 INIT_ERR = -2,
67 ARG_ERR = -3,
68 STATE_ERR = -4,
69 SIZE_ERR = -5,
70 CHECK_ERR = -6,
71 NOT_SUPPORT = -7,
72 NOT_FOUND = -8,
73 NO_RESPONSE = -9,
74 NO_MEM = -10,
75 NO_BUFF = -11,
76 TIMEOUT = -12,
77 EMPTY = -13,
78 FULL = -14,
79 BUSY = -15,
80 PTR_NULL = -16,
81 OUT_OF_RANGE = -17
82};
83
89enum class SizeLimitMode : uint8_t
90{
91 EQUAL = 0,
92 LESS = 1,
93 MORE = 2,
94 NONE = 3
95};
96
97#ifdef ASSERT
98#undef ASSERT
99#endif
100
101#ifdef LIBXR_DEBUG_BUILD
107#define ASSERT(arg) \
108 do \
109 { \
110 if (!(arg)) \
111 { \
112 libxr_fatal_error(__FILE__, __LINE__, false); \
113 } \
114 } while (0)
115
123#define ASSERT_FROM_CALLBACK(arg, in_isr) \
124 do \
125 { \
126 if (!(arg)) \
127 { \
128 libxr_fatal_error(__FILE__, __LINE__, (in_isr)); \
129 } \
130 } while (0)
131#else
132#define ASSERT(arg) (void(arg), (void)0)
133#define ASSERT_FROM_CALLBACK(arg, in_isr) \
134 do \
135 { \
136 (void)(arg); \
137 (void)(in_isr); \
138 } while (0)
139#endif
140
148extern "C" void libxr_fatal_error(const char* file, uint32_t line, bool in_isr);
149
150namespace LibXR
151{
152using ErrorCode = ErrorCode;
153using SizeLimitMode = SizeLimitMode;
154
164template <typename T1, typename T2>
165constexpr auto max(T1 a, T2 b) -> typename std::common_type<T1, T2>::type
166{
167 return (a > b) ? a : b;
168}
169
179template <typename T1, typename T2>
180constexpr auto min(T1 a, T2 b) -> typename std::common_type<T1, T2>::type
181{
182 return (a < b) ? a : b;
183}
184} // namespace LibXR
LibXR 命名空间
Definition ch32_can.hpp:14
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
计算两个数的最小值