Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
scoped_variant.h
Go to the documentation of this file.
1// Copyright (c) 2011 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_SCOPED_VARIANT_H_
6#define BASE_WIN_SCOPED_VARIANT_H_
7
8#include <windows.h>
9
10#include <oleauto.h>
11
12#include <cstdint>
13
14#include "base/base_export.h"
15#include "base/macros.h"
16
17namespace base {
18namespace win {
19
20// Scoped VARIANT class for automatically freeing a COM VARIANT at the
21// end of a scope. Additionally provides a few functions to make the
22// encapsulated VARIANT easier to use.
23// Instead of inheriting from VARIANT, we take the containment approach
24// in order to have more control over the usage of the variant and guard
25// against memory leaks.
27 public:
28 // Declaration of a global variant variable that's always VT_EMPTY
29 static const VARIANT kEmptyVariant;
30
31 // Default constructor.
33 // This is equivalent to what VariantInit does, but less code.
34 var_.vt = VT_EMPTY;
35 }
36
37 // Constructor to create a new VT_BSTR VARIANT.
38 // NOTE: Do not pass a BSTR to this constructor expecting ownership to
39 // be transferred
40 explicit ScopedVariant(const wchar_t* str);
41
42 // Creates a new VT_BSTR variant of a specified length.
43 ScopedVariant(const wchar_t* str, UINT length);
44
45 // Creates a new integral type variant and assigns the value to
46 // VARIANT.lVal (32 bit sized field).
47 explicit ScopedVariant(long value, VARTYPE vt = VT_I4);
48
49 // Creates a new integral type variant for the int type and assigns the value
50 // to VARIANT.lVal (32 bit sized field).
51 explicit ScopedVariant(int value);
52
53 // Creates a new boolean (VT_BOOL) variant and assigns the value to
54 // VARIANT.boolVal.
55 explicit ScopedVariant(bool value);
56
57 // Creates a new double-precision type variant. |vt| must be either VT_R8
58 // or VT_DATE.
59 explicit ScopedVariant(double value, VARTYPE vt = VT_R8);
60
61 // VT_DISPATCH
62 explicit ScopedVariant(IDispatch* dispatch);
63
64 // VT_UNKNOWN
65 explicit ScopedVariant(IUnknown* unknown);
66
67 // SAFEARRAY
68 explicit ScopedVariant(SAFEARRAY* safearray);
69
70 // Copies the variant.
71 explicit ScopedVariant(const VARIANT& var);
72
73 // Moves the wrapped variant into another ScopedVariant.
75
77
78 inline VARTYPE type() const { return var_.vt; }
79
80 // Give ScopedVariant ownership over an already allocated VARIANT.
81 void Reset(const VARIANT& var = kEmptyVariant);
82
83 // Releases ownership of the VARIANT to the caller.
84 VARIANT Release();
85
86 // Swap two ScopedVariant's.
87 void Swap(ScopedVariant& var);
88
89 // Returns a copy of the variant.
90 VARIANT Copy() const;
91
92 // The return value is 0 if the variants are equal, 1 if this object is
93 // greater than |other|, -1 if it is smaller.
94 // Comparison with an array VARIANT is not supported.
95 // 1. VT_NULL and VT_EMPTY is always considered less-than any other VARTYPE.
96 // 2. If both VARIANTS have either VT_UNKNOWN or VT_DISPATCH even if the
97 // VARTYPEs do not match, the address of its IID_IUnknown is compared to
98 // guarantee a logical ordering even though it is not a meaningful order.
99 // e.g. (a.Compare(b) != b.Compare(a)) unless (a == b).
100 // 3. If the VARTYPEs do not match, then the value of the VARTYPE is compared.
101 // 4. Comparing VT_BSTR values is a lexicographical comparison of the contents
102 // of the BSTR, taking into account |ignore_case|.
103 // 5. Otherwise returns the lexicographical comparison of the values held by
104 // the two VARIANTS that share the same VARTYPE.
105 int Compare(const VARIANT& other, bool ignore_case = false) const;
106
107 // Retrieves the pointer address.
108 // Used to receive a VARIANT as an out argument (and take ownership).
109 // The function DCHECKs on the current value being empty/null.
110 // Usage: GetVariant(var.receive());
111 VARIANT* Receive();
112
113 void Set(const wchar_t* str);
114
115 // Setters for simple types.
116 void Set(int8_t i8);
117 void Set(uint8_t ui8);
118 void Set(int16_t i16);
119 void Set(uint16_t ui16);
120 void Set(int32_t i32);
121 void Set(uint32_t ui32);
122 void Set(int64_t i64);
123 void Set(uint64_t ui64);
124 void Set(float r32);
125 void Set(double r64);
126 void Set(bool b);
127
128 // Creates a copy of |var| and assigns as this instance's value.
129 // Note that this is different from the Reset() method that's used to
130 // free the current value and assume ownership.
131 void Set(const VARIANT& var);
132
133 // COM object setters
134 void Set(IDispatch* disp);
135 void Set(IUnknown* unk);
136
137 // SAFEARRAY support
138 void Set(SAFEARRAY* array);
139
140 // Special setter for DATE since DATE is a double and we already have
141 // a setter for double.
142 void SetDate(DATE date);
143
144 // Allows const access to the contained variant without DCHECKs etc.
145 // This support is necessary for the V_XYZ (e.g. V_BSTR) set of macros to
146 // work properly but still doesn't allow modifications since we want control
147 // over that.
148 const VARIANT* ptr() const { return &var_; }
149
150 // Moves the ScopedVariant to another instance.
151 ScopedVariant& operator=(ScopedVariant&& var);
152
153 // Like other scoped classes (e.g. scoped_refptr, ScopedBstr,
154 // Microsoft::WRL::ComPtr) we support the assignment operator for the type we
155 // wrap.
156 ScopedVariant& operator=(const VARIANT& var);
157
158 // A hack to pass a pointer to the variant where the accepting
159 // function treats the variant as an input-only, read-only value
160 // but the function prototype requires a non const variant pointer.
161 // There's no DCHECK or anything here. Callers must know what they're doing.
162 VARIANT* AsInput() const {
163 // The nature of this function is const, so we declare
164 // it as such and cast away the constness here.
165 return const_cast<VARIANT*>(&var_);
166 }
167
168 // Allows the ScopedVariant instance to be passed to functions either by value
169 // or by const reference.
170 operator const VARIANT&() const { return var_; }
171
172 // Used as a debug check to see if we're leaking anything.
173 static bool IsLeakableVarType(VARTYPE vt);
174
175 protected:
176 VARIANT var_;
177
178 private:
179 // Comparison operators for ScopedVariant are not supported at this point.
180 // Use the Compare method instead.
181 bool operator==(const ScopedVariant& var) const;
182 bool operator!=(const ScopedVariant& var) const;
184};
185
186} // namespace win
187} // namespace base
188
189#endif // BASE_WIN_SCOPED_VARIANT_H_
bool operator!=(const sk_sp< T > &a, const sk_sp< U > &b)
Definition SkRefCnt.h:355
#define BASE_EXPORT
Definition base_export.h:26
static const VARIANT kEmptyVariant
const VARIANT * ptr() const
VARIANT * AsInput() const
bool operator==(const FlutterPoint &a, const FlutterPoint &b)
static bool b
size_t length
#define BASE_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:8
unsigned int UINT