1 |
|
---|
2 | /** \addtogroup platform */
|
---|
3 | /** @{*/
|
---|
4 | /**
|
---|
5 | * \defgroup platform_Assert Assert macros
|
---|
6 | * @{
|
---|
7 | */
|
---|
8 | /* mbed Microcontroller Library
|
---|
9 | * Copyright (c) 2006-2013 ARM Limited
|
---|
10 | *
|
---|
11 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
12 | * you may not use this file except in compliance with the License.
|
---|
13 | * You may obtain a copy of the License at
|
---|
14 | *
|
---|
15 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
16 | *
|
---|
17 | * Unless required by applicable law or agreed to in writing, software
|
---|
18 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
20 | * See the License for the specific language governing permissions and
|
---|
21 | * limitations under the License.
|
---|
22 | */
|
---|
23 | #ifndef MBED_ASSERT_H
|
---|
24 | #define MBED_ASSERT_H
|
---|
25 |
|
---|
26 | #include "mbed_preprocessor.h"
|
---|
27 |
|
---|
28 | #ifdef __cplusplus
|
---|
29 | extern "C" {
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | /** Internal mbed assert function which is invoked when MBED_ASSERT macro failes.
|
---|
33 | * This function is active only if NDEBUG is not defined prior to including this
|
---|
34 | * assert header file.
|
---|
35 | * In case of MBED_ASSERT failing condition, error() is called with the assertation message.
|
---|
36 | * @param expr Expresion to be checked.
|
---|
37 | * @param file File where assertation failed.
|
---|
38 | * @param line Failing assertation line number.
|
---|
39 | */
|
---|
40 | void mbed_assert_internal(const char *expr, const char *file, int line);
|
---|
41 |
|
---|
42 | #ifdef __cplusplus
|
---|
43 | }
|
---|
44 | #endif
|
---|
45 |
|
---|
46 | /** MBED_ASSERT
|
---|
47 | * Declare runtime assertions: results in runtime error if condition is false
|
---|
48 | *
|
---|
49 | * @note
|
---|
50 | * Use of MBED_ASSERT is limited to Debug and Develop builds.
|
---|
51 | *
|
---|
52 | * @code
|
---|
53 | *
|
---|
54 | * int Configure(serial_t *obj) {
|
---|
55 | * MBED_ASSERT(obj);
|
---|
56 | * }
|
---|
57 | * @endcode
|
---|
58 | */
|
---|
59 | #ifdef NDEBUG
|
---|
60 | #define MBED_ASSERT(expr) ((void)0)
|
---|
61 |
|
---|
62 | #else
|
---|
63 | #define MBED_ASSERT(expr) \
|
---|
64 | do { \
|
---|
65 | if (!(expr)) { \
|
---|
66 | mbed_assert_internal(#expr, __FILE__, __LINE__); \
|
---|
67 | } \
|
---|
68 | } while (0)
|
---|
69 | #endif
|
---|
70 |
|
---|
71 |
|
---|
72 | /** MBED_STATIC_ASSERT
|
---|
73 | * Declare compile-time assertions, results in compile-time error if condition is false
|
---|
74 | *
|
---|
75 | * The assertion acts as a declaration that can be placed at file scope, in a
|
---|
76 | * code block (except after a label), or as a member of a C++ class/struct/union.
|
---|
77 | *
|
---|
78 | * @note
|
---|
79 | * Use of MBED_STATIC_ASSERT as a member of a struct/union is limited:
|
---|
80 | * - In C++, MBED_STATIC_ASSERT is valid in class/struct/union scope.
|
---|
81 | * - In C, MBED_STATIC_ASSERT is not valid in struct/union scope, and
|
---|
82 | * MBED_STRUCT_STATIC_ASSERT is provided as an alternative that is valid
|
---|
83 | * in C and C++ class/struct/union scope.
|
---|
84 | *
|
---|
85 | * @code
|
---|
86 | * MBED_STATIC_ASSERT(MBED_LIBRARY_VERSION >= 120,
|
---|
87 | * "The mbed library must be at least version 120");
|
---|
88 | *
|
---|
89 | * int main() {
|
---|
90 | * MBED_STATIC_ASSERT(sizeof(int) >= sizeof(char),
|
---|
91 | * "An int must be larger than a char");
|
---|
92 | * }
|
---|
93 | * @endcode
|
---|
94 | */
|
---|
95 | #if defined(__cplusplus) && (__cplusplus >= 201103L || __cpp_static_assert >= 200410L)
|
---|
96 | #define MBED_STATIC_ASSERT(expr, msg) static_assert(expr, msg)
|
---|
97 | #elif !defined(__cplusplus) && __STDC_VERSION__ >= 201112L
|
---|
98 | #define MBED_STATIC_ASSERT(expr, msg) _Static_assert(expr, msg)
|
---|
99 | #elif defined(__cplusplus) && defined(__GNUC__) && defined(__GXX_EXPERIMENTAL_CXX0X__) \
|
---|
100 | && (__GNUC__*100 + __GNUC_MINOR__) > 403L
|
---|
101 | #define MBED_STATIC_ASSERT(expr, msg) __extension__ static_assert(expr, msg)
|
---|
102 | #elif !defined(__cplusplus) && defined(__GNUC__) && !defined(__CC_ARM) \
|
---|
103 | && (__GNUC__*100 + __GNUC_MINOR__) > 406L
|
---|
104 | #define MBED_STATIC_ASSERT(expr, msg) __extension__ _Static_assert(expr, msg)
|
---|
105 | #elif defined(__ICCARM__)
|
---|
106 | #define MBED_STATIC_ASSERT(expr, msg) static_assert(expr, msg)
|
---|
107 | #else
|
---|
108 | #define MBED_STATIC_ASSERT(expr, msg) \
|
---|
109 | enum {MBED_CONCAT(MBED_ASSERTION_AT_, __LINE__) = sizeof(char[(expr) ? 1 : -1])}
|
---|
110 | #endif
|
---|
111 |
|
---|
112 | /** MBED_STRUCT_STATIC_ASSERT
|
---|
113 | * Declare compile-time assertions, results in compile-time error if condition is false
|
---|
114 | *
|
---|
115 | * Unlike MBED_STATIC_ASSERT, MBED_STRUCT_STATIC_ASSERT can and must be used
|
---|
116 | * as a member of a C/C++ class/struct/union.
|
---|
117 | *
|
---|
118 | * @code
|
---|
119 | * struct thing {
|
---|
120 | * MBED_STATIC_ASSERT(2 + 2 == 4,
|
---|
121 | * "Hopefully the universe is mathematically consistent");
|
---|
122 | * };
|
---|
123 | * @endcode
|
---|
124 | */
|
---|
125 | #define MBED_STRUCT_STATIC_ASSERT(expr, msg) int : (expr) ? 0 : -1
|
---|
126 |
|
---|
127 |
|
---|
128 | #endif
|
---|
129 |
|
---|
130 | /** @}*/
|
---|
131 |
|
---|
132 | /**@}*/
|
---|
133 |
|
---|