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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
#include <oaidl.h>
#include <propidl.h>
#include <type_traits>
#include <utility>
#include <string_view>
#include <cassert>
#include <cstdint>
template <typename TVariant>
struct FFBaseVariant : TVariant {
bool hasValue() {
return this->vt != VT_EMPTY;
}
explicit operator bool() {
return this->hasValue();
}
template <typename T>
T get() {
// boolean
if constexpr (std::is_same_v<T, bool>) {
assert(this->vt == VT_BOOL);
return this->boolVal != VARIANT_FALSE;
}
// signed
else if constexpr (std::is_same_v<T, int8_t>) {
assert(this->vt == VT_I1);
return this->cVal;
} else if constexpr (std::is_same_v<T, int16_t>) {
assert(this->vt == VT_I2);
return this->iVal;
} else if constexpr (std::is_same_v<T, int32_t>) {
assert(this->vt == VT_I4 || this->vt == VT_INT);
return this->intVal;
} else if constexpr (std::is_same_v<T, int64_t>) {
assert(this->vt == VT_I8);
return this->llVal;
}
// unsigned
else if constexpr (std::is_same_v<T, uint8_t>) {
assert(this->vt == VT_UI1);
return this->bVal;
} else if constexpr (std::is_same_v<T, uint16_t>) {
assert(this->vt == VT_UI2);
return this->uiVal;
} else if constexpr (std::is_same_v<T, uint32_t>) {
assert(this->vt == VT_UI4 || this->vt == VT_UINT);
return this->uintVal;
} else if constexpr (std::is_same_v<T, uint64_t>) {
assert(this->vt == VT_UI8);
return this->ullVal;
}
// decimal
else if constexpr (std::is_same_v<T, float>) {
assert(this->vt == VT_R4);
return this->fltVal;
} else if constexpr (std::is_same_v<T, double>) {
assert(this->vt == VT_R8);
return this->dblVal;
}
// string
else if constexpr (std::is_same_v<T, std::string_view>) {
assert(this->vt == VT_LPSTR);
return this->pcVal;
} else if constexpr (std::is_same_v<T, std::wstring_view>) {
assert(this->vt == VT_BSTR || this->vt == VT_LPWSTR);
if (this->vt == VT_LPWSTR) {
return this->bstrVal;
} else {
return { this->bstrVal, SysStringLen(this->bstrVal) };
}
}
// array signed
else if constexpr (std::is_same_v<T, std::pair<const int8_t*, uint32_t>>) {
assert(this->vt & VT_ARRAY);
assert((this->vt & ~VT_ARRAY) == VT_I1);
return std::make_pair((int8_t*) this->parray->pvData, this->parray->cDims);
} else if constexpr (std::is_same_v<T, std::pair<const int16_t*, uint32_t>>) {
assert(this->vt & VT_ARRAY);
assert((this->vt & ~VT_ARRAY) == VT_I2);
return std::make_pair((int16_t*) this->parray->pvData, this->parray->cDims);
} else if constexpr (std::is_same_v<T, std::pair<const int32_t*, uint32_t>>) {
assert(this->vt & VT_ARRAY);
assert((this->vt & ~VT_ARRAY) == VT_I4);
return std::make_pair((int32_t*) this->parray->pvData, this->parray->cDims);
} else if constexpr (std::is_same_v<T, std::pair<const int64_t*, uint32_t>>) {
assert(this->vt & VT_ARRAY);
assert((this->vt & ~VT_ARRAY) == VT_I8);
return std::make_pair((int64_t*) this->parray->pvData, this->parray->cDims);
}
// array unsigned
else if constexpr (std::is_same_v<T, std::pair<const uint8_t*, uint32_t>>) {
assert(this->vt & VT_ARRAY);
assert((this->vt & ~VT_ARRAY) == VT_UI1);
return std::make_pair((uint8_t*) this->parray->pvData, this->parray->cDims);
} else if constexpr (std::is_same_v<T, std::pair<const uint16_t*, uint32_t>>) {
assert(this->vt & VT_ARRAY);
assert((this->vt & ~VT_ARRAY) == VT_UI2);
return std::make_pair((uint16_t*) this->parray->pvData, this->parray->cDims);
} else if constexpr (std::is_same_v<T, std::pair<const uint32_t*, uint32_t>>) {
assert(this->vt & VT_ARRAY);
assert((this->vt & ~VT_ARRAY) == VT_UI4);
return std::make_pair((uint32_t*) this->parray->pvData, this->parray->cDims);
} else if constexpr (std::is_same_v<T, std::pair<const uint64_t*, uint32_t>>) {
assert(this->vt & VT_ARRAY);
assert((this->vt & ~VT_ARRAY) == VT_UI8);
return std::make_pair((uint64_t*) this->parray->pvData, this->parray->cDims);
} else {
assert(false && "unsupported type");
__builtin_unreachable();
}
}
};
struct FFWmiVariant : FFBaseVariant<VARIANT> {
FFWmiVariant(const FFWmiVariant&) = delete;
FFWmiVariant(FFWmiVariant&&); // don't define it to enforce NRVO optimization
explicit FFWmiVariant() {
VariantInit(this);
}
explicit FFWmiVariant(std::initializer_list<PCWSTR> strings);
~FFWmiVariant() {
VariantClear(this);
}
};
static_assert(sizeof(FFWmiVariant) == sizeof(VARIANT), "");
struct FFPropVariant : FFBaseVariant<PROPVARIANT> {
FFPropVariant(const FFPropVariant&) = delete;
FFPropVariant(FFPropVariant&&); // don't define it to enforce NRVO optimization
explicit FFPropVariant() {
PropVariantInit(this);
}
~FFPropVariant() {
PropVariantClear(this);
}
};
static_assert(sizeof(FFPropVariant) == sizeof(PROPVARIANT), "");
namespace {
// Provide our bstr_t to avoid libstdc++ dependency
struct bstr_t {
explicit bstr_t(const wchar_t* str) noexcept : _bstr(SysAllocString(str)) {}
~bstr_t(void) noexcept {
SysFreeString(_bstr);
}
explicit operator const wchar_t*(void) const noexcept {
return _bstr;
}
operator BSTR(void) const noexcept {
return _bstr;
}
private:
BSTR _bstr;
};
} // namespace
|