Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
HResult.h
Go to the documentation of this file.
1 //
5 
6 #ifndef _INC_HResult_H
7 #define _INC_HResult_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "StrConst.h"
13 #include "cPair.h"
14 #include "FileName.h" // FILECHAR_t
15 #include "cUnitTestDecl.h"
16 #ifdef _WIN32
17 #include <winerror.h>
18 #endif
19 
20 namespace Gray
21 {
22 #if defined(_CPPUNWIND)
23 #define THROW_DEF throw() // indicate that a function can throw exceptions.
24 #else
25 #define THROW_DEF __noop
26 #endif
27 
28 #ifndef SUCCEEDED // __linux__
29 #define SUCCEEDED(x) (((HRESULT)(x)) >= S_OK)
30 #define FAILED(x) (((HRESULT)(x)) < S_OK)
31 #endif // __linux__
32 
33  extern GRAYCORE_LINK va_list k_va_list_empty; // For faking out the va_list. __GNUC__ doesn't allow a pointer to va_list. So use this to simulate nullptr.
34 
36  {
40 
41 #ifdef __linux__
42  FACILITY_NULL = 0,
43  FACILITY_RPC = 1,
44  FACILITY_DISPATCH = 2,
45  FACILITY_ITF = 4,
46  FACILITY_WIN32 = 7,
47  // FACILITY_DPLAY
48 #endif
50 #ifdef __GNUC__
51  FACILITY_SECURITY = 9,
52  FACILITY_INTERNET = 12,
53  FACILITY_COMPLUS = 17,
54  FACILITY_HTTP = 25,
55  FACILITY_FVE = 49,
56 #endif
57  FACILITY_MMSYS = 0x100,
58  // FACILITY_DXAPP = 0x200, //!< Facility for DirectX sample application codes. Internal App codes.
59  FACILITY_D3D = 0x876,
61  };
62 
64  {
68 #define HRESULT_WIN32_DEF(a,b,c) a=b,
69 #include "HResultWin32.tbl"
70 #undef HRESULT_WIN32_DEF
71  };
72 
73 #if ! defined(LSTATUS) && ! defined(_WIN32)
74  typedef LONG LSTATUS;
75 #endif
76 #ifndef MAKE_HRESULT
77 #define MAKE_HRESULT(sev,fac,code) ((HRESULT) (((unsigned long)(sev)<<31) | ((unsigned long)(fac)<<16) | ((unsigned long)(code))) )
78 #endif
79 #define HRESULT_WIN32_C(x) MAKE_HRESULT(1,FACILITY_WIN32,(WORD)(x))
80 
82  {
85 
86 #ifdef __linux__
87  S_OK = ((HRESULT)0),
88  S_FALSE = ((HRESULT)1),
89 
90  // Alias/Alternate names for existing FACILITY_WIN32 codes.
91  E_ACCESSDENIED = HRESULT_WIN32_C(ERROR_ACCESS_DENIED), // "General access denied error"
92  E_HANDLE = HRESULT_WIN32_C(ERROR_INVALID_HANDLE), // "Invalid handle"
93  E_OUTOFMEMORY = HRESULT_WIN32_C(ERROR_OUTOFMEMORY), // "Ran out of memory"
94  E_INVALIDARG = HRESULT_WIN32_C(ERROR_INVALID_PARAMETER), // "One or more arguments are invalid"
95 #endif
96 
97 #define HRESULT_ENTRY(a,b,c,d) a=MAKE_HRESULT(1,b,c),
98 #include "HResults.tbl"
99 #undef HRESULT_ENTRY
100  };
101 
103  {
107  public:
109  const char* m_pszMsg;
110 
111  public:
112  int FindCode(HRESULT hRes) const;
113  };
114 
116  {
122 
123  public:
124  typedef cPair<FACILITY_TYPE, const GChar_t*> Facility_t; // name the facilities.
125  static const Facility_t k_Facility[];
126 
128 
129  public:
130  HResult(HRESULT hRes) noexcept
131  : m_hRes(hRes)
132  {
133  }
134  HResult(FACILITY_TYPE eFacility, WORD wCode) noexcept
135  : m_hRes(MAKE_HRESULT(1, eFacility, wCode))
136  {
139  }
140  HResult(int eFacility, long wCode) noexcept
141  : m_hRes(MAKE_HRESULT(1, eFacility, wCode))
142  {
145  }
146 
147  operator HRESULT() const noexcept
148  {
149  return m_hRes;
150  }
151 
152  static inline DWORD GetCode(HRESULT hRes) noexcept
153  {
156  return (DWORD)((hRes) & 0xFFFF);
157  }
158  inline DWORD get_Code() const noexcept
159  {
160  return GetCode(m_hRes);
161  }
162 
163  static inline FACILITY_TYPE GetFacility(HRESULT hRes) noexcept
164  {
166  return (FACILITY_TYPE)(((hRes) >> 16) & 0x1fff);
167  }
168  inline FACILITY_TYPE get_Facility() const noexcept
169  {
170  return GetFacility(m_hRes);
171  }
172 
173  static inline bool IsFailure(HRESULT hRes) noexcept
174  {
177  return hRes < 0;
178  }
179  bool isFailure() const noexcept
180  {
183  return m_hRes < 0;
184  }
185 
186  static inline HRESULT Make(FACILITY_TYPE eFacility, WORD wCode) noexcept
187  {
189  return MAKE_HRESULT(1, eFacility, wCode);
190  }
191  static inline HRESULT Make(BYTE bReserved, FACILITY_TYPE eFacility, WORD wCode) noexcept
192  {
195  return MAKE_HRESULT(1, eFacility, wCode) | (((HRESULT)bReserved) << (16 + 11));
196  }
197 
198  static inline HRESULT FromWin32(DWORD dwWin32Code) noexcept
199  {
202  if ((HRESULT)dwWin32Code <= 0) // NO_ERROR
203  {
204  // <0 shouldn't happen! supposed to be unsigned. ASSUME its already a HRESULT code.
205  return ((HRESULT)dwWin32Code); // already HRESULT failure. see GetLastError() docs.
206  }
207  if (dwWin32Code > 0xFFFF)
208  {
209  // This is weird ! NOT a proper error code !?
210  dwWin32Code &= 0xFFFF;
211  }
212  return HRESULT_WIN32_C(dwWin32Code);
213  }
214 
215 #ifdef _WIN32
216  static inline HRESULT FromWaitRet(DWORD dwRet) noexcept
217  {
219  if (dwRet == WAIT_FAILED) // 0xFFFFFFFF
220  return E_HANDLE;
221  if (dwRet == WAIT_TIMEOUT) // 258L
222  return HRESULT_WIN32_C(ERROR_WAIT_TIMEOUT);
223  if (dwRet >= STATUS_ABANDONED_WAIT_0) // no idea.
224  return E_FAIL;
225  // WAIT_OBJECT_0 or STATUS_WAIT_0
226  return (HRESULT)dwRet; // Wait signaled done. 0=event 0
227  }
228 #endif
229 
230  static HRESULT GRAYCALL GetLast() noexcept;
231 
232  static inline HRESULT GetDef(HRESULT hRes, HRESULT hResDef = E_FAIL) noexcept
233  {
236  if (SUCCEEDED(hRes) && FAILED(hResDef))
237  {
238  return hResDef; // Oddly no error was supplied! provide a default error.
239  }
240  return hRes;
241  }
242  static inline HRESULT GetLastDef(HRESULT hResDef = E_FAIL) noexcept
243  {
246  return GetDef(GetLast(), hResDef);
247  }
248 
249  static HRESULT GRAYCALL FromPOSIX(int iErrNo) noexcept; // from <errno.h> style
250 
251 #ifndef UNDER_CE
252  static HRESULT GRAYCALL GetPOSIXLast() noexcept;
253  static HRESULT GRAYCALL GetPOSIXLastDef(HRESULT hResDef = E_FAIL) noexcept // static
254  {
255  return GetDef(GetPOSIXLast(), hResDef);
256  }
257 #endif
258 
259  static void GRAYCALL AddCodes(const HResultCode* pCodes);
260  static void GRAYCALL AddCodesDefault();
261  static HRESULT GRAYCALL AddCodesText(const char* pszText);
262  static HRESULT GRAYCALL AddCodesFile(const FILECHAR_t* pszFilePath);
263 
264  static const char* GRAYCALL GetTextBase(HRESULT hRes);
265  static StrLen_t GRAYCALL GetTextSys(HRESULT hRes, GChar_t* lpszError, StrLen_t nLenMaxError, void* pSource = nullptr, va_list vargs = k_va_list_empty);
266  static StrLen_t GRAYCALL GetTextV(HRESULT hRes, GChar_t* lpszError, StrLen_t nLenMaxError, void* pSource = nullptr, va_list vargs = k_va_list_empty);
267 
268  static HRESULT GRAYCALL GetHResFromStr(const GChar_t* pszError, StrLen_t nLenError = -1);
269 
271  };
272 }
273 
274 #endif // _INC_HResult_H
#define GRAYCORE_LINK
Definition: GrayCore.h:47
#define GRAYCALL
declare calling convention for static functions so everyone knows the arg passing scheme....
Definition: GrayCore.h:36
#define SUCCEEDED(x)
Definition: HResult.h:29
#define HRESULT_WIN32_C(x)
a constant LSTATUS/error_status_t with no check, unlike HRESULT_FROM_WIN32()
Definition: HResult.h:79
#define MAKE_HRESULT(sev, fac, code)
Definition: HResult.h:77
#define FAILED(x)
Definition: HResult.h:30
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#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: HResult.h:103
HRESULT m_hRes
Definition: HResult.h:108
const char * m_pszMsg
error code for a FACILITY_TYPE. might just use WORD?
Definition: HResult.h:109
Definition: HResult.h:116
static HRESULT GetLastDef(HRESULT hResDef=E_FAIL) noexcept
Definition: HResult.h:242
HResult(HRESULT hRes) noexcept
Definition: HResult.h:130
static FACILITY_TYPE GetFacility(HRESULT hRes) noexcept
Definition: HResult.h:163
HResult(FACILITY_TYPE eFacility, WORD wCode) noexcept
Definition: HResult.h:134
HResult(int eFacility, long wCode) noexcept
Definition: HResult.h:140
static HRESULT Make(BYTE bReserved, FACILITY_TYPE eFacility, WORD wCode) noexcept
Definition: HResult.h:191
static DWORD GetCode(HRESULT hRes) noexcept
Definition: HResult.h:152
FACILITY_TYPE get_Facility() const noexcept
Definition: HResult.h:168
bool isFailure() const noexcept
Definition: HResult.h:179
HRESULT m_hRes
Definition: HResult.h:127
static HRESULT Make(FACILITY_TYPE eFacility, WORD wCode) noexcept
Definition: HResult.h:186
cPair< FACILITY_TYPE, const GChar_t * > Facility_t
Definition: HResult.h:124
static bool IsFailure(HRESULT hRes) noexcept
Definition: HResult.h:173
static HRESULT FromWin32(DWORD dwWin32Code) noexcept
Definition: HResult.h:198
DWORD get_Code() const noexcept
Definition: HResult.h:158
Definition: cPair.h:249
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
LONG LSTATUS
AKA error_status_t. FACILITY_WIN32 codes returned from RegCreateKeyEx() etc. Maybe NOT GetLastError()...
Definition: HResult.h:74
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
HRESULT_OTHER_TYPE_
Definition: HResult.h:82
__DECL_IMPORT va_list k_va_list_empty
Definition: HResult.cpp:18
char FILECHAR_t
a UTF8 char in a file name. like TCHAR
Definition: FileName.h:22
HRESULT_WIN32_TYPE_
Definition: HResult.h:64
char GChar_t
My version of TCHAR, _TCHAR.
Definition: StrConst.h:26
FACILITY_TYPE
Definition: HResult.h:36
@ FACILITY_MMSYS
Facility for _WIN32 MMSYSTEM MMRESULT error codes. MMSYSERR_BASE.
Definition: HResult.h:57
@ FACILITY_D3D
max = 2048 = 0x800 = 11 bits ?
Definition: HResult.h:59
@ FACILITY_POSIX
Facility for POSIX _errno in a _WIN32 style code.
Definition: HResult.h:49