00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00026 #ifndef HH_SENF_Utils_Exception_
00027 #define HH_SENF_Utils_Exception_ 1
00028
00029
00030 #include <exception>
00031 #include <string>
00032 #include <iostream>
00033 #include <sstream>
00034 #include <boost/preprocessor/repeat.hpp>
00035 #include <boost/preprocessor/cat.hpp>
00036 #include <boost/utility.hpp>
00037 #include <boost/type_traits/is_convertible.hpp>
00038 #include <senf/config.hh>
00039
00040
00041
00042
00132 namespace senf {
00133
00141 class ExceptionMixin
00142 {
00143 public:
00144 std::string message() const;
00145 std::string backtrace() const;
00146
00147 void append(std::string text);
00148
00150 protected:
00151 explicit ExceptionMixin(std::string const & description = "");
00153
00158 std::string what_;
00159 private:
00160 #ifdef SENF_BACKTRACE
00161 void addBacktrace();
00162 #endif
00163 #ifdef SENF_DEBUG
00164 std::string::size_type excLen_;
00165 #endif
00166 };
00167
00178 class Exception
00179 : public ExceptionMixin, public std::exception
00180 {
00181 public:
00182 virtual ~Exception() throw();
00183
00184 virtual char const * what() const throw();
00186
00189 protected:
00190 explicit Exception(std::string const & description = "");
00191 };
00192
00200 template <class BaseException>
00201 class WrapException
00202 : public ExceptionMixin, public BaseException
00203 {
00204 public:
00205 typedef BaseException Base;
00206
00207 WrapException(BaseException const & base);
00208 virtual ~WrapException() throw();
00209
00210 virtual char const * what() const throw();
00211 };
00212
00220 # define SENF_WRAP_EXC(Ex) \
00221 catch (Ex const & base) { \
00222 if (dynamic_cast<senf::ExceptionMixin const *>(&base)) \
00223 throw; \
00224 else \
00225 throw senf::WrapException<Ex>(base); \
00226 }
00227
00228 template <class Exc, class Arg>
00229 typename boost::enable_if< boost::is_convertible<Exc*,ExceptionMixin*>, Exc & >::type
00230 operator<<(Exc const & exc, Arg const & arg);
00231
00239 # ifdef SENF_DEBUG
00240 # define _SENF_EXC_DEBUG_ARGS ,char const * file=0,int line=0
00241 # define _SENF_EXC_DEBUG_ARGS_ND ,char const *file,int line
00242 # define _SENF_EXC_DEBUG_ARGS_P ,file,line
00243 # else
00244 # define _SENF_EXC_DEBUG_ARGS
00245 # define _SENF_EXC_DEBUG_ARGS_ND
00246 # define _SENF_EXC_DEBUG_ARGS_P
00247 # endif
00248
00281 class SystemException : public Exception
00282 {
00283 public:
00284
00286
00287
00288 explicit SystemException(std::string const & descr = "" _SENF_EXC_DEBUG_ARGS);
00289 explicit SystemException(int code _SENF_EXC_DEBUG_ARGS);
00290 SystemException(std::string const & descr, int code _SENF_EXC_DEBUG_ARGS);
00291
00292 virtual ~SystemException() throw();
00293
00294
00295
00296
00297 int errorNumber() const;
00298 char const * errorString() const;
00299
00300 bool anyOf(int c0, int c1=0, int c2=0, int c3=0, int c4=0, int c5=0,
00301 int c6=0, int c7=0, int c8=0, int c9=0) const;
00303
00304 private:
00305 void init(std::string const & descr, int code _SENF_EXC_DEBUG_ARGS_ND);
00306
00307 int code_;
00308 };
00309
00310 # ifdef SENF_DEBUG
00311 # define SENF_EXC_DEBUGINFO ,__FILE__,__LINE__
00312 # else
00313 # define SENF_EXC_DEBUGINFO
00314 # endif
00315
00316 # define SENF_THROW_SYSTEM_EXCEPTION(desc) throw senf::SystemException(desc SENF_EXC_DEBUGINFO)
00317
00318 }
00319
00320
00321 #include "Exception.cci"
00322
00323 #include "Exception.cti"
00324 #endif
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336