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