Exception.h

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #ifndef ZYPP_BASE_EXCEPTION_H
00013 #define ZYPP_BASE_EXCEPTION_H
00014 
00015 #include <cerrno>
00016 #include <iosfwd>
00017 #include <stdexcept>
00018 
00020 namespace zypp
00021 { 
00022 
00023   namespace exception_detail
00024   { 
00025 
00029     struct CodeLocation
00030     {
00031       friend std::ostream & operator<<( std::ostream & str, const CodeLocation & obj );
00032 
00034       CodeLocation()
00035       : _line( 0 )
00036       {}
00037 
00039       CodeLocation( const std::string & file_r,
00040                     const std::string & func_r,
00041                     unsigned            line_r )
00042       : _file( file_r ), _func( func_r ), _line( line_r )
00043       {}
00044 
00046       std::string asString() const;
00047 
00048     private:
00049       std::string _file;
00050       std::string _func;
00051       unsigned    _line;
00052     };
00054 
00056     #define ZYPP_EX_CODELOCATION ::zypp::exception_detail::CodeLocation(__FILE__,__FUNCTION__,__LINE__)
00057 
00059     std::ostream & operator<<( std::ostream & str, const CodeLocation & obj );
00060 
00062   } // namespace exception_detail
00064 
00066   //
00067   //    CLASS NAME : Exception
00114   class Exception : public std::exception
00115   {
00116     friend std::ostream & operator<<( std::ostream & str, const Exception & obj );
00117 
00118   public:
00119     typedef exception_detail::CodeLocation CodeLocation;
00120 
00124     Exception();
00125 
00129     Exception( const std::string & msg_r );
00130 
00132     virtual ~Exception() throw();
00133 
00135     const CodeLocation & where() const
00136     { return _where; }
00137 
00139     void relocate( const CodeLocation & where_r ) const
00140     { _where = where_r; }
00141 
00147     const std::string & msg() const
00148     { return _msg; }
00149 
00151     std::string asString() const;
00152 
00154     std::string asUserString() const;
00155 
00156   protected:
00157 
00159     virtual std::ostream & dumpOn( std::ostream & str ) const;
00160 
00161   public:
00163     static std::string strErrno( int errno_r );
00165     static std::string strErrno( int errno_r, const std::string & msg_r );
00166 
00167   public:
00171     static void log( const Exception & excpt_r, const CodeLocation & where_r,
00172                      const char *const prefix_r );
00173 
00174   private:
00175     mutable CodeLocation _where;
00176     std::string _msg;
00177 
00179     virtual const char * what() const throw()
00180     { return _msg.c_str(); }
00181 
00186     std::ostream & dumpError( std::ostream & str ) const;
00187   };
00189 
00191   std::ostream & operator<<( std::ostream & str, const Exception & obj );
00192 
00194 
00196   template<class _Excpt>
00197     void _ZYPP_THROW( const _Excpt & excpt_r, const exception_detail::CodeLocation & where_r )
00198     {
00199       excpt_r.relocate( where_r );
00200       Exception::log( excpt_r, where_r, "THROW:   " );
00201       throw( excpt_r );
00202     }
00203 
00205   template<class _Excpt>
00206     void _ZYPP_CAUGHT( const _Excpt & excpt_r, const exception_detail::CodeLocation & where_r )
00207     {
00208       Exception::log( excpt_r, where_r, "CAUGHT:  " );
00209     }
00210 
00212   template<class _Excpt>
00213     void _ZYPP_RETHROW( const _Excpt & excpt_r, const exception_detail::CodeLocation & where_r )
00214     {
00215       Exception::log( excpt_r, where_r, "RETHROW: " );
00216       excpt_r.relocate( where_r );
00217       throw;
00218     }
00219 
00221 
00228 #define ZYPP_THROW(EXCPT)\
00229   _ZYPP_THROW( EXCPT, ZYPP_EX_CODELOCATION )
00230 
00232 #define ZYPP_CAUGHT(EXCPT)\
00233   _ZYPP_CAUGHT( EXCPT, ZYPP_EX_CODELOCATION )
00234 
00236 #define ZYPP_RETHROW(EXCPT)\
00237   _ZYPP_RETHROW( EXCPT, ZYPP_EX_CODELOCATION )
00238 
00239 
00241 #define ZYPP_THROW_MSG(EXCPTTYPE, MSG)\
00242   ZYPP_THROW( EXCPTTYPE( MSG ) )
00243 
00245 #define ZYPP_THROW_ERRNO(EXCPTTYPE)\
00246   ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(errno) ) )
00247 
00249 #define ZYPP_THROW_ERRNO1(EXCPTTYPE, ERRNO)\
00250   ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(ERRNO) ) )
00251 
00253 #define ZYPP_THROW_ERRNO_MSG(EXCPTTYPE, MSG)\
00254   ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(errno,MSG) ) )
00255 
00257 #define ZYPP_THROW_ERRNO_MSG1(EXCPTTYPE, ERRNO,MSG)\
00258   ZYPP_THROW( EXCPTTYPE( ::zypp::Exception::strErrno(ERRNO,MSG) ) )
00259 
00260 
00262 } // namespace zypp
00264 #endif // ZYPP_BASE_EXCEPTION_H

Generated on Thu Jul 6 00:07:20 2006 for zypp by  doxygen 1.4.6