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 
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.toLatin1().data());
160 }
161 
169 class FARSA_UTIL_TEMPLATE StandardLibraryException : public BaseException
170 {
171 public:
180  StandardLibraryException(const std::exception& origException) throw() :
181  BaseException()
182  {
183  strncpy(m_reason, origException.what(), 256);
184  m_reason[255] = '\0';
185  sprintf(m_errorMessage, "Standard library exception thrown, what(): %s", m_reason);
186  m_errorMessage[511] = '\0';
187  }
188 
195  BaseException(other)
196  {
197  strncpy(m_reason, other.m_reason, 256);
198  m_reason[255] = '\0';
199  strncpy(m_errorMessage, other.m_errorMessage, 512);
200  m_errorMessage[511] = '\0';
201  }
202 
209  {
210  if (&other == this) {
211  return *this;
212  }
213 
214  BaseException::operator=(other);
215  strncpy(m_reason, other.m_reason, 256);
216  m_reason[255] = '\0';
217  strncpy(m_errorMessage, other.m_errorMessage, 512);
218  m_errorMessage[511] = '\0';
219 
220  return *this;
221  }
222 
226  virtual ~StandardLibraryException() throw()
227  {
228  }
229 
235  virtual const char *what() const throw()
236  {
237  return m_errorMessage;
238  }
239 
245  const char *reason() const throw()
246  {
247  return m_reason;
248  }
249 
254  EXCEPTION_HELPER_FUNCTIONS(StandardLibraryException)
255 
256 private:
260  char m_reason[256];
261 
265  char m_errorMessage[512];
266 };
267 
273 {
274 public:
278  enum ErrorType {
279  DownloaderAlreadyAssociated,
282  UploaderAlreadyAssociated
285  };
286 
287 public:
294  BaseException(),
295  m_errorType(errorType)
296  {
297  switch (m_errorType) {
298  case DownloaderAlreadyAssociated:
299  sprintf(m_errorMessage, "The downloader is already associated with another uploader");
300  break;
301  case UploaderAlreadyAssociated:
302  sprintf(m_errorMessage, "The uploader is already associated with another downloader");
303  break;
304  }
305  m_errorMessage[255] = '\0';
306  }
307 
314  BaseException(other),
315  m_errorType(other.m_errorType)
316  {
317  strncpy(m_errorMessage, other.m_errorMessage, 256);
318  m_errorMessage[255] = '\0';
319  }
320 
327  {
328  if (&other == this) {
329  return *this;
330  }
331 
332  BaseException::operator=(other);
333  m_errorType = other.m_errorType;
334  strncpy(m_errorMessage, other.m_errorMessage, 256);
335  m_errorMessage[255] = '\0';
336 
337  return *this;
338  }
339 
344  {
345  }
346 
352  virtual const char *what() const throw()
353  {
354  return m_errorMessage;
355  }
356 
362  ErrorType errorType() const throw()
363  {
364  return m_errorType;
365  }
366 
371  EXCEPTION_HELPER_FUNCTIONS(UploaderDownloaderAssociationNotUniqueException)
372 
373 private:
377  ErrorType m_errorType;
378 
382  char m_errorMessage[256];
383 };
384 
390 {
391 public:
395  enum ErrorType {
396  DownloaderNotPresent,
400  UploaderNotPresent
404  };
405 
406 public:
413  BaseException(),
414  m_errorType(errorType)
415  {
416  switch (m_errorType) {
417  case DownloaderNotPresent:
418  sprintf(m_errorMessage, "The uploader is not associated with a downloader and cannot work correctly in this situation");
419  break;
420  case UploaderNotPresent:
421  sprintf(m_errorMessage, "The downloader is not associated with an uploader and cannot work correctly in this situation");
422  break;
423  }
424  m_errorMessage[255] = '\0';
425  }
426 
433  BaseException(other),
434  m_errorType(other.m_errorType)
435  {
436  strncpy(m_errorMessage, other.m_errorMessage, 256);
437  m_errorMessage[255] = '\0';
438  }
439 
446  {
447  if (&other == this) {
448  return *this;
449  }
450 
451  BaseException::operator=(other);
452  m_errorType = other.m_errorType;
453  strncpy(m_errorMessage, other.m_errorMessage, 256);
454  m_errorMessage[255] = '\0';
455 
456  return *this;
457  }
458 
463  {
464  }
465 
471  virtual const char *what() const throw()
472  {
473  return m_errorMessage;
474  }
475 
481  ErrorType errorType() const throw()
482  {
483  return m_errorType;
484  }
485 
490  EXCEPTION_HELPER_FUNCTIONS(UploaderDownloaderAssociationNotPresentException)
491 
492 private:
496  ErrorType m_errorType;
497 
501  char m_errorMessage[256];
502 };
503 
508 class InvalidNewDatumAvailableBehaviorException : public BaseException
509 {
510 public:
518  InvalidNewDatumAvailableBehaviorException(const char* description) throw() :
519  BaseException()
520  {
521  strncpy(m_description, description, 256);
522  m_description[255] = '\0';
523  sprintf(m_errorMessage, "Invalid combination of NewDatumAvailableBehavior and objects for notification: %s", m_description);
524  m_errorMessage[511] = '\0';
525  }
526 
533  BaseException(other)
534  {
535  strncpy(m_description, other.m_description, 256);
536  m_description[255] = '\0';
537  strncpy(m_errorMessage, other.m_errorMessage, 512);
538  m_errorMessage[511] = '\0';
539  }
540 
547  {
548  if (&other == this) {
549  return *this;
550  }
551 
552  BaseException::operator=(other);
553  strncpy(m_description, other.m_description, 256);
554  m_description[255] = '\0';
555  strncpy(m_errorMessage, other.m_errorMessage, 512);
556  m_errorMessage[511] = '\0';
557 
558  return *this;
559  }
560 
565  {
566  }
567 
573  virtual const char *what() const throw()
574  {
575  return m_errorMessage;
576  }
577 
583  const char *description() const throw()
584  {
585  return m_description;
586  }
587 
592  EXCEPTION_HELPER_FUNCTIONS(InvalidNewDatumAvailableBehaviorException)
593 
594 private:
598  char m_description[256];
599 
603  char m_errorMessage[512];
604 };
605 
609 class CircularDependencyException : public BaseException
610 {
611 public:
616  BaseException()
617  {
618  sprintf(m_errorMessage, "Circular dependency found");
619  m_errorMessage[255] = '\0';
620  }
621 
628  BaseException(other)
629  {
630  strncpy(m_errorMessage, other.m_errorMessage, 256);
631  m_errorMessage[255] = '\0';
632  }
633 
640  {
641  if (&other == this) {
642  return *this;
643  }
644 
645  BaseException::operator=(other);
646  strncpy(m_errorMessage, other.m_errorMessage, 256);
647  m_errorMessage[255] = '\0';
648 
649  return *this;
650  }
651 
656  {
657  }
658 
664  virtual const char *what() const throw()
665  {
666  return m_errorMessage;
667  }
668 
673  EXCEPTION_HELPER_FUNCTIONS(CircularDependencyException)
674 
675 private:
679  char m_errorMessage[256];
680 };
681 
682 } // end namespace farsa
683 
684 // All the suff below is to restore the warning state on Windows
685 #if defined(_MSC_VER)
686  #pragma warning(pop)
687 #endif
688 
689 #endif
This file contains the common type defitions used on the whole framework.
UploaderDownloaderAssociationNotUniqueException(const UploaderDownloaderAssociationNotUniqueException &other)
Copy constructor.
ErrorType errorType() const
Returns the type of error.
virtual ~CircularDependencyException()
Destructor.
UploaderDownloaderAssociationNotPresentException(ErrorType errorType)
Constructor.
The exception thrown when using invalid combinations of NewDatumAvailableBehavior and objects for not...
CircularDependencyException(const CircularDependencyException &other)
Copy constructor.
CircularDependencyException & operator=(const CircularDependencyException &other)
Copy operator.
const char * reason() const
Returns the what() message of the original exception.
virtual const char * what() const
Returns a C string describing the exception.
virtual const char * what() const
Returns a C string describing the exception.
A macro to deprecate functions.
The exception thrown when the association between uploader and downloader is not 1:1.
virtual ~RuntimeUserException()
Destructor.
ErrorType errorType() const
Returns the type of error.
StandardLibraryException(const StandardLibraryException &other)
Copy constructor.
RuntimeUserException(const RuntimeUserException &other)
Copy constructor.
An exception thrown when we catch standard library exceptions in a worker thread. ...
virtual ~StandardLibraryException()
Destructor.
void FARSA_UTIL_TEMPLATE throwUserRuntimeError(QString reason)
The helper function to throw an exception of type RuntimeUserException.
virtual const char * what() const
Returns a C string describing the exception.
virtual const char * what() const
Returns a C string describing the exception.
virtual const char * what() const
Returns a C string describing the exception.
The exception thrown when DependencySorter finds a circular dependency.
const char * description() const
Returns the description of the error.
The exception thrown when trying to create a datum or download without an uploader/downloader associa...
RuntimeUserException & operator=(const RuntimeUserException &other)
Copy operator.
UploaderDownloaderAssociationNotUniqueException & operator=(const UploaderDownloaderAssociationNotUniqueException &other)
Copy operator.
StandardLibraryException & operator=(const StandardLibraryException &other)
Copy operator.
UploaderDownloaderAssociationNotUniqueException(ErrorType errorType)
Constructor.
StandardLibraryException(const std::exception &origException)
Constructor.
UploaderDownloaderAssociationNotPresentException & operator=(const UploaderDownloaderAssociationNotPresentException &other)
Copy operator.
virtual const char * what() const
Returns a C string describing the exception.
RuntimeUserException(const char *reason)
Constructor.
A generic exception the user can throw at runtime.
const char * reason() const
Returns the description of why the exception was thrown.
UploaderDownloaderAssociationNotPresentException(const UploaderDownloaderAssociationNotPresentException &other)
Copy constructor.
InvalidNewDatumAvailableBehaviorException & operator=(const InvalidNewDatumAvailableBehaviorException &other)
Copy operator.
InvalidNewDatumAvailableBehaviorException(const char *description)
Constructor.
InvalidNewDatumAvailableBehaviorException(const InvalidNewDatumAvailableBehaviorException &other)
Copy constructor.