1 : // Copyright 2014 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 : //
15 : // Utilities for COM objects, error codes etc.
16 :
17 : #ifndef SYZYGY_COMMON_COM_UTILS_H_
18 : #define SYZYGY_COMMON_COM_UTILS_H_
19 :
20 : #include <windows.h>
21 : #include <wtypes.h>
22 : #include <ostream>
23 :
24 : namespace common {
25 :
26 : // Returns the provided hr if it is an error, otherwise default_error.
27 E : inline HRESULT AlwaysError(HRESULT hr, HRESULT default_error) {
28 E : if (FAILED(hr)) {
29 E : return hr;
30 i : } else {
31 E : return default_error;
32 : }
33 E : }
34 :
35 : // Returns the provided hr if it is an error, otherwise E_FAIL.
36 E : inline HRESULT AlwaysError(HRESULT hr) {
37 E : return AlwaysError(hr, E_FAIL);
38 E : }
39 :
40 : // Converts a Win32 result code to a HRESULT. If the 'win32_code'
41 : // does not indicate an error, it returns 'default_error'.
42 : inline HRESULT AlwaysErrorFromWin32(DWORD win32_code,
43 E : HRESULT default_error) {
44 E : HRESULT hr = HRESULT_FROM_WIN32(win32_code);
45 E : return AlwaysError(hr, default_error);
46 E : }
47 :
48 : // Converts a Win32 result code to a HRESULT. If the 'win32_code'
49 : // does not indicate an error, it returns E_FAIL
50 E : inline HRESULT AlwaysErrorFromWin32(DWORD win32_code) {
51 E : return AlwaysErrorFromWin32(win32_code, E_FAIL);
52 E : }
53 :
54 : // Returns the HRESULT equivalent of GetLastError(), unless it does not
55 : // represent an error in which case it returns 'default_error'.
56 E : inline HRESULT AlwaysErrorFromLastError(HRESULT default_error) {
57 E : return AlwaysErrorFromWin32(::GetLastError(), default_error);
58 E : }
59 :
60 : // Returns the HRESULT equivalent of GetLastError(), unless it does not
61 : // represent an error in which case it returns E_FAIL.
62 E : inline HRESULT AlwaysErrorFromLastError() {
63 E : return AlwaysErrorFromLastError(E_FAIL);
64 E : }
65 :
66 : // Return the specified string, or the empty string if it is NULL.
67 E : inline const wchar_t* ToString(BSTR str) {
68 E : return str ? str : L"";
69 E : }
70 :
71 : // Logs HRESULTs verbosely, with the error code and human-readable error
72 : // text if available.
73 : class LogHr {
74 : public:
75 E : explicit LogHr(HRESULT hr) : hr_(hr) {}
76 : private:
77 : HRESULT hr_;
78 : friend std::ostream& operator<<(std::ostream&, const LogHr&);
79 : };
80 :
81 : std::ostream& operator<<(std::ostream& os, const LogHr& hr);
82 :
83 : // Logs Windows errors verbosely, with the error code and human-readable error
84 : // text if available.
85 : class LogWe {
86 : public:
87 i : LogWe() : we_(::GetLastError()) {}
88 E : explicit LogWe(DWORD we) : we_(we) {}
89 : private:
90 : DWORD we_;
91 : friend std::ostream& operator<<(std::ostream&, const LogWe&);
92 : };
93 :
94 : std::ostream& operator<<(std::ostream& os, const LogWe& we);
95 :
96 : } // namespace common
97 :
98 : #endif // SYZYGY_COMMON_COM_UTILS_H_
|