assert.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*************************************************************************
  2. *
  3. * Copyright 2016 Realm Inc.
  4. *
  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
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  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.
  16. *
  17. **************************************************************************/
  18. #ifndef REALM_UTIL_ASSERT_HPP
  19. #define REALM_UTIL_ASSERT_HPP
  20. #include <realm/util/features.h>
  21. #include <realm/util/terminate.hpp>
  22. #if REALM_ENABLE_ASSERTIONS || defined(REALM_DEBUG)
  23. #define REALM_ASSERTIONS_ENABLED 1
  24. #else
  25. #define REALM_ASSERTIONS_ENABLED 0
  26. #endif
  27. #define REALM_ASSERT_RELEASE(condition) \
  28. (REALM_LIKELY(condition) ? static_cast<void>(0) \
  29. : realm::util::terminate("Assertion failed: " #condition, __FILE__, __LINE__))
  30. #if REALM_ASSERTIONS_ENABLED
  31. #define REALM_ASSERT(condition) REALM_ASSERT_RELEASE(condition)
  32. #else
  33. #define REALM_ASSERT(condition) static_cast<void>(sizeof bool(condition))
  34. #endif
  35. #ifdef REALM_DEBUG
  36. #define REALM_ASSERT_DEBUG(condition) REALM_ASSERT_RELEASE(condition)
  37. #else
  38. #define REALM_ASSERT_DEBUG(condition) static_cast<void>(sizeof bool(condition))
  39. #endif
  40. #define REALM_STRINGIFY(X) #X
  41. #define REALM_ASSERT_RELEASE_EX(condition, ...) \
  42. (REALM_LIKELY(condition) ? static_cast<void>(0) \
  43. : realm::util::terminate_with_info("Assertion failed: " #condition, __LINE__, __FILE__, \
  44. REALM_STRINGIFY((__VA_ARGS__)), __VA_ARGS__))
  45. #ifdef REALM_DEBUG
  46. #define REALM_ASSERT_DEBUG_EX REALM_ASSERT_RELEASE_EX
  47. #else
  48. #define REALM_ASSERT_DEBUG_EX(condition, ...) static_cast<void>(sizeof bool(condition))
  49. #endif
  50. // Becase the assert is used in noexcept methods, it's a bad idea to allocate
  51. // buffer space for the message so therefore we must pass it to terminate which
  52. // will 'cerr' it for us without needing any buffer
  53. #if REALM_ENABLE_ASSERTIONS || defined(REALM_DEBUG)
  54. #define REALM_ASSERT_EX REALM_ASSERT_RELEASE_EX
  55. #define REALM_ASSERT_3(left, cmp, right) \
  56. (REALM_LIKELY((left)cmp(right)) ? static_cast<void>(0) \
  57. : realm::util::terminate("Assertion failed: " \
  58. "" #left " " #cmp " " #right, \
  59. __FILE__, __LINE__, left, right))
  60. #define REALM_ASSERT_7(left1, cmp1, right1, logical, left2, cmp2, right2) \
  61. (REALM_LIKELY(((left1)cmp1(right1))logical((left2)cmp2(right2))) \
  62. ? static_cast<void>(0) \
  63. : realm::util::terminate("Assertion failed: " \
  64. "" #left1 " " #cmp1 " " #right1 " " #logical " " \
  65. "" #left2 " " #cmp2 " " #right2, \
  66. __FILE__, __LINE__, left1, right1, left2, right2))
  67. #define REALM_ASSERT_11(left1, cmp1, right1, logical1, left2, cmp2, right2, logical2, left3, cmp3, right3) \
  68. (REALM_LIKELY(((left1)cmp1(right1))logical1((left2)cmp2(right2)) logical2((left3)cmp3(right3))) \
  69. ? static_cast<void>(0) \
  70. : realm::util::terminate("Assertion failed: " \
  71. "" #left1 " " #cmp1 " " #right1 " " #logical1 " " \
  72. "" #left2 " " #cmp2 " " #right2 " " #logical2 " " \
  73. "" #left3 " " #cmp3 " " #right3, \
  74. __FILE__, __LINE__, left1, right1, left2, right2, left3, right3))
  75. #else
  76. #define REALM_ASSERT_EX(condition, ...) static_cast<void>(sizeof bool(condition))
  77. #define REALM_ASSERT_3(left, cmp, right) static_cast<void>(sizeof bool((left)cmp(right)))
  78. #define REALM_ASSERT_7(left1, cmp1, right1, logical, left2, cmp2, right2) \
  79. static_cast<void>(sizeof bool(((left1)cmp1(right1))logical((left2)cmp2(right2))))
  80. #define REALM_ASSERT_11(left1, cmp1, right1, logical1, left2, cmp2, right2, logical2, left3, cmp3, right3) \
  81. static_cast<void>(sizeof bool(((left1)cmp1(right1))logical1((left2)cmp2(right2)) logical2((left3)cmp3(right3))))
  82. #endif
  83. #define REALM_UNREACHABLE() realm::util::terminate("Unreachable code", __FILE__, __LINE__)
  84. #ifdef REALM_COVER
  85. #define REALM_COVER_NEVER(x) false
  86. #define REALM_COVER_ALWAYS(x) true
  87. #else
  88. #define REALM_COVER_NEVER(x) (x)
  89. #define REALM_COVER_ALWAYS(x) (x)
  90. #endif
  91. #endif // REALM_UTIL_ASSERT_HPP