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
00035 #ifndef KEYVALUE_SYS_EXCEPTION_EXCEPTION_H_
00036 #define KEYVALUE_SYS_EXCEPTION_EXCEPTION_H_
00037
00038 #include <iosfwd>
00039 #include <stdexcept>
00040
00041 #include "keyvalue/sys/message/MessageImpl.h"
00042
00043 namespace keyvalue {
00044
00056 namespace exception {
00057
00063 class Exception {
00064
00065 public:
00066
00076 Exception();
00077
00078 virtual
00079 ~Exception() throw();
00080
00097 virtual const char*
00098 what() const = 0;
00099
00109 virtual const Message&
00110 getMessage() const = 0;
00111
00112 };
00113
00117 std::ostream&
00118 operator<<(std::ostream& os, const Exception& exception);
00119
00132 template<typename StdExcept, typename MessageType>
00133 class ExceptionImpl: public Exception, public StdExcept {
00134
00135 public:
00136
00137 ExceptionImpl();
00138
00139 virtual
00140 ~ExceptionImpl() throw();
00141
00145 const Message&
00146 getMessage() const;
00147
00151 const char*
00152 what() const throw();
00153
00175 template<typename DataType>
00176 ExceptionImpl&
00177 operator&(const DataType& data);
00178
00179 private:
00180
00181 MessageType message_;
00182 mutable string str_;
00183
00184 };
00185
00247 template <typename LhsType, typename RhsType>
00248 LhsType&
00249 operator&(const LhsType& lhs, const RhsType& rhs) {
00250 LhsType& mutableLhs(const_cast<LhsType&>(lhs));
00251 mutableLhs.operator&(rhs);
00252 return mutableLhs;
00253 }
00254
00265 typedef ExceptionImpl<std::runtime_error, Error> RuntimeError;
00266
00280 typedef ExceptionImpl<std::logic_error, Logic> LogicError;
00281
00282
00283
00284
00285
00286 template <typename StdExcept, typename MessageType>
00287 ExceptionImpl<StdExcept, MessageType>::ExceptionImpl() : StdExcept("") {
00288 }
00289
00290
00291
00292
00293
00294 template <typename StdExcept, typename MessageType>
00295 ExceptionImpl<StdExcept, MessageType>::~ExceptionImpl() throw() {
00296 }
00297
00298
00299
00300
00301
00302 template <typename StdExcept, typename MessageType>
00303 const Message&
00304 ExceptionImpl<StdExcept, MessageType>::getMessage() const {
00305 return message_;
00306 }
00307
00308
00309
00310
00311
00312 template <typename StdExcept, typename MessageType>
00313 const char*
00314 ExceptionImpl<StdExcept, MessageType>::what() const throw() {
00315 message_.getString().swap(str_);
00316 return str_.c_str();
00317 }
00318
00319
00320
00321
00322
00323 template<typename StdExcept, typename MessageType>
00324 template<typename DataType>
00325 ExceptionImpl<StdExcept, MessageType>&
00326 ExceptionImpl<StdExcept, MessageType>::operator&(const DataType& data) {
00327 message_ & data;
00328 return *this;
00329 }
00330
00331 }
00332
00333 using exception::Exception;
00334 using exception::RuntimeError;
00335 using exception::LogicError;
00336
00337 }
00338
00339 #endif // KEYVALUE_SYS_EXCEPTION_EXCEPTION_H_