PX4 Firmware
PX4 Autopilot Software http://px4.io
px4_macros.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2014 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file px4_macros.h
36  *
37  * A set of useful macros for enhanced runtime and compile time
38  * error detection and warning suppression.
39  *
40  * Define NO_BLOAT to reduce bloat from file name inclusion.
41  *
42  * The arraySize() will compute the size of an array regardless
43  * it's type
44  *
45  * INVALID_CASE(c) should be used is case statements to ferret out
46  * unintended behavior
47  *
48  * UNUSED(var) will suppress compile time warnings of unused
49  * variables
50  *
51  * CCASSERT(predicate) Will generate a compile time error it the
52  * predicate is false
53  */
54 #include <assert.h>
55 
56 #ifndef _PX4_MACROS_H
57 #define _PX4_MACROS_H
58 
59 
60 #if !defined(arraySize)
61 #define arraySize(a) (sizeof((a))/sizeof((a[0])))
62 #endif
63 
64 #if !defined(NO_BLOAT)
65 #if defined(__BASE_FILE__)
66 #define _FILE_NAME_ __BASE_FILE__
67 #else
68 #define _FILE_NAME_ __FILE__
69 #endif
70 #else
71 #define _FILE_NAME_ ""
72 #endif
73 
74 #if !defined(INVALID_CASE)
75 #define INVALID_CASE(c) printf("Invalid Case %d, %s:%d",(c),__BASE_FILE__,__LINE__) /* todo use PANIC */
76 #endif
77 
78 #if !defined(UNUSED)
79 #define UNUSED(var) (void)(var)
80 #endif
81 
82 #if !defined(CAT)
83 #if !defined(_CAT)
84 #define _CAT(a, b) a ## b
85 #endif
86 #define CAT(a, b) _CAT(a, b)
87 #endif
88 
89 #if !defined(FREEZE_STR)
90 # define FREEZE_STR(s) #s
91 #endif
92 
93 #if !defined(STRINGIFY)
94 #define STRINGIFY(s) FREEZE_STR(s)
95 #endif
96 
97 #if !defined(CCASSERT)
98 #if defined(static_assert)
99 # define CCASSERT(predicate) static_assert(predicate, STRINGIFY(predicate))
100 # else
101 # define CCASSERT(predicate) _x_CCASSERT_LINE(predicate, __LINE__)
102 # if !defined(_x_CCASSERT_LINE)
103 # define _x_CCASSERT_LINE(predicate, line) typedef char CAT(constraint_violated_on_line_,line)[2*((predicate)!=0)-1] __attribute__ ((unused)) ;
104 # endif
105 # endif
106 #endif
107 
108 #if !defined(DO_PRAGMA)
109 # define DO_PRAGMA(x) _Pragma (#x)
110 #endif
111 
112 #if !defined(TODO)
113 # define TODO(x) DO_PRAGMA(message ("TODO - " #x))
114 #endif
115 
116 #endif /* _PX4_MACROS_H */