Utils: Removed ErrnoException and implemented generic Exception base-class
[senf.git] / Utils / Exception.hh
index a3c66a9..0675aee 100644 (file)
@@ -1,9 +1,9 @@
 // $Id$
 //
 // Copyright (C) 2006
-// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
-// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
-//     Stefan Bund <stefan.bund@fokus.fraunhofer.de>
+// Fraunhofer Institute for Open Communication Systems (FOKUS)
+// Competence Center NETwork research (NET), St. Augustin, GERMANY
+//     Stefan Bund <g0dil@berlios.de>
 //
 // This program is free software; you can redistribute it and/or modify
 // it under the terms of the GNU General Public License as published by
 // Custom includes
 #include <exception>
 #include <string>
+#include <iostream>
+#include <sstream>
+#include <boost/preprocessor/repeat.hpp>
+#include <boost/preprocessor/cat.hpp>
+#include <boost/utility.hpp>
 
 //#include "Exception.mpp"
 ///////////////////////////////hh.p////////////////////////////////////////
 
+/** \defgroup exception Exception classes
+
+    All exceptions in senf are derived from senf::Exception. This class adds the possibility to
+    extend the exception description while it is processed:
+
+    \code
+    try {
+
+        // Some code which might raise an arbitrary senf exception
+
+    }
+    catch (senf::Exception & e) {
+        e << "\handling user " << user;
+        throw;
+    }
+    \endcode
+
+    This will add the user information to any senf exception thrown. The Exception is however not a
+    stream. If you need to do more extensive formating, either use an intermediate string-stream or
+    use <a href="http://www.boost.org/libs/format/doc/format.html">Boost.Format</a>:
+
+    \code
+    try { 
+        // ...
+    }
+    catch (senf::Exception & e) {
+        e << boost::format("\ncall id 0x%04x@%s") % id % address;
+    }
+    \endcode
+
+    senf::SystemException is thrown for all operating system errors (failures which result in the
+    operating system setting the errno value). It is also derived from senf::Exception and can
+    therefore be extended as well.
+
+    Defining your own exception classes derived from senf::Exception is very simple:
+
+    \code
+    struct FooException : public senf::Exception
+    { FooException() : senf::Exception("Foo hit the fan") {} };
+    \endcode
+ */
+
 namespace senf {
 
+    /** \brief Generic exception base-class
+
+        Exception is a generic exception base-class which allows the exception to be later extended
+        by catching and re-throwing it (See example in \ref exception).
+
+        \ingroup exception
+      */
+    class Exception
+        : public std::exception
+    {
+    public:
+        ///////////////////////////////////////////////////////////////////////////
+        ///\name Structors and default members
+        ///@{
+
+        virtual ~Exception() throw();
+
+        ///@}
+        ///////////////////////////////////////////////////////////////////////////
+
+        virtual char const * what() const throw();
+
+        template <class Arg>
+        Exception & operator<<(Arg const & arg); ///< Extend exception description
+                                        /**< Adds \a arg converted to string to the end of the
+                                             exception description string. This operator allows to
+                                             use Exception instances like streams. The conversion is
+                                             performed using <code>boost::lexical_cast</code> and is
+                                             therefor identical to a streaming operation. 
+                                             \see \ref exception */
+
+    protected:
+        Exception(std::string const & description = ""); ///< Initialize exception with string
+                                        /**< \a description is the initial error description
+                                             string. This should probably be a string constant
+                                             describing the exception for most derived
+                                             exceptions. */
+
+    private:
+        std::string message_;
+    };
+
+
     /** \brief Exception handling standard UNIX errors (errno)
 
-        This exception is thrown to signal generic errno failures.
+        This exception is thrown to signal generic \c errno failures. Normally the \c errno value is
+        automatically taken from the \c errno variable but it may also be specified explicitly:
 
-        \todo make where and err accessors and make the member vars private
+        \code
+        // Standard usage: Take \c errno from environment
+        throw senf::SystemException("::open()") 
+            << " while opening configuration file: " << filename;
 
-        \idea Add a template class derived from SystemException which
-        takes the error number as a numeric argument. This allows
-        catching specific errno conditions: ErrnoException<EPIPE> etc.
+        // You may however explicitly specify the errno value
+        throw senf::SystemException("::open()", ENOFILE)
 
-        \idea Add a generic error thrower which takes the origin
-        string and errno value as an argument and will throw a
-        corresponding template class instance. This would just be a
-        big switch statement containing all possible errno values,
-        probably created using some macro metaprogramming.
+        // Or leave the location information empty
+        throw senf::SystemException(ENOFILE);
+        throw senf::SystemException();
+        \endcode
+
+        \ingroup exception
      */
-    class SystemException : public std::exception
+    class SystemException : public Exception
     {
     public:
-        explicit SystemException(int err); ///< SystemException without error lokus info
-                                        /**< \param[in] err error number (the errno value) */
-        SystemException(char const * where, int err); ///< SystemException with error location info
-                                        /**< \param[in] where description of error origin
-                                             \param[in] err error number (the errno value) */
-
-        virtual char const * what() const throw(); ///< Return verbose error description
+        ///////////////////////////////////////////////////////////////////////////
+        ///\name Structors and default members
+        ///@{
 
-        char const * where; ///< Error origin
-        int err; ///< Error number
+        explicit SystemException(std::string const & where = ""); 
+        explicit SystemException(int code);
+        SystemException(std::string const & where, int code);
 
         virtual ~SystemException() throw();
 
+        ///@}
+        ///////////////////////////////////////////////////////////////////////////
+
+        int errorNumber() const;        ///< Error code (\c errno number)
+        char const * description() const; ///< Error description (\c strerror() value)
+
+        bool anyOf(int c0, int c1=0, int c2=0, int c3=0, int c4=0, int c5=0, 
+                   int c6=0, int c7=0, int c8=0, int c9=0);
+                                        ///< \c true, if errorNumber() is one of \a c0 ... \a c9
+
+
+
     private:
-        void init();
-        std::string buffer_;
+        void init(std::string const & where, int code);
+        
+        int code_;
     };
 
-    enum NoThrow_t { nothrow };
-
 }
 
 ///////////////////////////////hh.e////////////////////////////////////////
 #include "Exception.cci"
 //#include "Exception.ct"
-//#include "Exception.cti"
+#include "Exception.cti"
 #endif
 
 \f