Ethereum  PoC-8
The C++ Implementation of Ethereum
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 <boost/exception/info.hpp>
28 #include <iostream>
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  if (!_a)
47  std::cerr << "Assertion failed:" << _aStr << " [func=" << _func << ", line=" << _line << ", file=" << _file << "]" << std::endl;
48  return !_a;
49 }
50 
51 template<class A, class B>
52 inline bool assertEqualAux(A const& _a, B const& _b, char const* _aStr, char const* _bStr, unsigned _line, char const* _file, char const* _func)
53 {
54  bool c = _a == _b;
55  if (!c)
56  {
57  std::cerr << "Assertion failed: " << _aStr << " == " << _bStr << " [func=" << _func << ", line=" << _line << ", file=" << _file << "]" << std::endl;
58  std::cerr << " Fail equality: " << _a << "==" << _b << std::endl;
59  }
60  return !c;
61 }
62 
66 #define assertThrow(_condition, _ExceptionType, _description) \
67  ::dev::assertThrowAux<_ExceptionType>(_condition, _description, __LINE__, __FILE__, ETH_FUNC)
68 
69 using errinfo_comment = boost::error_info<struct tag_comment, std::string>;
70 
71 template <class _ExceptionType>
72 inline void assertThrowAux(
73  bool _condition,
74  ::std::string const& _errorDescription,
75  unsigned _line,
76  char const* _file,
77  char const* _function
78 )
79 {
80  if (!_condition)
81  ::boost::throw_exception(
82  _ExceptionType() <<
83  ::dev::errinfo_comment(_errorDescription) <<
84  ::boost::throw_function(_function) <<
85  ::boost::throw_file(_file) <<
86  ::boost::throw_line(_line)
87  );
88 }
89 
90 template <class _ExceptionType>
91 inline void assertThrowAux(
92  void const* _pointer,
93  ::std::string const& _errorDescription,
94  unsigned _line,
95  char const* _file,
96  char const* _function
97 )
98 {
99  assertThrowAux<_ExceptionType>(_pointer != nullptr, _errorDescription, _line, _file, _function);
100 }
101 
102 }
dev::assertEqualAux
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:52
dev::assertThrowAux
void assertThrowAux(bool _condition, ::std::string const &_errorDescription, unsigned _line, char const *_file, char const *_function)
Definition: Assertions.h:72
dev
Definition: Address.cpp:21
dev::assertAux
bool assertAux(bool _a, char const *_aStr, unsigned _line, char const *_file, char const *_func)
Definition: Assertions.h:44
dev::errinfo_comment
boost::error_info< struct tag_comment, std::string > errinfo_comment
Definition: Assertions.h:69