c6e9ba35758b6534ec231d1458da376b28d4e576
[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 errno failures.
43
44         \todo make where and err accessors and make the member variables private
45
46         \idea Add a template class derived from SystemException which takes the error number as a
47         numeric argument. This allows catching specific errno conditions: ErrnoException<EPIPE> etc.
48
49         \idea Add a generic error thrower which takes the origin string and errno value as an
50         argument and will throw a corresponding template class instance. This would just be a big
51         switch statement containing all possible errno values, probably created using some macro
52         meta-programming.
53      */
54     class SystemException : public std::exception
55     {
56     public:
57         SystemException();              ///< SystemException without error location infor
58                                         /**< The error code is taken from the current value of the
59                                              global 'errno' variable  */
60
61         explicit SystemException(int code); ///< SystemException without error location info
62                                         /**< \param[in] code error number (the errno value) */
63
64         explicit SystemException(char const * where); ///< SystemException with error location info
65                                         /**< The error code is taken from the current value of the
66                                              global 'errno' variable 
67                                              \param[in] where description of error origin */
68
69         SystemException(char const * where, int code); ///< SystemException with error location info 
70                                         /**< \param[in] where description of error origin
71                                              \param[in] code error number (the errno value) */
72
73         virtual char const * what() const throw(); ///< Return verbose error description
74
75         char const * where() const;     ///< Error origin
76         int code() const;               ///< Error code (errno number)
77         char const * description() const; ///< Error description (strerror() value)
78
79         bool anyOf(int c0, int c1=0, int c2=0, int c3=0, int c4=0, int c5=0, 
80                    int c6=0, int c7=0, int c8=0, int c9=0);
81
82         virtual ~SystemException() throw();
83
84     private:
85         void init();
86
87         char const * const where_;
88         int const code_;
89         std::string buffer_;
90     };
91
92     template <int Code>
93     class ErrnoException : public SystemException
94     {
95     public:
96         static int const fixed_code = Code;
97
98         ErrnoException();
99         explicit ErrnoException(char const * where);
100     };
101
102     void throwErrno();
103     void throwErrno(char const * where);
104     void throwErrno(int code);
105     void throwErrno(char const * where, int code);
106
107     enum NoThrow_t { nothrow };
108
109 }
110
111 ///////////////////////////////hh.e////////////////////////////////////////
112 #include "Exception.cci"
113 //#include "Exception.ct"
114 #include "Exception.cti"
115 #endif
116
117 \f
118 // Local Variables:
119 // mode: c++
120 // fill-column: 100
121 // c-file-style: "senf"
122 // indent-tabs-mode: nil
123 // ispell-local-dictionary: "american"
124 // compile-command: "scons -u test"
125 // comment-column: 40
126 // End: