1 /*************************************************************************
3 * Copyright 2016 Realm Inc.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 **************************************************************************/
19 #ifndef REALM_UTIL_ASSERT_HPP
20 #define REALM_UTIL_ASSERT_HPP
22 #include <realm/util/features.h>
23 #include <realm/util/terminate.hpp>
25 #if REALM_ENABLE_ASSERTIONS || defined(REALM_DEBUG)
26 #define REALM_ASSERTIONS_ENABLED 1
28 #define REALM_ASSERTIONS_ENABLED 0
31 #define REALM_ASSERT_RELEASE(condition) \
32 (REALM_LIKELY(condition) ? static_cast<void>(0) \
33 : realm::util::terminate("Assertion failed: " #condition, __FILE__, __LINE__))
35 #if REALM_ASSERTIONS_ENABLED
36 #define REALM_ASSERT(condition) REALM_ASSERT_RELEASE(condition)
38 #define REALM_ASSERT(condition) static_cast<void>(sizeof bool(condition))
42 #define REALM_ASSERT_DEBUG(condition) REALM_ASSERT_RELEASE(condition)
44 #define REALM_ASSERT_DEBUG(condition) static_cast<void>(sizeof bool(condition))
47 #define REALM_STRINGIFY(X) #X
49 #define REALM_ASSERT_RELEASE_EX(condition, ...) \
50 (REALM_LIKELY(condition) ? static_cast<void>(0) \
51 : realm::util::terminate_with_info("Assertion failed: " #condition, __LINE__, __FILE__, \
52 REALM_STRINGIFY((__VA_ARGS__)), __VA_ARGS__))
55 #define REALM_ASSERT_DEBUG_EX REALM_ASSERT_RELEASE_EX
57 #define REALM_ASSERT_DEBUG_EX(condition, ...) static_cast<void>(sizeof bool(condition))
60 // Becase the assert is used in noexcept methods, it's a bad idea to allocate
61 // buffer space for the message so therefore we must pass it to terminate which
62 // will 'cerr' it for us without needing any buffer
63 #if REALM_ENABLE_ASSERTIONS || defined(REALM_DEBUG)
65 #define REALM_ASSERT_EX REALM_ASSERT_RELEASE_EX
67 #define REALM_ASSERT_3(left, cmp, right) \
68 (REALM_LIKELY((left)cmp(right)) ? static_cast<void>(0) \
69 : realm::util::terminate("Assertion failed: " \
70 "" #left " " #cmp " " #right, \
71 __FILE__, __LINE__, left, right))
73 #define REALM_ASSERT_7(left1, cmp1, right1, logical, left2, cmp2, right2) \
74 (REALM_LIKELY(((left1)cmp1(right1))logical((left2)cmp2(right2))) \
75 ? static_cast<void>(0) \
76 : realm::util::terminate("Assertion failed: " \
77 "" #left1 " " #cmp1 " " #right1 " " #logical " " \
78 "" #left2 " " #cmp2 " " #right2, \
79 __FILE__, __LINE__, left1, right1, left2, right2))
81 #define REALM_ASSERT_11(left1, cmp1, right1, logical1, left2, cmp2, right2, logical2, left3, cmp3, right3) \
82 (REALM_LIKELY(((left1)cmp1(right1))logical1((left2)cmp2(right2)) logical2((left3)cmp3(right3))) \
83 ? static_cast<void>(0) \
84 : realm::util::terminate("Assertion failed: " \
85 "" #left1 " " #cmp1 " " #right1 " " #logical1 " " \
86 "" #left2 " " #cmp2 " " #right2 " " #logical2 " " \
87 "" #left3 " " #cmp3 " " #right3, \
88 __FILE__, __LINE__, left1, right1, left2, right2, left3, right3))
90 #define REALM_ASSERT_EX(condition, ...) static_cast<void>(sizeof bool(condition))
91 #define REALM_ASSERT_3(left, cmp, right) static_cast<void>(sizeof bool((left)cmp(right)))
92 #define REALM_ASSERT_7(left1, cmp1, right1, logical, left2, cmp2, right2) \
93 static_cast<void>(sizeof bool(((left1)cmp1(right1))logical((left2)cmp2(right2))))
94 #define REALM_ASSERT_11(left1, cmp1, right1, logical1, left2, cmp2, right2, logical2, left3, cmp3, right3) \
95 static_cast<void>(sizeof bool(((left1)cmp1(right1))logical1((left2)cmp2(right2)) logical2((left3)cmp3(right3))))
98 #define REALM_UNREACHABLE() realm::util::terminate("Unreachable code", __FILE__, __LINE__)
100 #define REALM_COVER_NEVER(x) false
101 #define REALM_COVER_ALWAYS(x) true
103 #define REALM_COVER_NEVER(x) (x)
104 #define REALM_COVER_ALWAYS(x) (x)
107 #endif // REALM_UTIL_ASSERT_HPP