Assertions.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /**
  15. * @file Assertions.h
  16. * @author Christian <c@ethdev.com>
  17. * @date 2015
  18. *
  19. * Assertion handling.
  20. */
  21. #pragma once
  22. #include "Exceptions.h"
  23. #include "debugbreak.h"
  24. namespace dev
  25. {
  26. #if defined(_MSC_VER)
  27. #define ETH_FUNC __FUNCSIG__
  28. #elif defined(__GNUC__)
  29. #define ETH_FUNC __PRETTY_FUNCTION__
  30. #else
  31. #define ETH_FUNC __func__
  32. #endif
  33. #define asserts(A) ::dev::assertAux(A, #A, __LINE__, __FILE__, ETH_FUNC)
  34. #define assertsEqual(A, B) ::dev::assertEqualAux(A, B, #A, #B, __LINE__, __FILE__, ETH_FUNC)
  35. inline bool assertAux(bool _a, char const* _aStr, unsigned _line, char const* _file, char const* _func)
  36. {
  37. bool ret = _a;
  38. if (!ret)
  39. {
  40. std::cerr << "Assertion failed:" << _aStr << " [func=" << _func << ", line=" << _line << ", file=" << _file << "]" << std::endl;
  41. #if ETH_DEBUG
  42. debug_break();
  43. #endif
  44. }
  45. return !ret;
  46. }
  47. template<class A, class B>
  48. inline bool assertEqualAux(A const& _a, B const& _b, char const* _aStr, char const* _bStr, unsigned _line, char const* _file, char const* _func)
  49. {
  50. bool ret = _a == _b;
  51. if (!ret)
  52. {
  53. std::cerr << "Assertion failed: " << _aStr << " == " << _bStr << " [func=" << _func << ", line=" << _line << ", file=" << _file << "]" << std::endl;
  54. std::cerr << " Fail equality: " << _a << "==" << _b << std::endl;
  55. #if ETH_DEBUG
  56. debug_break();
  57. #endif
  58. }
  59. return !ret;
  60. }
  61. /// Assertion that throws an exception containing the given description if it is not met.
  62. /// Use it as assertThrow(1 == 1, ExceptionType, "Mathematics is wrong.");
  63. /// Do NOT supply an exception object as the second parameter.
  64. #define assertThrow(_condition, _ExceptionType, _description) \
  65. ::dev::assertThrowAux<_ExceptionType>(_condition, _description, __LINE__, __FILE__, ETH_FUNC)
  66. using errinfo_comment = boost::error_info<struct tag_comment, std::string>;
  67. template <class _ExceptionType>
  68. inline void assertThrowAux(
  69. bool _condition,
  70. ::std::string const& _errorDescription,
  71. unsigned _line,
  72. char const* _file,
  73. char const* _function
  74. )
  75. {
  76. if (!_condition)
  77. ::boost::throw_exception(
  78. _ExceptionType() <<
  79. ::dev::errinfo_comment(_errorDescription) <<
  80. ::boost::throw_function(_function) <<
  81. ::boost::throw_file(_file) <<
  82. ::boost::throw_line(_line)
  83. );
  84. }
  85. template <class _ExceptionType>
  86. inline void assertThrowAux(
  87. void const* _pointer,
  88. ::std::string const& _errorDescription,
  89. unsigned _line,
  90. char const* _file,
  91. char const* _function
  92. )
  93. {
  94. assertThrowAux<_ExceptionType>(_pointer != nullptr, _errorDescription, _line, _file, _function);
  95. }
  96. }