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
35 #include <wtf/Noncopyable.h>
36 #include <wtf/HashMap.h>
38 #include "COMVariantSetter.h"
40 template<typename ValueType, typename KeyType = typename WTF::String, typename HashType = typename WTF::StringHash>
41 class COMPropertyBag : public IPropertyBag, public IPropertyBag2 {
42 WTF_MAKE_NONCOPYABLE(COMPropertyBag);
44 typedef HashMap<KeyType, ValueType, HashType> HashMapType;
46 static COMPropertyBag* createInstance(const HashMapType&);
47 static COMPropertyBag* adopt(HashMapType&);
50 virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppvObject);
51 virtual ULONG STDMETHODCALLTYPE AddRef();
52 virtual ULONG STDMETHODCALLTYPE Release();
55 virtual HRESULT STDMETHODCALLTYPE Read(LPCOLESTR pszPropName, VARIANT*, IErrorLog*);
56 virtual HRESULT STDMETHODCALLTYPE Write(LPCOLESTR pszPropName, VARIANT*);
59 virtual HRESULT STDMETHODCALLTYPE Read(ULONG cProperties, PROPBAG2*, IErrorLog*, VARIANT* pvarValue, HRESULT* phrError);
60 virtual HRESULT STDMETHODCALLTYPE Write(ULONG cProperties, PROPBAG2*, VARIANT*);
61 virtual HRESULT STDMETHODCALLTYPE CountProperties(ULONG* pcProperties);
62 virtual HRESULT STDMETHODCALLTYPE GetPropertyInfo(ULONG iProperty, ULONG cProperties, PROPBAG2* pPropBag, ULONG* pcProperties);
63 virtual HRESULT STDMETHODCALLTYPE LoadObject(LPCOLESTR pstrName, DWORD dwHint, IUnknown*, IErrorLog*);
71 COMPropertyBag(const HashMapType& hashMap)
80 HashMapType m_hashMap;
83 // COMPropertyBag ------------------------------------------------------------------
84 template<typename ValueType, typename KeyType, typename HashType>
85 COMPropertyBag<ValueType, KeyType, HashType>* COMPropertyBag<typename ValueType, typename KeyType, HashType>::createInstance(const HashMapType& hashMap)
87 COMPropertyBag* instance = new COMPropertyBag(hashMap);
92 template<typename ValueType, typename KeyType, typename HashType>
93 COMPropertyBag<ValueType, KeyType, HashType>* COMPropertyBag<typename ValueType, typename KeyType, HashType>::adopt(HashMapType& hashMap)
95 COMPropertyBag* instance = new COMPropertyBag;
96 instance->m_hashMap.swap(hashMap);
101 // IUnknown ------------------------------------------------------------------------
102 template<typename ValueType, typename KeyType, typename HashType>
103 HRESULT STDMETHODCALLTYPE COMPropertyBag<ValueType, KeyType, HashType>::QueryInterface(REFIID riid, void** ppvObject)
106 if (IsEqualGUID(riid, IID_IUnknown))
107 *ppvObject = static_cast<IPropertyBag*>(this);
108 else if (IsEqualGUID(riid, IID_IPropertyBag))
109 *ppvObject = static_cast<IPropertyBag*>(this);
110 else if (IsEqualGUID(riid, IID_IPropertyBag2))
111 *ppvObject = static_cast<IPropertyBag2*>(this);
113 return E_NOINTERFACE;
119 template<typename ValueType, typename KeyType, typename HashType>
120 ULONG STDMETHODCALLTYPE COMPropertyBag<ValueType, KeyType, HashType>::AddRef()
125 template<typename ValueType, typename KeyType, typename HashType>
126 ULONG STDMETHODCALLTYPE COMPropertyBag<ValueType, KeyType, HashType>::Release()
128 ULONG newRef = --m_refCount;
135 // IPropertyBag --------------------------------------------------------------------
137 template<typename ValueType, typename KeyType, typename HashType>
138 HRESULT STDMETHODCALLTYPE COMPropertyBag<ValueType, KeyType, HashType>::Read(LPCOLESTR pszPropName, VARIANT* pVar, IErrorLog* pErrorLog)
143 HashMapType::const_iterator it = m_hashMap.find(String(pszPropName));
144 HashMapType::const_iterator end = m_hashMap.end();
148 VARTYPE requestedType = V_VT(pVar);
149 V_VT(pVar) = VT_EMPTY;
150 COMVariantSetter<ValueType>::setVariant(pVar, it->second);
152 if (requestedType != COMVariantSetter<ValueType>::variantType(it->second) && requestedType != VT_EMPTY)
153 return ::VariantChangeType(pVar, pVar, VARIANT_NOUSEROVERRIDE | VARIANT_ALPHABOOL, requestedType);
158 template<typename ValueType, typename KeyType, typename HashType>
159 HRESULT STDMETHODCALLTYPE COMPropertyBag<ValueType, KeyType, HashType>::Write(LPCOLESTR pszPropName, VARIANT* pVar)
164 template<typename ValueType, typename KeyType, typename HashType>
165 HRESULT STDMETHODCALLTYPE COMPropertyBag<ValueType, KeyType, HashType>::Read(ULONG cProperties, PROPBAG2* pPropBag, IErrorLog* pErrorLog, VARIANT* pvarValue, HRESULT* phrError)
167 if (!pPropBag || !pvarValue || !phrError)
172 for (ULONG i = 0; i < cProperties; ++i) {
173 VariantInit(&pvarValue[i]);
174 pvarValue[i].vt = pPropBag[i].vt;
175 phrError[i] = Read(pPropBag[i].pstrName, &pvarValue[i], pErrorLog);
176 if (FAILED(phrError[i]))
183 template<typename ValueType, typename KeyType, typename HashType>
184 HRESULT STDMETHODCALLTYPE COMPropertyBag<ValueType, KeyType, HashType>::Write(ULONG cProperties, PROPBAG2*, VARIANT*)
189 template<typename ValueType, typename KeyType, typename HashType>
190 HRESULT STDMETHODCALLTYPE COMPropertyBag<ValueType, KeyType, HashType>::CountProperties(ULONG* pcProperties)
195 *pcProperties = m_hashMap.size();
199 template<typename ValueType, typename KeyType, typename HashType>
200 HRESULT STDMETHODCALLTYPE COMPropertyBag<ValueType, KeyType, HashType>::GetPropertyInfo(ULONG iProperty, ULONG cProperties, PROPBAG2* pPropBag, ULONG* pcProperties)
202 if (!pPropBag || !pcProperties)
205 if (m_hashMap.size() <= iProperty)
209 typedef HashMapType::const_iterator Iterator;
210 Iterator current = m_hashMap.begin();
211 Iterator end = m_hashMap.end();
212 for (ULONG i = 0; i < iProperty; ++i, ++current)
214 for (ULONG j = 0; j < cProperties && current != end; ++j, ++current) {
215 // FIXME: the following fields aren't filled in
216 //pPropBag[j].cfType; // (CLIPFORMAT) Clipboard format or MIME type of the property.
217 //pPropBag[j].clsid; // (CLSID) CLSID of the object. This member is valid only if dwType is PROPBAG2_TYPE_OBJECT.
219 pPropBag[j].dwType = PROPBAG2_TYPE_DATA;
220 pPropBag[j].vt = COMVariantSetter<ValueType>::variantType(current->second);
221 pPropBag[j].dwHint = iProperty + j;
222 pPropBag[j].pstrName = (LPOLESTR)CoTaskMemAlloc(sizeof(wchar_t)*(current->first.length()+1));
223 if (!pPropBag[j].pstrName)
224 return E_OUTOFMEMORY;
225 wcscpy_s(pPropBag[j].pstrName, current->first.length()+1, static_cast<String>(current->first).charactersWithNullTermination());
231 template<typename ValueType, typename KeyType, typename HashType>
232 HRESULT STDMETHODCALLTYPE COMPropertyBag<ValueType, KeyType, HashType>::LoadObject(LPCOLESTR pstrName, DWORD dwHint, IUnknown*, IErrorLog*)
237 #endif // COMPropertyBag_h