mgcpp
A C++ Math Library Based on CUDA
exception.hpp
Go to the documentation of this file.
1 
2 // Copyright RedPortal, mujjingun 2017 - 2018.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 
7 #ifndef _MGCPP_SYSTEM_EXCEPTIONS_HPP_
8 #define _MGCPP_SYSTEM_EXCEPTIONS_HPP_
9 
10 #include <stdexcept>
11 
12 #ifndef MGCPP_ABORT_ON_ERROR
13 #define MGCPP_ABORT_ON_ERROR true
14 #endif
15 
16 #ifndef MGCPP_ERROR_MESSAGE_HANDLER
17 #define MGCPP_ERROR_MESSAGE_HANDLER(...) fprintf(stderr, __VA_ARGS__)
18 #endif
19 
20 #ifndef mgcpp_error_check
21 #define mgcpp_error_check(EXP) \
22  do { \
23  try { \
24  EXP; \
25  } catch (std::exception const& e) { \
26  MGCPP_ERROR_MESSAGE_HANDLER("[mgcpp errror] %s in %s line %d\n", \
27  e.what(), __FILE__, __LINE__); \
28  if (MGCPP_ABORT_ON_ERROR) \
29  exit(1); \
30  } \
31  } while (false)
32 #endif
33 
34 #ifdef ERROR_CHECK_EXCEPTION
35 #define MGCPP_THROW(EXCEPTION) \
36  do { \
37  MGCPP_ERROR_MESSAGE_HANDLER("[mgcpp errror] %s in %s line %d\n", \
38  EXCEPTION.what(), __FILE__, __LINE__); \
39  throw EXCEPTION; \
40  } while (false)
41 #endif
42 
43 #ifndef MGCPP_THROW
44 #define MGCPP_THROW(EXCEPTION) throw EXCEPTION
45 #endif
46 
47 #ifndef MGCPP_THROW_SYSTEM_ERROR
48 #define MGCPP_THROW_SYSTEM_ERROR(ERROR) MGCPP_THROW(std::system_error(ERROR))
49 #endif
50 
51 #ifndef MGCPP_THROW_BAD_ALLOC
52 #define MGCPP_THROW_BAD_ALLOC MGCPP_THROW(std::bad_alloc())
53 #endif
54 
55 #ifndef MGCPP_THROW_INVALID_ARGUMENT
56 #define MGCPP_THROW_INVALID_ARGUMENT(MESSAGE) \
57  MGCPP_THROW(std::invalid_argument(MESSAGE))
58 #endif
59 
60 #ifndef MGCPP_THROW_LENGTH_ERROR
61 #define MGCPP_THROW_LENGTH_ERROR(MESSAGE) \
62  MGCPP_THROW(std::length_error(MESSAGE))
63 #endif
64 
65 #ifndef MGCPP_THROW_OUT_OF_RANGE
66 #define MGCPP_THROW_OUT_OF_RANGE(MESSAGE) \
67  MGCPP_THROW(std::out_of_range(MESSAGE))
68 #endif
69 
70 #ifndef MGCPP_THROW_RUNTIME_ERROR
71 #define MGCPP_THROW_RUNTIME_ERROR(MESSAGE) \
72  MGCPP_THROW(std::runtime_error(MESSAGE))
73 #endif
74 
75 #ifndef MGCPP_THROW_OVERFLOW_ERROR
76 #define MGCPP_THROW_OVERFLOW_ERROR(MESSAGE) \
77  MGCPP_THROW(std::overflow_error(MESSAGE))
78 #endif
79 
80 #ifndef MGCPP_THROW_UNDERFLOW_ERROR
81 #define MGCPP_THROW_UNDERFLOW_ERROR(MESSAGE) \
82  MGCPP_THROW(std::underflow_error(MESSAGE))
83 #endif
84 
85 #endif