-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathcom_lib.h
76 lines (61 loc) · 1.67 KB
/
com_lib.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* Part of WinLamb - Win32 API Lambda Library
* https://github.com/rodrigocfd/winlamb
* Copyright 2017-present Rodrigo Cesar de Freitas Dias
* This library is released under the MIT License
*/
#pragma once
#include <system_error>
#include <Windows.h>
#include <objbase.h>
namespace wl {
// Wrappers to COM objects.
namespace com {
// Smart class to automate CoInitialize and CoUninitialize COM calls.
class lib {
private:
HRESULT _hr = -1;
public:
enum class init { NOW, LATER };
~lib() {
// https://stackoverflow.com/q/47123650/6923555
this->un_initialize();
}
explicit lib(init when) noexcept {
if (when == init::NOW) {
this->initialize();
}
}
lib(lib&& other) noexcept : _hr{other._hr} { other._hr = -1; }
lib& operator=(lib&& other) noexcept {
this->un_initialize();
std::swap(this->_hr, other._hr);
return *this;
}
void initialize() noexcept {
// #define FAILED(hr) (((HRESULT)(hr)) < 0)
if (FAILED(this->_hr)) {
// So that initialize() can be carelessly called multiple
// times, but CoInitialize() will be called only once.
this->_hr = CoInitialize(nullptr);
}
}
void un_initialize() noexcept {
// #define SUCCEEDED(hr) (((HRESULT)(hr)) >= 0)
if (SUCCEEDED(this->_hr)) {
CoUninitialize();
this->_hr = -1;
}
}
HRESULT hresult() const noexcept {
return this->_hr;
}
};
// COM utility which calls FAILED() macro upon HRESULT; if failed, throws a system_error.
inline void check_hr(HRESULT hr, const char* exceptionMsg) {
if (FAILED(hr)) {
throw std::system_error(hr, std::system_category(), exceptionMsg);
}
}
}//namespace com
}//namespace wl