| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_SCOPED_VARIANT_WIN_H_ | 5 // TODO(brettw) remove this file when all callers are converted to using the |
| 6 #define BASE_SCOPED_VARIANT_WIN_H_ | 6 // new location/namespace |
| 7 #pragma once | 7 #include "base/win/scoped_variant.h" |
| 8 | 8 |
| 9 #include <windows.h> | 9 using base::win::ScopedVariant; |
| 10 #include <oleauto.h> | |
| 11 | |
| 12 #include "base/basictypes.h" | |
| 13 #include "build/build_config.h" | |
| 14 | |
| 15 // Scoped VARIANT class for automatically freeing a COM VARIANT at the | |
| 16 // end of a scope. Additionally provides a few functions to make the | |
| 17 // encapsulated VARIANT easier to use. | |
| 18 // Instead of inheriting from VARIANT, we take the containment approach | |
| 19 // in order to have more control over the usage of the variant and guard | |
| 20 // against memory leaks. | |
| 21 class ScopedVariant { | |
| 22 public: | |
| 23 // Declaration of a global variant variable that's always VT_EMPTY | |
| 24 static const VARIANT kEmptyVariant; | |
| 25 | |
| 26 // Default constructor. | |
| 27 ScopedVariant() { | |
| 28 // This is equivalent to what VariantInit does, but less code. | |
| 29 var_.vt = VT_EMPTY; | |
| 30 } | |
| 31 | |
| 32 // Constructor to create a new VT_BSTR VARIANT. | |
| 33 // NOTE: Do not pass a BSTR to this constructor expecting ownership to | |
| 34 // be transferred | |
| 35 explicit ScopedVariant(const wchar_t* str); | |
| 36 | |
| 37 // Creates a new VT_BSTR variant of a specified length. | |
| 38 explicit ScopedVariant(const wchar_t* str, UINT length); | |
| 39 | |
| 40 // Creates a new integral type variant and assigns the value to | |
| 41 // VARIANT.lVal (32 bit sized field). | |
| 42 explicit ScopedVariant(int value, VARTYPE vt = VT_I4); | |
| 43 | |
| 44 // Creates a new double-precision type variant. |vt| must be either VT_R8 | |
| 45 // or VT_DATE. | |
| 46 explicit ScopedVariant(double value, VARTYPE vt = VT_R8); | |
| 47 | |
| 48 // VT_DISPATCH | |
| 49 explicit ScopedVariant(IDispatch* dispatch); | |
| 50 | |
| 51 // VT_UNKNOWN | |
| 52 explicit ScopedVariant(IUnknown* unknown); | |
| 53 | |
| 54 // SAFEARRAY | |
| 55 explicit ScopedVariant(SAFEARRAY* safearray); | |
| 56 | |
| 57 // Copies the variant. | |
| 58 explicit ScopedVariant(const VARIANT& var); | |
| 59 | |
| 60 ~ScopedVariant(); | |
| 61 | |
| 62 inline VARTYPE type() const { | |
| 63 return var_.vt; | |
| 64 } | |
| 65 | |
| 66 // Give ScopedVariant ownership over an already allocated VARIANT. | |
| 67 void Reset(const VARIANT& var = kEmptyVariant); | |
| 68 | |
| 69 // Releases ownership of the VARIANT to the caller. | |
| 70 VARIANT Release(); | |
| 71 | |
| 72 // Swap two ScopedVariant's. | |
| 73 void Swap(ScopedVariant& var); | |
| 74 | |
| 75 // Returns a copy of the variant. | |
| 76 VARIANT Copy() const; | |
| 77 | |
| 78 // The return value is 0 if the variants are equal, 1 if this object is | |
| 79 // greater than |var|, -1 if it is smaller. | |
| 80 int Compare(const VARIANT& var, bool ignore_case = false) const; | |
| 81 | |
| 82 // Retrieves the pointer address. | |
| 83 // Used to receive a VARIANT as an out argument (and take ownership). | |
| 84 // The function DCHECKs on the current value being empty/null. | |
| 85 // Usage: GetVariant(var.receive()); | |
| 86 VARIANT* Receive(); | |
| 87 | |
| 88 void Set(const wchar_t* str); | |
| 89 | |
| 90 // Setters for simple types. | |
| 91 void Set(int8 i8); | |
| 92 void Set(uint8 ui8); | |
| 93 void Set(int16 i16); | |
| 94 void Set(uint16 ui16); | |
| 95 void Set(int32 i32); | |
| 96 void Set(uint32 ui32); | |
| 97 void Set(int64 i64); | |
| 98 void Set(uint64 ui64); | |
| 99 void Set(float r32); | |
| 100 void Set(double r64); | |
| 101 void Set(bool b); | |
| 102 | |
| 103 // Creates a copy of |var| and assigns as this instance's value. | |
| 104 // Note that this is different from the Reset() method that's used to | |
| 105 // free the current value and assume ownership. | |
| 106 void Set(const VARIANT& var); | |
| 107 | |
| 108 // COM object setters | |
| 109 void Set(IDispatch* disp); | |
| 110 void Set(IUnknown* unk); | |
| 111 | |
| 112 // SAFEARRAY support | |
| 113 void Set(SAFEARRAY* array); | |
| 114 | |
| 115 // Special setter for DATE since DATE is a double and we already have | |
| 116 // a setter for double. | |
| 117 void SetDate(DATE date); | |
| 118 | |
| 119 // Allows const access to the contained variant without DCHECKs etc. | |
| 120 // This support is necessary for the V_XYZ (e.g. V_BSTR) set of macros to | |
| 121 // work properly but still doesn't allow modifications since we want control | |
| 122 // over that. | |
| 123 const VARIANT* operator&() const { | |
| 124 return &var_; | |
| 125 } | |
| 126 | |
| 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); | |
| 130 | |
| 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_); | |
| 139 } | |
| 140 | |
| 141 // Allows the ScopedVariant instance to be passed to functions either by value | |
| 142 // or by const reference. | |
| 143 operator const VARIANT&() const { | |
| 144 return var_; | |
| 145 } | |
| 146 | |
| 147 // Used as a debug check to see if we're leaking anything. | |
| 148 static bool IsLeakableVarType(VARTYPE vt); | |
| 149 | |
| 150 protected: | |
| 151 VARIANT var_; | |
| 152 | |
| 153 private: | |
| 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); | |
| 159 }; | |
| 160 | |
| 161 #endif // BASE_SCOPED_VARIANT_WIN_H_ | |
| OLD | NEW |