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
base / win / variant_conversions.h [blame]
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BASE_WIN_VARIANT_CONVERSIONS_H_
#define BASE_WIN_VARIANT_CONVERSIONS_H_
#include <oaidl.h>
#include <stdint.h>
#include <wtypes.h>
#include "base/check.h"
namespace base {
namespace win {
namespace internal {
// Returns true if a VARIANT of type |self| can be assigned to a
// variant of type |other|.
// Does not allow converting unsigned <-> signed or converting between
// different sized types, but does allow converting IDispatch* -> IUnknown*.
constexpr bool VarTypeIsConvertibleTo(VARTYPE self, VARTYPE other) {
// IDispatch inherits from IUnknown, so it's safe to
// upcast a VT_DISPATCH into an IUnknown*.
return (self == other) || (self == VT_DISPATCH && other == VT_UNKNOWN);
}
// VartypeToNativeType contains the underlying |Type| and offset to the
// VARIANT union member related to the |ElementVartype| for simple types.
template <VARTYPE ElementVartype>
struct VartypeToNativeType final {};
template <>
struct VartypeToNativeType<VT_BOOL> final {
using Type = VARIANT_BOOL;
static constexpr VARIANT_BOOL VARIANT::*kMemberOffset = &VARIANT::boolVal;
};
template <>
struct VartypeToNativeType<VT_I1> final {
using Type = int8_t;
static constexpr CHAR VARIANT::*kMemberOffset = &VARIANT::cVal;
};
template <>
struct VartypeToNativeType<VT_UI1> final {
using Type = uint8_t;
static constexpr BYTE VARIANT::*kMemberOffset = &VARIANT::bVal;
};
template <>
struct VartypeToNativeType<VT_I2> final {
using Type = int16_t;
static constexpr SHORT VARIANT::*kMemberOffset = &VARIANT::iVal;
};
template <>
struct VartypeToNativeType<VT_UI2> final {
using Type = uint16_t;
static constexpr USHORT VARIANT::*kMemberOffset = &VARIANT::uiVal;
};
template <>
struct VartypeToNativeType<VT_I4> final {
using Type = int32_t;
static constexpr LONG VARIANT::*kMemberOffset = &VARIANT::lVal;
};
template <>
struct VartypeToNativeType<VT_UI4> final {
using Type = uint32_t;
static constexpr ULONG VARIANT::*kMemberOffset = &VARIANT::ulVal;
};
template <>
struct VartypeToNativeType<VT_I8> final {
using Type = int64_t;
static constexpr LONGLONG VARIANT::*kMemberOffset = &VARIANT::llVal;
};
template <>
struct VartypeToNativeType<VT_UI8> final {
using Type = uint64_t;
static constexpr ULONGLONG VARIANT::*kMemberOffset = &VARIANT::ullVal;
};
template <>
struct VartypeToNativeType<VT_R4> final {
using Type = float;
static constexpr FLOAT VARIANT::*kMemberOffset = &VARIANT::fltVal;
};
template <>
struct VartypeToNativeType<VT_R8> final {
using Type = double;
static constexpr DOUBLE VARIANT::*kMemberOffset = &VARIANT::dblVal;
};
template <>
struct VartypeToNativeType<VT_DATE> final {
using Type = DATE;
static constexpr DATE VARIANT::*kMemberOffset = &VARIANT::date;
};
template <>
struct VartypeToNativeType<VT_BSTR> final {
using Type = BSTR;
static constexpr BSTR VARIANT::*kMemberOffset = &VARIANT::bstrVal;
};
template <>
struct VartypeToNativeType<VT_UNKNOWN> final {
using Type = IUnknown*;
static constexpr IUnknown* VARIANT::*kMemberOffset = &VARIANT::punkVal;
};
template <>
struct VartypeToNativeType<VT_DISPATCH> final {
using Type = IDispatch*;
static constexpr IDispatch* VARIANT::*kMemberOffset = &VARIANT::pdispVal;
};
// VariantConverter contains the underlying |Type| and helper methods
// related to the |ElementVartype| for simple types.
template <VARTYPE ElementVartype>
struct VariantConverter final {
using Type = typename VartypeToNativeType<ElementVartype>::Type;
static constexpr bool IsConvertibleTo(VARTYPE vartype) {
return VarTypeIsConvertibleTo(ElementVartype, vartype);
}
static constexpr bool IsConvertibleFrom(VARTYPE vartype) {
return VarTypeIsConvertibleTo(vartype, ElementVartype);
}
// Get the associated VARIANT union member value.
// Returns the value owned by the VARIANT without affecting the lifetime
// of managed contents.
// e.g. Does not affect IUnknown* reference counts or allocate a BSTR.
static Type RawGet(const VARIANT& var) {
DCHECK(IsConvertibleFrom(V_VT(&var)));
return var.*VartypeToNativeType<ElementVartype>::kMemberOffset;
}
// Set the associated VARIANT union member value.
// The caller is responsible for handling the lifetime of managed contents.
// e.g. Incrementing IUnknown* reference counts or allocating a BSTR.
static void RawSet(VARIANT* var, Type value) {
DCHECK(IsConvertibleTo(V_VT(var)));
var->*VartypeToNativeType<ElementVartype>::kMemberOffset = value;
}
};
} // namespace internal
} // namespace win
} // namespace base
#endif // BASE_WIN_VARIANT_CONVERSIONS_H_