1
// ***************************************************************** -*- C++ -*-
3
* Copyright (C) 2004, 2005 Andreas Huggel <ahuggel@gmx.net>
5
* This program is part of the Exiv2 distribution.
7
* This program is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU General Public License
9
* as published by the Free Software Foundation; either version 2
10
* of the License, or (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
@brief Error class for exceptions
25
@author Andreas Huggel (ahu)
26
<a href="mailto:ahuggel@gmx.net">ahuggel@gmx.net</a>
27
@date 15-Jan-04, ahu: created<BR>
28
11-Feb-04, ahu: isolated as a component
33
// *****************************************************************************
34
// included header files
37
// + standard includes
41
// *****************************************************************************
42
// namespace extensions
45
// *****************************************************************************
48
//! Helper structure defining an error message
51
ErrMsg(int code, const char* message)
52
: code_(code), message_(message)
55
int code_; //!< Error code
56
const char* message_; //!< Error message
60
@brief Error class interface. Allows the definition and use of a hierarchy
61
of error classes which can all be handled in one catch block.
67
//! Virtual destructor.
75
//! Return the error code.
76
virtual int code() const =0;
78
@brief Return the error message. Consider using the output operator
79
operator<<(std::ostream &os, const AnyError& error) instead.
80
@note Unlike std::exception::what(), this function returns an
83
virtual std::string what() const =0;
86
//! %AnyBase output operator
87
inline std::ostream& operator<<(std::ostream& os, const AnyError& error)
89
return os << error.what();
93
@brief Simple error class used for exceptions. An output operator is
94
provided to print errors to a stream.
96
class Error : public AnyError {
100
//! Constructor taking only an error code
101
explicit Error(int code)
102
: code_(code), count_(0)
105
//! Constructor taking an error code and one argument
107
Error(int code, const A& arg1)
108
: code_(code), count_(1), arg1_(toString(arg1))
111
//! Constructor taking an error code and two arguments
112
template<typename A, typename B>
113
Error(int code, const A& arg1, const B& arg2)
114
: code_(code), count_(2),
115
arg1_(toString(arg1)), arg2_(toString(arg2))
118
//! Constructor taking an error code and three arguments
119
template<typename A, typename B, typename C>
120
Error(int code, const A& arg1, const B& arg2, const C& arg3)
121
: code_(code), count_(3),
122
arg1_(toString(arg1)), arg2_(toString(arg2)), arg3_(toString(arg3))
129
virtual int code() const { return code_; }
130
virtual std::string what() const;
134
static int errorIdx(int code);
137
int code_; //!< Error code
138
int count_; //!< Number of arguments
139
std::string arg1_; //!< First argument
140
std::string arg2_; //!< Second argument
141
std::string arg3_; //!< Third argument
143
static const ErrMsg errMsg_[]; //!< List of error messages
148
#endif // #ifndef ERROR_HPP_