Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cException.h
Go to the documentation of this file.
1 //
5 
6 #ifndef _INC_cException_H
7 #define _INC_cException_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cExceptionBase.h"
13 #include "HResult.h"
14 #include "cObject.h" // DECLARE_DYNAMIC()
15 #include "cString.h"
16 #include "cUnitTestDecl.h"
17 
18 namespace Gray
19 {
21 
23 
24 #if defined(_MSC_VER)
25 #pragma warning(disable:4275) // non dll-interface class 'type_info' used as base for dll-interface class. http://msdn.microsoft.com/en-us/library/3tdb471s.aspx
26 #endif
27  class GRAYCORE_LINK cExceptionHolder : public cPtrFacade < cExceptionBase >
28  {
33 
34  public:
35  static const StrLen_t k_MSG_MAX_SIZE = 1024;
36 
37  private:
38  bool m_bDeleteEx;
39 
40  public:
41  cExceptionHolder() noexcept
42  : m_bDeleteEx(false)
43  {
44  }
45  explicit cExceptionHolder(cExceptionBase* pEx, bool bDeleteEx = true) noexcept
47  , m_bDeleteEx(bDeleteEx)
48  {
50  }
51  explicit cExceptionHolder(cExceptionBase& ex) noexcept
53  , m_bDeleteEx(false)
54  {
56  }
57  ~cExceptionHolder() noexcept
58  {
60  if (m_bDeleteEx && m_p != nullptr) // make sure DetachException() wasn't called.
61  {
62 #ifdef _MFC_VER // using _MFC_VER.
63  m_p->Delete();
64 #else
65  delete m_p;
66 #endif
67  }
68  }
69  void AttachException(cExceptionBase* pEx, bool bDeleteEx)
70  {
71  ASSERT(m_p == nullptr);
72  m_p = pEx;
73  m_bDeleteEx = bDeleteEx;
74  }
75  cException* get_Ex() const; // is Custom?
76 
77  BOOL GetErrorMessage(LOGCHAR_t* lpszError, StrLen_t nLenMaxError = k_MSG_MAX_SIZE) const;
78  cStringL get_ErrorStr() const;
79  LOGLEV_TYPE get_Severity() const;
80  };
81 
83  {
88 
89  public:
93 
94  public:
95  cException(const LOGCHAR_t* pszDescription, LOGLEV_TYPE eLogLevel = LOGLEV_ERROR) noexcept
96  : m_eSeverity(eLogLevel)
97 #if ! defined(_MFC_VER) && defined(_MSC_VER) && ! defined(UNDER_CE) // not using _MFC_VER.
98  , cExceptionBase(pszDescription)
99 #endif
100  , m_pszDescription((pszDescription == nullptr) ? k_szDescriptionDefault : pszDescription)
101  {
102  }
104  {
105  }
106  LOGLEV_TYPE get_Severity() const noexcept
107  {
108  return m_eSeverity;
109  }
110 
111  DECLARE_DYNAMIC(cException); // For MFC support.
112 
115  virtual BOOL GetErrorMessage(GChar_t* lpszError, UINT nLenMaxError = cExceptionHolder::k_MSG_MAX_SIZE, UINT* pnHelpContext = nullptr);
116  cStringL get_ErrorStr(); // similar to STL what()
117 
118 #ifndef _MFC_VER // using _MFC_VER.
119  virtual const char* what() const THROW_DEF
120  {
122  return m_pszDescription;
123  }
124 #endif // ! _MFC_VER
125 
127  };
128 
130  {
133  typedef cException SUPER_t;
134 
135  public:
137 
138  public:
139  cExceptionHResult(HRESULT hResultCode = E_FAIL, const LOGCHAR_t* pszDescription = nullptr, LOGLEV_TYPE eSeverity = LOGLEV_ERROR) THROW_DEF
140  : cException(pszDescription, eSeverity)
141  , m_hResultCode(hResultCode)
142  {
143  }
145  {
146  }
147  HRESULT get_HResultCode() const noexcept
148  {
149  return m_hResultCode;
150  }
151  virtual BOOL GetErrorMessage(GChar_t* lpszError, UINT nLenMaxError, UINT* pnHelpContext) override;
152  };
153 };
154 
155 #endif // _INC_cException_H
#define GRAYCORE_LINK
Definition: GrayCore.h:47
#define THROW_DEF
Definition: HResult.h:25
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define ASSERT(exp)
Definition: cDebugAssert.h:87
#define DECLARE_DYNAMIC(c)
Definition: cObject.h:105
#define UNITTEST2_PREDEF(x)
Definition: cUnitTestDecl.h:19
#define UNITTEST_FRIEND(n)
Define this in the class body to be unit tested. Allow the unit test to access private/protected stuf...
Definition: cUnitTestDecl.h:17
Definition: cExceptionBase.h:28
Definition: cException.h:130
HRESULT get_HResultCode() const noexcept
Definition: cException.h:147
cExceptionHResult(HRESULT hResultCode=E_FAIL, const LOGCHAR_t *pszDescription=nullptr, LOGLEV_TYPE eSeverity=LOGLEV_ERROR)((void) 0)
Definition: cException.h:139
HRESULT m_hResultCode
HRESULT S_OK=0, "winerror.h" code. 0x20000000 = start of custom codes. E_FAIL = unknown error.
Definition: cException.h:136
virtual ~cExceptionHResult()((void) 0)
Definition: cException.h:144
Definition: cException.h:28
cExceptionHolder() noexcept
Definition: cException.h:41
cExceptionHolder(cExceptionBase &ex) noexcept
Definition: cException.h:51
static const StrLen_t k_MSG_MAX_SIZE
arbitrary max message size.
Definition: cException.h:35
~cExceptionHolder() noexcept
Definition: cException.h:57
void AttachException(cExceptionBase *pEx, bool bDeleteEx)
Definition: cException.h:69
cExceptionHolder(cExceptionBase *pEx, bool bDeleteEx=true) noexcept
Definition: cException.h:45
Definition: cException.h:83
LOGLEV_TYPE get_Severity() const noexcept
Definition: cException.h:106
const LOGCHAR_t * m_pszDescription
this pointer should be to something static !?
Definition: cException.h:91
const LOGLEV_TYPE m_eSeverity
how bad is this ?
Definition: cException.h:90
virtual const char * what() const ((void) 0)
Definition: cException.h:119
virtual ~cException()((void) 0)
Definition: cException.h:103
cException(const LOGCHAR_t *pszDescription, LOGLEV_TYPE eLogLevel=LOGLEV_ERROR) noexcept
Definition: cException.h:95
static const LOGCHAR_t * k_szDescriptionDefault
Definition: cException.h:92
Definition: cPtrFacade.h:19
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
int StrLen_t
the length of a string in chars (bytes for UTF8, wchar_t for UNICODE). or offset in characters....
Definition: StrConst.h:32
LOGLEV_TYPE
Definition: cLogLevel.h:22
cStringT< LOGCHAR_t > cStringL
Log string.
Definition: cException.h:20
class __DECL_IMPORT cException
Definition: cExceptionBase.h:47
char GChar_t
My version of TCHAR, _TCHAR.
Definition: StrConst.h:26
char LOGCHAR_t
always just use UTF8 for logs, don't bother with UNICODE.
Definition: cLogLevel.h:17