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 |
public: |
48 |
/** |
49 |
\brief |
50 |
Default Constructor. Creates an exception with no message. |
51 |
*/ |
52 |
EsysException(); |
53 |
|
54 |
/** |
55 |
* \brief |
56 |
Constructor which creates a EsysException with the given message |
57 |
|
58 |
@param exceptionReason Input - Exception message. |
59 |
*/ |
60 |
EsysException(const std::string &exceptionReason); |
61 |
|
62 |
/** |
63 |
* \brief |
64 |
Constructor which creates a EsysException with the given message |
65 |
|
66 |
@param cStr - Exception message. |
67 |
*/ |
68 |
EsysException( const char *cStr ); |
69 |
|
70 |
/** |
71 |
* \brief |
72 |
Copy constructor |
73 |
|
74 |
@param inException Input - EsysException |
75 |
*/ |
76 |
EsysException(const EsysException &inException); |
77 |
|
78 |
/// Destructor |
79 |
virtual ~EsysException() THROW(); |
80 |
|
81 |
/** |
82 |
\brief |
83 |
Assignment operator. |
84 |
|
85 |
@param inException Input - Exception to be copied. |
86 |
*/ |
87 |
EsysException &operator=(const EsysException &inException); |
88 |
|
89 |
/** |
90 |
\brief |
91 |
Return the exception message in the form |
92 |
<Exception Name>: <Exception Message> |
93 |
|
94 |
@return the exception message. |
95 |
*/ |
96 |
const std::string & toString() const; |
97 |
|
98 |
/** |
99 |
\brief |
100 |
Return the name of the exception. This is expected to be overloaded |
101 |
in derived classes with the derived class name. |
102 |
|
103 |
@return the name of the exception. |
104 |
*/ |
105 |
virtual const std::string & exceptionName() const; |
106 |
|
107 |
/** |
108 |
\brief |
109 |
Return a reference to the string that contains the exception reason. |
110 |
|
111 |
@return the string for the exception reason. |
112 |
*/ |
113 |
const std::string& reason() const; |
114 |
|
115 |
/** |
116 |
\brief |
117 |
set the string for the reason for the exception. |
118 |
This allows ousiders to modify m_reason, but the practice is discouraged. |
119 |
If string insertions are required, use string methods. |
120 |
*/ |
121 |
void setReason(const std::string &new_reason); |
122 |
|
123 |
/** |
124 |
\brief |
125 |
Return a description of the exception in the same format as the toString |
126 |
method. |
127 |
|
128 |
@return a description of the exception. |
129 |
*/ |
130 |
virtual const char* what() const THROW(); |
131 |
|
132 |
|
133 |
/** |
134 |
\brief |
135 |
update m_exceptionMessage after a reason update. |
136 |
**/ |
137 |
void updateMessage(); |
138 |
|
139 |
|
140 |
private: |
141 |
// |
142 |
// the exception reason |
143 |
std::string m_reason; |
144 |
|
145 |
// |
146 |
// the full exception message |
147 |
std::string m_exceptionMessage; |
148 |
|
149 |
// |
150 |
// the exception name is immutable and class-wide. |
151 |
// Inheritor note; you need one of these too. |
152 |
// and an overloaded exceptionName() in your .cpp implementation file. |
153 |
static const std::string exceptionNameValue; |
154 |
|
155 |
}; |
156 |
|
157 |
/** |
158 |
\brief |
159 |
Stream insertion (print) operator for EsysExceptions |
160 |
|
161 |
@param output Input - Output stream. |
162 |
@param inException Input - The exception to be inserted into the output |
163 |
stream. |
164 |
*/ |
165 |
std::ostream &operator<<(std::ostream &output, EsysException &inException); |
166 |
|
167 |
|
168 |
//////////////////////////////////////////////////////////////////// |
169 |
|
170 |
inline |
171 |
const std::string & EsysException::reason() const |
172 |
{ |
173 |
return m_reason; |
174 |
} |
175 |
|
176 |
// return the message as a std::string |
177 |
inline |
178 |
const std::string & EsysException::toString() const |
179 |
{ |
180 |
return m_exceptionMessage; |
181 |
} |
182 |
|
183 |
inline |
184 |
void EsysException::setReason(const std::string &new_reason) |
185 |
{ |
186 |
m_reason = new_reason; |
187 |
updateMessage(); |
188 |
} |
189 |
|
190 |
inline |
191 |
const char* EsysException::what() const THROW() |
192 |
{ |
193 |
return m_exceptionMessage.c_str(); |
194 |
} |
195 |
|
196 |
|
197 |
|
198 |
inline |
199 |
void EsysException::updateMessage() |
200 |
{ |
201 |
m_exceptionMessage = exceptionName() + ": " + m_reason; |
202 |
} |
203 |
|
204 |
} |
205 |
|
206 |
#endif |