Schnek
exceptions.hpp
1 /*
2  * exceptions.hpp
3  *
4  * Created on: 20 Sep 2012
5  * Author: Holger Schmitz
6  * Email: holger@notjustphysics.com
7  *
8  * Copyright 2012 Holger Schmitz
9  *
10  * This file is part of Schnek.
11  *
12  * Schnek is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * Schnek is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with Schnek. If not, see <http://www.gnu.org/licenses/>.
24  *
25  */
26 
27 #ifndef EXCEPTIONS_HPP_
28 #define EXCEPTIONS_HPP_
29 
30 #include <boost/current_function.hpp>
31 #include <exception>
32 
33 namespace schnek {
34 
36 class ScheckException : public std::exception {
37  private:
38  std::string message;
39  public:
40  ScheckException(const std::string& file,
41  long line,
42  const std::string& functionName,
43  const std::string& message_ = "");
44 
45  ~ScheckException() throw() {}
47  const char* what() const throw ();
48 };
49 
50 } // namespace
51 
52 #define SCHNECK_FAIL(message) \
53 { \
54  std::ostringstream _schnek_msg_stream; \
55  _schnek_msg_stream << message; \
56  throw schnek::ScheckException(__FILE__,__LINE__, \
57  BOOST_CURRENT_FUNCTION,_schnek_msg_stream.str()); \
58 }
59 
60 
61 #define SCHNEK_ASSERT(condition,message) \
62 if (!(condition)) { \
63  std::ostringstream _schnek_msg_stream; \
64  _schnek_msg_stream << message; \
65  throw schnek::ScheckException(__FILE__,__LINE__, \
66  BOOST_CURRENT_FUNCTION,_schnek_msg_stream.str()); \
67 }
68 
69 #define SCHNEK_REQUIRE(condition,message) \
70 if (!(condition)) { \
71  std::ostringstream _schnek_msg_stream; \
72  _schnek_msg_stream << message; \
73  throw schnek::ScheckException(__FILE__,__LINE__, \
74  BOOST_CURRENT_FUNCTION,_schnek_msg_stream.str()); \
75 }
76 
77 #define SCHNEK_ENSURE(condition,message) \
78 if (!(condition)) { \
79  std::ostringstream _schnek_msg_stream; \
80  _schnek_msg_stream << message; \
81  throw schnek::ScheckException(__FILE__,__LINE__, \
82  BOOST_CURRENT_FUNCTION,_schnek_msg_stream.str()); \
83 }
84 
85 
86 // duplications of the macros allowing for other exceptions with comparable constructor
87 
88 #define SCHNECK_FAIL_E(message, exceptclass) \
89 { \
90  std::ostringstream _schnek_msg_stream; \
91  _schnek_msg_stream << message; \
92  throw schnek::exceptclass(__FILE__,__LINE__, \
93  BOOST_CURRENT_FUNCTION,_schnek_msg_stream.str()); \
94 }
95 
96 
97 #define SCHNEK_ASSERT_E(condition,message, exceptclass) \
98 if (!(condition)) { \
99  std::ostringstream _schnek_msg_stream; \
100  _schnek_msg_stream << message; \
101  throw schnek::exceptclass(__FILE__,__LINE__, \
102  BOOST_CURRENT_FUNCTION,_schnek_msg_stream.str()); \
103 }
104 
105 #define SCHNEK_REQUIRE_E(condition,message, exceptclass) \
106 if (!(condition)) { \
107  std::ostringstream _schnek_msg_stream; \
108  _schnek_msg_stream << message; \
109  throw schnek::exceptclass(__FILE__,__LINE__, \
110  BOOST_CURRENT_FUNCTION,_schnek_msg_stream.str()); \
111 }
112 
113 #define SCHNEK_ENSURE_E(condition,message, exceptclass) \
114 if (!(condition)) { \
115  std::ostringstream _schnek_msg_stream; \
116  _schnek_msg_stream << message; \
117  throw schnek::exceptclass(__FILE__,__LINE__, \
118  BOOST_CURRENT_FUNCTION,_schnek_msg_stream.str()); \
119 }
120 
121 #endif // EXCEPTIONS_HPP_
const char * what() const
returns the error message.
Definition: exceptions.cpp:41
Definition: algo.hpp:30
Base error class.
Definition: exceptions.hpp:36