Exception.hpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #ifndef _EXCEPTION_HPP_
00038 #define _EXCEPTION_HPP_
00039
00045 #include <string>
00046 #include <sstream>
00047 #include <cfloat>
00048 #include <climits>
00049 #include <cstdlib>
00050
00052 const unsigned UNSIGNED_UNSET = UINT_MAX;
00054 const int INT_UNSET = INT_MAX;
00056 const double DOUBLE_UNSET = DBL_MAX;
00057
00063 class Exception
00064 {
00065 public:
00073 Exception(const std::string& rMessage, const std::string& rFilename, unsigned lineNumber);
00074
00080 std::string GetMessage() const;
00081
00087 std::string GetShortMessage() const;
00088
00098 std::string CheckShortMessage(std::string expected) const;
00099
00109 std::string CheckShortMessageContains(std::string expected) const;
00110
00119 static void Terminate(const std::string& rMessage, const std::string& rFilename, unsigned lineNumber);
00120
00121 protected:
00130 void SetMessage(const std::string& rMessage,
00131 const std::string& rFilename, unsigned lineNumber);
00132
00133 private:
00134 std::string mMessage;
00135 std::string mShortMessage;
00136 };
00137
00143 #define EXCEPTION(message) \
00144 do { \
00145 std::stringstream msg_stream; \
00146 msg_stream << message; \
00147 throw Exception(msg_stream.str(), __FILE__, __LINE__); \
00148 } while (false)
00149
00150 #include <boost/preprocessor/stringize.hpp>
00151
00158 #define EXCEPT_IF_NOT(test) \
00159 if (!(test)) EXCEPTION("Assertion tripped: " BOOST_PP_STRINGIZE(test))
00160
00161
00168 #define TERMINATE(message) \
00169 do { \
00170 std::stringstream msg_stream; \
00171 msg_stream << message; \
00172 Exception::Terminate(msg_stream.str(), __FILE__, __LINE__); \
00173 } while (false)
00174
00206 #define NEVER_REACHED TERMINATE("Should have been impossible to reach this line of code"); exit(EXIT_FAILURE)
00207
00213 #ifdef NDEBUG
00214 #define UNUSED_OPT(var) var=var
00215 #else
00216 #define UNUSED_OPT(var)
00217 #endif
00218
00225 #define ABORT_IF_THROWS(block) \
00226 try { \
00227 block; \
00228 } catch (const Exception& e) { \
00229 TERMINATE(e.GetMessage()); \
00230 } catch (const std::exception &e) { \
00231 TERMINATE(e.what()); \
00232 } catch (...) { \
00233 TERMINATE("Unexpected exception thrown."); \
00234 }
00235
00236
00237
00238
00239
00254 #define EXPECT0(cmd, arg) { \
00255 std::string _arg(arg); \
00256 int retcode = cmd(_arg.c_str()); \
00257 if (retcode != 0) { \
00258 EXCEPTION("Error executing command: " #cmd "(" + _arg + ")"); \
00259 } }
00260
00261
00270 #define ABORT_IF_NON0_WITH_MSG(retcode, msg) \
00271 if (retcode != 0) { \
00272 TERMINATE(msg); \
00273 }
00274
00287 #define ABORT_IF_NON0(cmd, arg) { \
00288 std::string _arg(arg); \
00289 int ret = cmd(_arg.c_str()); \
00290 ABORT_IF_NON0_WITH_MSG(ret, "Error executing command: " #cmd "(" + _arg + ")") \
00291 }
00292
00301 #define EXPECT_NON0(cmd, arg) { \
00302 std::string _arg = (arg); \
00303 int ret = cmd(_arg.c_str()); \
00304 if (ret == 0) { \
00305 EXCEPTION("Command: " #cmd "(" + _arg + ") succeeded and it shouldn't have"); \
00306 } }
00307
00316 #define IGNORE_RET(cmd, arg) { \
00317 std::string _arg = (arg); \
00318 int ret = cmd(_arg.c_str()); \
00319 ret = ret; \
00320 }
00321
00322 #endif // _EXCEPTION_HPP_