Utils: Revamp documentation overview and add some missing docs
[senf.git] / Utils / Exception.hh
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief Exception public header */
25
26 #ifndef HH_Exception_
27 #define HH_Exception_ 1
28
29 // Custom includes
30 #include <exception>
31 #include <string>
32 #include <boost/preprocessor/repeat.hpp>
33 #include <boost/preprocessor/cat.hpp>
34
35 //#include "Exception.mpp"
36 ///////////////////////////////hh.p////////////////////////////////////////
37
38 namespace senf {
39
40     /** \brief Exception handling standard UNIX errors (errno)
41
42         This exception is thrown to signal generic \c errno failures. In addition to the \c errno
43         number (the code()), this class manages optional origin information. This parameter should
44         be provided to further describe, in what context the exception was created.
45
46         This exception should not be used directly. Instead the derived class ErrnoException should
47         be thrown via one of the senf::throwErrno() helpers.
48
49         \see ErrnoException
50      */
51     class SystemException : public std::exception
52     {
53     public:
54         SystemException();              ///< SystemException without error location infor
55                                         /**< The error code is taken from the current value of the
56                                              global \c errno variable  */
57
58         explicit SystemException(int code); ///< SystemException without error location info
59                                         /**< \param[in] code error number (the \c errno value) */
60
61         explicit SystemException(char const * where); ///< SystemException with error location info
62                                         /**< The error code is taken from the current value of the
63                                              global \c errno variable 
64                                              \param[in] where description of error origin */
65
66         SystemException(char const * where, int code); ///< SystemException with error location info 
67                                         /**< \param[in] where description of error origin
68                                              \param[in] code error number (the \c errno value) */
69
70         virtual char const * what() const throw(); ///< Return verbose error description
71
72         char const * where() const;     ///< Error origin
73         int code() const;               ///< Error code (\c errno number)
74         char const * description() const; ///< Error description (strerror() value)
75
76         bool anyOf(int c0, int c1=0, int c2=0, int c3=0, int c4=0, int c5=0, 
77                    int c6=0, int c7=0, int c8=0, int c9=0);
78                                         ///< \c true, if code() is one of \a c0 ... \a c9
79
80         virtual ~SystemException() throw();
81
82     private:
83         void init();
84
85         char const * const where_;
86         int const code_;                // This must be const to make the derived ErrnoException
87                                         // class a valid derived class.
88         std::string buffer_;
89     };
90
91     /** \brief Error specific system exception
92
93         This template restricts the generic SystemException to a specific, compile-time constant
94         error number \p Code. This allows a specific \c errno number to be cached explicitly.
95
96         This exception is normally thrown via one of the senf::throwErrno() helpers. These helpers
97         take the numeric \c errno value (either from the \c errno variable or from their
98         argument) and will throw the corresponding ErrnoException:
99         \code
100         if ((fd = ::open(filename, O_RDWR)) < 0)
101              senf::throwErrno("open()");
102         \endcode
103      */
104     template <int Code>
105     class ErrnoException : public SystemException
106     {
107     public:
108         static int const fixed_code = Code;
109
110         ErrnoException();               ///< ErrnoException without error location information
111         explicit ErrnoException(char const * where);
112                                         ///< ErrnoException with error location information
113     };
114
115     
116     /** \brief Throw ErrnoException based on current \c errno value
117         \related ErrnoException
118      */
119     void throwErrno();
120
121     /** \brief Throw ErrnoException based on current \c errno value (with location info)
122         \related ErrnoException
123      */
124     void throwErrno(char const * where);
125
126     /** \brief Throw ErrnoException based on given \c errno value
127         \related ErrnoException
128      */
129     void throwErrno(int code);
130
131     /** \brief Throw ErrnoException based on given \c errno value (with location info)
132         \related ErrnoException
133      */
134     void throwErrno(char const * where, int code);
135
136     enum NoThrow_t { nothrow };
137
138 }
139
140 ///////////////////////////////hh.e////////////////////////////////////////
141 #include "Exception.cci"
142 //#include "Exception.ct"
143 #include "Exception.cti"
144 #endif
145
146 \f
147 // Local Variables:
148 // mode: c++
149 // fill-column: 100
150 // c-file-style: "senf"
151 // indent-tabs-mode: nil
152 // ispell-local-dictionary: "american"
153 // compile-command: "scons -u test"
154 // comment-column: 40
155 // End: