μHAL (v2.6.5)
Part of the IPbus software repository
exception.hpp
Go to the documentation of this file.
1 /*
2 ---------------------------------------------------------------------------
3 
4  This file is part of uHAL.
5 
6  uHAL is a hardware access library and programming framework
7  originally developed for upgrades of the Level-1 trigger of the CMS
8  experiment at CERN.
9 
10  uHAL is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  uHAL is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with uHAL. If not, see <http://www.gnu.org/licenses/>.
22 
23 
24  Andrew Rose, Imperial College, London
25  email: awr01 <AT> imperial.ac.uk
26 
27  Marc Magrans de Abril, CERN
28  email: marc.magrans.de.abril <AT> cern.ch
29 
30 ---------------------------------------------------------------------------
31 */
32 
39 #ifndef _uhal_log_exception_hpp_
40 #define _uhal_log_exception_hpp_
41 
42 #include <exception>
43 #include <string>
44 #include <sys/time.h>
45 
46 #ifdef USE_BACKTRACE
47 #include "boost/thread.hpp"
48 #endif
49 
50 
54 #define UHAL_DEFINE_DERIVED_EXCEPTION_CLASS( ClassName , BaseClassName, ClassDescription )\
55  class ClassName : public BaseClassName {\
56  public:\
57  ClassName() : BaseClassName() {}\
58  void ThrowAsDerivedType_(){ throw ClassName(*this); } \
59  exception* clone(){ return new ClassName(*this); } \
60  protected:\
61  std::string description() const throw() { return std::string( ClassDescription ); } \
62 };
63 
64 #define UHAL_DEFINE_EXCEPTION_CLASS( ClassName , ClassDescription ) UHAL_DEFINE_DERIVED_EXCEPTION_CLASS(ClassName, uhal::exception::exception, ClassDescription)
65 
66 #define ExceptionClass UHAL_DEFINE_EXCEPTION_CLASS
67 
68 
69 
70 
74 #define ThrowAsDerivedType() ThrowAsDerivedType_(); throw 0;
75 
76 
77 #ifdef USE_BACKTRACE
78 #define MaxExceptionHistoryLength 100
79 #endif
80 
81 namespace uhal
82 {
83 
85  namespace exception
86  {
87 
89  class exception : public std::exception
90  {
91  public:
95  exception ();
96 
102 
108  exception& operator= ( const exception& aExc );
109 
113  virtual ~exception() throw();
114 
120  virtual const char* what() const throw();
121 
125  virtual void ThrowAsDerivedType_() = 0;
126 
131  virtual exception* clone() = 0;
132 
137  void append ( const char* aCStr ) throw();
138 
139 
140  protected:
145  virtual std::string description() const throw() = 0;
146 
147  private:
148 #ifdef USE_BACKTRACE
149 
152  std::vector< void* > mBacktrace;
153 
155  boost::thread::id mThreadId;
156 #endif
157  timeval mTime;
159 
161  char* mString;
162 
165  };
166 
167  }
168 }
169 
170 
171 #endif
exception & operator=(const exception &aExc)
Assignment operator.
Definition: exception.cpp:89
exception const exception & aExc
Constructor.
Definition: exception.hpp:101
virtual std::string description() const =0
Return the description associated with this type of exception.
void append(const char *aCStr)
Add additional information to the exception message.
Definition: exception.cpp:208
char * mAdditionalInfo
Memory into which additional information is added by calls to append.
Definition: exception.hpp:164
virtual void ThrowAsDerivedType_()=0
Function which casts a pointer from the base type of this object to a derived type of this object and...
An abstract base exception class providing an interface to a throw/ThrowAsDerivedType mechanism which...
Definition: exception.hpp:89
timeval mTime
The time at which the exception was thrown.
Definition: exception.hpp:158
virtual ~exception()
Destructor.
Definition: exception.cpp:100
virtual exception * clone()=0
Return a new object of the derived exception type cloned from this object.
virtual const char * what() const
Function which returns the error message associated with an exception If no error message has previou...
Definition: exception.cpp:116
char * mString
Memory which the call to "what()" uses when formatting the output string.
Definition: exception.hpp:161