CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_Common/AP_Common.h
Views: 1798
1
/*
2
This program is free software: you can redistribute it and/or modify
3
it under the terms of the GNU General Public License as published by
4
the Free Software Foundation, either version 3 of the License, or
5
(at your option) any later version.
6
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
11
12
You should have received a copy of the GNU General Public License
13
along with this program. If not, see <http://www.gnu.org/licenses/>.
14
*/
15
///
16
/// @file AP_Common.h
17
/// @brief Common definitions and utility routines for the ArduPilot
18
/// libraries.
19
///
20
21
#pragma once
22
23
#include <stdint.h>
24
#include <stdlib.h>
25
#include <type_traits>
26
#include <new>
27
28
// used to pack structures
29
#define PACKED __attribute__((__packed__))
30
31
// used to weaken symbols
32
#define WEAK __attribute__((__weak__))
33
34
// used to mark a function that may be unused in some builds
35
#define UNUSED_FUNCTION __attribute__((unused))
36
37
// used to mark an attribute that may be unused in some builds
38
#ifdef __clang__
39
#define UNUSED_PRIVATE_MEMBER __attribute__((unused))
40
#else
41
#define UNUSED_PRIVATE_MEMBER
42
#endif
43
44
// this can be used to optimize individual functions
45
#define OPTIMIZE(level) __attribute__((optimize(level)))
46
47
// sometimes we need to prevent inlining to prevent large stack usage
48
#ifndef NOINLINE
49
#define NOINLINE __attribute__((noinline))
50
#endif
51
52
// used to ignore results for functions marked as warn unused
53
#define IGNORE_RETURN(x) do {if (x) {}} while(0)
54
55
#define FMT_PRINTF(a,b) __attribute__((format(printf, a, b)))
56
#define FMT_SCANF(a,b) __attribute__((format(scanf, a, b)))
57
58
// used to forbid copy of objects
59
#define CLASS_NO_COPY(c) c(const c &other) = delete; c &operator=(const c&) = delete
60
61
#ifdef __has_cpp_attribute
62
# if __has_cpp_attribute(fallthrough)
63
# define FALLTHROUGH [[fallthrough]]
64
# elif __has_cpp_attribute(gnu::fallthrough)
65
# define FALLTHROUGH [[gnu::fallthrough]]
66
# endif
67
#endif
68
#ifndef FALLTHROUGH
69
# define FALLTHROUGH
70
#endif
71
72
#ifdef __GNUC__
73
#define WARN_IF_UNUSED __attribute__ ((warn_unused_result))
74
#else
75
#define WARN_IF_UNUSED
76
#endif
77
78
#define NORETURN __attribute__ ((noreturn))
79
80
#define ToRad(x) radians(x) // *pi/180
81
#define ToDeg(x) degrees(x) // *180/pi
82
83
/* Declare and implement const and non-const versions of the array subscript
84
* operator. The object is treated as an array of type_ values. */
85
#define DEFINE_BYTE_ARRAY_METHODS \
86
inline uint8_t &operator[](size_t i) { return reinterpret_cast<uint8_t *>(this)[i]; } \
87
inline uint8_t operator[](size_t i) const { return reinterpret_cast<const uint8_t *>(this)[i]; }
88
89
/*
90
check if bit bitnumber is set in value, returned as a
91
bool. Bitnumber starts at 0 for the first bit
92
*/
93
#define BIT_IS_SET(value, bitnumber) (((value) & (1U<<(bitnumber))) != 0)
94
#define BIT_IS_SET_64(value, bitnumber) (((value) & (uint64_t(1U)<<(bitnumber))) != 0)
95
96
// get high or low bytes from 2 byte integer
97
#define LOWBYTE(i) ((uint8_t)(i))
98
#define HIGHBYTE(i) ((uint8_t)(((uint16_t)(i))>>8))
99
100
#define ARRAY_SIZE(_arr) (sizeof(_arr) / sizeof(_arr[0]))
101
102
#define UINT16_VALUE(hbyte, lbyte) (static_cast<uint16_t>(((hbyte)<<8)|(lbyte)))
103
#define UINT32_VALUE(b3, b2, b1, b0) (static_cast<uint32_t>(((b3)<<24)|((b2)<<16)|((b1)<<8)|(b0)))
104
105
/*
106
* See UNUSED_RESULT. The difference is that it receives @uniq_ as the name to
107
* be used for its internal variable.
108
*
109
* @uniq_: a unique name to use for variable name
110
* @expr_: the expression to be evaluated
111
*/
112
#define _UNUSED_RESULT(uniq_, expr_) \
113
do { \
114
decltype(expr_) uniq_ __attribute__((unused)); \
115
uniq_ = expr_; \
116
} while (0)
117
118
/*
119
* Allow to call a function annotated with warn_unused_result attribute
120
* without getting a warning, because sometimes this is what we want to do.
121
*
122
* @expr_: the expression to be evaluated
123
*/
124
#define UNUSED_RESULT(expr_) _UNUSED_RESULT(__unique_name_##__COUNTER__, expr_)
125
126
// @}
127
128
// STR_VALUE returns the string equivalent for the passed cpp macro, so e.g.
129
// printf("%s", STR_VALUE(EINVAL)); will print "EINVAL"
130
#define STR_VALUE(x) #x
131
132
// assert_storage_size template: assert that the memory used to store an
133
// item is of a specific size.
134
// example invocation:
135
// assert_storage_size<class Location, 16> _assert_storage_size_Location;
136
// templates are used for this because the compiler's output will
137
// usually contain details of the template instantiation so you can
138
// see how the actual size differs from the expected size.
139
template<typename s, size_t s_size, size_t t> struct _assert_storage_size {
140
static_assert(s_size == t, "wrong size");
141
};
142
template<typename s, size_t t> struct assert_storage_size {
143
_assert_storage_size<s, sizeof(s), t> _member;
144
};
145
146
#define ASSERT_STORAGE_SIZE_JOIN( name, line ) ASSERT_STORAGE_SIZE_DO_JOIN( name, line )
147
#define ASSERT_STORAGE_SIZE_DO_JOIN( name, line ) name ## line
148
#define ASSERT_STORAGE_SIZE(structure, size) \
149
do { assert_storage_size<structure, size> ASSERT_STORAGE_SIZE_JOIN(assert_storage_sizex, __LINE__); (void)ASSERT_STORAGE_SIZE_JOIN(assert_storage_sizex, __LINE__); } while(false)
150
151
////////////////////////////////////////////////////////////////////////////////
152
/// @name Conversions
153
///
154
/// Conversion macros and factors.
155
///
156
//@{
157
158
/*
159
Return true if value is between lower and upper bound inclusive.
160
False otherwise.
161
*/
162
bool is_bounded_int32(int32_t value, int32_t lower_bound, int32_t upper_bound);
163
164
bool hex_to_uint8(uint8_t a, uint8_t &res); // return the uint8 value of an ascii hex character
165
166
/*
167
strncpy without the warning for not leaving room for nul termination
168
*/
169
size_t strncpy_noterm(char *dest, const char *src, size_t n);
170
171
// return the numeric value of an ascii hex character
172
uint8_t char_to_hex(char a);
173
174
/*
175
Bit manipulation
176
*/
177
//#define BIT_SET(value, bitnumber) ((value) |= (((typeof(value))1U) << (bitnumber)))
178
template <typename T> void BIT_SET (T& value, uint8_t bitnumber) noexcept {
179
static_assert(std::is_integral<T>::value, "Integral required.");
180
((value) |= ((T)(1U) << (bitnumber)));
181
}
182
//#define BIT_CLEAR(value, bitnumber) ((value) &= ~(((typeof(value))1U) << (bitnumber)))
183
template <typename T> void BIT_CLEAR (T& value, uint8_t bitnumber) noexcept {
184
static_assert(std::is_integral<T>::value, "Integral required.");
185
((value) &= ~((T)(1U) << (bitnumber)));
186
}
187
188
/*
189
See the comments in libraries/AP_Common/c++.cpp
190
*/
191
#ifndef NEW_NOTHROW
192
#define NEW_NOTHROW new(std::nothrow)
193
#endif
194
195
196