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 #ifndef _EXCEPTION_HPP_
00031 #define _EXCEPTION_HPP_
00032
00038 #include <string>
00039 #include <sstream>
00040 #include <cfloat>
00041 #include <climits>
00042 #include <cstdlib>
00043
00045 const unsigned UNSIGNED_UNSET = UINT_MAX;
00047 const int INT_UNSET = INT_MAX;
00049 const double DOUBLE_UNSET = DBL_MAX;
00050
00056 class Exception
00057 {
00058 public:
00066 Exception(const std::string& rMessage, const std::string& rFilename, unsigned lineNumber);
00067
00073 std::string GetMessage() const;
00074
00080 std::string GetShortMessage() const;
00081
00091 std::string CheckShortMessage(std::string expected) const;
00092
00102 std::string CheckShortMessageContains(std::string expected) const;
00103
00112 static void Terminate(const std::string& rMessage, const std::string& rFilename, unsigned lineNumber);
00113
00114 protected:
00123 void SetMessage(const std::string& rMessage,
00124 const std::string& rFilename, unsigned lineNumber);
00125
00126 private:
00127 std::string mMessage;
00128 std::string mShortMessage;
00129 };
00130
00136 #define EXCEPTION(message) \
00137 do { \
00138 std::stringstream msg_stream; \
00139 msg_stream << message; \
00140 throw Exception(msg_stream.str(), __FILE__, __LINE__); \
00141 } while (false)
00142
00143 #include <boost/preprocessor/stringize.hpp>
00144
00151 #define EXCEPT_IF_NOT(test) \
00152 if (!(test)) EXCEPTION("Assertion tripped: " BOOST_PP_STRINGIZE(test))
00153
00154
00161 #define TERMINATE(message) Exception::Terminate(message, __FILE__, __LINE__)
00162
00194 #define NEVER_REACHED TERMINATE("Should have been impossible to reach this line of code"); exit(EXIT_FAILURE)
00195
00201 #ifdef NDEBUG
00202 #define UNUSED_OPT(var) var=var
00203 #else
00204 #define UNUSED_OPT(var)
00205 #endif
00206
00219 #define EXPECT0(cmd, arg) { \
00220 std::string _arg(arg); \
00221 int ret = cmd(_arg.c_str()); \
00222 if (ret != 0) { \
00223 EXCEPTION("Error executing command: " #cmd "(" + _arg + ")"); \
00224 } }
00225
00226
00233 #define ABORT_IF_NON0_WITH_MSG(retcode, msg) \
00234 if (retcode != 0) { \
00235 TERMINATE(msg); \
00236 }
00237
00248 #define ABORT_IF_NON0(cmd, arg) { \
00249 std::string _arg(arg); \
00250 int ret = cmd(_arg.c_str()); \
00251 ABORT_IF_NON0_WITH_MSG(ret, "Error executing command: " #cmd "(" + _arg + ")") \
00252 }
00253
00260 #define EXPECT_NON0(cmd, arg) { \
00261 std::string _arg = (arg); \
00262 int ret = cmd(_arg.c_str()); \
00263 if (ret == 0) { \
00264 EXCEPTION("Command: " #cmd "(" + _arg + ") succeeded and it shouldn't have"); \
00265 } }
00266
00273 #define IGNORE_RET(cmd, arg) { \
00274 std::string _arg = (arg); \
00275 int ret = cmd(_arg.c_str()); \
00276 ret = ret; \
00277 }
00278
00279 #endif // _EXCEPTION_HPP_