Fabcoin Core  0.16.2
P2P Digital Currency
Assertions.h
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
25 #pragma once
26 
27 #include "Exceptions.h"
28 #include "debugbreak.h"
29 
30 namespace dev
31 {
32 
33 #if defined(_MSC_VER)
34 #define ETH_FUNC __FUNCSIG__
35 #elif defined(__GNUC__)
36 #define ETH_FUNC __PRETTY_FUNCTION__
37 #else
38 #define ETH_FUNC __func__
39 #endif
40 
41 #define asserts(A) ::dev::assertAux(A, #A, __LINE__, __FILE__, ETH_FUNC)
42 #define assertsEqual(A, B) ::dev::assertEqualAux(A, B, #A, #B, __LINE__, __FILE__, ETH_FUNC)
43 
44 inline bool assertAux(bool _a, char const* _aStr, unsigned _line, char const* _file, char const* _func)
45 {
46  bool ret = _a;
47  if (!ret)
48  {
49  std::cerr << "Assertion failed:" << _aStr << " [func=" << _func << ", line=" << _line << ", file=" << _file << "]" << std::endl;
50 #if ETH_DEBUG
51  debug_break();
52 #endif
53  }
54  return !ret;
55 }
56 
57 template<class A, class B>
58 inline bool assertEqualAux(A const& _a, B const& _b, char const* _aStr, char const* _bStr, unsigned _line, char const* _file, char const* _func)
59 {
60  bool ret = _a == _b;
61  if (!ret)
62  {
63  std::cerr << "Assertion failed: " << _aStr << " == " << _bStr << " [func=" << _func << ", line=" << _line << ", file=" << _file << "]" << std::endl;
64  std::cerr << " Fail equality: " << _a << "==" << _b << std::endl;
65 #if ETH_DEBUG
66  debug_break();
67 #endif
68  }
69  return !ret;
70 }
71 
75 #define assertThrow(_condition, _ExceptionType, _description) \
76  ::dev::assertThrowAux<_ExceptionType>(_condition, _description, __LINE__, __FILE__, ETH_FUNC)
77 
78 using errinfo_comment = boost::error_info<struct tag_comment, std::string>;
79 
80 template <class _ExceptionType>
81 inline void assertThrowAux(
82  bool _condition,
83  ::std::string const& _errorDescription,
84  unsigned _line,
85  char const* _file,
86  char const* _function
87 )
88 {
89  if (!_condition)
90  ::boost::throw_exception(
91  _ExceptionType() <<
92  ::dev::errinfo_comment(_errorDescription) <<
93  ::boost::throw_function(_function) <<
94  ::boost::throw_file(_file) <<
95  ::boost::throw_line(_line)
96  );
97 }
98 
99 template <class _ExceptionType>
100 inline void assertThrowAux(
101  void const* _pointer,
102  ::std::string const& _errorDescription,
103  unsigned _line,
104  char const* _file,
105  char const* _function
106 )
107 {
108  assertThrowAux<_ExceptionType>(_pointer != nullptr, _errorDescription, _line, _file, _function);
109 }
110 
111 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
void assertThrowAux(bool _condition,::std::string const &_errorDescription, unsigned _line, char const *_file, char const *_function)
Definition: Assertions.h:81
bool assertAux(bool _a, char const *_aStr, unsigned _line, char const *_file, char const *_func)
Definition: Assertions.h:44
boost::error_info< struct tag_comment, std::string > errinfo_comment
Definition: Assertions.h:78
bool assertEqualAux(A const &_a, B const &_b, char const *_aStr, char const *_bStr, unsigned _line, char const *_file, char const *_func)
Definition: Assertions.h:58