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