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.
5 #ifndef BASE_WIN_SCOPED_VARIANT_H_
6 #define BASE_WIN_SCOPED_VARIANT_H_
11 #include "base/base_export.h"
12 #include "base/basictypes.h"
17 // Scoped VARIANT class for automatically freeing a COM VARIANT at the
18 // end of a scope. Additionally provides a few functions to make the
19 // encapsulated VARIANT easier to use.
20 // Instead of inheriting from VARIANT, we take the containment approach
21 // in order to have more control over the usage of the variant and guard
22 // against memory leaks.
23 class BASE_EXPORT ScopedVariant
{
25 // Declaration of a global variant variable that's always VT_EMPTY
26 static const VARIANT kEmptyVariant
;
28 // Default constructor.
30 // This is equivalent to what VariantInit does, but less code.
34 // Constructor to create a new VT_BSTR VARIANT.
35 // NOTE: Do not pass a BSTR to this constructor expecting ownership to
37 explicit ScopedVariant(const wchar_t* str
);
39 // Creates a new VT_BSTR variant of a specified length.
40 ScopedVariant(const wchar_t* str
, UINT length
);
42 // Creates a new integral type variant and assigns the value to
43 // VARIANT.lVal (32 bit sized field).
44 explicit ScopedVariant(int value
, VARTYPE vt
= VT_I4
);
46 // Creates a new double-precision type variant. |vt| must be either VT_R8
48 explicit ScopedVariant(double value
, VARTYPE vt
= VT_R8
);
51 explicit ScopedVariant(IDispatch
* dispatch
);
54 explicit ScopedVariant(IUnknown
* unknown
);
57 explicit ScopedVariant(SAFEARRAY
* safearray
);
59 // Copies the variant.
60 explicit ScopedVariant(const VARIANT
& var
);
64 inline VARTYPE
type() const {
68 // Give ScopedVariant ownership over an already allocated VARIANT.
69 void Reset(const VARIANT
& var
= kEmptyVariant
);
71 // Releases ownership of the VARIANT to the caller.
74 // Swap two ScopedVariant's.
75 void Swap(ScopedVariant
& var
);
77 // Returns a copy of the variant.
80 // The return value is 0 if the variants are equal, 1 if this object is
81 // greater than |var|, -1 if it is smaller.
82 int Compare(const VARIANT
& var
, bool ignore_case
= false) const;
84 // Retrieves the pointer address.
85 // Used to receive a VARIANT as an out argument (and take ownership).
86 // The function DCHECKs on the current value being empty/null.
87 // Usage: GetVariant(var.receive());
90 void Set(const wchar_t* str
);
92 // Setters for simple types.
96 void Set(uint16 ui16
);
98 void Set(uint32 ui32
);
100 void Set(uint64 ui64
);
102 void Set(double r64
);
105 // Creates a copy of |var| and assigns as this instance's value.
106 // Note that this is different from the Reset() method that's used to
107 // free the current value and assume ownership.
108 void Set(const VARIANT
& var
);
110 // COM object setters
111 void Set(IDispatch
* disp
);
112 void Set(IUnknown
* unk
);
115 void Set(SAFEARRAY
* array
);
117 // Special setter for DATE since DATE is a double and we already have
118 // a setter for double.
119 void SetDate(DATE date
);
121 // Allows const access to the contained variant without DCHECKs etc.
122 // This support is necessary for the V_XYZ (e.g. V_BSTR) set of macros to
123 // work properly but still doesn't allow modifications since we want control
125 const VARIANT
* ptr() const { return &var_
; }
127 // Like other scoped classes (e.g scoped_refptr, ScopedComPtr, ScopedBstr)
128 // we support the assignment operator for the type we wrap.
129 ScopedVariant
& operator=(const VARIANT
& var
);
131 // A hack to pass a pointer to the variant where the accepting
132 // function treats the variant as an input-only, read-only value
133 // but the function prototype requires a non const variant pointer.
134 // There's no DCHECK or anything here. Callers must know what they're doing.
135 VARIANT
* AsInput() const {
136 // The nature of this function is const, so we declare
137 // it as such and cast away the constness here.
138 return const_cast<VARIANT
*>(&var_
);
141 // Allows the ScopedVariant instance to be passed to functions either by value
142 // or by const reference.
143 operator const VARIANT
&() const {
147 // Used as a debug check to see if we're leaking anything.
148 static bool IsLeakableVarType(VARTYPE vt
);
154 // Comparison operators for ScopedVariant are not supported at this point.
155 // Use the Compare method instead.
156 bool operator==(const ScopedVariant
& var
) const;
157 bool operator!=(const ScopedVariant
& var
) const;
158 DISALLOW_COPY_AND_ASSIGN(ScopedVariant
);
164 #endif // BASE_WIN_SCOPED_VARIANT_H_