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