Utils: Removed ErrnoException and implemented generic Exception base-class
[senf.git] / Utils / Exception.cci
1 // $Id$
2 //
3 // Copyright (C) 2006 Stefan Bund <g0dil@senf.berlios.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the
17 // Free Software Foundation, Inc.,
18 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 /** \file
21     \brief Exception inline non-template implementation */
22
23 // Custom includes
24 #include <errno.h>
25 #include <cstring>
26
27 #define prefix_ inline
28 ///////////////////////////////cci.p///////////////////////////////////////
29
30 ///////////////////////////////////////////////////////////////////////////
31 // senf::Exception
32 prefix_ senf::Exception::Exception(std::string const & description)
33     : message_(description)
34 {}
35
36 ///////////////////////////////////////////////////////////////////////////
37
38 prefix_ senf::SystemException::SystemException(std::string const & where)
39 {
40     init(where, errno);
41 }
42
43 prefix_ senf::SystemException::SystemException(int code)
44 {
45     init("", code);
46 }
47
48 prefix_ senf::SystemException::SystemException(std::string const & where, int code)
49 {
50     init(where, code);
51 }
52
53 prefix_ int senf::SystemException::errorNumber()
54     const
55 {
56     return code_;
57 }
58
59 prefix_ char const * senf::SystemException::description()
60     const
61 {
62     return std::strerror(code_);
63 }
64
65 prefix_ bool senf::SystemException::anyOf(int c0, int c1, int c2, int c3, int c4, int c5,
66                                           int c6, int c7, int c8, int c9)
67 {
68     return 
69         (c0 && code_ == c0) ||
70         (c1 && code_ == c1) ||
71         (c2 && code_ == c2) ||
72         (c3 && code_ == c3) ||
73         (c4 && code_ == c4) ||
74         (c5 && code_ == c5) ||
75         (c6 && code_ == c6) ||
76         (c7 && code_ == c7) ||
77         (c8 && code_ == c8) ||
78         (c9 && code_ == c9);
79 }
80
81 prefix_  senf::SystemException::~SystemException()
82     throw()
83 {}
84
85 prefix_ void senf::SystemException::init(std::string const & where, int code)
86 {
87     code_ = code;
88     if (! where.empty())
89         (*this) << where << ": ";
90     (*this) << "(" << code << ") " << description();
91 }
92
93 ///////////////////////////////cci.e///////////////////////////////////////
94 #undef prefix_
95
96 \f
97 // Local Variables:
98 // mode: c++
99 // fill-column: 100
100 // c-file-style: "senf"
101 // indent-tabs-mode: nil
102 // ispell-local-dictionary: "american"
103 // compile-command: "scons -u test"
104 // comment-column: 40
105 // End: