scoped_variant.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. #ifndef BASE_WIN_SCOPED_VARIANT_H_
  5. #define BASE_WIN_SCOPED_VARIANT_H_
  6. #include <windows.h>
  7. #include <oleauto.h>
  8. #include <stdint.h>
  9. #include "base/base_export.h"
  10. #include "base/macros.h"
  11. namespace base {
  12. namespace win {
  13. // Scoped VARIANT class for automatically freeing a COM VARIANT at the
  14. // end of a scope. Additionally provides a few functions to make the
  15. // encapsulated VARIANT easier to use.
  16. // Instead of inheriting from VARIANT, we take the containment approach
  17. // in order to have more control over the usage of the variant and guard
  18. // against memory leaks.
  19. class BASE_EXPORT ScopedVariant {
  20. public:
  21. // Declaration of a global variant variable that's always VT_EMPTY
  22. static const VARIANT kEmptyVariant;
  23. // Default constructor.
  24. ScopedVariant() {
  25. // This is equivalent to what VariantInit does, but less code.
  26. var_.vt = VT_EMPTY;
  27. }
  28. // Constructor to create a new VT_BSTR VARIANT.
  29. // NOTE: Do not pass a BSTR to this constructor expecting ownership to
  30. // be transferred
  31. explicit ScopedVariant(const wchar_t* str);
  32. // Creates a new VT_BSTR variant of a specified length.
  33. ScopedVariant(const wchar_t* str, UINT length);
  34. // Creates a new integral type variant and assigns the value to
  35. // VARIANT.lVal (32 bit sized field).
  36. explicit ScopedVariant(long value, VARTYPE vt = VT_I4);
  37. // Creates a new integral type variant for the int type and assigns the value
  38. // to VARIANT.lVal (32 bit sized field).
  39. explicit ScopedVariant(int value);
  40. // Creates a new boolean (VT_BOOL) variant and assigns the value to
  41. // VARIANT.boolVal.
  42. explicit ScopedVariant(bool value);
  43. // Creates a new double-precision type variant. |vt| must be either VT_R8
  44. // or VT_DATE.
  45. explicit ScopedVariant(double value, VARTYPE vt = VT_R8);
  46. // VT_DISPATCH
  47. explicit ScopedVariant(IDispatch* dispatch);
  48. // VT_UNKNOWN
  49. explicit ScopedVariant(IUnknown* unknown);
  50. // SAFEARRAY
  51. explicit ScopedVariant(SAFEARRAY* safearray);
  52. // Copies the variant.
  53. explicit ScopedVariant(const VARIANT& var);
  54. // Moves the wrapped variant into another ScopedVariant.
  55. ScopedVariant(ScopedVariant&& var);
  56. ~ScopedVariant();
  57. inline VARTYPE type() const { return var_.vt; }
  58. // Give ScopedVariant ownership over an already allocated VARIANT.
  59. void Reset(const VARIANT& var = kEmptyVariant);
  60. // Releases ownership of the VARIANT to the caller.
  61. VARIANT Release();
  62. // Swap two ScopedVariant's.
  63. void Swap(ScopedVariant& var);
  64. // Returns a copy of the variant.
  65. VARIANT Copy() const;
  66. // The return value is 0 if the variants are equal, 1 if this object is
  67. // greater than |var|, -1 if it is smaller.
  68. int Compare(const VARIANT& var, bool ignore_case = false) const;
  69. // Retrieves the pointer address.
  70. // Used to receive a VARIANT as an out argument (and take ownership).
  71. // The function DCHECKs on the current value being empty/null.
  72. // Usage: GetVariant(var.receive());
  73. VARIANT* Receive();
  74. void Set(const wchar_t* str);
  75. // Setters for simple types.
  76. void Set(int8_t i8);
  77. void Set(uint8_t ui8);
  78. void Set(int16_t i16);
  79. void Set(uint16_t ui16);
  80. void Set(int32_t i32);
  81. void Set(uint32_t ui32);
  82. void Set(int64_t i64);
  83. void Set(uint64_t ui64);
  84. void Set(float r32);
  85. void Set(double r64);
  86. void Set(bool b);
  87. // Creates a copy of |var| and assigns as this instance's value.
  88. // Note that this is different from the Reset() method that's used to
  89. // free the current value and assume ownership.
  90. void Set(const VARIANT& var);
  91. // COM object setters
  92. void Set(IDispatch* disp);
  93. void Set(IUnknown* unk);
  94. // SAFEARRAY support
  95. void Set(SAFEARRAY* array);
  96. // Special setter for DATE since DATE is a double and we already have
  97. // a setter for double.
  98. void SetDate(DATE date);
  99. // Allows const access to the contained variant without DCHECKs etc.
  100. // This support is necessary for the V_XYZ (e.g. V_BSTR) set of macros to
  101. // work properly but still doesn't allow modifications since we want control
  102. // over that.
  103. const VARIANT* ptr() const { return &var_; }
  104. // Moves the ScopedVariant to another instance.
  105. ScopedVariant& operator=(ScopedVariant&& var);
  106. // Like other scoped classes (e.g. scoped_refptr, ScopedBstr,
  107. // Microsoft::WRL::ComPtr) we support the assignment operator for the type we
  108. // wrap.
  109. ScopedVariant& operator=(const VARIANT& var);
  110. // A hack to pass a pointer to the variant where the accepting
  111. // function treats the variant as an input-only, read-only value
  112. // but the function prototype requires a non const variant pointer.
  113. // There's no DCHECK or anything here. Callers must know what they're doing.
  114. VARIANT* AsInput() const {
  115. // The nature of this function is const, so we declare
  116. // it as such and cast away the constness here.
  117. return const_cast<VARIANT*>(&var_);
  118. }
  119. // Allows the ScopedVariant instance to be passed to functions either by value
  120. // or by const reference.
  121. operator const VARIANT&() const { return var_; }
  122. // Used as a debug check to see if we're leaking anything.
  123. static bool IsLeakableVarType(VARTYPE vt);
  124. protected:
  125. VARIANT var_;
  126. private:
  127. // Comparison operators for ScopedVariant are not supported at this point.
  128. // Use the Compare method instead.
  129. bool operator==(const ScopedVariant& var) const;
  130. bool operator!=(const ScopedVariant& var) const;
  131. DISALLOW_COPY_AND_ASSIGN(ScopedVariant);
  132. };
  133. } // namespace win
  134. } // namespace base
  135. #endif // BASE_WIN_SCOPED_VARIANT_H_