COMVariantSetter.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (C) 2007, 2008 Apple Inc. All Rights Reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef COMVariantSetter_h
  26. #define COMVariantSetter_h
  27. #include <WebCore/BString.h>
  28. #include <WebCore/COMPtr.h>
  29. #include <wtf/Assertions.h>
  30. #include <wtf/Forward.h>
  31. template<typename T> struct COMVariantSetter {};
  32. template<typename T> struct COMVariantSetterBase
  33. {
  34. static inline VARENUM variantType(const T&)
  35. {
  36. return COMVariantSetter<T>::VariantType;
  37. }
  38. };
  39. template<> struct COMVariantSetter<WTF::String> : COMVariantSetterBase<WTF::String>
  40. {
  41. static const VARENUM VariantType = VT_BSTR;
  42. static void setVariant(VARIANT* variant, const WTF::String& value)
  43. {
  44. ASSERT(V_VT(variant) == VT_EMPTY);
  45. V_VT(variant) = VariantType;
  46. V_BSTR(variant) = WebCore::BString(value).release();
  47. }
  48. };
  49. template<> struct COMVariantSetter<bool> : COMVariantSetterBase<bool>
  50. {
  51. static const VARENUM VariantType = VT_BOOL;
  52. static void setVariant(VARIANT* variant, bool value)
  53. {
  54. ASSERT(V_VT(variant) == VT_EMPTY);
  55. V_VT(variant) = VariantType;
  56. V_BOOL(variant) = value;
  57. }
  58. };
  59. template<> struct COMVariantSetter<unsigned long long> : COMVariantSetterBase<unsigned long long>
  60. {
  61. static const VARENUM VariantType = VT_UI8;
  62. static void setVariant(VARIANT* variant, unsigned long long value)
  63. {
  64. ASSERT(V_VT(variant) == VT_EMPTY);
  65. V_VT(variant) = VariantType;
  66. V_UI8(variant) = value;
  67. }
  68. };
  69. template<> struct COMVariantSetter<int> : COMVariantSetterBase<int>
  70. {
  71. static const VARENUM VariantType = VT_I4;
  72. static void setVariant(VARIANT* variant, int value)
  73. {
  74. ASSERT(V_VT(variant) == VT_EMPTY);
  75. V_VT(variant) = VariantType;
  76. V_I4(variant) = value;
  77. }
  78. };
  79. template<> struct COMVariantSetter<float> : COMVariantSetterBase<float>
  80. {
  81. static const VARENUM VariantType = VT_R4;
  82. static void setVariant(VARIANT* variant, float value)
  83. {
  84. ASSERT(V_VT(variant) == VT_EMPTY);
  85. V_VT(variant) = VariantType;
  86. V_R4(variant) = value;
  87. }
  88. };
  89. template<typename T> struct COMVariantSetter<COMPtr<T> > : COMVariantSetterBase<COMPtr<T> >
  90. {
  91. static const VARENUM VariantType = VT_UNKNOWN;
  92. static void setVariant(VARIANT* variant, const COMPtr<T>& value)
  93. {
  94. ASSERT(V_VT(variant) == VT_EMPTY);
  95. V_VT(variant) = VariantType;
  96. V_UNKNOWN(variant) = value.get();
  97. value->AddRef();
  98. }
  99. };
  100. template<typename COMType, typename UnderlyingType>
  101. struct COMIUnknownVariantSetter : COMVariantSetterBase<UnderlyingType>
  102. {
  103. static const VARENUM VariantType = VT_UNKNOWN;
  104. static void setVariant(VARIANT* variant, const UnderlyingType& value)
  105. {
  106. ASSERT(V_VT(variant) == VT_EMPTY);
  107. V_VT(variant) = VariantType;
  108. V_UNKNOWN(variant) = COMType::createInstance(value);
  109. }
  110. };
  111. class COMVariant {
  112. public:
  113. COMVariant()
  114. {
  115. ::VariantInit(&m_variant);
  116. }
  117. template<typename UnderlyingType>
  118. COMVariant(UnderlyingType value)
  119. {
  120. ::VariantInit(&m_variant);
  121. COMVariantSetter<UnderlyingType>::setVariant(&m_variant, value);
  122. }
  123. ~COMVariant()
  124. {
  125. ::VariantClear(&m_variant);
  126. }
  127. COMVariant(const COMVariant& other)
  128. {
  129. ::VariantInit(&m_variant);
  130. other.copyTo(&m_variant);
  131. }
  132. COMVariant& operator=(const COMVariant& other)
  133. {
  134. other.copyTo(&m_variant);
  135. return *this;
  136. }
  137. void copyTo(VARIANT* dest) const
  138. {
  139. ::VariantCopy(dest, const_cast<VARIANT*>(&m_variant));
  140. }
  141. VARENUM variantType() const { return static_cast<VARENUM>(V_VT(&m_variant)); }
  142. private:
  143. VARIANT m_variant;
  144. };
  145. template<> struct COMVariantSetter<COMVariant>
  146. {
  147. static inline VARENUM variantType(const COMVariant& value)
  148. {
  149. return value.variantType();
  150. }
  151. static void setVariant(VARIANT* variant, const COMVariant& value)
  152. {
  153. ASSERT(V_VT(variant) == VT_EMPTY);
  154. value.copyTo(variant);
  155. }
  156. };
  157. #endif // COMVariantSetter