00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00023
00024 #ifndef BTK_COMMON_EXCEPTIONS_HPP
00025 #define BTK_COMMON_EXCEPTIONS_HPP
00026
00027 #include <string>
00028 #include <sstream>
00029 #include <stdexcept>
00030
00031 namespace BTK {
00032 namespace EXCEPTIONS {
00033
00034 struct BTKException
00035 {
00036 BTKException(std::string const & msg,
00037 std::string const & file,
00038 unsigned line,
00039 std::string const & function)
00040 {
00041 std::ostringstream mstr;
00042 mstr << msg << " thrown from "
00043 << file << ", line " << line;
00044
00045 if (!function.empty())
00046 mstr << ", fxn = " << function;
00047
00048 _msg = mstr.str();
00049 }
00050
00051 virtual ~BTKException() {}
00052
00053 virtual char const * what() const throw()
00054 { return _msg.c_str(); }
00055
00056 private:
00057 std::string _msg;
00058 };
00059
00060
00061
00062
00063
00064 #define BASE_BTK_EXCEPTION_TYPE(exception_name,base) \
00065 struct exception_name : public base, public BTKException \
00066 { \
00067 exception_name(std::string const & msg = #exception_name, \
00068 std::string const & file = "", \
00069 unsigned line = 0, \
00070 std::string const & func = "") : \
00071 base(msg), BTKException(msg,file,line,func) {} \
00072 \
00073 virtual char const * what() const throw() \
00074 { return BTKException::what(); } \
00075 \
00076 virtual ~exception_name() throw() {} \
00077 };
00078
00079
00080 BASE_BTK_EXCEPTION_TYPE(BTKLogicError,std::logic_error);
00081 BASE_BTK_EXCEPTION_TYPE(BTKDomainError,std::domain_error);
00082 BASE_BTK_EXCEPTION_TYPE(BTKInvalidArgument,std::invalid_argument);
00083 BASE_BTK_EXCEPTION_TYPE(BTKLengthError,std::length_error);
00084 BASE_BTK_EXCEPTION_TYPE(BTKOutOfRange,std::out_of_range);
00085
00086
00087 BASE_BTK_EXCEPTION_TYPE(BTKRuntimeError,std::runtime_error);
00088 BASE_BTK_EXCEPTION_TYPE(BTKRangeError,std::range_error);
00089 BASE_BTK_EXCEPTION_TYPE(BTKUnderflowError,std::underflow_error);
00090 BASE_BTK_EXCEPTION_TYPE(BTKOverflowError,std::overflow_error);
00091
00092 #undef BASE_BTK_EXCEPTION_TYPE
00093
00094 #define NEW_BTK_EXCEPTION_TYPE(exception_name,base_btk_exception) \
00095 struct exception_name : public base_btk_exception \
00096 { \
00097 explicit exception_name(char const *msg = #exception_name, \
00098 char const *file = "", \
00099 unsigned line = 0, \
00100 char const *func = "") : \
00101 base_btk_exception(msg,file,line,func) {} \
00102 \
00103 virtual ~exception_name() throw() {} \
00104 }
00105
00106 #define BTK_THROW(btk_exception) \
00107 throw BTK::EXCEPTIONS::btk_exception(#btk_exception,__FILE__,__LINE__)
00108
00109 #define BTK_THROW_MSG(btk_exception,msg) \
00110 throw BTK::EXCEPTIONS::btk_exception(msg,__FILE__,__LINE__)
00111
00112 }
00113 }
00114
00115 #endif