/[pcsx2_0.9.7]/trunk/common/include/Utilities/Exceptions.h
ViewVC logotype

Diff of /trunk/common/include/Utilities/Exceptions.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 61 by william, Tue Sep 7 03:24:11 2010 UTC revision 62 by william, Tue Sep 7 11:08:22 2010 UTC
# Line 66  namespace Exception Line 66  namespace Exception
66          protected:          protected:
67                  wxString m_message_diag;                // (untranslated) a "detailed" message of what disastrous thing has occurred!                  wxString m_message_diag;                // (untranslated) a "detailed" message of what disastrous thing has occurred!
68                  wxString m_message_user;                // (translated) a "detailed" message of what disastrous thing has occurred!                  wxString m_message_user;                // (translated) a "detailed" message of what disastrous thing has occurred!
                 wxString m_stacktrace;                  // contains the stack trace string dump (unimplemented)  
69    
70          public:          public:
71                  virtual ~BaseException() throw()=0;     // the =0; syntax forces this class into "abstract" mode.                  virtual ~BaseException() throw()=0;     // the =0; syntax forces this class into "abstract" mode.
# Line 77  namespace Exception Line 76  namespace Exception
76                  wxString& DiagMsg() { return m_message_diag; }                  wxString& DiagMsg() { return m_message_diag; }
77                  wxString& UserMsg() { return m_message_user; }                  wxString& UserMsg() { return m_message_user; }
78    
79                    BaseException& SetBothMsgs( const wxChar* msg_diag );
80                    BaseException& SetDiagMsg( const wxString& msg_diag );
81                    BaseException& SetUserMsg( const wxString& msg_user );
82    
83                  // Returns a message suitable for diagnostic / logging purposes.                  // Returns a message suitable for diagnostic / logging purposes.
84                  // This message is always in English, and includes a full stack trace.                  // This message is always in English, and includes a full stack trace.
85                  virtual wxString FormatDiagnosticMessage() const;                  virtual wxString FormatDiagnosticMessage() const;
86    
87                  // Returns a message suitable for end-user display.                  // Returns a message suitable for end-user display.
88                  // This message is usually meant for display in a user popup or such.                  // This message is usually meant for display in a user popup or such.
89                  virtual wxString FormatDisplayMessage() const { return m_message_user; }                  virtual wxString FormatDisplayMessage() const;
90    
91                  virtual void Rethrow() const=0;                  virtual void Rethrow() const=0;
92                  virtual BaseException* Clone() const=0;                  virtual BaseException* Clone() const=0;
   
         protected:  
                 // Construction using two pre-formatted pre-translated messages  
                 void InitBaseEx( const wxString& msg_eng, const wxString& msg_xlt );  
   
                 // Construction using one translation key.  
                 void InitBaseEx( const char* msg_eng );  
93          };          };
94    
95          // --------------------------------------------------------------------------------------          // --------------------------------------------------------------------------------------
# Line 118  namespace Exception Line 114  namespace Exception
114                  virtual u32 GetPc() const=0;                  virtual u32 GetPc() const=0;
115                  virtual bool IsDelaySlot() const=0;                  virtual bool IsDelaySlot() const=0;
116                  virtual wxString Message() const { return m_message; }                  virtual wxString Message() const { return m_message; }
117    
118                    virtual void Rethrow() const=0;
119                    virtual Ps2Generic* Clone() const=0;
120          };          };
121    
122  // Some helper macros for defining the standard constructors of internationalized constructors  // Some helper macros for defining the standard constructors of internationalized constructors
# Line 129  namespace Exception Line 128  namespace Exception
128  //     it will be optionally translated.  //     it will be optionally translated.
129  //  //
130  // BUGZ??  I'd rather use 'classname' on the Clone() prototype, but for some reason it generates  // BUGZ??  I'd rather use 'classname' on the Clone() prototype, but for some reason it generates
131  // ambiguity errors on virtual inheritence (it really shouldn't!).  So I have to force it to the  // ambiguity errors on virtual inheritance (it really shouldn't!).  So I have to force it to the
132  // BaseException base class.  Not sure if this is Stupid Standard Tricks or Stupid MSVC Tricks. --air  // BaseException base class.  Not sure if this is Stupid Standard Tricks or Stupid MSVC Tricks. --air
133  //  //
134  // (update: web searches indicate it's MSVC specific -- happens in 2008, not sure about 2010).  // (update: web searches indicate it's MSVC specific -- happens in 2008, not sure about 2010).
135  //  //
136  #define DEFINE_EXCEPTION_COPYTORS( classname ) \  #define DEFINE_EXCEPTION_COPYTORS( classname, parent ) \
137          virtual ~classname() throw() {} \  private: \
138          virtual void Rethrow() const { throw *this; } \          typedef parent _parent; \
139          virtual BaseException* Clone() const { return new classname( *this ); }  public: \
   
 // This is here because MSVC's support for covariant return types on Clone() is broken, and will  
 // not work with virtual class inheritance (see DEFINE_EXCEPTION_COPYTORS for details)  
 #define DEFINE_EXCEPTION_COPYTORS_COVARIANT( classname ) \  
