Flutter Engine
The Flutter Engine
variant_util.h
Go to the documentation of this file.
1// Copyright 2020 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef BASE_WIN_VARIANT_UTIL_H_
6#define BASE_WIN_VARIANT_UTIL_H_
7
8#include "base/logging.h"
9
10namespace base {
11namespace win {
12namespace internal {
13
14// Returns true if a VARIANT of type |self| can be assigned to a
15// variant of type |other|.
16// Does not allow converting unsigned <-> signed or converting between
17// different sized types, but does allow converting IDispatch* -> IUnknown*.
18constexpr bool VarTypeIsConvertibleTo(VARTYPE self, VARTYPE other) {
19 // IDispatch inherits from IUnknown, so it's safe to
20 // upcast a VT_DISPATCH into an IUnknown*.
21 return (self == other) || (self == VT_DISPATCH && other == VT_UNKNOWN);
22}
23
24// VartypeToNativeType contains the underlying |Type| and offset to the
25// VARIANT union member related to the |ElementVartype| for simple types.
26template <VARTYPE ElementVartype>
27struct VartypeToNativeType final {};
28
29template <>
30struct VartypeToNativeType<VT_BOOL> final {
31 using Type = VARIANT_BOOL;
32 static constexpr VARIANT_BOOL VARIANT::*kMemberOffset = &VARIANT::boolVal;
33};
34
35template <>
36struct VartypeToNativeType<VT_I1> final {
37 using Type = int8_t;
38 static constexpr CHAR VARIANT::*kMemberOffset = &VARIANT::cVal;
39};
40
41template <>
42struct VartypeToNativeType<VT_UI1> final {
43 using Type = uint8_t;
44 static constexpr BYTE VARIANT::*kMemberOffset = &VARIANT::bVal;
45};
46
47template <>
48struct VartypeToNativeType<VT_I2> final {
49 using Type = int16_t;
50 static constexpr SHORT VARIANT::*kMemberOffset = &VARIANT::iVal;
51};
52
53template <>
54struct VartypeToNativeType<VT_UI2> final {
55 using Type = uint16_t;
56 static constexpr USHORT VARIANT::*kMemberOffset = &VARIANT::uiVal;
57};
58
59template <>
60struct VartypeToNativeType<VT_I4> final {
61 using Type = int32_t;
62 static constexpr LONG VARIANT::*kMemberOffset = &VARIANT::lVal;
63};
64
65template <>
66struct VartypeToNativeType<VT_UI4> final {
67 using Type = uint32_t;
68 static constexpr ULONG VARIANT::*kMemberOffset = &VARIANT::ulVal;
69};
70
71template <>
72struct VartypeToNativeType<VT_I8> final {
73 using Type = int64_t;
74 static constexpr LONGLONG VARIANT::*kMemberOffset = &VARIANT::llVal;
75};
76
77template <>
78struct VartypeToNativeType<VT_UI8> final {
79 using Type = uint64_t;
80 static constexpr ULONGLONG VARIANT::*kMemberOffset = &VARIANT::ullVal;
81};
82
83template <>
84struct VartypeToNativeType<VT_R4> final {
85 using Type = float;
86 static constexpr FLOAT VARIANT::*kMemberOffset = &VARIANT::fltVal;
87};
88
89template <>
90struct VartypeToNativeType<VT_R8> final {
91 using Type = double;
92 static constexpr DOUBLE VARIANT::*kMemberOffset = &VARIANT::dblVal;
93};
94
95template <>
96struct VartypeToNativeType<VT_DATE> final {
97 using Type = DATE;
98 static constexpr DATE VARIANT::*kMemberOffset = &VARIANT::date;
99};
100
101template <>
102struct VartypeToNativeType<VT_BSTR> final {
103 using Type = BSTR;
104 static constexpr BSTR VARIANT::*kMemberOffset = &VARIANT::bstrVal;
105};
106
107template <>
108struct VartypeToNativeType<VT_UNKNOWN> final {
109 using Type = IUnknown*;
110 static constexpr IUnknown* VARIANT::*kMemberOffset = &VARIANT::punkVal;
111};
112
113template <>
114struct VartypeToNativeType<VT_DISPATCH> final {
115 using Type = IDispatch*;
116 static constexpr IDispatch* VARIANT::*kMemberOffset = &VARIANT::pdispVal;
117};
118
119// VariantUtil contains the underlying |Type| and helper methods
120// related to the |ElementVartype| for simple types.
121template <VARTYPE ElementVartype>
122struct VariantUtil final {
124 static constexpr bool IsConvertibleTo(VARTYPE vartype) {
125 return VarTypeIsConvertibleTo(ElementVartype, vartype);
126 }
127 static constexpr bool IsConvertibleFrom(VARTYPE vartype) {
128 return VarTypeIsConvertibleTo(vartype, ElementVartype);
129 }
130 // Get the associated VARIANT union member value.
131 // Returns the value owned by the VARIANT without affecting the lifetime
132 // of managed contents.
133 // e.g. Does not affect IUnknown* reference counts or allocate a BSTR.
134 static Type RawGet(const VARIANT& var) {
135 BASE_DCHECK(IsConvertibleFrom(V_VT(&var)));
137 }
138 // Set the associated VARIANT union member value.
139 // The caller is responsible for handling the lifetime of managed contents.
140 // e.g. Incrementing IUnknown* reference counts or allocating a BSTR.
141 static void RawSet(VARIANT* var, Type value) {
142 BASE_DCHECK(IsConvertibleTo(V_VT(var)));
144 }
145};
146
147} // namespace internal
148} // namespace win
149} // namespace base
150
151#endif // BASE_WIN_VARIANT_UTIL_H_
uint8_t value
constexpr bool VarTypeIsConvertibleTo(VARTYPE self, VARTYPE other)
Definition: variant_util.h:18
static constexpr bool IsConvertibleTo(VARTYPE vartype)
Definition: variant_util.h:124
static constexpr bool IsConvertibleFrom(VARTYPE vartype)
Definition: variant_util.h:127
static void RawSet(VARIANT *var, Type value)
Definition: variant_util.h:141
static Type RawGet(const VARIANT &var)
Definition: variant_util.h:134
#define BASE_DCHECK(condition)
Definition: logging.h:63
long LONG
Definition: windows_types.h:23
unsigned char BYTE
Definition: windows_types.h:38
__int64 LONGLONG
Definition: windows_types.h:24
short SHORT
Definition: windows_types.h:29
DWORD ULONG
Definition: windows_types.h:40
unsigned __int64 ULONGLONG
Definition: windows_types.h:25
char CHAR
Definition: windows_types.h:28