1 |
|
2 |
/* $Id$ */ |
3 |
|
4 |
/******************************************************* |
5 |
* |
6 |
* Copyright 2003-2007 by ACceSS MNRF |
7 |
* Copyright 2007 by University of Queensland |
8 |
* |
9 |
* http://esscc.uq.edu.au |
10 |
* Primary Business: Queensland, Australia |
11 |
* Licensed under the Open Software License version 3.0 |
12 |
* http://www.opensource.org/licenses/osl-3.0.php |
13 |
* |
14 |
*******************************************************/ |
15 |
|
16 |
#ifndef ESYSEXCEPTION_H |
17 |
#define ESYSEXCEPTION_H |
18 |
#include "system_dep.h" |
19 |
|
20 |
#include <string> |
21 |
#include <exception> |
22 |
#include <iostream> |
23 |
|
24 |
namespace esysUtils |
25 |
{ |
26 |
/** |
27 |
\page esys_exception Esys Exceptions |
28 |
A base class for exception classes used within Esys system. |
29 |
|
30 |
\version 1.0.0 |
31 |
|
32 |
\section class_desc Class Description: |
33 |
A base class for exception classes used within Esys system. |
34 |
|
35 |
\section class_limits Class Limitations: |
36 |
None |
37 |
|
38 |
\section class_conds Class Conditions of Use: |
39 |
None |
40 |
|
41 |
\section throws Throws: |
42 |
None |
43 |
|
44 |
*/ |
45 |
class EsysException : public std::exception |
46 |
{ |
47 |
|
48 |
protected: |
49 |
|
50 |
typedef std::exception Parent; |
51 |
|
52 |
|
53 |
public: |
54 |
/** |
55 |
\brief |
56 |
Default Constructor. Creates an exception with no message. |
57 |
*/ |
58 |
ESYSUTILS_DLL_API |
59 |
EsysException(); |
60 |
|
61 |
/** |
62 |
* \brief |
63 |
Constructor which creates a EsysException with the given message |
64 |
|
65 |
@param exceptionReason Input - Exception message. |
66 |
*/ |
67 |
ESYSUTILS_DLL_API |
68 |
EsysException(const std::string &exceptionReason); |
69 |
|
70 |
/** |
71 |
* \brief |
72 |
Constructor which creates a EsysException with the given message |
73 |
|
74 |
@param cStr - Exception message. |
75 |
*/ |
76 |
ESYSUTILS_DLL_API |
77 |
EsysException( const char *cStr ); |
78 |
|
79 |
/** |
80 |
* \brief |
81 |
Copy constructor |
82 |
|
83 |
@param other Input - EsysException |
84 |
*/ |
85 |
ESYSUTILS_DLL_API |
86 |
EsysException(const EsysException &other); |
87 |
|
88 |
/// Destructor |
89 |
ESYSUTILS_DLL_API |
90 |
virtual ~EsysException() THROW_ANY; |
91 |
|
92 |
/** |
93 |
\brief |
94 |
Return the exception message in the form |
95 |
<Exception Name>: <Exception Message> |
96 |
|
97 |
@return the exception message. |
98 |
*/ |
99 |
inline |
100 |
const std::string & toString() const; |
101 |
|
102 |
/** |
103 |
\brief |
104 |
Return the name of the exception. This is expected to be overloaded |
105 |
in derived classes with the derived class name. |
106 |
|
107 |
@return the name of the exception. |
108 |
*/ |
109 |
ESYSUTILS_DLL_API |
110 |
virtual const std::string & exceptionName() const; |
111 |
|
112 |
/** |
113 |
\brief |
114 |
Return a reference to the string that contains the exception reason. |
115 |
|
116 |
@return the string for the exception reason. |
117 |
*/ |
118 |
inline |
119 |
const std::string& reason() const; |
120 |
|
121 |
/** |
122 |
\brief |
123 |
set the string for the reason for the exception. |
124 |
This allows ousiders to modify m_reason, but the practice is discouraged. |
125 |
If string insertions are required, use string methods. |
126 |
*/ |
127 |
inline |
128 |
void setReason(const std::string &new_reason); |
129 |
|
130 |
/** |
131 |
\brief |
132 |
Return a description of the exception in the same format as the toString |
133 |
method. |
134 |
|
135 |
@return a description of the exception. |
136 |
*/ |
137 |
inline |
138 |
virtual const char* what() const THROW_ANY; |
139 |
|
140 |
|
141 |
/** |
142 |
\brief |
143 |
update m_exceptionMessage after a reason update. |
144 |
**/ |
145 |
inline |
146 |
void updateMessage(); |
147 |
|
148 |
|
149 |
private: |
150 |
// |
151 |
// the exception reason |
152 |
std::string m_reason; |
153 |
|
154 |
// |
155 |
// the full exception message |
156 |
std::string m_exceptionMessage; |
157 |
|
158 |
// |
159 |
// the exception name is immutable and class-wide. |
160 |
// Inheritor note; you need one of these too. |
161 |
// and an overloaded exceptionName() in your .cpp implementation file. |
162 |
static const std::string exceptionNameValue; |
163 |
|
164 |
}; |
165 |
|
166 |
/** |
167 |
\brief |
168 |
Stream insertion (print) operator for EsysExceptions |
169 |
|
170 |
@param output Input - Output stream. |
171 |
@param inException Input - The exception to be inserted into the output |
172 |
stream. |
173 |
*/ |
174 |
ESYSUTILS_DLL_API |
175 |
std::ostream &operator<<(std::ostream &output, EsysException &inException); |
176 |
|
177 |
|
178 |
//////////////////////////////////////////////////////////////////// |
179 |
|
180 |
const std::string & EsysException::reason() const |
181 |
{ |
182 |
return m_reason; |
183 |
} |
184 |
|
185 |
// return the message as a std::string |
186 |
const std::string & EsysException::toString() const |
187 |
{ |
188 |
return m_exceptionMessage; |
189 |
} |
190 |
|
191 |
void EsysException::setReason(const std::string &new_reason) |
192 |
{ |
193 |
m_reason = new_reason; |
194 |
updateMessage(); |
195 |
} |
196 |
|
197 |
const char* EsysException::what() const THROW_ANY |
198 |
{ |
199 |
return m_exceptionMessage.c_str(); |
200 |
} |
201 |
|
202 |
void EsysException::updateMessage() |
203 |
{ |
204 |
m_exceptionMessage = exceptionName() + ": " + m_reason; |
205 |
} |
206 |
|
207 |
} |
208 |
|
209 |
#endif |