utilitiesexceptions.h
1 /***************************************************************************
2  * Copyright (C) 2008 by Tomassino Ferrauto *
3  * t_ferrauto@yahoo.it *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
21 #ifndef UTILITIESEXCEPTIONS_H
22 #define UTILITIESEXCEPTIONS_H
23 
24 #include <exception>
25 #include <cstring>
26 #include <cstdio>
27 #include "baseexception.h"
28 #include "utilitiesconfig.h"
29 
30 // All the suff below is to avoid warnings on Windows about the use of the
31 // unsafe function sprintf and strcpy...
32 #if defined(_MSC_VER)
33  #pragma warning(push)
34  #pragma warning(disable:4996)
35 #endif
36 
37 namespace farsa {
38 // NOTE: I don't use snprintf instead of sprintf because it seems not to be in
39 // the current C++ standard (C++03, it is instead in the C99 standard). Note
40 // however that no buffer overflow is possible (buffer lengths are carefully
41 // checked)
42 
52 class FARSA_UTIL_TEMPLATE RuntimeUserException : public BaseException
53 {
54 public:
62  RuntimeUserException(const char* reason) throw() :
63  BaseException()
64  {
65  strncpy(m_reason, reason, 256);
66  m_reason[255] = '\0';
67  sprintf(m_errorMessage, "Generic runtime exception, reason: %s", m_reason);
68  m_errorMessage[511] = '\0';
69  }
70 
77  BaseException(other)
78  {
79  strncpy(m_reason, other.m_reason, 256);
80  m_reason[255] = '\0';
81  strncpy(m_errorMessage, other.m_errorMessage, 512);
82  m_errorMessage[511] = '\0';
83  }
84 
90  RuntimeUserException& operator=(const RuntimeUserException& other) throw()
91  {
92  if (&other == this) {
93  return *this;
94  }
95 
96  BaseException::operator=(other);
97  strncpy(m_reason, other.m_reason, 256);
98  m_reason[255] = '\0';
99  strncpy(m_errorMessage, other.m_errorMessage, 512);
100  m_errorMessage[511] = '\0';
101 
102  return *this;
103  }
104 
108  virtual ~RuntimeUserException() throw()
109  {
110  }
111 
117  virtual const char *what() const throw()
118  {
119  return m_errorMessage;
120  }
121 
127  const char *reason() const throw()
128  {
129  return m_reason;
130  }
131 
136  EXCEPTION_HELPER_FUNCTIONS(RuntimeUserException)
137 
138 private:
142  char m_reason[256];
143 
147  char m_errorMessage[512];
148 };
149 
157 inline void FARSA_UTIL_TEMPLATE throwUserRuntimeError(QString reason)
158 {
159  throw RuntimeUserException(reason.toAscii().data());
160 }
161 
162 } // end namespace farsa
163 
164 // All the suff below is to restore the warning state on Windows
165 #if defined(_MSC_VER)
166  #pragma warning(pop)
167 #endif
168 
169 #endif