Exception.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *
00003  * Copyright (C) 2009-2011 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  * If you modify this library, or any covered work, by linking or combining
00021  * it with Excel (or a modified version of that program), containing parts
00022  * covered by the terms of End-User License Agreement for Microsoft
00023  * Software, the licensors of KeyValue grant you additional permission to
00024  * convey the resulting work.
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 }; // class Exception
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 }; // class ExceptionImpl
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  * ExceptionImpl::ExceptionImpl()
00284  *------------------------------------------------------------------------*/
00285 
00286 template <typename StdExcept, typename MessageType>
00287 ExceptionImpl<StdExcept, MessageType>::ExceptionImpl() : StdExcept("") {
00288 }
00289 
00290 /*--------------------------------------------------------------------------
00291  * ExceptionImpl::~ExceptionImpl()
00292  *------------------------------------------------------------------------*/
00293 
00294 template <typename StdExcept, typename MessageType>
00295 ExceptionImpl<StdExcept, MessageType>::~ExceptionImpl() throw() {
00296 }
00297 
00298 /*--------------------------------------------------------------------------
00299  * ExceptionImpl::getMessage()
00300  *------------------------------------------------------------------------*/
00301 
00302 template <typename StdExcept, typename MessageType>
00303 const Message&
00304 ExceptionImpl<StdExcept, MessageType>::getMessage() const {
00305   return message_;
00306 }
00307 
00308 /*--------------------------------------------------------------------------
00309  * ExceptionImpl::what()
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  * ExceptionImpl::operator&()
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 } // namespace exception
00332 
00333 using exception::Exception;
00334 using exception::RuntimeError;
00335 using exception::LogicError;
00336 
00337 } // namespace keyvalue
00338 
00339 #endif // KEYVALUE_SYS_EXCEPTION_EXCEPTION_H_