2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef COMPropertyBag_h
27 #define COMPropertyBag_h
33 #include <wtf/Noncopyable.h>
34 #include <wtf/HashMap.h>
36 #include "COMVariantSetter.h"
38 template<typename ValueType, typename KeyType = typename WTF::String, typename HashType = typename WTF::StringHash>
39 class COMPropertyBag : public IPropertyBag, public IPropertyBag2 {
40 WTF_MAKE_NONCOPYABLE(COMPropertyBag);
42 typedef HashMap<KeyType, ValueType, HashType> HashMapType;
44 static COMPropertyBag* createInstance(const HashMapType&);
45 static COMPropertyBag* adopt(HashMapType&);
48 virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppvObject);
49 virtual ULONG STDMETHODCALLTYPE AddRef();
50 virtual ULONG STDMETHODCALLTYPE Release();
53 virtual HRESULT STDMETHODCALLTYPE Read(LPCOLESTR pszPropName, VARIANT*, IErrorLog*);
54 virtual HRESULT STDMETHODCALLTYPE Write(LPCOLESTR pszPropName, VARIANT*);
57 virtual HRESULT STDMETHODCALLTYPE Read(ULONG cProperties, PROPBAG2*, IErrorLog*, VARIANT* pvarValue, HRESULT* phrError);
58 virtual HRESULT STDMETHODCALLTYPE Write(ULONG cProperties, PROPBAG2*, VARIANT*);
59 virtual HRESULT STDMETHODCALLTYPE CountProperties(ULONG* pcProperties);
60 virtual HRESULT STDMETHODCALLTYPE GetPropertyInfo(ULONG iProperty, ULONG cProperties, PROPBAG2* pPropBag, ULONG* pcProperties);
61 virtual HRESULT STDMETHODCALLTYPE LoadObject(LPCOLESTR pstrName, DWORD dwHint, IUnknown*, IErrorLog*);
69 COMPropertyBag(const HashMapType& hashMap)
78 HashMapType m_hashMap;
81 // COMPropertyBag ------------------------------------------------------------------
82 template<typename ValueType, typename KeyType, typename HashType>
83 COMPropertyBag<ValueType, KeyType, HashType>* COMPropertyBag<typename ValueType, typename KeyType, HashType>::createInstance(const HashMapType& hashMap)
85 COMPropertyBag* instance = new COMPropertyBag(hashMap);
90 template<typename ValueType, typename KeyType, typename HashType>
91 COMPropertyBag<ValueType, KeyType, HashType>* COMPropertyBag<typename ValueType, typename KeyType, HashType>::adopt(HashMapType& hashMap)
93 COMPropertyBag* instance = new COMPropertyBag;
94 instance->m_hashMap.swap(hashMap);
99 // IUnknown ------------------------------------------------------------------------
100 template<typename ValueType, typename KeyType, typename HashType>
101 HRESULT STDMETHODCALLTYPE COMPropertyBag<ValueType, KeyType, HashType>::QueryInterface(REFIID riid, void** ppvObject)
104 if (IsEqualGUID(riid, IID_IUnknown))
105 *ppvObject = static_cast<IPropertyBag*>(this);
106 else if (IsEqualGUID(riid, IID_IPropertyBag))
107 *ppvObject = static_cast<IPropertyBag*>(this);
108 else if (IsEqualGUID(riid, IID_IPropertyBag2))
109 *ppvObject = static_cast<IPropertyBag2*>(this);
111 return E_NOINTERFACE;
117 template<typename ValueType, typename KeyType, typename HashType>
118 ULONG STDMETHODCALLTYPE COMPropertyBag<ValueType, KeyType, HashType>::AddRef()
123 template<typename ValueType, typename KeyType, typename HashType>
124 ULONG STDMETHODCALLTYPE COMPropertyBag<ValueType, KeyType, HashType>::Release()
126 ULONG newRef = --m_refCount;
133 // IPropertyBag --------------------------------------------------------------------
135 template<typename ValueType, typename KeyType, typename HashType>
136 HRESULT STDMETHODCALLTYPE COMPropertyBag<ValueType, KeyType, HashType>::Read(LPCOLESTR pszPropName, VARIANT* pVar, IErrorLog* pErrorLog)
141 HashMapType::const_iterator it = m_hashMap.find(String(pszPropName));
142 HashMapType::const_iterator end = m_hashMap.end();
146 VARTYPE requestedType = V_VT(pVar);
147 V_VT(pVar) = VT_EMPTY;
148 COMVariantSetter<ValueType>::setVariant(pVar, it->second);
150 if (requestedType != COMVariantSetter<ValueType>::variantType(it->second) && requestedType != VT_EMPTY)
151 return ::VariantChangeType(pVar, pVar, VARIANT_NOUSEROVERRIDE | VARIANT_ALPHABOOL, requestedType);
156 template<typename ValueType, typename KeyType, typename HashType>
157 HRESULT STDMETHODCALLTYPE COMPropertyBag<ValueType, KeyType, HashType>::Write(LPCOLESTR pszPropName, VARIANT* pVar)
162 template<typename ValueType, typename KeyType, typename HashType>
163 HRESULT STDMETHODCALLTYPE COMPropertyBag<ValueType, KeyType, HashType>::Read(ULONG cProperties, PROPBAG2* pPropBag, IErrorLog* pErrorLog, VARIANT* pvarValue, HRESULT* phrError)
165 if (!pPropBag || !pvarValue || !phrError)
170 for (ULONG i = 0; i < cProperties; ++i) {
171 VariantInit(&pvarValue[i]);
172 pvarValue[i].vt = pPropBag[i].vt;
173 phrError[i] = Read(pPropBag[i].pstrName, &pvarValue[i], pErrorLog);
174 if (FAILED(phrError[i]))
181 template<typename ValueType, typename KeyType, typename HashType>
182 HRESULT STDMETHODCALLTYPE COMPropertyBag<ValueType, KeyType, HashType>::Write(ULONG cProperties, PROPBAG2*, VARIANT*)
187 template<typename ValueType, typename KeyType, typename HashType>
188 HRESULT STDMETHODCALLTYPE COMPropertyBag<ValueType, KeyType, HashType>::CountProperties(ULONG* pcProperties)
193 *pcProperties = m_hashMap.size();
197 template<typename ValueType, typename KeyType, typename HashType>
198 HRESULT STDMETHODCALLTYPE COMPropertyBag<ValueType, KeyType, HashType>::GetPropertyInfo(ULONG iProperty, ULONG cProperties, PROPBAG2* pPropBag, ULONG* pcProperties)
200 if (!pPropBag || !pcProperties)
203 if (m_hashMap.size() <= iProperty)
207 typedef HashMapType::const_iterator Iterator;
208 Iterator current = m_hashMap.begin();
209 Iterator end = m_hashMap.end();
210 for (ULONG i = 0; i < iProperty; ++i, ++current)
212 for (ULONG j = 0; j < cProperties && current != end; ++j, ++current) {
213 // FIXME: the following fields aren't filled in
214 //pPropBag[j].cfType; // (CLIPFORMAT) Clipboard format or MIME type of the property.
215 //pPropBag[j].clsid; // (CLSID) CLSID of the object. This member is valid only if dwType is PROPBAG2_TYPE_OBJECT.
217 pPropBag[j].dwType = PROPBAG2_TYPE_DATA;
218 pPropBag[j].vt = COMVariantSetter<ValueType>::variantType(current->second);
219 pPropBag[j].dwHint = iProperty + j;
220 pPropBag[j].pstrName = (LPOLESTR)CoTaskMemAlloc(sizeof(wchar_t)*(current->first.length()+1));
221 if (!pPropBag[j].pstrName)
222 return E_OUTOFMEMORY;
223 wcscpy_s(pPropBag[j].pstrName, current->first.length()+1, static_cast<String>(current->first).charactersWithNullTermination());
229 template<typename ValueType, typename KeyType, typename HashType>
230 HRESULT STDMETHODCALLTYPE COMPropertyBag<ValueType, KeyType, HashType>::LoadObject(LPCOLESTR pstrName, DWORD dwHint, IUnknown*, IErrorLog*)
235 #endif // COMPropertyBag_h