/[escript]/branches/windows_from_1456_trunk_1522_merged_in/esysUtils/src/EsysException.h
ViewVC logotype

Contents of /branches/windows_from_1456_trunk_1522_merged_in/esysUtils/src/EsysException.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1526 - (show annotations)
Mon Apr 28 10:19:08 2008 UTC (12 years, 6 months ago) by phornby
File MIME type: text/plain
File size: 4927 byte(s)
Add operator= and modify copy constructor to guard
against implementations that do pointer copies in
std::exception......
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 Assignment needed to override any automatic assignment
95 of std::exception, which can potentially copy around char *'s,
96 causeing trouble in some implementations of STL.
97 It will only copy the reason string, and update the message.
98
99 @return re-assigned exception.
100 */
101 ESYSUTILS_DLL_API
102 virtual EsysException &
103 operator=(const EsysException &other) THROW_ANY;
104
105 /**
106 \brief
107 Return the exception message in the form
108 &lt;Exception Name&gt;: &lt;Exception Message&gt;
109
110 @return the exception message.
111 */
112 inline
113 const std::string & toString() const;
114
115 /**
116 \brief
117 Return the name of the exception. This is expected to be overloaded
118 in derived classes with the derived class name.
119
120 @return the name of the exception.
121 */
122 ESYSUTILS_DLL_API
123 virtual const std::string & exceptionName() const;
124
125 /**
126 \brief
127 Return a reference to the string that contains the exception reason.
128
129 @return the string for the exception reason.
130 */
131 inline
132 const std::string& reason() const;
133
134 /**
135 \brief
136 set the string for the reason for the exception.
137 This allows ousiders to modify m_reason, but the practice is discouraged.
138 If string insertions are required, use string methods.
139 */
140 inline
141 void setReason(const std::string &new_reason);
142
143 /**
144 \brief
145 Return a description of the exception in the same format as the toString
146 method.
147
148 @return a description of the exception.
149 */
150 inline
151 virtual const char* what() const THROW_ANY;
152
153
154 /**
155 \brief
156 update m_exceptionMessage after a reason update.
157 **/
158 inline
159 void updateMessage();
160
161
162 private:
163 //
164 // the exception reason
165 std::string m_reason;
166
167 //
168 // the full exception message
169 std::string m_exceptionMessage;
170
171 //
172 // the exception name is immutable and class-wide.
173 // Inheritor note; you need one of these too.
174 // and an overloaded exceptionName() in your .cpp implementation file.
175 static const std::string exceptionNameValue;
176
177 };
178
179 /**
180 \brief
181 Stream insertion (print) operator for EsysExceptions
182
183 @param output Input - Output stream.
184 @param inException Input - The exception to be inserted into the output
185 stream.
186 */
187 ESYSUTILS_DLL_API
188 std::ostream &operator<<(std::ostream &output, EsysException &inException);
189
190
191 ////////////////////////////////////////////////////////////////////
192
193 const std::string & EsysException::reason() const
194 {
195 return m_reason;
196 }
197
198 // return the message as a std::string
199 const std::string & EsysException::toString() const
200 {
201 return m_exceptionMessage;
202 }
203
204 void EsysException::setReason(const std::string &new_reason)
205 {
206 m_reason = new_reason;
207 updateMessage();
208 }
209
210 const char* EsysException::what() const THROW_ANY
211 {
212 return m_exceptionMessage.c_str();
213 }
214
215 void EsysException::updateMessage()
216 {
217 m_exceptionMessage = exceptionName() + ": " + m_reason;
218 }
219
220 }
221
222 #endif

Properties

Name Value
svn:eol-style native
svn:executable *
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26