140          virtual ~classname() throw() {} \          virtual ~classname() throw() {} \
141          virtual void Rethrow() const { throw *this; } \          virtual void Rethrow() const { throw *this; } \
142          virtual classname* Clone() const { return new classname( *this ); }          virtual classname* Clone() const { return new classname( *this ); }
143    
144  #define DEFINE_RUNTIME_EXCEPTION( classname, defmsg ) \  #define DEFINE_EXCEPTION_MESSAGES( classname ) \
145          DEFINE_EXCEPTION_COPYTORS( classname ) \  public: \
146   \          classname& SetBothMsgs( const wxChar* msg_diag )        { BaseException::SetBothMsgs(msg_diag); return *this; } \
147          explicit classname( const char* msg=defmsg )                                                    { BaseException::InitBaseEx( msg ); } \          classname& SetDiagMsg( const wxString& msg_diag )       { m_message_diag = msg_diag;                    return *this; } \
148          explicit classname( const wxString& msg_eng, const wxString& msg_xlt )  { BaseException::InitBaseEx( msg_eng, msg_xlt); }          classname& SetUserMsg( const wxString& msg_user )       { m_message_user = msg_user;                    return *this; }
149    
150  #define DEFINE_LOGIC_EXCEPTION( classname, defmsg ) \  #define DEFINE_RUNTIME_EXCEPTION( classname, parent, message ) \
151          DEFINE_EXCEPTION_COPYTORS( classname ) \          DEFINE_EXCEPTION_COPYTORS( classname, parent ) \
152   \          classname() { SetDiagMsg(message); } \
153          explicit classname( const char* msg=defmsg )            { BaseException::InitBaseEx( msg ); } \          DEFINE_EXCEPTION_MESSAGES( classname )
154          explicit classname( const wxString& msg_eng )           { BaseException::InitBaseEx( msg_eng, wxEmptyString ); }          
155            
156          // ---------------------------------------------------------------------------------------          // ---------------------------------------------------------------------------------------
157          //  RuntimeError - Generalized Exceptions with Recoverable Traits!          //  RuntimeError - Generalized Exceptions with Recoverable Traits!
158          // ---------------------------------------------------------------------------------------          // ---------------------------------------------------------------------------------------
159    
160          class RuntimeError : public virtual BaseException          class RuntimeError : public BaseException
161          {          {
162                    DEFINE_EXCEPTION_COPYTORS( RuntimeError, BaseException )
163                    DEFINE_EXCEPTION_MESSAGES( RuntimeError )
164    
165          public:          public:
166                  bool    IsSilent;                  bool    IsSilent;
         public:  
                 DEFINE_RUNTIME_EXCEPTION( RuntimeError, wxLt("An unhandled runtime error has occurred, somewhere in the depths of Pcsx2's cluttered brain-matter.") )  
167    
168                    RuntimeError() { IsSilent = false; }
169                  RuntimeError( const std::runtime_error& ex, const wxString& prefix=wxEmptyString );                  RuntimeError( const std::runtime_error& ex, const wxString& prefix=wxEmptyString );
170                  RuntimeError( const std::exception& ex, const wxString& prefix=wxEmptyString );                  RuntimeError( const std::exception& ex, const wxString& prefix=wxEmptyString );
171          };          };
# Line 182  namespace Exception Line 179  namespace Exception
179          //          //
180          // I chose to have this exception derive from RuntimeError, since if one is thrown from outside          // I chose to have this exception derive from RuntimeError, since if one is thrown from outside
181          // an App message loop we'll still want it to be handled in a reasonably graceful manner.          // an App message loop we'll still want it to be handled in a reasonably graceful manner.
182          class CancelEvent : public virtual RuntimeError          class CancelEvent : public RuntimeError
183          {          {
184          public:                  DEFINE_RUNTIME_EXCEPTION( CancelEvent, RuntimeError, wxLt("No reason given.") )
                 DEFINE_EXCEPTION_COPYTORS( CancelEvent )  
   
                 explicit CancelEvent( const char* logmsg )  
                 {  
                         m_message_diag = fromUTF8( logmsg );  
                         // overridden message formatters only use the diagnostic version...  
                 }  
185    
186                  explicit CancelEvent( const wxString& logmsg=L"No reason given." )          public:
187                    explicit CancelEvent( const wxString& logmsg )
188                  {                  {
189                          m_message_diag = logmsg;                          m_message_diag = logmsg;
190                          // overridden message formatters only use the diagnostic version...                          // overridden message formatters only use the diagnostic version...
# Line 203  namespace Exception Line 194  namespace Exception
194                  virtual wxString FormatDiagnosticMessage() const;                  virtual wxString FormatDiagnosticMessage() const;
195          };          };
196    
197          // --------------------------------------------------------------------------------------          // ---------------------------------------------------------------------------------------
198          class ObjectIsNull : public virtual CancelEvent          //  OutOfMemory
199            // ---------------------------------------------------------------------------------------
200            // This exception has a custom-formatted Diagnostic string.  The parameter give when constructing
201            // the exception is a block/alloc name, which is used as a formatting parameter in the diagnostic
202            // output.  The default diagnostic message is "Out of memory exception, while allocating the %s."
203            // where %s is filled in with the block name.
204            //
205            // The user string is not custom-formatted, and should contain *NO* %s tags.
206            //
207            class OutOfMemory : public RuntimeError
208          {          {
209          public:                  DEFINE_RUNTIME_EXCEPTION( OutOfMemory, RuntimeError, wxLt("Out of memory?!") )
                 wxString ObjectName;  
210    
211                  DEFINE_EXCEPTION_COPYTORS( ObjectIsNull )          public:
212                    wxString        AllocDescription;
213    
214                  explicit ObjectIsNull( const char* objname="unspecified" )          public:
215                  {                  OutOfMemory( const wxString& allocdesc );
                         m_message_diag = fromUTF8( objname );  
                         // overridden message formatters only use the diagnostic version...  
                 }  
216    
217                  virtual wxString FormatDisplayMessage() const;                  virtual wxString FormatDisplayMessage() const;
218                  virtual wxString FormatDiagnosticMessage() const;                  virtual wxString FormatDiagnosticMessage() const;
219          };          };
220    
         // ---------------------------------------------------------------------------------------  
         //  OutOfMemory / InvalidOperation / InvalidArgument / ParseError  
         // ---------------------------------------------------------------------------------------  
   
         class OutOfMemory : public virtual RuntimeError  
         {  
         public:  
                 DEFINE_RUNTIME_EXCEPTION( OutOfMemory, wxLt("Out of Memory") )  
         };  
   
221          class ParseError : public RuntimeError          class ParseError : public RuntimeError
222          {          {
223          public:                  DEFINE_RUNTIME_EXCEPTION( ParseError, RuntimeError, wxLt("Parse error") );
                 DEFINE_RUNTIME_EXCEPTION( ParseError, "Parse error" );  
224          };          };
225    
226          // ---------------------------------------------------------------------------------------          // ---------------------------------------------------------------------------------------
# Line 245  namespace Exception Line 231  namespace Exception
231          // This exception is a specific type of OutOfMemory error that isn't "really" an out of          // This exception is a specific type of OutOfMemory error that isn't "really" an out of
232          // memory error.  More likely it's caused by a plugin or driver reserving a range of memory          // memory error.  More likely it's caused by a plugin or driver reserving a range of memory
233          // we'd really like to have access to.          // we'd really like to have access to.
234          class VirtualMemoryMapConflict : public virtual OutOfMemory          class VirtualMemoryMapConflict : public OutOfMemory
235          {          {
236          public:                  DEFINE_RUNTIME_EXCEPTION( VirtualMemoryMapConflict, OutOfMemory, wxLt("Virtual memory map confict: Unable to claim specific required memory regions.") )
                 DEFINE_RUNTIME_EXCEPTION( VirtualMemoryMapConflict, wxLt("Virtual memory map confict: Unable to claim specific required memory regions.") )  
237          };          };
238    
239          class HardwareDeficiency : public virtual RuntimeError          class HardwareDeficiency : public RuntimeError
240          {          {
241          public:          public:
242                  DEFINE_RUNTIME_EXCEPTION( HardwareDeficiency, wxLt("Your machine's hardware is incapable of running PCSX2.  Sorry dood.") );                  DEFINE_RUNTIME_EXCEPTION( HardwareDeficiency, RuntimeError, wxLt("Your machine's hardware is incapable of running PCSX2.  Sorry dood.") );
243          };          };
244    
245          // ---------------------------------------------------------------------------------------          // ---------------------------------------------------------------------------------------
# Line 262  namespace Exception Line 247  namespace Exception
247          //   Stream / BadStream / CannotCreateStream / FileNotFound / AccessDenied / EndOfStream          //   Stream / BadStream / CannotCreateStream / FileNotFound / AccessDenied / EndOfStream
248          // ---------------------------------------------------------------------------------------          // ---------------------------------------------------------------------------------------
249    
250  #define DEFINE_STREAM_EXCEPTION( classname, defmsg ) \          #define DEFINE_STREAM_EXCEPTION_ACCESSORS( classname ) \
251          DEFINE_EXCEPTION_COPYTORS( classname ) \                  virtual classname& SetStreamName( const wxString& name )        { StreamName = name;                    return *this; } \
252   \                  virtual classname& SetStreamName( const char* name )            { StreamName = fromUTF8(name);  return *this; }
253          explicit classname( const wxString& objname=wxString(), const char* msg=defmsg ) \  
254          { \          #define DEFINE_STREAM_EXCEPTION( classname, parent, message ) \
255                  BaseException::InitBaseEx( msg ); \                  DEFINE_RUNTIME_EXCEPTION( classname, parent, message ) \
256                  StreamName = objname; \                  classname( const wxString& filename ) { \
257          } \                          StreamName = filename; \
258          explicit classname( const wxString& objname, const wxString& msg_eng, const wxString& msg_xlt ) \                          SetBothMsgs(message); \
259          { \                  } \
260                  BaseException::InitBaseEx( msg_eng, msg_xlt ); \                  DEFINE_STREAM_EXCEPTION_ACCESSORS( classname )
261                  StreamName = objname; \          
         } \  
         explicit classname( const char* objname, const char* msg=defmsg ) \  
         { \  
                 BaseException::InitBaseEx( msg ); \  
                 StreamName = fromUTF8( objname ); \  
         } \  
         explicit classname( const char* objname, const wxString& msg_eng, const wxString& msg_xlt ) \  
         { \  
                 BaseException::InitBaseEx( msg_eng, msg_xlt ); \  
                 StreamName = fromUTF8( objname ); \  
         } \  
         explicit classname( const char* objname, const wxString& msg_eng ) \  
         { \  
                 BaseException::InitBaseEx( msg_eng, msg_eng ); \  
                 StreamName = fromUTF8( objname ); \  
         } \  
         explicit classname( const wxString& objname, const wxString& msg_eng ) \  
         { \  
                 BaseException::InitBaseEx( msg_eng, msg_eng ); \  
                 StreamName = objname; \  
         }  
   
262          // Generic stream error.  Contains the name of the stream and a message.          // Generic stream error.  Contains the name of the stream and a message.
263          // This exception is usually thrown via derived classes, except in the (rare) case of a          // This exception is usually thrown via derived classes, except in the (rare) case of a
264          // generic / unknown error.          // generic / unknown error.
265          //          //
266          class Stream : public virtual RuntimeError          class Stream : public RuntimeError
267          {          {
268          public:                  DEFINE_STREAM_EXCEPTION( Stream, RuntimeError, wxLt("General file operation error.") )
                 wxString StreamName;            // name of the stream (if applicable)  
269    
270          public:          public:
271                  DEFINE_STREAM_EXCEPTION( Stream, "General file operation error." )                  wxString StreamName;            // name of the stream (if applicable)
272    
273                  virtual wxString FormatDiagnosticMessage() const;                  virtual wxString FormatDiagnosticMessage() const;
274                  virtual wxString FormatDisplayMessage() const;                  virtual wxString FormatDisplayMessage() const;
# Line 316  namespace Exception Line 278  namespace Exception
278          // connection, or anything else that would indicate a failure to read the data after the          // connection, or anything else that would indicate a failure to read the data after the
279          // stream was successfully opened.          // stream was successfully opened.
280          //          //
281          class BadStream : public virtual Stream          class BadStream : public Stream
282          {          {
283          public:                  DEFINE_STREAM_EXCEPTION( BadStream, Stream, wxLt("File data is corrupted or incomplete, or the stream connection closed unexpectedly.") )
                 DEFINE_STREAM_EXCEPTION( BadStream, wxLt("File data is corrupted or incomplete, or the stream connection closed unexpectedly.") )  
284          };          };
285    
286          // A generic exception for odd-ball stream creation errors.          // A generic exception for odd-ball stream creation errors.
287          //          //
288          class CannotCreateStream : public virtual Stream          class CannotCreateStream : public Stream
289          {          {
290          public:                  DEFINE_STREAM_EXCEPTION( CannotCreateStream, Stream, wxLt("File could not be created or opened.") )
                 DEFINE_STREAM_EXCEPTION( CannotCreateStream, wxLt("File could not be created or opened.") )  
291          };          };
292    
293          // Exception thrown when an attempt to open a non-existent file is made.          // Exception thrown when an attempt to open a non-existent file is made.
294          // (this exception can also mean file permissions are invalid)          // (this exception can also mean file permissions are invalid)
295          //          //
296          class FileNotFound : public virtual CannotCreateStream          class FileNotFound : public CannotCreateStream
297          {          {
298          public:          public:
299                  DEFINE_STREAM_EXCEPTION( FileNotFound, wxLt("File not found.") )                  DEFINE_STREAM_EXCEPTION( FileNotFound, CannotCreateStream, wxLt("File not found.") )
300          };          };
301    
302          class AccessDenied : public virtual CannotCreateStream          class AccessDenied : public CannotCreateStream
303          {          {
304          public:          public:
305                  DEFINE_STREAM_EXCEPTION( AccessDenied, wxLt("Permission denied to file.") )                  DEFINE_STREAM_EXCEPTION( AccessDenied, CannotCreateStream, wxLt("Permission denied to file.") )
306          };          };
307    
308          // EndOfStream can be used either as an error, or used just as a shortcut for manual          // EndOfStream can be used either as an error, or used just as a shortcut for manual
309          // feof checks.          // feof checks.
310          //          //
311          class EndOfStream : public virtual Stream          class EndOfStream : public Stream
312          {          {
313          public:          public:
314                  DEFINE_STREAM_EXCEPTION( EndOfStream, wxLt("Unexpected end of file or stream.") );                  DEFINE_STREAM_EXCEPTION( EndOfStream, Stream, wxLt("Unexpected end of file or stream.") );
315          };          };
316    
317  #ifdef __WXMSW__  #ifdef __WXMSW__
# Line 360  namespace Exception Line 320  namespace Exception
320          // --------------------------------------------------------------------------------------          // --------------------------------------------------------------------------------------
321          class WinApiError : public RuntimeError          class WinApiError : public RuntimeError
322          {          {
323                    DEFINE_EXCEPTION_COPYTORS( WinApiError, RuntimeError )
324                    DEFINE_EXCEPTION_MESSAGES( WinApiError )
325    
326          public:          public:
327                  int             ErrorId;                  int             ErrorId;
328    
329          public:          public:
330                  DEFINE_EXCEPTION_COPYTORS( WinApiError )                  WinApiError();
   
                         WinApiError( const char* msg="" );  
331    
332                  wxString GetMsgFromWindows() const;                  wxString GetMsgFromWindows() const;
333                  virtual wxString FormatDisplayMessage() const;                  virtual wxString FormatDisplayMessage() const;

Legend:
Removed from v.61  
changed lines
  Added in v.62

  ViewVC Help
Powered by ViewVC 1.1.22