PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/Doduo1.1.1/WebKit/win/COMPropertyBag.h

https://github.com/weissms/owb-mirror
C Header | 220 lines | 149 code | 37 blank | 34 comment | 21 complexity | dbe4740ff2874cb9aa37286a804018a7 MD5 | raw file
  1. /*
  2. * Copyright (C) 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 COMPropertyBag_h
  26. #define COMPropertyBag_h
  27. #define NOMINMAX
  28. #include <unknwn.h>
  29. #include <wtf/Noncopyable.h>
  30. #include <wtf/HashMap.h>
  31. #include "COMVariantSetter.h"
  32. template<typename ValueType, typename HashType = typename WebCore::StringHash>
  33. class COMPropertyBag : public IPropertyBag, public IPropertyBag2, Noncopyable {
  34. public:
  35. typedef HashMap<WebCore::String, ValueType, HashType> HashMapType;
  36. static COMPropertyBag* createInstance(const HashMapType&);
  37. static COMPropertyBag* adopt(HashMapType&);
  38. // IUnknown
  39. virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppvObject);
  40. virtual ULONG STDMETHODCALLTYPE AddRef();
  41. virtual ULONG STDMETHODCALLTYPE Release();
  42. // IPropertyBag
  43. virtual HRESULT STDMETHODCALLTYPE Read(LPCOLESTR pszPropName, VARIANT*, IErrorLog*);
  44. virtual HRESULT STDMETHODCALLTYPE Write(LPCOLESTR pszPropName, VARIANT*);
  45. // IPropertyBag2
  46. virtual HRESULT STDMETHODCALLTYPE Read(ULONG cProperties, PROPBAG2*, IErrorLog*, VARIANT* pvarValue, HRESULT* phrError);
  47. virtual HRESULT STDMETHODCALLTYPE Write(ULONG cProperties, PROPBAG2*, VARIANT*);
  48. virtual HRESULT STDMETHODCALLTYPE CountProperties(ULONG* pcProperties);
  49. virtual HRESULT STDMETHODCALLTYPE GetPropertyInfo(ULONG iProperty, ULONG cProperties, PROPBAG2* pPropBag, ULONG* pcProperties);
  50. virtual HRESULT STDMETHODCALLTYPE LoadObject(LPCOLESTR pstrName, DWORD dwHint, IUnknown*, IErrorLog*);
  51. private:
  52. COMPropertyBag()
  53. : m_refCount(0)
  54. {
  55. }
  56. COMPropertyBag(const HashMapType& hashMap)
  57. : m_refCount(0)
  58. , m_hashMap(hashMap)
  59. {
  60. }
  61. ~COMPropertyBag() {}
  62. ULONG m_refCount;
  63. HashMapType m_hashMap;
  64. };
  65. // COMPropertyBag ------------------------------------------------------------------
  66. template<typename ValueType, typename HashType>
  67. COMPropertyBag<ValueType, HashType>* COMPropertyBag<typename ValueType, HashType>::createInstance(const HashMapType& hashMap)
  68. {
  69. COMPropertyBag* instance = new COMPropertyBag(hashMap);
  70. instance->AddRef();
  71. return instance;
  72. }
  73. template<typename ValueType, typename HashType>
  74. COMPropertyBag<ValueType, HashType>* COMPropertyBag<typename ValueType, HashType>::adopt(HashMapType& hashMap)
  75. {
  76. COMPropertyBag* instance = new COMPropertyBag;
  77. instance->m_hashMap.swap(hashMap);
  78. instance->AddRef();
  79. return instance;
  80. }
  81. // IUnknown ------------------------------------------------------------------------
  82. template<typename ValueType, typename HashType>
  83. HRESULT STDMETHODCALLTYPE COMPropertyBag<ValueType, HashType>::QueryInterface(REFIID riid, void** ppvObject)
  84. {
  85. *ppvObject = 0;
  86. if (IsEqualGUID(riid, IID_IUnknown))
  87. *ppvObject = static_cast<IPropertyBag*>(this);
  88. else if (IsEqualGUID(riid, IID_IPropertyBag))
  89. *ppvObject = static_cast<IPropertyBag*>(this);
  90. else if (IsEqualGUID(riid, IID_IPropertyBag2))
  91. *ppvObject = static_cast<IPropertyBag2*>(this);
  92. else
  93. return E_NOINTERFACE;
  94. AddRef();
  95. return S_OK;
  96. }
  97. template<typename ValueType, typename HashType>
  98. ULONG STDMETHODCALLTYPE COMPropertyBag<ValueType, HashType>::AddRef()
  99. {
  100. return ++m_refCount;
  101. }
  102. template<typename ValueType, typename HashType>
  103. ULONG STDMETHODCALLTYPE COMPropertyBag<ValueType, HashType>::Release()
  104. {
  105. ULONG newRef = --m_refCount;
  106. if (!newRef)
  107. delete this;
  108. return newRef;
  109. }
  110. // IPropertyBag --------------------------------------------------------------------
  111. template<typename ValueType, typename HashType>
  112. HRESULT STDMETHODCALLTYPE COMPropertyBag<ValueType, HashType>::Read(LPCOLESTR pszPropName, VARIANT* pVar, IErrorLog* pErrorLog)
  113. {
  114. if (!pszPropName)
  115. return E_POINTER;
  116. HashMapType::const_iterator it = m_hashMap.find(String(pszPropName));
  117. HashMapType::const_iterator end = m_hashMap.end();
  118. if (it == end)
  119. return E_INVALIDARG;
  120. VARTYPE requestedType = V_VT(pVar);
  121. V_VT(pVar) = VT_EMPTY;
  122. COMVariantSetter<ValueType>::setVariant(pVar, it->second);
  123. if (requestedType != COMVariantSetter<ValueType>::VariantType && requestedType != VT_EMPTY)
  124. return ::VariantChangeType(pVar, pVar, VARIANT_NOUSEROVERRIDE | VARIANT_ALPHABOOL, requestedType);
  125. return S_OK;
  126. }
  127. template<typename ValueType, typename HashType>
  128. HRESULT STDMETHODCALLTYPE COMPropertyBag<ValueType, HashType>::Write(LPCOLESTR pszPropName, VARIANT* pVar)
  129. {
  130. return E_FAIL;
  131. }
  132. template<typename ValueType, typename HashType>
  133. HRESULT STDMETHODCALLTYPE COMPropertyBag<ValueType, HashType>::Read(ULONG cProperties, PROPBAG2*, IErrorLog*, VARIANT* pvarValue, HRESULT* phrError)
  134. {
  135. return E_NOTIMPL;
  136. }
  137. template<typename ValueType, typename HashType>
  138. HRESULT STDMETHODCALLTYPE COMPropertyBag<ValueType, HashType>::Write(ULONG cProperties, PROPBAG2*, VARIANT*)
  139. {
  140. return E_NOTIMPL;
  141. }
  142. template<typename ValueType, typename HashType>
  143. HRESULT STDMETHODCALLTYPE COMPropertyBag<ValueType, HashType>::CountProperties(ULONG* pcProperties)
  144. {
  145. if (!pcProperties)
  146. return E_POINTER;
  147. *pcProperties = m_hashMap.size();
  148. return S_OK;
  149. }
  150. template<typename ValueType, typename HashType>
  151. HRESULT STDMETHODCALLTYPE COMPropertyBag<ValueType, HashType>::GetPropertyInfo(ULONG iProperty, ULONG cProperties, PROPBAG2* pPropBag, ULONG* pcProperties)
  152. {
  153. if (!pPropBag || !pcProperties)
  154. return E_POINTER;
  155. if (m_hashMap.size() <= iProperty)
  156. return E_INVALIDARG;
  157. *pcProperties = 0;
  158. typedef HashMapType::const_iterator Iterator;
  159. Iterator current = m_hashMap.begin();
  160. Iterator end = m_hashMap.end();
  161. for (ULONG i = 0; i < iProperty; ++i, ++current)
  162. ;
  163. for (ULONG j = 0; j < cProperties, current != end; ++j, ++current) {
  164. // FIXME: the following fields aren't filled in
  165. //pPropBag[j].dwType; // (DWORD) Type of property. This will be one of the PROPBAG2_TYPE values.
  166. //pPropBag[j].cfType; // (CLIPFORMAT) Clipboard format or MIME type of the property.
  167. //pPropBag[j].clsid; // (CLSID) CLSID of the object. This member is valid only if dwType is PROPBAG2_TYPE_OBJECT.
  168. pPropBag[j].vt = COMVariantSetter<ValueType>::VariantType;
  169. pPropBag[j].dwHint = iProperty + j;
  170. pPropBag[j].pstrName = (LPOLESTR)CoTaskMemAlloc(sizeof(wchar_t)*(current->first.length()+1));
  171. if (!pPropBag[j].pstrName)
  172. return E_OUTOFMEMORY;
  173. wcscpy_s(pPropBag[j].pstrName, current->first.length()+1, static_cast<String>(current->first).charactersWithNullTermination());
  174. ++*pcProperties;
  175. }
  176. return S_OK;
  177. }
  178. template<typename ValueType, typename HashType>
  179. HRESULT STDMETHODCALLTYPE COMPropertyBag<ValueType, HashType>::LoadObject(LPCOLESTR pstrName, DWORD dwHint, IUnknown*, IErrorLog*)
  180. {
  181. return E_NOTIMPL;
  182. }
  183. #endif // COMPropertyBag_h