COMPropertyBag.h 8.9 KB

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