exceptions.hh

Go to the documentation of this file.
00001 // -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002 // vi: set et ts=8 sw=2 sts=2:
00003 // $Id$
00004 
00005 #ifndef DUNE_EXCEPTIONS_HH
00006 #define DUNE_EXCEPTIONS_HH
00007 
00008 #include <string>
00009 #include <sstream>
00010 
00011 namespace Dune {
00012 
00071 /* forward declarations */
00072 class Exception;
00073 struct ExceptionHook;
00074 
00092 class Exception {
00093 public:
00094   Exception ();
00095   void message(const std::string &message); 
00096   const std::string& what() const;          
00097   static void registerHook (ExceptionHook * hook);  
00098   static void clearHook ();                         
00099 private:
00100   std::string _message;
00101   static ExceptionHook * _hook;
00102 };
00103 
00169 struct ExceptionHook
00170 {
00171   virtual ~ExceptionHook() {}
00172   virtual void operator () () = 0;
00173 };
00174 
00175 /*
00176   Implementation of Dune::Exception
00177  */
00178 
00179 inline Exception::Exception ()
00180 {
00181   // call the hook if necessary
00182   if (_hook != 0) _hook->operator()();
00183 }
00184 
00185 inline void Exception::registerHook (ExceptionHook * hook)
00186 {
00187   _hook = hook;
00188 }
00189 
00190 inline void Exception::clearHook ()
00191 {
00192   _hook = 0;
00193 }
00194 
00195 inline void Exception::message(const std::string &message)
00196 {
00197   _message = message;
00198 }
00199 
00200 inline const std::string& Exception::what() const
00201 {
00202   return _message;
00203 }
00204 
00205 inline std::ostream& operator<<(std::ostream &stream, const Exception &e)
00206 {
00207   return stream << e.what();
00208 }
00209 
00210 #ifndef DOXYGEN
00211 // the "format" the exception-type gets printed.  __FILE__ and
00212 // __LINE__ are standard C-defines, the GNU cpp-infofile claims that
00213 // C99 defines __func__ as well. __FUNCTION__ is a GNU-extension
00214 #define THROWSPEC(E) #E << " [" << __func__ << ":" << __FILE__ << ":" << __LINE__ << "]: "
00215 #endif // DOXYGEN
00216 
00242 // this is the magic: use the usual do { ... } while (0) trick, create
00243 // the full message via a string stream and throw the created object
00244 #define DUNE_THROW(E, m) do { E th__ex; std::ostringstream th__out; \
00245  th__out << THROWSPEC(E) << m; th__ex.message(th__out.str()); throw th__ex; \
00246  } while (0)
00247 
00257 class IOError : public Exception {};
00258 
00267 class MathError : public Exception {};
00268 
00280 class RangeError : public Exception {};
00281 
00289 class NotImplemented : public Exception {};
00290 
00297 class SystemError : public Exception {};
00298 
00302 class OutOfMemoryError : public SystemError {};
00303 
00307 class InvalidStateException : public Exception {};
00308 
00313 class ParallelError : public Exception {};
00314 
00315 } // end namespace
00316 
00317 #endif

Generated on Fri Apr 29 2011 with Doxygen (ver 1.7.1) [doxygen-log,error-log].