gatelib  2.1
g_Exception.h
1 #pragma once
2 
3 #include "g_common_functions.h"
4 #include "g_exception_macros.h"
5 #include <list>
6 
7 namespace g
8 {
9  class Exception;
10 
11  typedef void ( *ExceptionActionPf ) ( const Exception& );
12 
13  #define MAX_CONTEXT 128
14  #define G_EXCEPTION_UNDEF_CODE -1
15 
17  class G_LIB_ITEM Exception
18  {
19  public:
20  class Context;
21 
22  // standard copy constructor
23  Exception ( const std::wstring& msg , const Context& context );
24  Exception ( const std::string& msg , const Context& context );
25  Exception ( const Exception& , const Context& context );
26  Exception ( );
27  virtual ~Exception ( );
28 
29  static void do_FatalAction ( const Exception& );
30  static void default_FatalAction ( const Exception& );
31 
32  static ExceptionActionPf get_ThreadFatalAction ( );
33  static void set_ThreadFatalAction ( ExceptionActionPf );
34 
35  static void do_UnhandledExceptionHandling ( const Exception& , const char* thread_name );
36  static void do_UnhandledExceptionHandling ( const std::exception& , const char* thread_name );
37  static void do_UnhandledExceptionHandling ( const char* thread_name );
45  static void generic_ExceptionCancelHandling ( );
46 
50  class G_LIB_ITEM Context
51  {
52  public:
53  Context ( );
54  Context ( const char* context_str , const char* file = "" , int line = -1 );
55  //Context ( const Context& o );
56 
57  const std::string& getMethod ( ) const { return mMethod; }
58  const std::string& getFile ( ) const { return mFile; }
59  int getLine ( ) const { return mLine; }
60 
61  std::string getString ( ) const;
62  std::wstring getStringW ( ) const { return to_Wstring ( getString ( ) ); }
63 
64  private:
65  std::string mMethod;
66  std::string mFile;
67  int mLine;
68  };
69 
70  class G_LIB_ITEM ContextList
71  {
72  public:
73  //uses standard copy constructor
74  ContextList( );
75  virtual ~ContextList( );
76 
77  //In order to allow
78  void addContext ( const Context& );
79  int getSize ( ) const { return mSize; }
80 
81  std::wstring getStringW ( ) const { return to_Wstring ( getString ( ) ); }
82  std::string getString ( ) const;
83 
84  const Context& operator [] ( int index ) const;
85 
86  private:
87  int mSize;
88  int mOffset;
89  //in order to save space mempry is reserved (avoid to call default constructor)
90  Context mContextList[MAX_CONTEXT];
91  };
92 
93  template < class E_T > static void raise_Helper ( E_T& aException );
94  template < class E_T > static void do_Raise ( const char* aContextStr , const char* aFile , int aLine );
95  template < class E_T > static void do_RaiseMsg ( const char* aMsg , const char* aContextStr , const char* aFile , int aLine );
96  template < class E_T > static void do_FatalAction ( const char* aMsg , const char* aContextStr , const char* aFile , int aLine ) { do_FatalAction ( E_T ( aMsg , Context(aContextStr,aFile,aLine) ) ); }
97  template < class E_T > static void do_Rethrow ( const E_T& aInnerExc , const char* aContextStr , const char* aFile , int aLine );
98 
99  static ExceptionActionPf get_AlternateRaiseAction ( );
100  static void set_ThreadAlternateRaiseAction ( ExceptionActionPf );
101 
102  void showMsg ( ) const;
103  void showFullMsg ( ) const;
104  const std::wstring& getErrorMsgW ( ) const { return mMessage; }
105  const std::string getErrorMsg ( ) const { return to_StdString(getErrorMsgW()); }
106 
107  std::wstring getFullErrorMsgW ( ) const;
108  std::string getFullErrorMsg ( ) const { return to_StdString(getFullErrorMsgW()); }
109 
110  static const char* get_Tag ( ) { return "Exception"; }
111  virtual const char* getTag ( ) const { return Exception::get_Tag(); }
112 
113  const std::wstring getTagW ( ) const { return to_Wstring(getTag()); }
114  const ContextList& getContextList ( ) const { return mContextList; }
115 
116  //Returns error code if any
117  GInt64_t getCode ( ) const { return mCode; }
118 
119  static void default_MsgShowAction ( const Exception& );
120  static void default_MsgFullShowAction ( const Exception& );
121 
122  static ExceptionActionPf get_ThreadMsgShowAction ( );
123  static void set_ThreadMsgShowAction ( ExceptionActionPf );
124 
125  static ExceptionActionPf get_ThreadMsgFullShowAction ( );
126  static void set_ThreadMsgFullShowAction ( ExceptionActionPf );
127 
128  //Action to be perfomed for showing message
129  static ExceptionActionPf msg_showAction;
130  static ExceptionActionPf msg_fullShowAction;
131 
132  //fatal action handling
133  static ExceptionActionPf fatal_ActionPf;
134  static ExceptionActionPf alternate_RaiseAction;
135 
136  protected:
137  std::wstring mMessage;
138  GInt64_t mCode;
139  ContextList mContextList;
140  };
141 
143  {
144  public:
145  UnimplemetedException ( const Context& context ) : Exception ( std::string("Unimplemented context ") + context.getString() , context ) {}
146 
147  static const char* get_Tag ( ) { return "UnimplemetedException"; }
148  virtual const char* getTag ( ) const { return get_Tag(); }
149  };
150 
151  template < class E_T > void Exception::do_Rethrow ( const E_T& aInnerExc , const char* aContextStr , const char* aFile , int aLine )
152  {
153  E_T exception_instance ( aInnerExc , Context(aContextStr,aFile,aLine) );
154  Exception::raise_Helper ( exception_instance );
155  }
156 
157  template < class E_T > void Exception::do_Raise ( const char* aContextStr , const char* aFile , int aLine )
158  {
159  E_T exception_instance ( g::Exception::Context(aContextStr,aFile,aLine) );
160  Exception::raise_Helper ( exception_instance );
161  }
162 
163  template < class E_T > void Exception::do_RaiseMsg ( const char* aMsg , const char* aContextStr , const char* aFile , int aLine )
164  {
165  E_T exception_instance ( E_T ( aMsg , g::Exception::Context(aContextStr,aFile,aLine) ) );
166  Exception::raise_Helper ( exception_instance );
167  }
168 
169  template < class E_T > void Exception::raise_Helper ( E_T& aException )
170  {
171  ExceptionActionPf thread_action_pf = get_AlternateRaiseAction ( );
172 
173  if ( thread_action_pf != NULL )
174  {
175  ( *thread_action_pf ) ( aException );
176  }
177  else
178  {
179  throw aException;
180  }
181  }
182 
183  G_EXC_DEFINE ( AssertException , Exception );
184  G_EXC_DEFINE ( SysException , Exception );
185 }//namespace g
186 
Definition: g_Exception.h:50
Definition: g_Exception.h:70
Definition: g_Exception.h:142
#define G_EXC_DEFINE(aexctypename, abaseexctypename)
Definition: g_exception_macros.h:39
Definition: g.mthread.ThreadSimpleEvent.h:5
std::wstring to_Wstring(const std::string &aString)
Converts a string to a wide strings.
Definition: g_common_functions.h:83
Anchestor Exception class for g::lib.
Definition: g_Exception.h:17
std::string to_StdString(const std::wstring &aString)
std::wstring to std::string
Definition: g_common_functions.h:80