123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- /*
- * Copyright (C) 2007, 2008 Apple Inc. All Rights Reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
- * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #ifndef COMVariantSetter_h
- #define COMVariantSetter_h
- #include <WebCore/BString.h>
- #include <WebCore/COMPtr.h>
- #include <wtf/Assertions.h>
- #include <wtf/Forward.h>
- template<typename T> struct COMVariantSetter {};
- template<typename T> struct COMVariantSetterBase
- {
- static inline VARENUM variantType(const T&)
- {
- return COMVariantSetter<T>::VariantType;
- }
- };
- template<> struct COMVariantSetter<WTF::String> : COMVariantSetterBase<WTF::String>
- {
- static const VARENUM VariantType = VT_BSTR;
- static void setVariant(VARIANT* variant, const WTF::String& value)
- {
- ASSERT(V_VT(variant) == VT_EMPTY);
- V_VT(variant) = VariantType;
- V_BSTR(variant) = WebCore::BString(value).release();
- }
- };
- template<> struct COMVariantSetter<bool> : COMVariantSetterBase<bool>
- {
- static const VARENUM VariantType = VT_BOOL;
- static void setVariant(VARIANT* variant, bool value)
- {
- ASSERT(V_VT(variant) == VT_EMPTY);
- V_VT(variant) = VariantType;
- V_BOOL(variant) = value;
- }
- };
- template<> struct COMVariantSetter<unsigned long long> : COMVariantSetterBase<unsigned long long>
- {
- static const VARENUM VariantType = VT_UI8;
- static void setVariant(VARIANT* variant, unsigned long long value)
- {
- ASSERT(V_VT(variant) == VT_EMPTY);
- V_VT(variant) = VariantType;
- V_UI8(variant) = value;
- }
- };
- template<> struct COMVariantSetter<int> : COMVariantSetterBase<int>
- {
- static const VARENUM VariantType = VT_I4;
- static void setVariant(VARIANT* variant, int value)
- {
- ASSERT(V_VT(variant) == VT_EMPTY);
- V_VT(variant) = VariantType;
- V_I4(variant) = value;
- }
- };
- template<> struct COMVariantSetter<float> : COMVariantSetterBase<float>
- {
- static const VARENUM VariantType = VT_R4;
- static void setVariant(VARIANT* variant, float value)
- {
- ASSERT(V_VT(variant) == VT_EMPTY);
- V_VT(variant) = VariantType;
- V_R4(variant) = value;
- }
- };
- template<typename T> struct COMVariantSetter<COMPtr<T> > : COMVariantSetterBase<COMPtr<T> >
- {
- static const VARENUM VariantType = VT_UNKNOWN;
- static void setVariant(VARIANT* variant, const COMPtr<T>& value)
- {
- ASSERT(V_VT(variant) == VT_EMPTY);
- V_VT(variant) = VariantType;
- V_UNKNOWN(variant) = value.get();
- value->AddRef();
- }
- };
- template<typename COMType, typename UnderlyingType>
- struct COMIUnknownVariantSetter : COMVariantSetterBase<UnderlyingType>
- {
- static const VARENUM VariantType = VT_UNKNOWN;
- static void setVariant(VARIANT* variant, const UnderlyingType& value)
- {
- ASSERT(V_VT(variant) == VT_EMPTY);
- V_VT(variant) = VariantType;
- V_UNKNOWN(variant) = COMType::createInstance(value);
- }
- };
- class COMVariant {
- public:
- COMVariant()
- {
- ::VariantInit(&m_variant);
- }
- template<typename UnderlyingType>
- COMVariant(UnderlyingType value)
- {
- ::VariantInit(&m_variant);
- COMVariantSetter<UnderlyingType>::setVariant(&m_variant, value);
- }
- ~COMVariant()
- {
- ::VariantClear(&m_variant);
- }
- COMVariant(const COMVariant& other)
- {
- ::VariantInit(&m_variant);
- other.copyTo(&m_variant);
- }
- COMVariant& operator=(const COMVariant& other)
- {
- other.copyTo(&m_variant);
- return *this;
- }
- void copyTo(VARIANT* dest) const
- {
- ::VariantCopy(dest, const_cast<VARIANT*>(&m_variant));
- }
- VARENUM variantType() const { return static_cast<VARENUM>(V_VT(&m_variant)); }
- private:
- VARIANT m_variant;
- };
- template<> struct COMVariantSetter<COMVariant>
- {
- static inline VARENUM variantType(const COMVariant& value)
- {
- return value.variantType();
- }
- static void setVariant(VARIANT* variant, const COMVariant& value)
- {
- ASSERT(V_VT(variant) == VT_EMPTY);
- value.copyTo(variant);
- }
- };
- #endif // COMVariantSetter
|