Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_Common/AP_Common.h
Views: 1798
/*1This program is free software: you can redistribute it and/or modify2it under the terms of the GNU General Public License as published by3the Free Software Foundation, either version 3 of the License, or4(at your option) any later version.56This program is distributed in the hope that it will be useful,7but WITHOUT ANY WARRANTY; without even the implied warranty of8MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9GNU General Public License for more details.1011You should have received a copy of the GNU General Public License12along with this program. If not, see <http://www.gnu.org/licenses/>.13*/14///15/// @file AP_Common.h16/// @brief Common definitions and utility routines for the ArduPilot17/// libraries.18///1920#pragma once2122#include <stdint.h>23#include <stdlib.h>24#include <type_traits>25#include <new>2627// used to pack structures28#define PACKED __attribute__((__packed__))2930// used to weaken symbols31#define WEAK __attribute__((__weak__))3233// used to mark a function that may be unused in some builds34#define UNUSED_FUNCTION __attribute__((unused))3536// used to mark an attribute that may be unused in some builds37#ifdef __clang__38#define UNUSED_PRIVATE_MEMBER __attribute__((unused))39#else40#define UNUSED_PRIVATE_MEMBER41#endif4243// this can be used to optimize individual functions44#define OPTIMIZE(level) __attribute__((optimize(level)))4546// sometimes we need to prevent inlining to prevent large stack usage47#ifndef NOINLINE48#define NOINLINE __attribute__((noinline))49#endif5051// used to ignore results for functions marked as warn unused52#define IGNORE_RETURN(x) do {if (x) {}} while(0)5354#define FMT_PRINTF(a,b) __attribute__((format(printf, a, b)))55#define FMT_SCANF(a,b) __attribute__((format(scanf, a, b)))5657// used to forbid copy of objects58#define CLASS_NO_COPY(c) c(const c &other) = delete; c &operator=(const c&) = delete5960#ifdef __has_cpp_attribute61# if __has_cpp_attribute(fallthrough)62# define FALLTHROUGH [[fallthrough]]63# elif __has_cpp_attribute(gnu::fallthrough)64# define FALLTHROUGH [[gnu::fallthrough]]65# endif66#endif67#ifndef FALLTHROUGH68# define FALLTHROUGH69#endif7071#ifdef __GNUC__72#define WARN_IF_UNUSED __attribute__ ((warn_unused_result))73#else74#define WARN_IF_UNUSED75#endif7677#define NORETURN __attribute__ ((noreturn))7879#define ToRad(x) radians(x) // *pi/18080#define ToDeg(x) degrees(x) // *180/pi8182/* Declare and implement const and non-const versions of the array subscript83* operator. The object is treated as an array of type_ values. */84#define DEFINE_BYTE_ARRAY_METHODS \85inline uint8_t &operator[](size_t i) { return reinterpret_cast<uint8_t *>(this)[i]; } \86inline uint8_t operator[](size_t i) const { return reinterpret_cast<const uint8_t *>(this)[i]; }8788/*89check if bit bitnumber is set in value, returned as a90bool. Bitnumber starts at 0 for the first bit91*/92#define BIT_IS_SET(value, bitnumber) (((value) & (1U<<(bitnumber))) != 0)93#define BIT_IS_SET_64(value, bitnumber) (((value) & (uint64_t(1U)<<(bitnumber))) != 0)9495// get high or low bytes from 2 byte integer96#define LOWBYTE(i) ((uint8_t)(i))97#define HIGHBYTE(i) ((uint8_t)(((uint16_t)(i))>>8))9899#define ARRAY_SIZE(_arr) (sizeof(_arr) / sizeof(_arr[0]))100101#define UINT16_VALUE(hbyte, lbyte) (static_cast<uint16_t>(((hbyte)<<8)|(lbyte)))102#define UINT32_VALUE(b3, b2, b1, b0) (static_cast<uint32_t>(((b3)<<24)|((b2)<<16)|((b1)<<8)|(b0)))103104/*105* See UNUSED_RESULT. The difference is that it receives @uniq_ as the name to106* be used for its internal variable.107*108* @uniq_: a unique name to use for variable name109* @expr_: the expression to be evaluated110*/111#define _UNUSED_RESULT(uniq_, expr_) \112do { \113decltype(expr_) uniq_ __attribute__((unused)); \114uniq_ = expr_; \115} while (0)116117/*118* Allow to call a function annotated with warn_unused_result attribute119* without getting a warning, because sometimes this is what we want to do.120*121* @expr_: the expression to be evaluated122*/123#define UNUSED_RESULT(expr_) _UNUSED_RESULT(__unique_name_##__COUNTER__, expr_)124125// @}126127// STR_VALUE returns the string equivalent for the passed cpp macro, so e.g.128// printf("%s", STR_VALUE(EINVAL)); will print "EINVAL"129#define STR_VALUE(x) #x130131// assert_storage_size template: assert that the memory used to store an132// item is of a specific size.133// example invocation:134// assert_storage_size<class Location, 16> _assert_storage_size_Location;135// templates are used for this because the compiler's output will136// usually contain details of the template instantiation so you can137// see how the actual size differs from the expected size.138template<typename s, size_t s_size, size_t t> struct _assert_storage_size {139static_assert(s_size == t, "wrong size");140};141template<typename s, size_t t> struct assert_storage_size {142_assert_storage_size<s, sizeof(s), t> _member;143};144145#define ASSERT_STORAGE_SIZE_JOIN( name, line ) ASSERT_STORAGE_SIZE_DO_JOIN( name, line )146#define ASSERT_STORAGE_SIZE_DO_JOIN( name, line ) name ## line147#define ASSERT_STORAGE_SIZE(structure, size) \148do { assert_storage_size<structure, size> ASSERT_STORAGE_SIZE_JOIN(assert_storage_sizex, __LINE__); (void)ASSERT_STORAGE_SIZE_JOIN(assert_storage_sizex, __LINE__); } while(false)149150////////////////////////////////////////////////////////////////////////////////151/// @name Conversions152///153/// Conversion macros and factors.154///155//@{156157/*158Return true if value is between lower and upper bound inclusive.159False otherwise.160*/161bool is_bounded_int32(int32_t value, int32_t lower_bound, int32_t upper_bound);162163bool hex_to_uint8(uint8_t a, uint8_t &res); // return the uint8 value of an ascii hex character164165/*166strncpy without the warning for not leaving room for nul termination167*/168size_t strncpy_noterm(char *dest, const char *src, size_t n);169170// return the numeric value of an ascii hex character171uint8_t char_to_hex(char a);172173/*174Bit manipulation175*/176//#define BIT_SET(value, bitnumber) ((value) |= (((typeof(value))1U) << (bitnumber)))177template <typename T> void BIT_SET (T& value, uint8_t bitnumber) noexcept {178static_assert(std::is_integral<T>::value, "Integral required.");179((value) |= ((T)(1U) << (bitnumber)));180}181//#define BIT_CLEAR(value, bitnumber) ((value) &= ~(((typeof(value))1U) << (bitnumber)))182template <typename T> void BIT_CLEAR (T& value, uint8_t bitnumber) noexcept {183static_assert(std::is_integral<T>::value, "Integral required.");184((value) &= ~((T)(1U) << (bitnumber)));185}186187/*188See the comments in libraries/AP_Common/c++.cpp189*/190#ifndef NEW_NOTHROW191#define NEW_NOTHROW new(std::nothrow)192#endif193194195196