assert.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // sol2
  2. // The MIT License (MIT)
  3. // Copyright (c) 2013-2022 Rapptz, ThePhD and contributors
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. // this software and associated documentation files (the "Software"), to deal in
  6. // the Software without restriction, including without limitation the rights to
  7. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. // the Software, and to permit persons to whom the Software is furnished to do so,
  9. // subject to the following conditions:
  10. // The above copyright notice and this permission notice shall be included in all
  11. // copies or substantial portions of the Software.
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. #pragma once
  19. #ifndef SOL_ASSERT_HPP
  20. #define SOL_ASSERT_HPP
  21. #include <sol/forward.hpp>
  22. #if SOL_IS_ON(SOL2_CI)
  23. struct pre_main {
  24. pre_main() {
  25. #ifdef _MSC_VER
  26. _set_abort_behavior(0, _WRITE_ABORT_MSG);
  27. #endif
  28. }
  29. } inline sol2_ci_dont_lock_ci_please = {};
  30. #endif // Prevent lockup when doing Continuous Integration
  31. // clang-format off
  32. #if SOL_IS_ON(SOL_USER_ASSERT)
  33. #define SOL_ASSERT(...) SOL_C_ASSERT(__VA_ARGS__)
  34. #else
  35. #if SOL_IS_ON(SOL_DEBUG_BUILD)
  36. #include <exception>
  37. #include <iostream>
  38. #include <cstdlib>
  39. #define SOL_ASSERT(...) \
  40. do { \
  41. if (!(__VA_ARGS__)) { \
  42. std::cerr << "Assertion `" #__VA_ARGS__ "` failed in " << __FILE__ << " line " << __LINE__ << std::endl; \
  43. std::terminate(); \
  44. } \
  45. } while (false)
  46. #else
  47. #define SOL_ASSERT(...) \
  48. do { \
  49. if (false) { \
  50. (void)(__VA_ARGS__); \
  51. } \
  52. } while (false)
  53. #endif
  54. #endif
  55. #if SOL_IS_ON(SOL_USER_ASSERT_MSG)
  56. #define SOL_ASSERT_MSG(message, ...) SOL_ASSERT_MSG(message, __VA_ARGS__)
  57. #else
  58. #if SOL_IS_ON(SOL_DEBUG_BUILD)
  59. #include <exception>
  60. #include <iostream>
  61. #include <cstdlib>
  62. #define SOL_ASSERT_MSG(message, ...) \
  63. do { \
  64. if (!(__VA_ARGS__)) { \
  65. std::cerr << "Assertion `" #__VA_ARGS__ "` failed in " << __FILE__ << " line " << __LINE__ << ": " << message << std::endl; \
  66. std::terminate(); \
  67. } \
  68. } while (false)
  69. #else
  70. #define SOL_ASSERT_MSG(message, ...) \
  71. do { \
  72. if (false) { \
  73. (void)(__VA_ARGS__); \
  74. (void)sizeof(message); \
  75. } \
  76. } while (false)
  77. #endif
  78. #endif
  79. // clang-format on
  80. #endif // SOL_ASSERT_HPP