utilities/include/utilitiesexceptions.h

00001 /***************************************************************************
00002  *   Copyright (C) 2008 by Tomassino Ferrauto                              *
00003  *   t_ferrauto@yahoo.it                                                   *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #ifndef UTILITIESEXCEPTIONS_H
00022 #define UTILITIESEXCEPTIONS_H
00023 
00024 #include <exception>
00025 #include <cstring>
00026 #include <cstdio>
00027 #include "baseexception.h"
00028 #include "utilitiesconfig.h"
00029 
00030 // All the suff below is to avoid warnings on Windows about the use of the
00031 // unsafe function sprintf and strcpy...
00032 #if defined(_MSC_VER)
00033     #pragma warning(push)
00034     #pragma warning(disable:4996)
00035 #endif
00036 
00037 namespace farsa {
00038 // NOTE: I don't use snprintf instead of sprintf because it seems not to be in
00039 // the current C++ standard (C++03, it is instead in the C99 standard). Note
00040 // however that no buffer overflow is possible (buffer lengths are carefully
00041 // checked)
00042 
00052 class FARSA_UTIL_TEMPLATE RuntimeUserException : public BaseException
00053 {
00054 public:
00062     RuntimeUserException(const char* reason) throw() :
00063         BaseException()
00064     {
00065         strncpy(m_reason, reason, 256);
00066         m_reason[255] = '\0';
00067         sprintf(m_errorMessage, "Generic runtime exception, reason: %s", m_reason);
00068         m_errorMessage[511] = '\0';
00069     }
00070 
00076     RuntimeUserException(const RuntimeUserException& other) throw() :
00077         BaseException(other)
00078     {
00079         strncpy(m_reason, other.m_reason, 256);
00080         m_reason[255] = '\0';
00081         strncpy(m_errorMessage, other.m_errorMessage, 512);
00082         m_errorMessage[511] = '\0';
00083     }
00084 
00090     RuntimeUserException& operator=(const RuntimeUserException& other) throw()
00091     {
00092         if (&other == this) {
00093             return *this;
00094         }
00095 
00096         BaseException::operator=(other);
00097         strncpy(m_reason, other.m_reason, 256);
00098         m_reason[255] = '\0';
00099         strncpy(m_errorMessage, other.m_errorMessage, 512);
00100         m_errorMessage[512] = '\0';
00101 
00102         return *this;
00103     }
00104 
00108     virtual ~RuntimeUserException() throw()
00109     {
00110     }
00111 
00117     virtual const char *what() const throw()
00118     {
00119         return m_errorMessage;
00120     }
00121 
00127     const char *reason() const throw()
00128     {
00129         return m_reason;
00130     }
00131 
00136     EXCEPTION_HELPER_FUNCTIONS(RuntimeUserException)
00137 
00138 private:
00142     char m_reason[256];
00143 
00147     char m_errorMessage[512];
00148 };
00149 
00157 inline void FARSA_UTIL_TEMPLATE throwUserRuntimeError(QString reason)
00158 {
00159     throw RuntimeUserException(reason.toAscii().data());
00160 }
00161 
00162 } // end namespace farsa
00163 
00164 // All the suff below is to restore the warning state on Windows
00165 #if defined(_MSC_VER)
00166     #pragma warning(pop)
00167 #endif
00168 
00169 #endif