1 | /** @file
|
---|
2 | * MS COM / XPCOM Abstraction Layer - Safe array helper class declaration.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2017 Oracle Corporation
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * The contents of this file may alternatively be used under the terms
|
---|
17 | * of the Common Development and Distribution License Version 1.0
|
---|
18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
20 | * CDDL are applicable instead of those of the GPL.
|
---|
21 | *
|
---|
22 | * You may elect to license modified versions of this file under the
|
---|
23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #ifndef ___VBox_com_array_h
|
---|
27 | #define ___VBox_com_array_h
|
---|
28 |
|
---|
29 |
|
---|
30 | /** @defgroup grp_com_arrays COM/XPCOM Arrays
|
---|
31 | * @ingroup grp_com
|
---|
32 | * @{
|
---|
33 | *
|
---|
34 | * The COM/XPCOM array support layer provides a cross-platform way to pass
|
---|
35 | * arrays to and from COM interface methods and consists of the com::SafeArray
|
---|
36 | * template and a set of ComSafeArray* macros part of which is defined in
|
---|
37 | * VBox/com/defs.h.
|
---|
38 | *
|
---|
39 | * This layer works with interface attributes and method parameters that have
|
---|
40 | * the 'safearray="yes"' attribute in the XIDL definition:
|
---|
41 | * @code
|
---|
42 |
|
---|
43 | <interface name="ISomething" ...>
|
---|
44 |
|
---|
45 | <method name="testArrays">
|
---|
46 | <param name="inArr" type="long" dir="in" safearray="yes"/>
|
---|
47 | <param name="outArr" type="long" dir="out" safearray="yes"/>
|
---|
48 | <param name="retArr" type="long" dir="return" safearray="yes"/>
|
---|
49 | </method>
|
---|
50 |
|
---|
51 | </interface>
|
---|
52 |
|
---|
53 | * @endcode
|
---|
54 | *
|
---|
55 | * Methods generated from this and similar definitions are implemented in
|
---|
56 | * component classes using the following declarations:
|
---|
57 | * @code
|
---|
58 |
|
---|
59 | STDMETHOD(TestArrays)(ComSafeArrayIn(LONG, aIn),
|
---|
60 | ComSafeArrayOut(LONG, aOut),
|
---|
61 | ComSafeArrayOut(LONG, aRet));
|
---|
62 |
|
---|
63 | * @endcode
|
---|
64 | *
|
---|
65 | * And the following function bodies:
|
---|
66 | * @code
|
---|
67 |
|
---|
68 | STDMETHODIMP Component::TestArrays(ComSafeArrayIn(LONG, aIn),
|
---|
69 | ComSafeArrayOut(LONG, aOut),
|
---|
70 | ComSafeArrayOut(LONG, aRet))
|
---|
71 | {
|
---|
72 | if (ComSafeArrayInIsNull(aIn))
|
---|
73 | return E_INVALIDARG;
|
---|
74 | if (ComSafeArrayOutIsNull(aOut))
|
---|
75 | return E_POINTER;
|
---|
76 | if (ComSafeArrayOutIsNull(aRet))
|
---|
77 | return E_POINTER;
|
---|
78 |
|
---|
79 | // Use SafeArray to access the input array parameter
|
---|
80 |
|
---|
81 | com::SafeArray<LONG> in(ComSafeArrayInArg(aIn));
|
---|
82 |
|
---|
83 | for (size_t i = 0; i < in.size(); ++ i)
|
---|
84 | LogFlow(("*** in[%u]=%d\n", i, in[i]));
|
---|
85 |
|
---|
86 | // Use SafeArray to create the return array (the same technique is used
|
---|
87 | // for output array parameters)
|
---|
88 |
|
---|
89 | SafeArray<LONG> ret(in.size() * 2);
|
---|
90 | for (size_t i = 0; i < in.size(); ++ i)
|
---|
91 | {
|
---|
92 | ret[i] = in[i];
|
---|
93 | ret[i + in.size()] = in[i] * 10;
|
---|
94 | }
|
---|
95 |
|
---|
96 | ret.detachTo(ComSafeArrayOutArg(aRet));
|
---|
97 |
|
---|
98 | return S_OK;
|
---|
99 | }
|
---|
100 |
|
---|
101 | * @endcode
|
---|
102 | *
|
---|
103 | * Such methods can be called from the client code using the following pattern:
|
---|
104 | * @code
|
---|
105 |
|
---|
106 | ComPtr<ISomething> component;
|
---|
107 |
|
---|
108 | // ...
|
---|
109 |
|
---|
110 | com::SafeArray<LONG> in(3);
|
---|
111 | in[0] = -1;
|
---|
112 | in[1] = -2;
|
---|
113 | in[2] = -3;
|
---|
114 |
|
---|
115 | com::SafeArray<LONG> out;
|
---|
116 | com::SafeArray<LONG> ret;
|
---|
117 |
|
---|
118 | HRESULT rc = component->TestArrays(ComSafeArrayAsInParam(in),
|
---|
119 | ComSafeArrayAsOutParam(out),
|
---|
120 | ComSafeArrayAsOutParam(ret));
|
---|
121 |
|
---|
122 | if (SUCCEEDED(rc))
|
---|
123 | for (size_t i = 0; i < ret.size(); ++ i)
|
---|
124 | printf("*** ret[%u]=%d\n", i, ret[i]);
|
---|
125 |
|
---|
126 | * @endcode
|
---|
127 | *
|
---|
128 | * For interoperability with standard C++ containers, there is a template
|
---|
129 | * constructor that takes such a container as argument and performs a deep copy
|
---|
130 | * of its contents. This can be used in method implementations like this:
|
---|
131 | * @code
|
---|
132 |
|
---|
133 | STDMETHODIMP Component::COMGETTER(Values)(ComSafeArrayOut(int, aValues))
|
---|
134 | {
|
---|
135 | // ... assume there is a |std::list<int> mValues| data member
|
---|
136 |
|
---|
137 | com::SafeArray<int> values(mValues);
|
---|
138 | values.detachTo(ComSafeArrayOutArg(aValues));
|
---|
139 |
|
---|
140 | return S_OK;
|
---|
141 | }
|
---|
142 |
|
---|
143 | * @endcode
|
---|
144 | *
|
---|
145 | * The current implementation of the SafeArray layer supports all types normally
|
---|
146 | * allowed in XIDL as array element types (including 'wstring' and 'uuid').
|
---|
147 | * However, 'pointer-to-...' types (e.g. 'long *', 'wstring *') are not
|
---|
148 | * supported and therefore cannot be used as element types.
|
---|
149 | *
|
---|
150 | * Note that for GUID arrays you should use SafeGUIDArray and
|
---|
151 | * SafeConstGUIDArray, customized SafeArray<> specializations.
|
---|
152 | *
|
---|
153 | * Also note that in order to pass input BSTR array parameters declared
|
---|
154 | * using the ComSafeArrayIn(IN_BSTR, aParam) macro to the SafeArray<>
|
---|
155 | * constructor using the ComSafeArrayInArg() macro, you should use IN_BSTR
|
---|
156 | * as the SafeArray<> template argument, not just BSTR.
|
---|
157 | *
|
---|
158 | * Arrays of interface pointers are also supported but they require to use a
|
---|
159 | * special SafeArray implementation, com::SafeIfacePointer, which takes the
|
---|
160 | * interface class name as a template argument (e.g.
|
---|
161 | * com::SafeIfacePointer\<IUnknown\>). This implementation functions
|
---|
162 | * identically to com::SafeArray.
|
---|
163 | */
|
---|
164 |
|
---|
165 | #ifdef VBOX_WITH_XPCOM
|
---|
166 | # include <nsMemory.h>
|
---|
167 | #endif
|
---|
168 |
|
---|
169 | #include "VBox/com/defs.h"
|
---|
170 |
|
---|
171 | #if RT_GNUC_PREREQ(4, 6) || (defined(_MSC_VER) && (_MSC_VER >= 1600))
|
---|
172 | /** @def VBOX_WITH_TYPE_TRAITS
|
---|
173 | * Type traits are a C++ 11 feature, so not available everywhere (yet).
|
---|
174 | * Only GCC 4.6 or newer and MSVC++ 16.0 (Visual Studio 2010) or newer.
|
---|
175 | */
|
---|
176 | # define VBOX_WITH_TYPE_TRAITS
|
---|
177 | #endif
|
---|
178 |
|
---|
179 | #ifdef VBOX_WITH_TYPE_TRAITS
|
---|
180 | # include <type_traits>
|
---|
181 | #endif
|
---|
182 |
|
---|
183 | #include "VBox/com/ptr.h"
|
---|
184 | #include "VBox/com/assert.h"
|
---|
185 | #include "iprt/cpp/list.h"
|
---|
186 |
|
---|
187 | #ifdef VBOX_WITH_XPCOM
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Wraps the given com::SafeArray instance to generate an expression that is
|
---|
191 | * suitable for passing it to functions that take input safearray parameters
|
---|
192 | * declared using the ComSafeArrayIn macro.
|
---|
193 | *
|
---|
194 | * @param aArray com::SafeArray instance to pass as an input parameter.
|
---|
195 | */
|
---|
196 | #define ComSafeArrayAsInParam(aArray) \
|
---|
197 | (PRUint32)(aArray).size(), (aArray).__asInParam_Arr((aArray).raw())
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * Wraps the given com::SafeArray instance to generate an expression that is
|
---|
201 | * suitable for passing it to functions that take output safearray parameters
|
---|
202 | * declared using the ComSafeArrayOut macro.
|
---|
203 | *
|
---|
204 | * @param aArray com::SafeArray instance to pass as an output parameter.
|
---|
205 | */
|
---|
206 | #define ComSafeArrayAsOutParam(aArray) \
|
---|
207 | (aArray).__asOutParam_Size(), (aArray).__asOutParam_Arr()
|
---|
208 |
|
---|
209 | #else /* !VBOX_WITH_XPCOM */
|
---|
210 |
|
---|
211 | #define ComSafeArrayAsInParam(aArray) (aArray).__asInParam()
|
---|
212 |
|
---|
213 | #define ComSafeArrayAsOutParam(aArray) (aArray).__asOutParam()
|
---|
214 |
|
---|
215 | #endif /* !VBOX_WITH_XPCOM */
|
---|
216 |
|
---|
217 | /**
|
---|
218 | *
|
---|
219 | */
|
---|
220 | namespace com
|
---|
221 | {
|
---|
222 |
|
---|
223 | #ifdef VBOX_WITH_XPCOM
|
---|
224 |
|
---|
225 | ////////////////////////////////////////////////////////////////////////////////
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * Provides various helpers for SafeArray.
|
---|
229 | *
|
---|
230 | * @param T Type of array elements.
|
---|
231 | */
|
---|
232 | template<typename T>
|
---|
233 | struct SafeArrayTraits
|
---|
234 | {
|
---|
235 | protected:
|
---|
236 |
|
---|
237 | /** Initializes memory for aElem. */
|
---|
238 | static void Init(T &aElem) { aElem = 0; }
|
---|
239 |
|
---|
240 | /** Initializes memory occupied by aElem. */
|
---|
241 | static void Uninit(T &aElem) { aElem = 0; }
|
---|
242 |
|
---|
243 | /** Creates a deep copy of aFrom and stores it in aTo. */
|
---|
244 | static void Copy(const T &aFrom, T &aTo) { aTo = aFrom; }
|
---|
245 |
|
---|
246 | public:
|
---|
247 |
|
---|
248 | /* Magic to workaround strict rules of par. 4.4.4 of the C++ standard (that
|
---|
249 | * in particular forbid casts of 'char **' to 'const char **'). Then initial
|
---|
250 | * reason for this magic is that XPIDL declares input strings
|
---|
251 | * (char/PRUnichar pointers) as const but doesn't do so for pointers to
|
---|
252 | * arrays. */
|
---|
253 | static T *__asInParam_Arr(T *aArr) { return aArr; }
|
---|
254 | static T *__asInParam_Arr(const T *aArr) { return const_cast<T *>(aArr); }
|
---|
255 | };
|
---|
256 |
|
---|
257 | template<typename T>
|
---|
258 | struct SafeArrayTraits<T *>
|
---|
259 | {
|
---|
260 | // Arbitrary pointers are not supported
|
---|
261 | };
|
---|
262 |
|
---|
263 | template<>
|
---|
264 | struct SafeArrayTraits<PRUnichar *>
|
---|
265 | {
|
---|
266 | protected:
|
---|
267 |
|
---|
268 | static void Init(PRUnichar * &aElem) { aElem = NULL; }
|
---|
269 |
|
---|
270 | static void Uninit(PRUnichar * &aElem)
|
---|
271 | {
|
---|
272 | if (aElem)
|
---|
273 | {
|
---|
274 | ::SysFreeString(aElem);
|
---|
275 | aElem = NULL;
|
---|
276 | }
|
---|
277 | }
|
---|
278 |
|
---|
279 | static void Copy(const PRUnichar * aFrom, PRUnichar * &aTo)
|
---|
280 | {
|
---|
281 | AssertCompile(sizeof(PRUnichar) == sizeof(OLECHAR));
|
---|
282 | aTo = aFrom ? ::SysAllocString((const OLECHAR *)aFrom) : NULL;
|
---|
283 | }
|
---|
284 |
|
---|
285 | public:
|
---|
286 |
|
---|
287 | /* Magic to workaround strict rules of par. 4.4.4 of the C++ standard */
|
---|
288 | static const PRUnichar **__asInParam_Arr(PRUnichar **aArr)
|
---|
289 | {
|
---|
290 | return const_cast<const PRUnichar **>(aArr);
|
---|
291 | }
|
---|
292 | static const PRUnichar **__asInParam_Arr(const PRUnichar **aArr) { return aArr; }
|
---|
293 | };
|
---|
294 |
|
---|
295 | template<>
|
---|
296 | struct SafeArrayTraits<const PRUnichar *>
|
---|
297 | {
|
---|
298 | protected:
|
---|
299 |
|
---|
300 | static void Init(const PRUnichar * &aElem) { aElem = NULL; }
|
---|
301 | static void Uninit(const PRUnichar * &aElem)
|
---|
302 | {
|
---|
303 | if (aElem)
|
---|
304 | {
|
---|
305 | ::SysFreeString(const_cast<PRUnichar *>(aElem));
|
---|
306 | aElem = NULL;
|
---|
307 | }
|
---|
308 | }
|
---|
309 |
|
---|
310 | static void Copy(const PRUnichar * aFrom, const PRUnichar * &aTo)
|
---|
311 | {
|
---|
312 | AssertCompile(sizeof(PRUnichar) == sizeof(OLECHAR));
|
---|
313 | aTo = aFrom ? ::SysAllocString((const OLECHAR *)aFrom) : NULL;
|
---|
314 | }
|
---|
315 |
|
---|
316 | public:
|
---|
317 |
|
---|
318 | /* Magic to workaround strict rules of par. 4.4.4 of the C++ standard */
|
---|
319 | static const PRUnichar **__asInParam_Arr(const PRUnichar **aArr) { return aArr; }
|
---|
320 | };
|
---|
321 |
|
---|
322 | template<>
|
---|
323 | struct SafeArrayTraits<nsID *>
|
---|
324 | {
|
---|
325 | protected:
|
---|
326 |
|
---|
327 | static void Init(nsID * &aElem) { aElem = NULL; }
|
---|
328 |
|
---|
329 | static void Uninit(nsID * &aElem)
|
---|
330 | {
|
---|
331 | if (aElem)
|
---|
332 | {
|
---|
333 | ::nsMemory::Free(aElem);
|
---|
334 | aElem = NULL;
|
---|
335 | }
|
---|
336 | }
|
---|
337 |
|
---|
338 | static void Copy(const nsID * aFrom, nsID * &aTo)
|
---|
339 | {
|
---|
340 | if (aFrom)
|
---|
341 | {
|
---|
342 | aTo = (nsID *) ::nsMemory::Alloc(sizeof(nsID));
|
---|
343 | if (aTo)
|
---|
344 | *aTo = *aFrom;
|
---|
345 | }
|
---|
346 | else
|
---|
347 | aTo = NULL;
|
---|
348 | }
|
---|
349 |
|
---|
350 | /* This specification is also reused for SafeConstGUIDArray, so provide a
|
---|
351 | * no-op Init() and Uninit() which are necessary for SafeArray<> but should
|
---|
352 | * be never called in context of SafeConstGUIDArray. */
|
---|
353 |
|
---|
354 | static void Init(const nsID * &aElem) { NOREF(aElem); AssertFailed(); }
|
---|
355 | static void Uninit(const nsID * &aElem) { NOREF(aElem); AssertFailed(); }
|
---|
356 |
|
---|
357 | public:
|
---|
358 |
|
---|
359 | /** Magic to workaround strict rules of par. 4.4.4 of the C++ standard. */
|
---|
360 | static const nsID **__asInParam_Arr(nsID **aArr)
|
---|
361 | {
|
---|
362 | return const_cast<const nsID **>(aArr);
|
---|
363 | }
|
---|
364 | static const nsID **__asInParam_Arr(const nsID **aArr) { return aArr; }
|
---|
365 | };
|
---|
366 |
|
---|
367 | #else /* !VBOX_WITH_XPCOM */
|
---|
368 |
|
---|
369 | ////////////////////////////////////////////////////////////////////////////////
|
---|
370 |
|
---|
371 | struct SafeArrayTraitsBase
|
---|
372 | {
|
---|
373 | protected:
|
---|
374 |
|
---|
375 | static SAFEARRAY *CreateSafeArray(VARTYPE aVarType, SAFEARRAYBOUND *aBound)
|
---|
376 | { return SafeArrayCreate(aVarType, 1, aBound); }
|
---|
377 | };
|
---|
378 |
|
---|
379 | /**
|
---|
380 | * Provides various helpers for SafeArray.
|
---|
381 | *
|
---|
382 | * @param T Type of array elements.
|
---|
383 | *
|
---|
384 | * Specializations of this template must provide the following methods:
|
---|
385 | *
|
---|
386 | // Returns the VARTYPE of COM SafeArray elements to be used for T
|
---|
387 | static VARTYPE VarType();
|
---|
388 |
|
---|
389 | // Returns the number of VarType() elements necessary for aSize
|
---|
390 | // elements of T
|
---|
391 | static ULONG VarCount(size_t aSize);
|
---|
392 |
|
---|
393 | // Returns the number of elements of T that fit into the given number of
|
---|
394 | // VarType() elements (opposite to VarCount(size_t aSize)).
|
---|
395 | static size_t Size(ULONG aVarCount);
|
---|
396 |
|
---|
397 | // Creates a deep copy of aFrom and stores it in aTo
|
---|
398 | static void Copy(ULONG aFrom, ULONG &aTo);
|
---|
399 | */
|
---|
400 | template<typename T>
|
---|
401 | struct SafeArrayTraits : public SafeArrayTraitsBase
|
---|
402 | {
|
---|
403 | protected:
|
---|
404 |
|
---|
405 | // Arbitrary types are treated as passed by value and each value is
|
---|
406 | // represented by a number of VT_Ix type elements where VT_Ix has the
|
---|
407 | // biggest possible bitness necessary to represent T w/o a gap. COM enums
|
---|
408 | // fall into this category.
|
---|
409 |
|
---|
410 | static VARTYPE VarType()
|
---|
411 | {
|
---|
412 | #ifdef VBOX_WITH_TYPE_TRAITS
|
---|
413 | if ( std::is_integral<T>::value
|
---|
414 | && !std::is_signed<T>::value)
|
---|
415 | {
|
---|
416 | if (sizeof(T) % 8 == 0) return VT_UI8;
|
---|
417 | if (sizeof(T) % 4 == 0) return VT_UI4;
|
---|
418 | if (sizeof(T) % 2 == 0) return VT_UI2;
|
---|
419 | return VT_UI1;
|
---|
420 | }
|
---|
421 | #endif
|
---|
422 | if (sizeof(T) % 8 == 0) return VT_I8;
|
---|
423 | if (sizeof(T) % 4 == 0) return VT_I4;
|
---|
424 | if (sizeof(T) % 2 == 0) return VT_I2;
|
---|
425 | return VT_I1;
|
---|
426 | }
|
---|
427 |
|
---|
428 | /*
|
---|
429 | * Fallback method in case type traits (VBOX_WITH_TYPE_TRAITS)
|
---|
430 | * are not available. Always returns unsigned types.
|
---|
431 | */
|
---|
432 | static VARTYPE VarTypeUnsigned()
|
---|
433 | {
|
---|
434 | if (sizeof(T) % 8 == 0) return VT_UI8;
|
---|
435 | if (sizeof(T) % 4 == 0) return VT_UI4;
|
---|
436 | if (sizeof(T) % 2 == 0) return VT_UI2;
|
---|
437 | return VT_UI1;
|
---|
438 | }
|
---|
439 |
|
---|
440 | static ULONG VarCount(size_t aSize)
|
---|
441 | {
|
---|
442 | if (sizeof(T) % 8 == 0) return (ULONG)((sizeof(T) / 8) * aSize);
|
---|
443 | if (sizeof(T) % 4 == 0) return (ULONG)((sizeof(T) / 4) * aSize);
|
---|
444 | if (sizeof(T) % 2 == 0) return (ULONG)((sizeof(T) / 2) * aSize);
|
---|
445 | return (ULONG)(sizeof(T) * aSize);
|
---|
446 | }
|
---|
447 |
|
---|
448 | static size_t Size(ULONG aVarCount)
|
---|
449 | {
|
---|
450 | if (sizeof(T) % 8 == 0) return (size_t)(aVarCount * 8) / sizeof(T);
|
---|
451 | if (sizeof(T) % 4 == 0) return (size_t)(aVarCount * 4) / sizeof(T);
|
---|
452 | if (sizeof(T) % 2 == 0) return (size_t)(aVarCount * 2) / sizeof(T);
|
---|
453 | return (size_t) aVarCount / sizeof(T);
|
---|
454 | }
|
---|
455 |
|
---|
456 | static void Copy(T aFrom, T &aTo) { aTo = aFrom; }
|
---|
457 | };
|
---|
458 |
|
---|
459 | template<typename T>
|
---|
460 | struct SafeArrayTraits<T *>
|
---|
461 | {
|
---|
462 | // Arbitrary pointer types are not supported
|
---|
463 | };
|
---|
464 |
|
---|
465 | /* Although the generic SafeArrayTraits template would work for all integers,
|
---|
466 | * we specialize it for some of them in order to use the correct VT_ type */
|
---|
467 |
|
---|
468 | template<>
|
---|
469 | struct SafeArrayTraits<LONG> : public SafeArrayTraitsBase
|
---|
470 | {
|
---|
471 | protected:
|
---|
472 |
|
---|
473 | static VARTYPE VarType() { return VT_I4; }
|
---|
474 | static ULONG VarCount(size_t aSize) { return (ULONG)aSize; }
|
---|
475 | static size_t Size(ULONG aVarCount) { return (size_t)aVarCount; }
|
---|
476 |
|
---|
477 | static void Copy(LONG aFrom, LONG &aTo) { aTo = aFrom; }
|
---|
478 | };
|
---|
479 |
|
---|
480 | template<>
|
---|
481 | struct SafeArrayTraits<ULONG> : public SafeArrayTraitsBase
|
---|
482 | {
|
---|
483 | protected:
|
---|
484 |
|
---|
485 | static VARTYPE VarType() { return VT_UI4; }
|
---|
486 | static ULONG VarCount(size_t aSize) { return (ULONG)aSize; }
|
---|
487 | static size_t Size(ULONG aVarCount) { return (size_t)aVarCount; }
|
---|
488 |
|
---|
489 | static void Copy(ULONG aFrom, ULONG &aTo) { aTo = aFrom; }
|
---|
490 | };
|
---|
491 |
|
---|
492 | template<>
|
---|
493 | struct SafeArrayTraits<LONG64> : public SafeArrayTraitsBase
|
---|
494 | {
|
---|
495 | protected:
|
---|
496 |
|
---|
497 | static VARTYPE VarType() { return VT_I8; }
|
---|
498 | static ULONG VarCount(size_t aSize) { return (ULONG)aSize; }
|
---|
499 | static size_t Size(ULONG aVarCount) { return (size_t)aVarCount; }
|
---|
500 |
|
---|
501 | static void Copy(LONG64 aFrom, LONG64 &aTo) { aTo = aFrom; }
|
---|
502 | };
|
---|
503 |
|
---|
504 | template<>
|
---|
505 | struct SafeArrayTraits<ULONG64> : public SafeArrayTraitsBase
|
---|
506 | {
|
---|
507 | protected:
|
---|
508 |
|
---|
509 | static VARTYPE VarType() { return VT_UI8; }
|
---|
510 | static ULONG VarCount(size_t aSize) { return (ULONG)aSize; }
|
---|
511 | static size_t Size(ULONG aVarCount) { return (size_t)aVarCount; }
|
---|
512 |
|
---|
513 | static void Copy(ULONG64 aFrom, ULONG64 &aTo) { aTo = aFrom; }
|
---|
514 | };
|
---|
515 |
|
---|
516 | template<>
|
---|
517 | struct SafeArrayTraits<BSTR> : public SafeArrayTraitsBase
|
---|
518 | {
|
---|
519 | protected:
|
---|
520 |
|
---|
521 | static VARTYPE VarType() { return VT_BSTR; }
|
---|
522 | static ULONG VarCount(size_t aSize) { return (ULONG)aSize; }
|
---|
523 | static size_t Size(ULONG aVarCount) { return (size_t)aVarCount; }
|
---|
524 |
|
---|
525 | static void Copy(BSTR aFrom, BSTR &aTo)
|
---|
526 | {
|
---|
527 | aTo = aFrom ? ::SysAllocString((const OLECHAR *)aFrom) : NULL;
|
---|
528 | }
|
---|
529 | };
|
---|
530 |
|
---|
531 | template<>
|
---|
532 | struct SafeArrayTraits<GUID> : public SafeArrayTraitsBase
|
---|
533 | {
|
---|
534 | protected:
|
---|
535 |
|
---|
536 | /* Use the 64-bit unsigned integer type for GUID */
|
---|
537 | static VARTYPE VarType() { return VT_UI8; }
|
---|
538 |
|
---|
539 | /* GUID is 128 bit, so we need two VT_UI8 */
|
---|
540 | static ULONG VarCount(size_t aSize)
|
---|
541 | {
|
---|
542 | AssertCompileSize(GUID, 16);
|
---|
543 | return (ULONG)(aSize * 2);
|
---|
544 | }
|
---|
545 |
|
---|
546 | static size_t Size(ULONG aVarCount) { return (size_t)aVarCount / 2; }
|
---|
547 |
|
---|
548 | static void Copy(GUID aFrom, GUID &aTo) { aTo = aFrom; }
|
---|
549 | };
|
---|
550 |
|
---|
551 | /**
|
---|
552 | * Helper for SafeArray::__asOutParam() that automatically updates m.raw after a
|
---|
553 | * non-NULL m.arr assignment.
|
---|
554 | */
|
---|
555 | class OutSafeArrayDipper
|
---|
556 | {
|
---|
557 | OutSafeArrayDipper(SAFEARRAY **aArr, void **aRaw)
|
---|
558 | : arr(aArr), raw(aRaw) { Assert(*aArr == NULL && *aRaw == NULL); }
|
---|
559 |
|
---|
560 | SAFEARRAY **arr;
|
---|
561 | void **raw;
|
---|
562 |
|
---|
563 | template<class, class> friend class SafeArray;
|
---|
564 |
|
---|
565 | public:
|
---|
566 |
|
---|
567 | ~OutSafeArrayDipper()
|
---|
568 | {
|
---|
569 | if (*arr != NULL)
|
---|
570 | {
|
---|
571 | HRESULT rc = SafeArrayAccessData(*arr, raw);
|
---|
572 | AssertComRC(rc);
|
---|
573 | }
|
---|
574 | }
|
---|
575 |
|
---|
576 | operator SAFEARRAY **() { return arr; }
|
---|
577 | };
|
---|
578 |
|
---|
579 | #endif /* !VBOX_WITH_XPCOM */
|
---|
580 |
|
---|
581 | ////////////////////////////////////////////////////////////////////////////////
|
---|
582 |
|
---|
583 | /**
|
---|
584 | * The SafeArray class represents the safe array type used in COM to pass arrays
|
---|
585 | * to/from interface methods.
|
---|
586 | *
|
---|
587 | * This helper class hides all MSCOM/XPCOM specific implementation details and,
|
---|
588 | * together with ComSafeArrayIn, ComSafeArrayOut and ComSafeArrayRet macros,
|
---|
589 | * provides a platform-neutral way to handle safe arrays in the method
|
---|
590 | * implementation.
|
---|
591 | *
|
---|
592 | * When an instance of this class is destroyed, it automatically frees all
|
---|
593 | * resources occupied by individual elements of the array as well as by the
|
---|
594 | * array itself. However, when the value of an element is manually changed
|
---|
595 | * using #operator[] or by accessing array data through the #raw() pointer, it is
|
---|
596 | * the caller's responsibility to free resources occupied by the previous
|
---|
597 | * element's value.
|
---|
598 | *
|
---|
599 | * Also, objects of this class do not support copy and assignment operations and
|
---|
600 | * therefore cannot be returned from functions by value. In other words, this
|
---|
601 | * class is just a temporary storage for handling interface method calls and not
|
---|
602 | * intended to be used to store arrays as data members and such -- you should
|
---|
603 | * use normal list/vector classes for that.
|
---|
604 | *
|
---|
605 | * @note The current implementation supports only one-dimensional arrays.
|
---|
606 | *
|
---|
607 | * @note This class is not thread-safe.
|
---|
608 | */
|
---|
609 | template<typename T, class Traits = SafeArrayTraits<T> >
|
---|
610 | class SafeArray : public Traits
|
---|
611 | {
|
---|
612 | public:
|
---|
613 |
|
---|
614 | /**
|
---|
615 | * Creates a null array.
|
---|
616 | */
|
---|
617 | SafeArray() {}
|
---|
618 |
|
---|
619 | /**
|
---|
620 | * Creates a new array of the given size. All elements of the newly created
|
---|
621 | * array initialized with null values.
|
---|
622 | *
|
---|
623 | * @param aSize Initial number of elements in the array.
|
---|
624 | *
|
---|
625 | * @note If this object remains null after construction it means that there
|
---|
626 | * was not enough memory for creating an array of the requested size.
|
---|
627 | * The constructor will also assert in this case.
|
---|
628 | */
|
---|
629 | SafeArray(size_t aSize) { resize(aSize); }
|
---|
630 |
|
---|
631 | /**
|
---|
632 | * Weakly attaches this instance to the existing array passed in a method
|
---|
633 | * parameter declared using the ComSafeArrayIn macro. When using this call,
|
---|
634 | * always wrap the parameter name in the ComSafeArrayInArg macro call like
|
---|
635 | * this:
|
---|
636 | * <pre>
|
---|
637 | * SafeArray safeArray(ComSafeArrayInArg(aArg));
|
---|
638 | * </pre>
|
---|
639 | *
|
---|
640 | * Note that this constructor doesn't take the ownership of the array. In
|
---|
641 | * particular, it means that operations that operate on the ownership (e.g.
|
---|
642 | * #detachTo()) are forbidden and will assert.
|
---|
643 | *
|
---|
644 | * @param aArg Input method parameter to attach to.
|
---|
645 | */
|
---|
646 | SafeArray(ComSafeArrayIn(T, aArg))
|
---|
647 | {
|
---|
648 | if (aArg)
|
---|
649 | {
|
---|
650 | #ifdef VBOX_WITH_XPCOM
|
---|
651 |
|
---|
652 | m.size = aArgSize;
|
---|
653 | m.arr = aArg;
|
---|
654 | m.isWeak = true;
|
---|
655 |
|
---|
656 | #else /* !VBOX_WITH_XPCOM */
|
---|
657 |
|
---|
658 | SAFEARRAY *arg = aArg;
|
---|
659 |
|
---|
660 | AssertReturnVoid(arg->cDims == 1);
|
---|
661 |
|
---|
662 | VARTYPE vt;
|
---|
663 | HRESULT rc = SafeArrayGetVartype(arg, &vt);
|
---|
664 | AssertComRCReturnVoid(rc);
|
---|
665 | # ifndef VBOX_WITH_TYPE_TRAITS
|
---|
666 | AssertMsgReturnVoid(
|
---|
667 | vt == VarType()
|
---|
668 | || vt == VarTypeUnsigned(),
|
---|
669 | ("Expected vartype %d or %d, got %d.\n",
|
---|
670 | VarType(), VarTypeUnsigned(), vt));
|
---|
671 | # else /* !VBOX_WITH_TYPE_TRAITS */
|
---|
672 | AssertMsgReturnVoid(
|
---|
673 | vt == VarType(),
|
---|
674 | ("Expected vartype %d, got %d.\n",
|
---|
675 | VarType(), vt));
|
---|
676 | # endif
|
---|
677 | rc = SafeArrayAccessData(arg, (void HUGEP **)&m.raw);
|
---|
678 | AssertComRCReturnVoid(rc);
|
---|
679 |
|
---|
680 | m.arr = arg;
|
---|
681 | m.isWeak = true;
|
---|
682 |
|
---|
683 | #endif /* !VBOX_WITH_XPCOM */
|
---|
684 | }
|
---|
685 | }
|
---|
686 |
|
---|
687 | /**
|
---|
688 | * Creates a deep copy of the given standard C++ container that stores
|
---|
689 | * T objects.
|
---|
690 | *
|
---|
691 | * @param aCntr Container object to copy.
|
---|
692 | *
|
---|
693 | * @tparam C Standard C++ container template class (normally deduced from
|
---|
694 | * @c aCntr).
|
---|
695 | */
|
---|
696 | template<template<typename, typename> class C, class A>
|
---|
697 | SafeArray(const C<T, A> & aCntr)
|
---|
698 | {
|
---|
699 | resize(aCntr.size());
|
---|
700 | AssertReturnVoid(!isNull());
|
---|
701 |
|
---|
702 | size_t i = 0;
|
---|
703 | for (typename C<T, A>::const_iterator it = aCntr.begin();
|
---|
704 | it != aCntr.end(); ++ it, ++ i)
|
---|
705 | #ifdef VBOX_WITH_XPCOM
|
---|
706 | SafeArray::Copy(*it, m.arr[i]);
|
---|
707 | #else
|
---|
708 | Copy(*it, m.raw[i]);
|
---|
709 | #endif
|
---|
710 | }
|
---|
711 |
|
---|
712 | /**
|
---|
713 | * Creates a deep copy of the given standard C++ map that stores T objects
|
---|
714 | * as values.
|
---|
715 | *
|
---|
716 | * @param aMap Map object to copy.
|
---|
717 | *
|
---|
718 | * @tparam C Standard C++ map template class (normally deduced from
|
---|
719 | * @a aMap).
|
---|
720 | * @tparam L Standard C++ compare class (deduced from @a aMap).
|
---|
721 | * @tparam A Standard C++ allocator class (deduced from @a aMap).
|
---|
722 | * @tparam K Map key class (deduced from @a aMap).
|
---|
723 | */
|
---|
724 | template<template<typename, typename, typename, typename>
|
---|
725 | class C, class L, class A, class K>
|
---|
726 | SafeArray(const C<K, T, L, A> & aMap)
|
---|
727 | {
|
---|
728 | typedef C<K, T, L, A> Map;
|
---|
729 |
|
---|
730 | resize(aMap.size());
|
---|
731 | AssertReturnVoid(!isNull());
|
---|
732 |
|
---|
733 | int i = 0;
|
---|
734 | for (typename Map::const_iterator it = aMap.begin();
|
---|
735 | it != aMap.end(); ++ it, ++ i)
|
---|
736 | #ifdef VBOX_WITH_XPCOM
|
---|
737 | Copy(it->second, m.arr[i]);
|
---|
738 | #else
|
---|
739 | Copy(it->second, m.raw[i]);
|
---|
740 | #endif
|
---|
741 | }
|
---|
742 |
|
---|
743 | /**
|
---|
744 | * Destroys this instance after calling #setNull() to release allocated
|
---|
745 | * resources. See #setNull() for more details.
|
---|
746 | */
|
---|
747 | virtual ~SafeArray() { setNull(); }
|
---|
748 |
|
---|
749 | /**
|
---|
750 | * Returns @c true if this instance represents a null array.
|
---|
751 | */
|
---|
752 | bool isNull() const { return m.arr == NULL; }
|
---|
753 |
|
---|
754 | /**
|
---|
755 | * Returns @c true if this instance does not represents a null array.
|
---|
756 | */
|
---|
757 | bool isNotNull() const { return m.arr != NULL; }
|
---|
758 |
|
---|
759 | /**
|
---|
760 | * Resets this instance to null and, if this instance is not a weak one,
|
---|
761 | * releases any resources occupied by the array data.
|
---|
762 | *
|
---|
763 | * @note This method destroys (cleans up) all elements of the array using
|
---|
764 | * the corresponding cleanup routine for the element type before the
|
---|
765 | * array itself is destroyed.
|
---|
766 | */
|
---|
767 | virtual void setNull() { m.uninit(); }
|
---|
768 |
|
---|
769 | /**
|
---|
770 | * Returns @c true if this instance is weak. A weak instance doesn't own the
|
---|
771 | * array data and therefore operations manipulating the ownership (e.g.
|
---|
772 | * #detachTo()) are forbidden and will assert.
|
---|
773 | */
|
---|
774 | bool isWeak() const { return m.isWeak; }
|
---|
775 |
|
---|
776 | /** Number of elements in the array. */
|
---|
777 | size_t size() const
|
---|
778 | {
|
---|
779 | #ifdef VBOX_WITH_XPCOM
|
---|
780 | if (m.arr)
|
---|
781 | return m.size;
|
---|
782 | return 0;
|
---|
783 | #else
|
---|
784 | if (m.arr)
|
---|
785 | return Size(m.arr->rgsabound[0].cElements);
|
---|
786 | return 0;
|
---|
787 | #endif
|
---|
788 | }
|
---|
789 |
|
---|
790 | /**
|
---|
791 | * Appends a copy of the given element at the end of the array.
|
---|
792 | *
|
---|
793 | * The array size is increased by one by this method and the additional
|
---|
794 | * space is allocated as needed.
|
---|
795 | *
|
---|
796 | * This method is handy in cases where you want to assign a copy of the
|
---|
797 | * existing value to the array element, for example:
|
---|
798 | * <tt>Bstr string; array.push_back(string);</tt>. If you create a string
|
---|
799 | * just to put it in the array, you may find #appendedRaw() more useful.
|
---|
800 | *
|
---|
801 | * @param aElement Element to append.
|
---|
802 | *
|
---|
803 | * @return @c true on success and @c false if there is not enough
|
---|
804 | * memory for resizing.
|
---|
805 | */
|
---|
806 | bool push_back(const T &aElement)
|
---|
807 | {
|
---|
808 | if (!ensureCapacity(size() + 1))
|
---|
809 | return false;
|
---|
810 |
|
---|
811 | #ifdef VBOX_WITH_XPCOM
|
---|
812 | SafeArray::Copy(aElement, m.arr[m.size]);
|
---|
813 | ++ m.size;
|
---|
814 | #else
|
---|
815 | Copy(aElement, m.raw[size() - 1]);
|
---|
816 | #endif
|
---|
817 | return true;
|
---|
818 | }
|
---|
819 |
|
---|
820 | /**
|
---|
821 | * Appends an empty element at the end of the array and returns a raw
|
---|
822 | * pointer to it suitable for assigning a raw value (w/o constructing a
|
---|
823 | * copy).
|
---|
824 | *
|
---|
825 | * The array size is increased by one by this method and the additional
|
---|
826 | * space is allocated as needed.
|
---|
827 | *
|
---|
828 | * Note that in case of raw assignment, value ownership (for types with
|
---|
829 | * dynamically allocated data and for interface pointers) is transferred to
|
---|
830 | * the safe array object.
|
---|
831 | *
|
---|
832 | * This method is handy for operations like
|
---|
833 | * <tt>Bstr("foo").detachTo(array.appendedRaw());</tt>. Don't use it as
|
---|
834 | * an l-value (<tt>array.appendedRaw() = SysAllocString(L"tralala");</tt>)
|
---|
835 | * since this doesn't check for a NULL condition; use #resize() instead. If
|
---|
836 | * you need to assign a copy of the existing value instead of transferring
|
---|
837 | * the ownership, look at #push_back().
|
---|
838 | *
|
---|
839 | * @return Raw pointer to the added element or NULL if no memory.
|
---|
840 | */
|
---|
841 | T *appendedRaw()
|
---|
842 | {
|
---|
843 | if (!ensureCapacity(size() + 1))
|
---|
844 | return NULL;
|
---|
845 |
|
---|
846 | #ifdef VBOX_WITH_XPCOM
|
---|
847 | SafeArray::Init(m.arr[m.size]);
|
---|
848 | ++ m.size;
|
---|
849 | return &m.arr[m.size - 1];
|
---|
850 | #else
|
---|
851 | /* nothing to do here, SafeArrayCreate() has performed element
|
---|
852 | * initialization */
|
---|
853 | return &m.raw[size() - 1];
|
---|
854 | #endif
|
---|
855 | }
|
---|
856 |
|
---|
857 | /**
|
---|
858 | * Resizes the array preserving its contents when possible. If the new size
|
---|
859 | * is larger than the old size, new elements are initialized with null
|
---|
860 | * values. If the new size is less than the old size, the contents of the
|
---|
861 | * array beyond the new size is lost.
|
---|
862 | *
|
---|
863 | * @param aNewSize New number of elements in the array.
|
---|
864 | * @return @c true on success and @c false if there is not enough
|
---|
865 | * memory for resizing.
|
---|
866 | */
|
---|
867 | bool resize(size_t aNewSize)
|
---|
868 | {
|
---|
869 | if (!ensureCapacity(aNewSize))
|
---|
870 | return false;
|
---|
871 |
|
---|
872 | #ifdef VBOX_WITH_XPCOM
|
---|
873 |
|
---|
874 | if (m.size < aNewSize)
|
---|
875 | {
|
---|
876 | /* initialize the new elements */
|
---|
877 | for (size_t i = m.size; i < aNewSize; ++ i)
|
---|
878 | SafeArray::Init(m.arr[i]);
|
---|
879 | }
|
---|
880 |
|
---|
881 | /** @todo Fix this! */
|
---|
882 | m.size = (PRUint32)aNewSize;
|
---|
883 | #else
|
---|
884 | /* nothing to do here, SafeArrayCreate() has performed element
|
---|
885 | * initialization */
|
---|
886 | #endif
|
---|
887 | return true;
|
---|
888 | }
|
---|
889 |
|
---|
890 | /**
|
---|
891 | * Reinitializes this instance by preallocating space for the given number
|
---|
892 | * of elements. The previous array contents is lost.
|
---|
893 | *
|
---|
894 | * @param aNewSize New number of elements in the array.
|
---|
895 | * @return @c true on success and @c false if there is not enough
|
---|
896 | * memory for resizing.
|
---|
897 | */
|
---|
898 | bool reset(size_t aNewSize)
|
---|
899 | {
|
---|
900 | m.uninit();
|
---|
901 | return resize(aNewSize);
|
---|
902 | }
|
---|
903 |
|
---|
904 | /**
|
---|
905 | * Returns a pointer to the raw array data. Use this raw pointer with care
|
---|
906 | * as no type or bound checking is done for you in this case.
|
---|
907 | *
|
---|
908 | * @note This method returns @c NULL when this instance is null.
|
---|
909 | * @see #operator[]
|
---|
910 | */
|
---|
911 | T *raw()
|
---|
912 | {
|
---|
913 | #ifdef VBOX_WITH_XPCOM
|
---|
914 | return m.arr;
|
---|
915 | #else
|
---|
916 | return m.raw;
|
---|
917 | #endif
|
---|
918 | }
|
---|
919 |
|
---|
920 | /**
|
---|
921 | * Const version of #raw().
|
---|
922 | */
|
---|
923 | const T *raw() const
|
---|
924 | {
|
---|
925 | #ifdef VBOX_WITH_XPCOM
|
---|
926 | return m.arr;
|
---|
927 | #else
|
---|
928 | return m.raw;
|
---|
929 | #endif
|
---|
930 | }
|
---|
931 |
|
---|
932 | /**
|
---|
933 | * Array access operator that returns an array element by reference. A bit
|
---|
934 | * safer than #raw(): asserts and returns an invalid reference if this
|
---|
935 | * instance is null or if the index is out of bounds.
|
---|
936 | *
|
---|
937 | * @note For weak instances, this call will succeed but the behavior of
|
---|
938 | * changing the contents of an element of the weak array instance is
|
---|
939 | * undefined and may lead to a program crash on some platforms.
|
---|
940 | */
|
---|
941 | T &operator[] (size_t aIdx)
|
---|
942 | {
|
---|
943 | AssertReturn(m.arr != NULL, *((T *)NULL));
|
---|
944 | AssertReturn(aIdx < size(), *((T *)NULL));
|
---|
945 | #ifdef VBOX_WITH_XPCOM
|
---|
946 | return m.arr[aIdx];
|
---|
947 | #else
|
---|
948 | AssertReturn(m.raw != NULL, *((T *)NULL));
|
---|
949 | return m.raw[aIdx];
|
---|
950 | #endif
|
---|
951 | }
|
---|
952 |
|
---|
953 | /**
|
---|
954 | * Const version of #operator[] that returns an array element by value.
|
---|
955 | */
|
---|
956 | const T operator[] (size_t aIdx) const
|
---|
957 | {
|
---|
958 | AssertReturn(m.arr != NULL, *((T *)1));
|
---|
959 | AssertReturn(aIdx < size(), *((T *)1));
|
---|
960 | #ifdef VBOX_WITH_XPCOM
|
---|
961 | return m.arr[aIdx];
|
---|
962 | #else
|
---|
963 | AssertReturn(m.raw != NULL, *((T *)NULL));
|
---|
964 | return m.raw[aIdx];
|
---|
965 | #endif
|
---|
966 | }
|
---|
967 |
|
---|
968 | /**
|
---|
969 | * Creates a copy of this array and stores it in a method parameter declared
|
---|
970 | * using the ComSafeArrayOut macro. When using this call, always wrap the
|
---|
971 | * parameter name in the ComSafeArrayOutArg macro call like this:
|
---|
972 | * <pre>
|
---|
973 | * safeArray.cloneTo(ComSafeArrayOutArg(aArg));
|
---|
974 | * </pre>
|
---|
975 | *
|
---|
976 | * @note It is assumed that the ownership of the returned copy is
|
---|
977 | * transferred to the caller of the method and he is responsible to free the
|
---|
978 | * array data when it is no longer needed.
|
---|
979 | *
|
---|
980 | * @param aArg Output method parameter to clone to.
|
---|
981 | */
|
---|
982 | virtual const SafeArray &cloneTo(ComSafeArrayOut(T, aArg)) const
|
---|
983 | {
|
---|
984 | /// @todo Implement me!
|
---|
985 | #ifdef VBOX_WITH_XPCOM
|
---|
986 | NOREF(aArgSize);
|
---|
987 | NOREF(aArg);
|
---|
988 | #else
|
---|
989 | NOREF(aArg);
|
---|
990 | #endif
|
---|
991 | AssertFailedReturn(*this);
|
---|
992 | }
|
---|
993 |
|
---|
994 | void cloneTo(SafeArray<T>& aOther) const
|
---|
995 | {
|
---|
996 | aOther.reset(size());
|
---|
997 | aOther.initFrom(*this);
|
---|
998 | }
|
---|
999 |
|
---|
1000 |
|
---|
1001 | /**
|
---|
1002 | * Transfers the ownership of this array's data to the specified location
|
---|
1003 | * declared using the ComSafeArrayOut macro and makes this array a null
|
---|
1004 | * array. When using this call, always wrap the parameter name in the
|
---|
1005 | * ComSafeArrayOutArg macro call like this:
|
---|
1006 | * <pre>
|
---|
1007 | * safeArray.detachTo(ComSafeArrayOutArg(aArg));
|
---|
1008 | * </pre>
|
---|
1009 | *
|
---|
1010 | * Detaching the null array is also possible in which case the location will
|
---|
1011 | * receive NULL.
|
---|
1012 | *
|
---|
1013 | * @note Since the ownership of the array data is transferred to the
|
---|
1014 | * caller of the method, he is responsible to free the array data when it is
|
---|
1015 | * no longer needed.
|
---|
1016 | *
|
---|
1017 | * @param aArg Location to detach to.
|
---|
1018 | */
|
---|
1019 | virtual SafeArray &detachTo(ComSafeArrayOut(T, aArg))
|
---|
1020 | {
|
---|
1021 | AssertReturn(!m.isWeak, *this);
|
---|
1022 |
|
---|
1023 | #ifdef VBOX_WITH_XPCOM
|
---|
1024 |
|
---|
1025 | AssertReturn(aArgSize != NULL, *this);
|
---|
1026 | AssertReturn(aArg != NULL, *this);
|
---|
1027 |
|
---|
1028 | *aArgSize = m.size;
|
---|
1029 | *aArg = m.arr;
|
---|
1030 |
|
---|
1031 | m.isWeak = false;
|
---|
1032 | m.size = 0;
|
---|
1033 | m.arr = NULL;
|
---|
1034 |
|
---|
1035 | #else /* !VBOX_WITH_XPCOM */
|
---|
1036 |
|
---|
1037 | AssertReturn(aArg != NULL, *this);
|
---|
1038 | *aArg = m.arr;
|
---|
1039 |
|
---|
1040 | if (m.raw)
|
---|
1041 | {
|
---|
1042 | HRESULT rc = SafeArrayUnaccessData(m.arr);
|
---|
1043 | AssertComRCReturn(rc, *this);
|
---|
1044 | m.raw = NULL;
|
---|
1045 | }
|
---|
1046 |
|
---|
1047 | m.isWeak = false;
|
---|
1048 | m.arr = NULL;
|
---|
1049 |
|
---|
1050 | #endif /* !VBOX_WITH_XPCOM */
|
---|
1051 |
|
---|
1052 | return *this;
|
---|
1053 | }
|
---|
1054 |
|
---|
1055 | /**
|
---|
1056 | * Returns a copy of this SafeArray as RTCList<T>.
|
---|
1057 | */
|
---|
1058 | RTCList<T> toList()
|
---|
1059 | {
|
---|
1060 | RTCList<T> list(size());
|
---|
1061 | for (size_t i = 0; i < size(); ++i)
|
---|
1062 | #ifdef VBOX_WITH_XPCOM
|
---|
1063 | list.append(m.arr[i]);
|
---|
1064 | #else
|
---|
1065 | list.append(m.raw[i]);
|
---|
1066 | #endif
|
---|
1067 | return list;
|
---|
1068 | }
|
---|
1069 |
|
---|
1070 | inline void initFrom(const com::SafeArray<T> & aRef);
|
---|
1071 | inline void initFrom(const T* aPtr, size_t aSize);
|
---|
1072 |
|
---|
1073 | // Public methods for internal purposes only.
|
---|
1074 |
|
---|
1075 | #ifdef VBOX_WITH_XPCOM
|
---|
1076 |
|
---|
1077 | /** Internal function. Never call it directly. */
|
---|
1078 | PRUint32 *__asOutParam_Size() { setNull(); return &m.size; }
|
---|
1079 |
|
---|
1080 | /** Internal function Never call it directly. */
|
---|
1081 | T **__asOutParam_Arr() { Assert(isNull()); return &m.arr; }
|
---|
1082 |
|
---|
1083 | #else /* !VBOX_WITH_XPCOM */
|
---|
1084 |
|
---|
1085 | /** Internal function Never call it directly. */
|
---|
1086 | SAFEARRAY * __asInParam() { return m.arr; }
|
---|
1087 |
|
---|
1088 | /** Internal function Never call it directly. */
|
---|
1089 | OutSafeArrayDipper __asOutParam()
|
---|
1090 | { setNull(); return OutSafeArrayDipper(&m.arr, (void **)&m.raw); }
|
---|
1091 |
|
---|
1092 | #endif /* !VBOX_WITH_XPCOM */
|
---|
1093 |
|
---|
1094 | static const SafeArray Null;
|
---|
1095 |
|
---|
1096 | protected:
|
---|
1097 |
|
---|
1098 | DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP(SafeArray);
|
---|
1099 |
|
---|
1100 | /**
|
---|
1101 | * Ensures that the array is big enough to contain aNewSize elements.
|
---|
1102 | *
|
---|
1103 | * If the new size is greater than the current capacity, a new array is
|
---|
1104 | * allocated and elements from the old array are copied over. The size of
|
---|
1105 | * the array doesn't change, only the capacity increases (which is always
|
---|
1106 | * greater than the size). Note that the additionally allocated elements are
|
---|
1107 | * left uninitialized by this method.
|
---|
1108 | *
|
---|
1109 | * If the new size is less than the current size, the existing array is
|
---|
1110 | * truncated to the specified size and the elements outside the new array
|
---|
1111 | * boundary are freed.
|
---|
1112 | *
|
---|
1113 | * If the new size is the same as the current size, nothing happens.
|
---|
1114 | *
|
---|
1115 | * @param aNewSize New size of the array.
|
---|
1116 | *
|
---|
1117 | * @return @c true on success and @c false if not enough memory.
|
---|
1118 | */
|
---|
1119 | bool ensureCapacity(size_t aNewSize)
|
---|
1120 | {
|
---|
1121 | AssertReturn(!m.isWeak, false);
|
---|
1122 |
|
---|
1123 | #ifdef VBOX_WITH_XPCOM
|
---|
1124 |
|
---|
1125 | /* Note: we distinguish between a null array and an empty (zero
|
---|
1126 | * elements) array. Therefore we never use zero in malloc (even if
|
---|
1127 | * aNewSize is zero) to make sure we get a non-null pointer. */
|
---|
1128 |
|
---|
1129 | if (m.size == aNewSize && m.arr != NULL)
|
---|
1130 | return true;
|
---|
1131 |
|
---|
1132 | /* Allocate in 16-byte pieces. */
|
---|
1133 | size_t newCapacity = RT_MAX((aNewSize + 15) / 16 * 16, 16);
|
---|
1134 |
|
---|
1135 | if (m.capacity != newCapacity)
|
---|
1136 | {
|
---|
1137 | T *newArr = (T *)nsMemory::Alloc(RT_MAX(newCapacity, 1) * sizeof(T));
|
---|
1138 | AssertReturn(newArr != NULL, false);
|
---|
1139 |
|
---|
1140 | if (m.arr != NULL)
|
---|
1141 | {
|
---|
1142 | if (m.size > aNewSize)
|
---|
1143 | {
|
---|
1144 | /* Truncation takes place, uninit exceeding elements and
|
---|
1145 | * shrink the size. */
|
---|
1146 | for (size_t i = aNewSize; i < m.size; ++ i)
|
---|
1147 | SafeArray::Uninit(m.arr[i]);
|
---|
1148 |
|
---|
1149 | /** @todo Fix this! */
|
---|
1150 | m.size = (PRUint32)aNewSize;
|
---|
1151 | }
|
---|
1152 |
|
---|
1153 | /* Copy the old contents. */
|
---|
1154 | memcpy(newArr, m.arr, m.size * sizeof(T));
|
---|
1155 | nsMemory::Free((void *)m.arr);
|
---|
1156 | }
|
---|
1157 |
|
---|
1158 | m.arr = newArr;
|
---|
1159 | }
|
---|
1160 | else
|
---|
1161 | {
|
---|
1162 | if (m.size > aNewSize)
|
---|
1163 | {
|
---|
1164 | /* Truncation takes place, uninit exceeding elements and
|
---|
1165 | * shrink the size. */
|
---|
1166 | for (size_t i = aNewSize; i < m.size; ++ i)
|
---|
1167 | SafeArray::Uninit(m.arr[i]);
|
---|
1168 |
|
---|
1169 | /** @todo Fix this! */
|
---|
1170 | m.size = (PRUint32)aNewSize;
|
---|
1171 | }
|
---|
1172 | }
|
---|
1173 |
|
---|
1174 | /** @todo Fix this! */
|
---|
1175 | m.capacity = (PRUint32)newCapacity;
|
---|
1176 |
|
---|
1177 | #else
|
---|
1178 |
|
---|
1179 | SAFEARRAYBOUND bound = { VarCount(aNewSize), 0 };
|
---|
1180 | HRESULT rc;
|
---|
1181 |
|
---|
1182 | if (m.arr == NULL)
|
---|
1183 | {
|
---|
1184 | m.arr = CreateSafeArray(VarType(), &bound);
|
---|
1185 | AssertReturn(m.arr != NULL, false);
|
---|
1186 | }
|
---|
1187 | else
|
---|
1188 | {
|
---|
1189 | SafeArrayUnaccessData(m.arr);
|
---|
1190 |
|
---|
1191 | rc = SafeArrayRedim(m.arr, &bound);
|
---|
1192 | AssertComRCReturn(rc == S_OK, false);
|
---|
1193 | }
|
---|
1194 |
|
---|
1195 | rc = SafeArrayAccessData(m.arr, (void HUGEP **)&m.raw);
|
---|
1196 | AssertComRCReturn(rc, false);
|
---|
1197 |
|
---|
1198 | #endif
|
---|
1199 | return true;
|
---|
1200 | }
|
---|
1201 |
|
---|
1202 | struct Data
|
---|
1203 | {
|
---|
1204 | Data()
|
---|
1205 | : isWeak(false)
|
---|
1206 | #ifdef VBOX_WITH_XPCOM
|
---|
1207 | , capacity(0), size(0), arr(NULL)
|
---|
1208 | #else
|
---|
1209 | , arr(NULL), raw(NULL)
|
---|
1210 | #endif
|
---|
1211 | {}
|
---|
1212 |
|
---|
1213 | ~Data() { uninit(); }
|
---|
1214 |
|
---|
1215 | void uninit()
|
---|
1216 | {
|
---|
1217 | #ifdef VBOX_WITH_XPCOM
|
---|
1218 |
|
---|
1219 | if (arr)
|
---|
1220 | {
|
---|
1221 | if (!isWeak)
|
---|
1222 | {
|
---|
1223 | for (size_t i = 0; i < size; ++ i)
|
---|
1224 | SafeArray::Uninit(arr[i]);
|
---|
1225 |
|
---|
1226 | nsMemory::Free((void *)arr);
|
---|
1227 | }
|
---|
1228 | else
|
---|
1229 | isWeak = false;
|
---|
1230 |
|
---|
1231 | arr = NULL;
|
---|
1232 | }
|
---|
1233 |
|
---|
1234 | size = capacity = 0;
|
---|
1235 |
|
---|
1236 | #else /* !VBOX_WITH_XPCOM */
|
---|
1237 |
|
---|
1238 | if (arr)
|
---|
1239 | {
|
---|
1240 | if (raw)
|
---|
1241 | {
|
---|
1242 | SafeArrayUnaccessData(arr);
|
---|
1243 | raw = NULL;
|
---|
1244 | }
|
---|
1245 |
|
---|
1246 | if (!isWeak)
|
---|
1247 | {
|
---|
1248 | HRESULT rc = SafeArrayDestroy(arr);
|
---|
1249 | AssertComRCReturnVoid(rc);
|
---|
1250 | }
|
---|
1251 | else
|
---|
1252 | isWeak = false;
|
---|
1253 |
|
---|
1254 | arr = NULL;
|
---|
1255 | }
|
---|
1256 |
|
---|
1257 | #endif /* !VBOX_WITH_XPCOM */
|
---|
1258 | }
|
---|
1259 |
|
---|
1260 | bool isWeak : 1;
|
---|
1261 |
|
---|
1262 | #ifdef VBOX_WITH_XPCOM
|
---|
1263 | PRUint32 capacity;
|
---|
1264 | PRUint32 size;
|
---|
1265 | T *arr;
|
---|
1266 | #else
|
---|
1267 | SAFEARRAY *arr;
|
---|
1268 | T *raw;
|
---|
1269 | #endif
|
---|
1270 | };
|
---|
1271 |
|
---|
1272 | Data m;
|
---|
1273 | };
|
---|
1274 |
|
---|
1275 | /* Few fast specializations for primitive array types */
|
---|
1276 | template<>
|
---|
1277 | inline void com::SafeArray<BYTE>::initFrom(const com::SafeArray<BYTE> & aRef)
|
---|
1278 | {
|
---|
1279 | size_t sSize = aRef.size();
|
---|
1280 | resize(sSize);
|
---|
1281 | ::memcpy(raw(), aRef.raw(), sSize);
|
---|
1282 | }
|
---|
1283 | template<>
|
---|
1284 | inline void com::SafeArray<BYTE>::initFrom(const BYTE* aPtr, size_t aSize)
|
---|
1285 | {
|
---|
1286 | resize(aSize);
|
---|
1287 | ::memcpy(raw(), aPtr, aSize);
|
---|
1288 | }
|
---|
1289 |
|
---|
1290 |
|
---|
1291 | template<>
|
---|
1292 | inline void com::SafeArray<SHORT>::initFrom(const com::SafeArray<SHORT> & aRef)
|
---|
1293 | {
|
---|
1294 | size_t sSize = aRef.size();
|
---|
1295 | resize(sSize);
|
---|
1296 | ::memcpy(raw(), aRef.raw(), sSize * sizeof(SHORT));
|
---|
1297 | }
|
---|
1298 | template<>
|
---|
1299 | inline void com::SafeArray<SHORT>::initFrom(const SHORT* aPtr, size_t aSize)
|
---|
1300 | {
|
---|
1301 | resize(aSize);
|
---|
1302 | ::memcpy(raw(), aPtr, aSize * sizeof(SHORT));
|
---|
1303 | }
|
---|
1304 |
|
---|
1305 | template<>
|
---|
1306 | inline void com::SafeArray<USHORT>::initFrom(const com::SafeArray<USHORT> & aRef)
|
---|
1307 | {
|
---|
1308 | size_t sSize = aRef.size();
|
---|
1309 | resize(sSize);
|
---|
1310 | ::memcpy(raw(), aRef.raw(), sSize * sizeof(USHORT));
|
---|
1311 | }
|
---|
1312 | template<>
|
---|
1313 | inline void com::SafeArray<USHORT>::initFrom(const USHORT* aPtr, size_t aSize)
|
---|
1314 | {
|
---|
1315 | resize(aSize);
|
---|
1316 | ::memcpy(raw(), aPtr, aSize * sizeof(USHORT));
|
---|
1317 | }
|
---|
1318 |
|
---|
1319 | template<>
|
---|
1320 | inline void com::SafeArray<LONG>::initFrom(const com::SafeArray<LONG> & aRef)
|
---|
1321 | {
|
---|
1322 | size_t sSize = aRef.size();
|
---|
1323 | resize(sSize);
|
---|
1324 | ::memcpy(raw(), aRef.raw(), sSize * sizeof(LONG));
|
---|
1325 | }
|
---|
1326 | template<>
|
---|
1327 | inline void com::SafeArray<LONG>::initFrom(const LONG* aPtr, size_t aSize)
|
---|
1328 | {
|
---|
1329 | resize(aSize);
|
---|
1330 | ::memcpy(raw(), aPtr, aSize * sizeof(LONG));
|
---|
1331 | }
|
---|
1332 |
|
---|
1333 |
|
---|
1334 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1335 |
|
---|
1336 | #ifdef VBOX_WITH_XPCOM
|
---|
1337 |
|
---|
1338 | /**
|
---|
1339 | * Version of com::SafeArray for arrays of GUID.
|
---|
1340 | *
|
---|
1341 | * In MS COM, GUID arrays store GUIDs by value and therefore input arrays are
|
---|
1342 | * represented using |GUID *| and out arrays -- using |GUID **|. In XPCOM,
|
---|
1343 | * GUID arrays store pointers to nsID so that input arrays are |const nsID **|
|
---|
1344 | * and out arrays are |nsID ***|. Due to this difference, it is impossible to
|
---|
1345 | * work with arrays of GUID on both platforms by simply using com::SafeArray
|
---|
1346 | * <GUID>. This class is intended to provide some level of cross-platform
|
---|
1347 | * behavior.
|
---|
1348 | *
|
---|
1349 | * The basic usage pattern is basically similar to com::SafeArray<> except that
|
---|
1350 | * you use ComSafeGUIDArrayIn* and ComSafeGUIDArrayOut* macros instead of
|
---|
1351 | * ComSafeArrayIn* and ComSafeArrayOut*. Another important nuance is that the
|
---|
1352 | * raw() array type is different (nsID **, or GUID ** on XPCOM and GUID * on MS
|
---|
1353 | * COM) so it is recommended to use operator[] instead which always returns a
|
---|
1354 | * GUID by value.
|
---|
1355 | *
|
---|
1356 | * Note that due to const modifiers, you cannot use SafeGUIDArray for input GUID
|
---|
1357 | * arrays. Please use SafeConstGUIDArray for this instead.
|
---|
1358 | *
|
---|
1359 | * Other than mentioned above, the functionality of this class is equivalent to
|
---|
1360 | * com::SafeArray<>. See the description of that template and its methods for
|
---|
1361 | * more information.
|
---|
1362 | *
|
---|
1363 | * Output GUID arrays are handled by a separate class, SafeGUIDArrayOut, since
|
---|
1364 | * this class cannot handle them because of const modifiers.
|
---|
1365 | */
|
---|
1366 | class SafeGUIDArray : public SafeArray<nsID *>
|
---|
1367 | {
|
---|
1368 | public:
|
---|
1369 |
|
---|
1370 | typedef SafeArray<nsID *> Base;
|
---|
1371 |
|
---|
1372 | class nsIDRef
|
---|
1373 | {
|
---|
1374 | public:
|
---|
1375 |
|
---|
1376 | nsIDRef(nsID * &aVal) : mVal(aVal) {}
|
---|
1377 |
|
---|
1378 | operator const nsID &() const { return mVal ? *mVal : *Empty; }
|
---|
1379 | operator nsID() const { return mVal ? *mVal : *Empty; }
|
---|
1380 |
|
---|
1381 | const nsID *operator&() const { return mVal ? mVal : Empty; }
|
---|
1382 |
|
---|
1383 | nsIDRef &operator= (const nsID &aThat)
|
---|
1384 | {
|
---|
1385 | if (mVal == NULL)
|
---|
1386 | Copy(&aThat, mVal);
|
---|
1387 | else
|
---|
1388 | *mVal = aThat;
|
---|
1389 | return *this;
|
---|
1390 | }
|
---|
1391 |
|
---|
1392 | private:
|
---|
1393 |
|
---|
1394 | nsID * &mVal;
|
---|
1395 |
|
---|
1396 | static const nsID *Empty;
|
---|
1397 |
|
---|
1398 | friend class SafeGUIDArray;
|
---|
1399 | };
|
---|
1400 |
|
---|
1401 | /** See SafeArray<>::SafeArray(). */
|
---|
1402 | SafeGUIDArray() {}
|
---|
1403 |
|
---|
1404 | /** See SafeArray<>::SafeArray(size_t). */
|
---|
1405 | SafeGUIDArray(size_t aSize) : Base(aSize) {}
|
---|
1406 |
|
---|
1407 | /**
|
---|
1408 | * Array access operator that returns an array element by reference. As a
|
---|
1409 | * special case, the return value of this operator on XPCOM is an nsID (GUID)
|
---|
1410 | * reference, instead of an nsID pointer (the actual SafeArray template
|
---|
1411 | * argument), for compatibility with the MS COM version.
|
---|
1412 | *
|
---|
1413 | * The rest is equivalent to SafeArray<>::operator[].
|
---|
1414 | */
|
---|
1415 | nsIDRef operator[] (size_t aIdx)
|
---|
1416 | {
|
---|
1417 | Assert(m.arr != NULL);
|
---|
1418 | Assert(aIdx < size());
|
---|
1419 | return nsIDRef(m.arr[aIdx]);
|
---|
1420 | }
|
---|
1421 |
|
---|
1422 | /**
|
---|
1423 | * Const version of #operator[] that returns an array element by value.
|
---|
1424 | */
|
---|
1425 | const nsID &operator[] (size_t aIdx) const
|
---|
1426 | {
|
---|
1427 | Assert(m.arr != NULL);
|
---|
1428 | Assert(aIdx < size());
|
---|
1429 | return m.arr[aIdx] ? *m.arr[aIdx] : *nsIDRef::Empty;
|
---|
1430 | }
|
---|
1431 | };
|
---|
1432 |
|
---|
1433 | /**
|
---|
1434 | * Version of com::SafeArray for const arrays of GUID.
|
---|
1435 | *
|
---|
1436 | * This class is used to work with input GUID array parameters in method
|
---|
1437 | * implementations. See SafeGUIDArray for more details.
|
---|
1438 | */
|
---|
1439 | class SafeConstGUIDArray : public SafeArray<const nsID *,
|
---|
1440 | SafeArrayTraits<nsID *> >
|
---|
1441 | {
|
---|
1442 | public:
|
---|
1443 |
|
---|
1444 | typedef SafeArray<const nsID *, SafeArrayTraits<nsID *> > Base;
|
---|
1445 |
|
---|
1446 | /** See SafeArray<>::SafeArray(). */
|
---|
1447 | SafeConstGUIDArray() {}
|
---|
1448 |
|
---|
1449 | /* See SafeArray<>::SafeArray(ComSafeArrayIn(T, aArg)). */
|
---|
1450 | SafeConstGUIDArray(ComSafeGUIDArrayIn(aArg))
|
---|
1451 | : Base(ComSafeGUIDArrayInArg(aArg)) {}
|
---|
1452 |
|
---|
1453 | /**
|
---|
1454 | * Array access operator that returns an array element by reference. As a
|
---|
1455 | * special case, the return value of this operator on XPCOM is nsID (GUID)
|
---|
1456 | * instead of nsID *, for compatibility with the MS COM version.
|
---|
1457 | *
|
---|
1458 | * The rest is equivalent to SafeArray<>::operator[].
|
---|
1459 | */
|
---|
1460 | const nsID &operator[] (size_t aIdx) const
|
---|
1461 | {
|
---|
1462 | AssertReturn(m.arr != NULL, **((const nsID * *)1));
|
---|
1463 | AssertReturn(aIdx < size(), **((const nsID * *)1));
|
---|
1464 | return *m.arr[aIdx];
|
---|
1465 | }
|
---|
1466 |
|
---|
1467 | private:
|
---|
1468 |
|
---|
1469 | /* These are disabled because of const. */
|
---|
1470 | bool reset(size_t aNewSize) { NOREF(aNewSize); return false; }
|
---|
1471 | };
|
---|
1472 |
|
---|
1473 | #else /* !VBOX_WITH_XPCOM */
|
---|
1474 |
|
---|
1475 | typedef SafeArray<GUID> SafeGUIDArray;
|
---|
1476 | typedef SafeArray<const GUID, SafeArrayTraits<GUID> > SafeConstGUIDArray;
|
---|
1477 |
|
---|
1478 | #endif /* !VBOX_WITH_XPCOM */
|
---|
1479 |
|
---|
1480 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1481 |
|
---|
1482 | #ifdef VBOX_WITH_XPCOM
|
---|
1483 |
|
---|
1484 | template<class I>
|
---|
1485 | struct SafeIfaceArrayTraits
|
---|
1486 | {
|
---|
1487 | protected:
|
---|
1488 |
|
---|
1489 | static void Init(I * &aElem) { aElem = NULL; }
|
---|
1490 | static void Uninit(I * &aElem)
|
---|
1491 | {
|
---|
1492 | if (aElem)
|
---|
1493 | {
|
---|
1494 | aElem->Release();
|
---|
1495 | aElem = NULL;
|
---|
1496 | }
|
---|
1497 | }
|
---|
1498 |
|
---|
1499 | static void Copy(I * aFrom, I * &aTo)
|
---|
1500 | {
|
---|
1501 | if (aFrom != NULL)
|
---|
1502 | {
|
---|
1503 | aTo = aFrom;
|
---|
1504 | aTo->AddRef();
|
---|
1505 | }
|
---|
1506 | else
|
---|
1507 | aTo = NULL;
|
---|
1508 | }
|
---|
1509 |
|
---|
1510 | public:
|
---|
1511 |
|
---|
1512 | /* Magic to workaround strict rules of par. 4.4.4 of the C++ standard. */
|
---|
1513 | static I **__asInParam_Arr(I **aArr) { return aArr; }
|
---|
1514 | static I **__asInParam_Arr(const I **aArr) { return const_cast<I **>(aArr); }
|
---|
1515 | };
|
---|
1516 |
|
---|
1517 | #else /* !VBOX_WITH_XPCOM */
|
---|
1518 |
|
---|
1519 | template<class I>
|
---|
1520 | struct SafeIfaceArrayTraits
|
---|
1521 | {
|
---|
1522 | protected:
|
---|
1523 |
|
---|
1524 | static VARTYPE VarType() { return VT_DISPATCH; }
|
---|
1525 | static ULONG VarCount(size_t aSize) { return (ULONG)aSize; }
|
---|
1526 | static size_t Size(ULONG aVarCount) { return (size_t)aVarCount; }
|
---|
1527 |
|
---|
1528 | static void Copy(I * aFrom, I * &aTo)
|
---|
1529 | {
|
---|
1530 | if (aFrom != NULL)
|
---|
1531 | {
|
---|
1532 | aTo = aFrom;
|
---|
1533 | aTo->AddRef();
|
---|
1534 | }
|
---|
1535 | else
|
---|
1536 | aTo = NULL;
|
---|
1537 | }
|
---|
1538 |
|
---|
1539 | static SAFEARRAY *CreateSafeArray(VARTYPE aVarType, SAFEARRAYBOUND *aBound)
|
---|
1540 | {
|
---|
1541 | NOREF(aVarType);
|
---|
1542 | return SafeArrayCreateEx(VT_DISPATCH, 1, aBound, (PVOID)&COM_IIDOF(I));
|
---|
1543 | }
|
---|
1544 | };
|
---|
1545 |
|
---|
1546 | #endif /* !VBOX_WITH_XPCOM */
|
---|
1547 |
|
---|
1548 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1549 |
|
---|
1550 | /**
|
---|
1551 | * Version of com::SafeArray for arrays of interface pointers.
|
---|
1552 | *
|
---|
1553 | * Except that it manages arrays of interface pointers, the usage of this class
|
---|
1554 | * is identical to com::SafeArray.
|
---|
1555 | *
|
---|
1556 | * @param I Interface class (no asterisk).
|
---|
1557 | */
|
---|
1558 | template<class I>
|
---|
1559 | class SafeIfaceArray : public SafeArray<I *, SafeIfaceArrayTraits<I> >
|
---|
1560 | {
|
---|
1561 | public:
|
---|
1562 |
|
---|
1563 | typedef SafeArray<I *, SafeIfaceArrayTraits<I> > Base;
|
---|
1564 |
|
---|
1565 | /**
|
---|
1566 | * Creates a null array.
|
---|
1567 | */
|
---|
1568 | SafeIfaceArray() {}
|
---|
1569 |
|
---|
1570 | /**
|
---|
1571 | * Creates a new array of the given size. All elements of the newly created
|
---|
1572 | * array initialized with null values.
|
---|
1573 | *
|
---|
1574 | * @param aSize Initial number of elements in the array. Must be greater
|
---|
1575 | * than 0.
|
---|
1576 | *
|
---|
1577 | * @note If this object remains null after construction it means that there
|
---|
1578 | * was not enough memory for creating an array of the requested size.
|
---|
1579 | * The constructor will also assert in this case.
|
---|
1580 | */
|
---|
1581 | SafeIfaceArray(size_t aSize) { Base::resize(aSize); }
|
---|
1582 |
|
---|
1583 | /**
|
---|
1584 | * Weakly attaches this instance to the existing array passed in a method
|
---|
1585 | * parameter declared using the ComSafeArrayIn macro. When using this call,
|
---|
1586 | * always wrap the parameter name in the ComSafeArrayOutArg macro call like
|
---|
1587 | * this:
|
---|
1588 | * <pre>
|
---|
1589 | * SafeArray safeArray(ComSafeArrayInArg(aArg));
|
---|
1590 | * </pre>
|
---|
1591 | *
|
---|
1592 | * Note that this constructor doesn't take the ownership of the array. In
|
---|
1593 | * particular, this means that operations that operate on the ownership
|
---|
1594 | * (e.g. #detachTo()) are forbidden and will assert.
|
---|
1595 | *
|
---|
1596 | * @param aArg Input method parameter to attach to.
|
---|
1597 | */
|
---|
1598 | SafeIfaceArray(ComSafeArrayIn(I *, aArg))
|
---|
1599 | {
|
---|
1600 | if (aArg)
|
---|
1601 | {
|
---|
1602 | #ifdef VBOX_WITH_XPCOM
|
---|
1603 |
|
---|
1604 | Base::m.size = aArgSize;
|
---|
1605 | Base::m.arr = aArg;
|
---|
1606 | Base::m.isWeak = true;
|
---|
1607 |
|
---|
1608 | #else /* !VBOX_WITH_XPCOM */
|
---|
1609 |
|
---|
1610 | SAFEARRAY *arg = aArg;
|
---|
1611 |
|
---|
1612 | AssertReturnVoid(arg->cDims == 1);
|
---|
1613 |
|
---|
1614 | VARTYPE vt;
|
---|
1615 | HRESULT rc = SafeArrayGetVartype(arg, &vt);
|
---|
1616 | AssertComRCReturnVoid(rc);
|
---|
1617 | AssertMsgReturnVoid(vt == VT_UNKNOWN || vt == VT_DISPATCH,
|
---|
1618 | ("Expected vartype VT_UNKNOWN or VT_DISPATCH, got %d.\n",
|
---|
1619 | vt));
|
---|
1620 | GUID guid;
|
---|
1621 | rc = SafeArrayGetIID(arg, &guid);
|
---|
1622 | AssertComRCReturnVoid(rc);
|
---|
1623 | AssertMsgReturnVoid(InlineIsEqualGUID(COM_IIDOF(I), guid),
|
---|
1624 | ("Expected IID {%RTuuid}, got {%RTuuid}.\n",
|
---|
1625 | &COM_IIDOF(I), &guid));
|
---|
1626 |
|
---|
1627 | rc = SafeArrayAccessData(arg, (void HUGEP **)&m.raw);
|
---|
1628 | AssertComRCReturnVoid(rc);
|
---|
1629 |
|
---|
1630 | m.arr = arg;
|
---|
1631 | m.isWeak = true;
|
---|
1632 |
|
---|
1633 | #endif /* !VBOX_WITH_XPCOM */
|
---|
1634 | }
|
---|
1635 | }
|
---|
1636 |
|
---|
1637 | /**
|
---|
1638 | * Creates a deep copy of the given standard C++ container that stores
|
---|
1639 | * interface pointers as objects of the ComPtr\<I\> class.
|
---|
1640 | *
|
---|
1641 | * @param aCntr Container object to copy.
|
---|
1642 | *
|
---|
1643 | * @tparam C Standard C++ container template class (normally deduced from
|
---|
1644 | * @c aCntr).
|
---|
1645 | * @tparam A Standard C++ allocator class (deduced from @c aCntr).
|
---|
1646 | * @tparam OI Argument to the ComPtr template (deduced from @c aCntr).
|
---|
1647 | */
|
---|
1648 | template<template<typename, typename> class C, class A, class OI>
|
---|
1649 | SafeIfaceArray(const C<ComPtr<OI>, A> & aCntr)
|
---|
1650 | {
|
---|
1651 | typedef C<ComPtr<OI>, A> List;
|
---|
1652 |
|
---|
1653 | Base::resize(aCntr.size());
|
---|
1654 | AssertReturnVoid(!Base::isNull());
|
---|
1655 |
|
---|
1656 | int i = 0;
|
---|
1657 | for (typename List::const_iterator it = aCntr.begin();
|
---|
1658 | it != aCntr.end(); ++ it, ++ i)
|
---|
1659 | #ifdef VBOX_WITH_XPCOM
|
---|
1660 | this->Copy(*it, Base::m.arr[i]);
|
---|
1661 | #else
|
---|
1662 | Copy(*it, Base::m.raw[i]);
|
---|
1663 | #endif
|
---|
1664 | }
|
---|
1665 |
|
---|
1666 | /**
|
---|
1667 | * Creates a deep copy of the given standard C++ container that stores
|
---|
1668 | * interface pointers as objects of the ComObjPtr\<I\> class.
|
---|
1669 | *
|
---|
1670 | * @param aCntr Container object to copy.
|
---|
1671 | *
|
---|
1672 | * @tparam C Standard C++ container template class (normally deduced from
|
---|
1673 | * @c aCntr).
|
---|
1674 | * @tparam A Standard C++ allocator class (deduced from @c aCntr).
|
---|
1675 | * @tparam OI Argument to the ComObjPtr template (deduced from @c aCntr).
|
---|
1676 | */
|
---|
1677 | template<template<typename, typename> class C, class A, class OI>
|
---|
1678 | SafeIfaceArray(const C<ComObjPtr<OI>, A> & aCntr)
|
---|
1679 | {
|
---|
1680 | typedef C<ComObjPtr<OI>, A> List;
|
---|
1681 |
|
---|
1682 | Base::resize(aCntr.size());
|
---|
1683 | AssertReturnVoid(!Base::isNull());
|
---|
1684 |
|
---|
1685 | int i = 0;
|
---|
1686 | for (typename List::const_iterator it = aCntr.begin();
|
---|
1687 | it != aCntr.end(); ++ it, ++ i)
|
---|
1688 | #ifdef VBOX_WITH_XPCOM
|
---|
1689 | SafeIfaceArray::Copy(*it, Base::m.arr[i]);
|
---|
1690 | #else
|
---|
1691 | Copy(*it, Base::m.raw[i]);
|
---|
1692 | #endif
|
---|
1693 | }
|
---|
1694 |
|
---|
1695 | /**
|
---|
1696 | * Creates a deep copy of the given standard C++ map whose values are
|
---|
1697 | * interface pointers stored as objects of the ComPtr\<I\> class.
|
---|
1698 | *
|
---|
1699 | * @param aMap Map object to copy.
|
---|
1700 | *
|
---|
1701 | * @tparam C Standard C++ map template class (normally deduced from
|
---|
1702 | * @c aCntr).
|
---|
1703 | * @tparam L Standard C++ compare class (deduced from @c aCntr).
|
---|
1704 | * @tparam A Standard C++ allocator class (deduced from @c aCntr).
|
---|
1705 | * @tparam K Map key class (deduced from @c aCntr).
|
---|
1706 | * @tparam OI Argument to the ComPtr template (deduced from @c aCntr).
|
---|
1707 | */
|
---|
1708 | template<template<typename, typename, typename, typename>
|
---|
1709 | class C, class L, class A, class K, class OI>
|
---|
1710 | SafeIfaceArray(const C<K, ComPtr<OI>, L, A> & aMap)
|
---|
1711 | {
|
---|
1712 | typedef C<K, ComPtr<OI>, L, A> Map;
|
---|
1713 |
|
---|
1714 | Base::resize(aMap.size());
|
---|
1715 | AssertReturnVoid(!Base::isNull());
|
---|
1716 |
|
---|
1717 | int i = 0;
|
---|
1718 | for (typename Map::const_iterator it = aMap.begin();
|
---|
1719 | it != aMap.end(); ++ it, ++ i)
|
---|
1720 | #ifdef VBOX_WITH_XPCOM
|
---|
1721 | SafeIfaceArray::Copy(it->second, Base::m.arr[i]);
|
---|
1722 | #else
|
---|
1723 | Copy(it->second, Base::m.raw[i]);
|
---|
1724 | #endif
|
---|
1725 | }
|
---|
1726 |
|
---|
1727 | /**
|
---|
1728 | * Creates a deep copy of the given standard C++ map whose values are
|
---|
1729 | * interface pointers stored as objects of the ComObjPtr\<I\> class.
|
---|
1730 | *
|
---|
1731 | * @param aMap Map object to copy.
|
---|
1732 | *
|
---|
1733 | * @tparam C Standard C++ map template class (normally deduced from
|
---|
1734 | * @c aCntr).
|
---|
1735 | * @tparam L Standard C++ compare class (deduced from @c aCntr).
|
---|
1736 | * @tparam A Standard C++ allocator class (deduced from @c aCntr).
|
---|
1737 | * @tparam K Map key class (deduced from @c aCntr).
|
---|
1738 | * @tparam OI Argument to the ComObjPtr template (deduced from @c aCntr).
|
---|
1739 | */
|
---|
1740 | template<template<typename, typename, typename, typename>
|
---|
1741 | class C, class L, class A, class K, class OI>
|
---|
1742 | SafeIfaceArray(const C<K, ComObjPtr<OI>, L, A> & aMap)
|
---|
1743 | {
|
---|
1744 | typedef C<K, ComObjPtr<OI>, L, A> Map;
|
---|
1745 |
|
---|
1746 | Base::resize(aMap.size());
|
---|
1747 | AssertReturnVoid(!Base::isNull());
|
---|
1748 |
|
---|
1749 | int i = 0;
|
---|
1750 | for (typename Map::const_iterator it = aMap.begin();
|
---|
1751 | it != aMap.end(); ++ it, ++ i)
|
---|
1752 | #ifdef VBOX_WITH_XPCOM
|
---|
1753 | SafeIfaceArray::Copy(it->second, Base::m.arr[i]);
|
---|
1754 | #else
|
---|
1755 | Copy(it->second, Base::m.raw[i]);
|
---|
1756 | #endif
|
---|
1757 | }
|
---|
1758 |
|
---|
1759 | void setElement(size_t iIdx, I* obj)
|
---|
1760 | {
|
---|
1761 | #ifdef VBOX_WITH_XPCOM
|
---|
1762 | SafeIfaceArray::Copy(obj, Base::m.arr[iIdx]);
|
---|
1763 | #else
|
---|
1764 | Copy(obj, Base::m.raw[iIdx]);
|
---|
1765 | #endif
|
---|
1766 | }
|
---|
1767 | };
|
---|
1768 |
|
---|
1769 | } /* namespace com */
|
---|
1770 |
|
---|
1771 | /** @} */
|
---|
1772 |
|
---|
1773 | #endif /* !___VBox_com_array_h */
|
---|
1774 |
|
---|