Exception.h
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
00029 #ifndef KEYVALUE_SYS_EXCEPTION_EXCEPTION_H_
00030 #define KEYVALUE_SYS_EXCEPTION_EXCEPTION_H_
00031
00032 #include <iosfwd>
00033 #include <stdexcept>
00034
00035 #include "keyvalue/sys/message/MessageImpl.h"
00036
00037 namespace keyvalue {
00038
00050 namespace exception {
00051
00057 class Exception {
00058
00059 public:
00060
00070 Exception();
00071
00072 virtual
00073 ~Exception() throw();
00074
00091 virtual const char*
00092 what() const = 0;
00093
00103 virtual const Message&
00104 getMessage() const = 0;
00105
00106 };
00107
00111 std::ostream&
00112 operator<<(std::ostream& os, const Exception& exception);
00113
00126 template<typename StdExcept, typename MessageType>
00127 class ExceptionImpl: public Exception, public StdExcept {
00128
00129 public:
00130
00131 ExceptionImpl();
00132
00133 virtual
00134 ~ExceptionImpl() throw();
00135
00139 const Message&
00140 getMessage() const;
00141
00145 const char*
00146 what() const throw();
00147
00171 template<typename DataType>
00172 ExceptionImpl&
00173 operator&(const DataType& data);
00174
00175 private:
00176
00177 MessageType message_;
00178 mutable string str_;
00179
00180 };
00181
00244 template <typename LhsType, typename RhsType>
00245 LhsType&
00246 operator&(const LhsType& lhs, const RhsType& rhs) {
00247 LhsType& mutableLhs(const_cast<LhsType&>(lhs));
00248 mutableLhs.operator&(rhs);
00249 return mutableLhs;
00250 }
00251
00262 typedef ExceptionImpl<std::runtime_error, Error> RuntimeError;
00263
00277 typedef ExceptionImpl<std::logic_error, Logic> LogicError;
00278
00279
00280
00281
00282
00283 template <typename StdExcept, typename MessageType>
00284 ExceptionImpl<StdExcept, MessageType>::ExceptionImpl() : StdExcept("") {
00285 }
00286
00287
00288
00289
00290
00291 template <typename StdExcept, typename MessageType>
00292 ExceptionImpl<StdExcept, MessageType>::~ExceptionImpl() throw() {
00293 }
00294
00295
00296
00297
00298
00299 template <typename StdExcept, typename MessageType>
00300 const Message&
00301 ExceptionImpl<StdExcept, MessageType>::getMessage() const {
00302 return message_;
00303 }
00304
00305
00306
00307
00308
00309 template <typename StdExcept, typename MessageType>
00310 const char*
00311 ExceptionImpl<StdExcept, MessageType>::what() const throw() {
00312 message_.getString().swap(str_);
00313 return str_.c_str();
00314 }
00315
00316
00317
00318
00319
00320 template<typename StdExcept, typename MessageType>
00321 template<typename DataType>
00322 ExceptionImpl<StdExcept, MessageType>&
00323 ExceptionImpl<StdExcept, MessageType>::operator&(const DataType& data) {
00324 message_ & data;
00325 return *this;
00326 }
00327
00328 }
00329
00330 using exception::Exception;
00331 using exception::RuntimeError;
00332 using exception::LogicError;
00333
00334 }
00335
00336 #endif // KEYVALUE_SYS_EXCEPTION_EXCEPTION_H_