Exception.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *
00003  * Copyright (C) 2009-2010 Cassio Neri Moreira
00004  *
00005  * This file is part of the KeyValue library.
00006  *
00007  * The KeyValue library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License as published
00009  * by the Free Software Foundation, either version 3 of the License, or (at
00010  * your option) any later version.
00011  *
00012  * The KeyValue library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
00015  * Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License along
00018  * with KeyValue. If not, see <http://www.gnu.org/licenses/>.
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 }; // class Exception
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 }; // class ExceptionImpl
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  * ExceptionImpl::ExceptionImpl()
00281  *------------------------------------------------------------------------*/
00282 
00283 template <typename StdExcept, typename MessageType>
00284 ExceptionImpl<StdExcept, MessageType>::ExceptionImpl() : StdExcept("") {
00285 }
00286 
00287 /*--------------------------------------------------------------------------
00288  * ExceptionImpl::~ExceptionImpl()
00289  *------------------------------------------------------------------------*/
00290 
00291 template <typename StdExcept, typename MessageType>
00292 ExceptionImpl<StdExcept, MessageType>::~ExceptionImpl() throw() {
00293 }
00294 
00295 /*--------------------------------------------------------------------------
00296  * ExceptionImpl::getMessage()
00297  *------------------------------------------------------------------------*/
00298 
00299 template <typename StdExcept, typename MessageType>
00300 const Message&
00301 ExceptionImpl<StdExcept, MessageType>::getMessage() const {
00302   return message_;
00303 }
00304 
00305 /*--------------------------------------------------------------------------
00306  * ExceptionImpl::what()
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  * ExceptionImpl::operator&()
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 } // namespace exception
00329 
00330 using exception::Exception;
00331 using exception::RuntimeError;
00332 using exception::LogicError;
00333 
00334 } // namespace keyvalue
00335 
00336 #endif // KEYVALUE_SYS_EXCEPTION_EXCEPTION_H_

Generated on Sat Mar 20 15:08:29 2010 for KeyValue by  doxygen 1.6.1