|
1 | 1 | #ifndef COMHELPERS_H |
2 | 2 | #define COMHELPERS_H |
3 | 3 |
|
| 4 | +#include <cassert> |
| 5 | +#include <string> |
| 6 | +#include <string_view> |
4 | 7 | #include <utility> |
5 | 8 | #include <windows.h> |
6 | 9 |
|
7 | 10 |
|
8 | 11 | class PropVariant { |
9 | | - PROPVARIANT mProp; |
| 12 | + PROPVARIANT mProp{}; |
10 | 13 |
|
11 | 14 | public: |
12 | 15 | PropVariant() { PropVariantInit(&mProp); } |
13 | | - PropVariant(const PropVariant&) = delete; |
| 16 | + PropVariant(const PropVariant &rhs) : PropVariant{} { PropVariantCopy(&mProp, &rhs.mProp); } |
14 | 17 | ~PropVariant() { clear(); } |
15 | 18 |
|
16 | | - PropVariant& operator=(const PropVariant&) = delete; |
| 19 | + auto operator=(const PropVariant &rhs) -> PropVariant& |
| 20 | + { |
| 21 | + if(this != &rhs) |
| 22 | + PropVariantCopy(&mProp, &rhs.mProp); |
| 23 | + return *this; |
| 24 | + } |
17 | 25 |
|
18 | 26 | void clear() { PropVariantClear(&mProp); } |
19 | 27 |
|
20 | | - PROPVARIANT* get() noexcept { return &mProp; } |
| 28 | + auto get() noexcept -> PROPVARIANT* { return &mProp; } |
21 | 29 |
|
22 | | - PROPVARIANT& operator*() noexcept { return mProp; } |
23 | | - const PROPVARIANT& operator*() const noexcept { return mProp; } |
| 30 | + /* NOLINTBEGIN(cppcoreguidelines-pro-type-union-access) */ |
| 31 | + [[nodiscard]] |
| 32 | + auto type() const noexcept -> VARTYPE { return mProp.vt; } |
24 | 33 |
|
25 | | - PROPVARIANT* operator->() noexcept { return &mProp; } |
26 | | - const PROPVARIANT* operator->() const noexcept { return &mProp; } |
| 34 | + template<typename T> [[nodiscard]] |
| 35 | + auto value() const -> T |
| 36 | + { |
| 37 | + if constexpr(std::is_same_v<T,UINT>) |
| 38 | + { |
| 39 | + assert(mProp.vt == VT_UI4 || mProp.vt == VT_UINT); |
| 40 | + return mProp.uintVal; |
| 41 | + } |
| 42 | + else if constexpr(std::is_same_v<T,ULONG>) |
| 43 | + { |
| 44 | + assert(mProp.vt == VT_UI4 || mProp.vt == VT_UINT); |
| 45 | + return mProp.ulVal; |
| 46 | + } |
| 47 | + else if constexpr(std::is_same_v<T,std::wstring_view> || std::is_same_v<T,std::wstring> |
| 48 | + || std::is_same_v<T,LPWSTR> || std::is_same_v<T,LPCWSTR>) |
| 49 | + { |
| 50 | + assert(mProp.vt == VT_LPWSTR); |
| 51 | + return mProp.pwszVal; |
| 52 | + } |
| 53 | + } |
| 54 | + /* NOLINTEND(cppcoreguidelines-pro-type-union-access) */ |
27 | 55 | }; |
28 | 56 |
|
29 | 57 | struct ComWrapper { |
|
0 commit comments