Alphabetical Class Index  Class Hierarchy   File Members   Compound Members   File List  

HResult.h
1 //-----------------------------------------------------------------------------
2 // A utility class that helps with HRESULTS and error checking.
3 // To Use:
4 //
5 // HResult hr = comObj->func();
6 //
7 // If the result fails, an exception will be thrown with the
8 // offending HRESULT.
9 // Typically, one uses this as:
10 //
11 // HRESULT someFunction()
12 // {
13 // HResult hr;
14 // try
15 // {
16 // hr = comObj->func();
17 // }
18 // catch(HRESULT hResult)
19 // {
20 // throw hResult;
21 // }
22 // return S_OK;
23 // }
24 
25 #ifndef _HRESULT_H__
26 #define _HRESULT_H__
27 #ifdef __APPLE__
28 #include <CoreFoundation/CFPluginCOM.h>
29 #endif
30 
31 #ifndef UNREFERENCED
32 #define UNREFERENCED(param) ((void)(param))
33 #endif
34 
35 
36 #ifdef _DEBUG
37 #ifndef _WINDEF_
38 extern "C" __declspec(dllimport) void __stdcall OutputDebugStringA (char const * str);
39 #define OutputDebugString OutputDebugStringA
40 #endif
41 
42 #include <stdio.h>
43 #include <stdarg.h>
44 inline void dprintf (char const * format, ...) {
45  char buffer[1024];
46  va_list arguments;
47 
48  va_start (arguments, format);
49  vsprintf (buffer, format, arguments);
50  va_end (arguments);
51 #ifdef _MSC_VER
52 #ifndef _WIN32_WCE
53  OutputDebugStringA (buffer);
54 #endif
55 #else
56  fprintf (stderr, buffer);
57 #endif
58 }
59 #else
60 inline void dprintf (char const * format, ...) {
61  UNREFERENCED(format);
62 }
63 #endif
64 
65 
66 class HResult
67 {
68 public:
69  HResult(HRESULT hr)
70  {
71  m_hr = hr;
72  if(FAILED(hr))
73  {
74  //throw hr;
75  dprintf("HRESULT error %x\n", hr);
76  }
77  }
78  HResult()
79  {
80  }
81 
82  HResult& operator=(HRESULT hr)
83  {
84  m_hr = hr;
85  if(FAILED(hr))
86  {
87  //throw hr;
88  dprintf("HRESULT error %x\n", hr);
89  }
90  return *this;
91  }
92 
93  bool operator==(HRESULT hr)
94  {
95  if (m_hr==hr)
96  {
97  return true;
98  }
99  else
100  {
101  return false;
102  }
103  }
104 
105  bool Succeeded()
106  {
107  return SUCCEEDED(m_hr);
108  }
109 
110  bool Failed()
111  {
112  return FAILED(m_hr);
113  }
114 private:
115 
116  HRESULT m_hr;
117 
118 };
119 #endif // _HRESULT_H__
Definition: HResult.h:66