Utils: Implement more flexible SystemException
[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 prefix_ senf::SystemException::SystemException(SystemException const & other)
31     : std::stringstream(other.str(),std::ios::out), code_(other.code_)
32 {}
33
34 prefix_ senf::SystemException::SystemException(std::string const & where, int code)
35     : std::stringstream(std::ios::out), code_(code)
36 {
37     if (! where.empty())
38         (*this) << where << ": ";
39     (*this) << "(" << code_ << ") " << description();
40 }
41
42 prefix_ char const * senf::SystemException::what()
43     const throw()
44 {
45     /// \fixme Replace the 'stringstream' base-class with our own stream with a specialized
46     /// streambuf which allows to efficiently access the contents as a C string.
47     buffer_ = this->str();
48     return buffer_.c_str();
49 }
50
51 prefix_ int senf::SystemException::code()
52     const
53 {
54     return code_;
55 }
56
57 prefix_ char const * senf::SystemException::description()
58     const
59 {
60     return std::strerror(code_);
61 }
62
63 prefix_ bool senf::SystemException::anyOf(int c0, int c1, int c2, int c3, int c4, int c5,
64                                           int c6, int c7, int c8, int c9)
65 {
66     return 
67         (c0 && code_ == c0) ||
68         (c1 && code_ == c1) ||
69         (c2 && code_ == c2) ||
70         (c3 && code_ == c3) ||
71         (c4 && code_ == c4) ||
72         (c5 && code_ == c5) ||
73         (c6 && code_ == c6) ||
74         (c7 && code_ == c7) ||
75         (c8 && code_ == c8) ||
76         (c9 && code_ == c9);
77 }
78
79 prefix_  senf::SystemException::~SystemException()
80     throw()
81 {}
82
83 prefix_ void senf::throwErrno()
84 {
85     throwErrno("", errno);
86 }
87
88 prefix_ void senf::throwErrno(std::string const & where)
89 {
90     throwErrno(where, errno);
91 }
92
93 prefix_ void senf::throwErrno(int code)
94 {
95     throwErrno("", code);
96 }
97
98 ///////////////////////////////cci.e///////////////////////////////////////
99 #undef prefix_
100
101 \f
102 // Local Variables:
103 // mode: c++
104 // fill-column: 100
105 // c-file-style: "senf"
106 // indent-tabs-mode: nil
107 // ispell-local-dictionary: "american"
108 // compile-command: "scons -u test"
109 // comment-column: 40
110 // End: