-
Notifications
You must be signed in to change notification settings - Fork 1
/
automation_collections.hpp
423 lines (359 loc) · 12.1 KB
/
automation_collections.hpp
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/* /////////////////////////////////////////////////////////////////////////
* File: atlstl/automation/automation_collections.hpp
*
* Purpose: Adaptor classes for creating COM collection instances.
*
* Created: 16th April 1999
* Updated: 10th August 2009
*
* Home: http://stlsoft.org/
*
* Copyright (c) 1999-2009, Matthew Wilson and Synesis Software
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - 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.
* - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
* any contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 THE COPYRIGHT OWNER 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.
*
* ////////////////////////////////////////////////////////////////////// */
/** \file atlstl/automation/automation_collections.hpp
* \brief [C++ only; requires ATL library] Definition of the
* atlstl::generic_automation_collection class template, with which
* COM Collections may be readily defined
* (\ref group__library__com_automation "COM Automation" Library).
*/
#ifndef ATLSTL_INCL_ATLSTL_AUTOMATION_HPP_AUTOMATION_COLLECTIONS
#define ATLSTL_INCL_ATLSTL_AUTOMATION_HPP_AUTOMATION_COLLECTIONS
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define ATLSTL_VER_ATLSTL_AUTOMATION_HPP_AUTOMATION_COLLECTIONS_MAJOR 3
# define ATLSTL_VER_ATLSTL_AUTOMATION_HPP_AUTOMATION_COLLECTIONS_MINOR 2
# define ATLSTL_VER_ATLSTL_AUTOMATION_HPP_AUTOMATION_COLLECTIONS_REVISION 2
# define ATLSTL_VER_ATLSTL_AUTOMATION_HPP_AUTOMATION_COLLECTIONS_EDIT 107
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* /////////////////////////////////////////////////////////////////////////
* Compatibility
*/
/*
[Incompatibilies-start]
STLSOFT_COMPILER_IS_DMC:
[Incompatibilies-end]
*/
/* /////////////////////////////////////////////////////////////////////////
* Includes
*/
#ifndef ATLSTL_INCL_ATLSTL_HPP_ATLSTL
# include <atlstl/atlstl.hpp>
#endif /* !ATLSTL_INCL_ATLSTL_HPP_ATLSTL */
#ifndef STLSOFT_INCL_SYS_H_ATLCOM
# define STLSOFT_INCL_SYS_H_ATLCOM
# include <atlcom.h>
#endif /* !STLSOFT_INCL_SYS_H_ATLCOM */
/* /////////////////////////////////////////////////////////////////////////
* Namespace
*/
#ifndef _ATLSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
/* There is no stlsoft namespace, so must define ::atlstl */
namespace atlstl
{
# else
/* Define stlsoft::atlstl_project */
namespace stlsoft
{
namespace atlstl_project
{
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_ATLSTL_NO_NAMESPACE */
/* /////////////////////////////////////////////////////////////////////////
* Classes
*/
// [[synesis:class:collection: atlstl::generic_collection_base<T<D>, T<ThreadModel>, T<I>, int>]]
template< ss_typename_param_k D
, ss_typename_param_k ThreadModel
, ss_typename_param_k I
, int DispidCount
>
class generic_collection_base
: public I
, public CComObjectRootEx<ThreadModel>
{
/// \name Member Types
/// @{
public:
typedef D derived_class_type;
// typedef generic_collection_base<D, pfn, ThreadModel> class_type;
typedef generic_collection_base<D, ThreadModel, I, DispidCount> class_type;
/// @}
/// \name Construction
/// @{
protected:
generic_collection_base()
{}
/// @}
/// \name Interface map
/// @{
protected:
BEGIN_COM_MAP(generic_collection_base)
COM_INTERFACE_ENTRY(IDispatch) // This is a constraint for I to be IDispatch, or a dual interface
END_COM_MAP()
/// @}
/// \name IDispatch members
/// @{
protected:
STDMETHOD(GetTypeInfoCount)(UINT* pctinfo)
{
*pctinfo = 0;
return S_OK;
}
STDMETHOD(GetTypeInfo)(UINT iTInfo, LCID /* lcid */, ITypeInfo** ppTInfo)
{
if(0 != iTInfo)
{
return DISP_E_BADINDEX;
}
else if(NULL == ppTInfo)
{
return E_POINTER;
}
else
{
*ppTInfo = NULL;
return S_FALSE;
}
}
STDMETHOD(GetIDsOfNames)( REFIID /* riid */
, LPOLESTR* rgszNames
, UINT cNames
, LCID /* lcid */
, DISPID* rgDispId)
{
if(1 == cNames)
{
if(0 == ::wcscmp(rgszNames[0], L"Count"))
{
derived_class_type* pThis = static_cast<derived_class_type*>(this);
HRESULT hr = pThis->SupportsCount();
if(S_OK == hr)
{
rgDispId[0] = DispidCount;
return hr;
}
}
}
return DISP_E_UNKNOWNNAME;
}
STDMETHOD(Invoke)( DISPID dispidMember
, REFIID /* riid */
, LCID /* lcid */
, WORD /* wFlags */
, DISPPARAMS* pDispParams
, VARIANT* pVarResult
, EXCEPINFO* /* pExcepInfo */
, UINT* /* puArgErr */)
{
if(DISPID_NEWENUM == dispidMember)
{
derived_class_type* pThis = static_cast<derived_class_type*>(this);
if(0 != pDispParams->cArgs)
{
return DISP_E_BADPARAMCOUNT;
}
else
{
LPUNKNOWN punkEnumerator;
HRESULT hr;
::VariantInit(pVarResult);
// hr = (pThis->*pfn)(&punkEnumerator);
hr = pThis->get__NewEnum(&punkEnumerator);
if(SUCCEEDED(hr))
{
pVarResult->vt = VT_UNKNOWN;
pVarResult->punkVal = punkEnumerator;
}
return hr;
}
}
else if(DispidCount == dispidMember)
{
derived_class_type* pThis = static_cast<derived_class_type*>(this);
HRESULT hr;
::VariantInit(pVarResult);
hr = pThis->get_Count(&pVarResult->lVal);
if(SUCCEEDED(hr))
{
pVarResult->vt = VT_I4;
}
return hr;
}
else
{
return DISP_E_MEMBERNOTFOUND;
}
}
/// @}
};
template< ss_typename_param_k I1
, ss_typename_param_k I2
, ss_typename_param_k I3
>
inline LPUNKNOWN get_clone(I1* instance, HRESULT (STDAPICALLTYPE I3::*pfn)(I2**), HRESULT &hr)
{
I2* clone;
hr = (instance->*pfn)(&clone);
if(FAILED(hr))
{
clone = NULL;
}
return clone;
}
/** Class for defining COM Automation collections
*/
// [[synesis:class:collection: atlstl::generic_automation_collection<T<E>, T<ThreadModel>, T<I>, int>]]
template< ss_typename_param_k E
, ss_typename_param_k ThreadModel = CComObjectThreadModel
, ss_typename_param_k I = IDispatch
, int DispidCount = 20001
> // Enumerator type
class generic_automation_collection
: public generic_collection_base<generic_automation_collection<E, ThreadModel, I, DispidCount>, ThreadModel, I, DispidCount>
{
public:
typedef E enumerator_type;
typedef generic_automation_collection<E, ThreadModel, I, DispidCount> class_type;
public:
generic_automation_collection()
: m_enumerator(NULL)
, m_count(static_cast<as_size_t>(~0))
{}
void SetEnumerator(enumerator_type* enumerator, as_bool_t bAddRef)
{
ATLSTL_ASSERT(NULL != enumerator);
ATLSTL_ASSERT(NULL == m_enumerator);
m_enumerator = enumerator;
if(bAddRef)
{
m_enumerator->AddRef();
}
}
void SetCount(as_size_t count)
{
m_count = count;
}
~generic_automation_collection()
{
m_enumerator->Release();
}
HRESULT SupportsCount() const
{
return (static_cast<as_size_t>(~0) == m_count) ? S_FALSE : S_OK;
}
public:
template< ss_typename_param_k ITER
, ss_typename_param_k ITF
, ss_typename_param_k N
>
HRESULT Init(ITER begin, ITER end, ITF* owner, N flags)
{
ATLSTL_ASSERT(NULL != m_enumerator);
return m_enumerator->Init(begin, end, owner, flags);
}
template< ss_typename_param_k ITER
, ss_typename_param_k ITF
>
HRESULT Init(ITER begin, ITER end, ITF* owner)
{
ATLSTL_ASSERT(NULL != m_enumerator);
return m_enumerator->Init(begin, end, owner);
}
public:
HRESULT get__NewEnum(LPUNKNOWN* punk)
{
ATLSTL_ASSERT(NULL != m_enumerator);
HRESULT hr;
*punk = get_clone(m_enumerator, &enumerator_type::Clone, hr);
#if 0
typedef HRESULT (STDAPICALLTYPE enumerator_type::_CComEnumBase::*pfn_t)(LPUNKNOWN*);
typedef HRESULT (STDAPICALLTYPE enumerator_type::_CComEnumBase::*pfnv_t)(void*);
union
{
pfn_t pfn;
pfnv_t pfnv;
} u;
u.pfnv = (pfnv_t)(&enumerator_type::Clone);
return (m_enumerator->*u.pfn)(punk);
#else /* ? 0 */
return hr;
#endif /* 0 */
}
HRESULT get_Count(long* pVal)
{
ATLSTL_ASSERT(NULL != pVal);
if(static_cast<as_size_t>(~0) == m_count)
{
return E_UNEXPECTED;
}
else
{
*pVal = static_cast<long>(m_count);
return S_OK;
}
}
/// \name Member Variables
/// @{
private:
enumerator_type* m_enumerator;
as_size_t m_count;
/// @}
private:
generic_automation_collection(class_type const&);
class_type& operator =(class_type const&);
};
#if 0
template< ss_typename_param_k C //!< Collection interface
, ss_typename_param_k E //!< Enumerator interface
, ss_typename_param_k T //!< Element type
, ss_typename_param_k XXXXXXXXX
>
class simple_automation_collection
{
};
#endif /* 0 */
////////////////////////////////////////////////////////////////////////////
// Unit-testing
#ifdef STLSOFT_UNITTEST
# include "./unittest/automation_collections_unittest_.h"
#endif /* STLSOFT_UNITTEST */
/* ////////////////////////////////////////////////////////////////////// */
#ifndef _ATLSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
} // namespace atlstl
# else
} // namespace atlstl_project
} // namespace stlsoft
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_ATLSTL_NO_NAMESPACE */
/* ////////////////////////////////////////////////////////////////////// */
#endif /* !ATLSTL_INCL_ATLSTL_AUTOMATION_HPP_AUTOMATION_COLLECTIONS */
/* ///////////////////////////// end of file //////////////////////////// */