1 | /** @file
|
---|
2 | * MS COM / XPCOM Abstraction Layer - Safe array helper class declaration.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2016 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 | (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 | m.size = aNewSize;
|
---|
882 | #else
|
---|
883 | /* nothing to do here, SafeArrayCreate() has performed element
|
---|
884 | * initialization */
|
---|
885 | #endif
|
---|
886 | return true;
|
---|
887 | }
|
---|
888 |
|
---|
889 | /**
|
---|
890 | * Reinitializes this instance by preallocating space for the given number
|
---|
891 | * of elements. The previous array contents is lost.
|
---|
892 | *
|
---|
893 | * @param aNewSize New number of elements in the array.
|
---|
894 | * @return @c true on success and @c false if there is not enough
|
---|
895 | * memory for resizing.
|
---|
896 | */
|
---|
897 | bool reset(size_t aNewSize)
|
---|
898 | {
|
---|
899 | m.uninit();
|
---|
900 | return resize(aNewSize);
|
---|
901 | }
|
---|
902 |
|
---|
903 | /**
|
---|
904 | * Returns a pointer to the raw array data. Use this raw pointer with care
|
---|
905 | * as no type or bound checking is done for you in this case.
|
---|
906 | *
|
---|
907 | * @note This method returns @c NULL when this instance is null.
|
---|
908 | * @see #operator[]
|
---|
909 | */
|
---|
910 | T *raw()
|
---|
911 | {
|
---|
912 | #ifdef VBOX_WITH_XPCOM
|
---|
913 | return m.arr;
|
---|
914 | #else
|
---|
915 | return m.raw;
|
---|
916 | #endif
|
---|
917 | }
|
---|
918 |
|
---|
919 | /**
|
---|
920 | * Const version of #raw().
|
---|
921 | */
|
---|
922 | const T *raw() const
|
---|
923 | {
|
---|
924 | #ifdef VBOX_WITH_XPCOM
|
---|
925 | return m.arr;
|
---|
926 | #else
|
---|
927 | return m.raw;
|
---|
928 | #endif
|
---|
929 | }
|
---|
930 |
|
---|
931 | /**
|
---|
932 | * Array access operator that returns an array element by reference. A bit
|
---|
933 | * safer than #raw(): asserts and returns an invalid reference if this
|
---|
934 | * instance is null or if the index is out of bounds.
|
---|
935 | *
|
---|
936 | * @note For weak instances, this call will succeed but the behavior of
|
---|
937 | * changing the contents of an element of the weak array instance is
|
---|
938 | * undefined and may lead to a program crash on some platforms.
|
---|
939 | */
|
---|
940 | T &operator[] (size_t aIdx)
|
---|
941 | {
|
---|
942 | AssertReturn(m.arr != NULL, *((T *)NULL));
|
---|
943 | AssertReturn(aIdx < size(), *((T *)NULL));
|
---|
944 | #ifdef VBOX_WITH_XPCOM
|
---|
945 | return m.arr[aIdx];
|
---|
946 | #else
|
---|
947 | AssertReturn(m.raw != NULL, *((T *)NULL));
|
---|
948 | return m.raw[aIdx];
|
---|
949 | #endif
|
---|
950 | }
|
---|
951 |
|
---|
952 | /**
|
---|
953 | * Const version of #operator[] that returns an array element by value.
|
---|
954 | */
|
---|
955 | const T operator[] (size_t aIdx) const
|
---|
956 | {
|
---|
957 | AssertReturn(m.arr != NULL, *((T *)NULL));
|
---|
958 | AssertReturn(aIdx < size(), *((T *)NULL));
|
---|
959 | #ifdef VBOX_WITH_XPCOM
|
---|
960 | return m.arr[aIdx];
|
---|
961 | #else
|
---|
962 | AssertReturn(m.raw != NULL, *((T *)NULL));
|
---|
963 | return m.raw[aIdx];
|
---|
964 | #endif
|
---|
965 | }
|
---|
966 |
|
---|
967 | /**
|
---|
968 | * Creates a copy of this array and stores it in a method parameter declared
|
---|
969 | * using the ComSafeArrayOut macro. When using this call, always wrap the
|
---|
970 | * parameter name in the ComSafeArrayOutArg macro call like this:
|
---|
971 | * <pre>
|
---|
972 | * safeArray.cloneTo(ComSafeArrayOutArg(aArg));
|
---|
973 | * </pre>
|
---|
974 | *
|
---|
975 | * @note It is assumed that the ownership of the returned copy is
|
---|
976 | * transferred to the caller of the method and he is responsible to free the
|
---|
977 | * array data when it is no longer needed.
|
---|
978 | *
|
---|
979 | * @param aArg Output method parameter to clone to.
|
---|
980 | */
|
---|
981 | virtual const SafeArray &cloneTo(ComSafeArrayOut(T, aArg)) const
|
---|
982 | {
|
---|
983 | /// @todo Implement me!
|
---|
984 | #ifdef VBOX_WITH_XPCOM
|
---|
985 | NOREF(aArgSize);
|
---|
986 | NOREF(aArg);
|
---|
987 | #else
|
---|
988 | NOREF(aArg);
|
---|
989 | #endif
|
---|
990 | AssertFailedReturn(*this);
|
---|
991 | }
|
---|
992 |
|
---|
993 | void cloneTo(SafeArray<T>& aOther) const
|
---|
994 | {
|
---|
995 | aOther.reset(size());
|
---|
996 | aOther.initFrom(*this);
|
---|
997 | }
|
---|
998 |
|
---|
999 |
|
---|
1000 | /**
|
---|
1001 | * Transfers the ownership of this array's data to the specified location
|
---|
1002 | * declared using the ComSafeArrayOut macro and makes this array a null
|
---|
1003 | * array. When using this call, always wrap the parameter name in the
|
---|
1004 | * ComSafeArrayOutArg macro call like this:
|
---|
1005 | * <pre>
|
---|
1006 | * safeArray.detachTo(ComSafeArrayOutArg(aArg));
|
---|
1007 | * </pre>
|
---|
1008 | *
|
---|
1009 | * Detaching the null array is also possible in which case the location will
|
---|
1010 | * receive NULL.
|
---|
1011 | *
|
---|
1012 | * @note Since the ownership of the array data is transferred to the
|
---|
1013 | * caller of the method, he is responsible to free the array data when it is
|
---|
1014 | * no longer needed.
|
---|
1015 | *
|
---|
1016 | * @param aArg Location to detach to.
|
---|
1017 | */
|
---|
1018 | virtual SafeArray &detachTo(ComSafeArrayOut(T, aArg))
|
---|
1019 | {
|
---|
1020 | AssertReturn(!m.isWeak, *this);
|
---|
1021 |
|
---|
1022 | #ifdef VBOX_WITH_XPCOM
|
---|
1023 |
|
---|
1024 | AssertReturn(aArgSize != NULL, *this);
|
---|
1025 | AssertReturn(aArg != NULL, *this);
|
---|
1026 |
|
---|
1027 | *aArgSize = m.size;
|
---|
1028 | *aArg = m.arr;
|
---|
1029 |
|
---|
1030 | m.isWeak = false;
|
---|
1031 | m.size = 0;
|
---|
1032 | m.arr = NULL;
|
---|
1033 |
|
---|
1034 | #else /* !VBOX_WITH_XPCOM */
|
---|
1035 |
|
---|
1036 | AssertReturn(aArg != NULL, *this);
|
---|
1037 | *aArg = m.arr;
|
---|
1038 |
|
---|
1039 | if (m.raw)
|
---|
1040 | {
|
---|
1041 | HRESULT rc = SafeArrayUnaccessData(m.arr);
|
---|
1042 | AssertComRCReturn(rc, *this);
|
---|
1043 | m.raw = NULL;
|
---|
1044 | }
|
---|
1045 |
|
---|
1046 | m.isWeak = false;
|
---|
1047 | m.arr = NULL;
|
---|
1048 |
|
---|
1049 | #endif /* !VBOX_WITH_XPCOM */
|
---|
1050 |
|
---|
1051 | return *this;
|
---|
1052 | }
|
---|
1053 |
|
---|
1054 | /**
|
---|
1055 | * Returns a copy of this SafeArray as RTCList<T>.
|
---|
1056 | */
|
---|
1057 | RTCList<T> toList()
|
---|
1058 | {
|
---|
1059 | RTCList<T> list(size());
|
---|
1060 | for (size_t i = 0; i < size(); ++i)
|
---|
1061 | #ifdef VBOX_WITH_XPCOM
|
---|
1062 | list.append(m.arr[i]);
|
---|
1063 | #else
|
---|
1064 | list.append(m.raw[i]);
|
---|
1065 | #endif
|
---|
1066 | return list;
|
---|
1067 | }
|
---|
1068 |
|
---|
1069 | inline void initFrom(const com::SafeArray<T> & aRef);
|
---|
1070 | inline void initFrom(const T* aPtr, size_t aSize);
|
---|
1071 |
|
---|
1072 | // Public methods for internal purposes only.
|
---|
1073 |
|
---|
1074 | #ifdef VBOX_WITH_XPCOM
|
---|
1075 |
|
---|
1076 | /** Internal function. Never call it directly. */
|
---|
1077 | PRUint32 *__asOutParam_Size() { setNull(); return &m.size; }
|
---|
1078 |
|
---|
1079 | /** Internal function Never call it directly. */
|
---|
1080 | T **__asOutParam_Arr() { Assert(isNull()); return &m.arr; }
|
---|
1081 |
|
---|
1082 | #else /* !VBOX_WITH_XPCOM */
|
---|
1083 |
|
---|
1084 | /** Internal function Never call it directly. */
|
---|
1085 | SAFEARRAY * __asInParam() { return m.arr; }
|
---|
1086 |
|
---|
1087 | /** Internal function Never call it directly. */
|
---|
1088 | OutSafeArrayDipper __asOutParam()
|
---|
1089 | { setNull(); return OutSafeArrayDipper(&m.arr, (void **)&m.raw); }
|
---|
1090 |
|
---|
1091 | #endif /* !VBOX_WITH_XPCOM */
|
---|
1092 |
|
---|
1093 | static const SafeArray Null;
|
---|
1094 |
|
---|
1095 | protected:
|
---|
1096 |
|
---|
1097 | DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP(SafeArray)
|
---|
1098 |
|
---|
1099 | /**
|
---|
1100 | * Ensures that the array is big enough to contain aNewSize elements.
|
---|
1101 | *
|
---|
1102 | * If the new size is greater than the current capacity, a new array is
|
---|
1103 | * allocated and elements from the old array are copied over. The size of
|
---|
1104 | * the array doesn't change, only the capacity increases (which is always
|
---|
1105 | * greater than the size). Note that the additionally allocated elements are
|
---|
1106 | * left uninitialized by this method.
|
---|
1107 | *
|
---|
1108 | * If the new size is less than the current size, the existing array is
|
---|
1109 | * truncated to the specified size and the elements outside the new array
|
---|
1110 | * boundary are freed.
|
---|
1111 | *
|
---|
1112 | * If the new size is the same as the current size, nothing happens.
|
---|
1113 | *
|
---|
1114 | * @param aNewSize New size of the array.
|
---|
1115 | *
|
---|
1116 | * @return @c true on success and @c false if not enough memory.
|
---|
1117 | */
|
---|
1118 | bool ensureCapacity(size_t aNewSize)
|
---|
1119 | {
|
---|
1120 | AssertReturn(!m.isWeak, false);
|
---|
1121 |
|
---|
1122 | #ifdef VBOX_WITH_XPCOM
|
---|
1123 |
|
---|
1124 | /* Note: we distinguish between a null array and an empty (zero
|
---|
1125 | * elements) array. Therefore we never use zero in malloc (even if
|
---|
1126 | * aNewSize is zero) to make sure we get a non-null pointer. */
|
---|
1127 |
|
---|
1128 | if (m.size == aNewSize && m.arr != NULL)
|
---|
1129 | return true;
|
---|
1130 |
|
---|
1131 | /* Allocate in 16-byte pieces. */
|
---|
1132 | size_t newCapacity = RT_MAX((aNewSize + 15) / 16 * 16, 16);
|
---|
1133 |
|
---|
1134 | if (m.capacity != newCapacity)
|
---|
1135 | {
|
---|
1136 | T *newArr = (T *)nsMemory::Alloc(RT_MAX(newCapacity, 1) * sizeof(T));
|
---|
1137 | AssertReturn(newArr != NULL, false);
|
---|
1138 |
|
---|
1139 | if (m.arr != NULL)
|
---|
1140 | {
|
---|
1141 | if (m.size > aNewSize)
|
---|
1142 | {
|
---|
1143 | /* Truncation takes place, uninit exceeding elements and
|
---|
1144 | * shrink the size. */
|
---|
1145 | for (size_t i = aNewSize; i < m.size; ++ i)
|
---|
1146 | SafeArray::Uninit(m.arr[i]);
|
---|
1147 |
|
---|
1148 | m.size = aNewSize;
|
---|
1149 | }
|
---|
1150 |
|
---|
1151 | /* Copy the old contents. */
|
---|
1152 | memcpy(newArr, m.arr, m.size * sizeof(T));
|
---|
1153 | nsMemory::Free((void *)m.arr);
|
---|
1154 | }
|
---|
1155 |
|
---|
1156 | m.arr = newArr;
|
---|
1157 | }
|
---|
1158 | else
|
---|
1159 | {
|
---|
1160 | if (m.size > aNewSize)
|
---|
1161 | {
|
---|
1162 | /* Truncation takes place, uninit exceeding elements and
|
---|
1163 | * shrink the size. */
|
---|
1164 | for (size_t i = aNewSize; i < m.size; ++ i)
|
---|
1165 | SafeArray::Uninit(m.arr[i]);
|
---|
1166 |
|
---|
1167 | m.size = aNewSize;
|
---|
1168 | }
|
---|
1169 | }
|
---|
1170 |
|
---|
1171 | m.capacity = newCapacity;
|
---|
1172 |
|
---|
1173 | #else
|
---|
1174 |
|
---|
1175 | SAFEARRAYBOUND bound = { VarCount(aNewSize), 0 };
|
---|
1176 | HRESULT rc;
|
---|
1177 |
|
---|
1178 | if (m.arr == NULL)
|
---|
1179 | {
|
---|
1180 | m.arr = CreateSafeArray(VarType(), &bound);
|
---|
1181 | AssertReturn(m.arr != NULL, false);
|
---|
1182 | }
|
---|
1183 | else
|
---|
1184 | {
|
---|
1185 | SafeArrayUnaccessData(m.arr);
|
---|
1186 |
|
---|
1187 | rc = SafeArrayRedim(m.arr, &bound);
|
---|
1188 | AssertComRCReturn(rc == S_OK, false);
|
---|
1189 | }
|
---|
1190 |
|
---|
1191 | rc = SafeArrayAccessData(m.arr, (void HUGEP **)&m.raw);
|
---|
1192 | AssertComRCReturn(rc, false);
|
---|
1193 |
|
---|
1194 | #endif
|
---|
1195 | return true;
|
---|
1196 | }
|
---|
1197 |
|
---|
1198 | struct Data
|
---|
1199 | {
|
---|
1200 | Data()
|
---|
1201 | : isWeak(false)
|
---|
1202 | #ifdef VBOX_WITH_XPCOM
|
---|
1203 | , capacity(0), size(0), arr(NULL)
|
---|
1204 | #else
|
---|
1205 | , arr(NULL), raw(NULL)
|
---|
1206 | #endif
|
---|
1207 | {}
|
---|
1208 |
|
---|
1209 | ~Data() { uninit(); }
|
---|
1210 |
|
---|
1211 | void uninit()
|
---|
1212 | {
|
---|
1213 | #ifdef VBOX_WITH_XPCOM
|
---|
1214 |
|
---|
1215 | if (arr)
|
---|
1216 | {
|
---|
1217 | if (!isWeak)
|
---|
1218 | {
|
---|
1219 | for (size_t i = 0; i < size; ++ i)
|
---|
1220 | SafeArray::Uninit(arr[i]);
|
---|
1221 |
|
---|
1222 | nsMemory::Free((void *)arr);
|
---|
1223 | }
|
---|
1224 | else
|
---|
1225 | isWeak = false;
|
---|
1226 |
|
---|
1227 | arr = NULL;
|
---|
1228 | }
|
---|
1229 |
|
---|
1230 | size = capacity = 0;
|
---|
1231 |
|
---|
1232 | #else /* !VBOX_WITH_XPCOM */
|
---|
1233 |
|
---|
1234 | if (arr)
|
---|
1235 | {
|
---|
1236 | if (raw)
|
---|
1237 | {
|
---|
1238 | SafeArrayUnaccessData(arr);
|
---|
1239 | raw = NULL;
|
---|
1240 | }
|
---|
1241 |
|
---|
1242 | if (!isWeak)
|
---|
1243 | {
|
---|
1244 | HRESULT rc = SafeArrayDestroy(arr);
|
---|
1245 | AssertComRCReturnVoid(rc);
|
---|
1246 | }
|
---|
1247 | else
|
---|
1248 | isWeak = false;
|
---|
1249 |
|
---|
1250 | arr = NULL;
|
---|
1251 | }
|
---|
1252 |
|
---|
1253 | #endif /* !VBOX_WITH_XPCOM */
|
---|
1254 | }
|
---|
1255 |
|
---|
1256 | bool isWeak : 1;
|
---|
1257 |
|
---|
1258 | #ifdef VBOX_WITH_XPCOM
|
---|
1259 | PRUint32 capacity;
|
---|
1260 | PRUint32 size;
|
---|
1261 | T *arr;
|
---|
1262 | #else
|
---|
1263 | SAFEARRAY *arr;
|
---|
1264 | T *raw;
|
---|
1265 | #endif
|
---|
1266 | };
|
---|
1267 |
|
---|
1268 | Data m;
|
---|
1269 | };
|
---|
1270 |
|
---|
1271 | /* Few fast specializations for primitive array types */
|
---|
1272 | template<>
|
---|
1273 | inline void com::SafeArray<BYTE>::initFrom(const com::SafeArray<BYTE> & aRef)
|
---|
1274 | {
|
---|
1275 | size_t sSize = aRef.size();
|
---|
1276 | resize(sSize);
|
---|
1277 | ::memcpy(raw(), aRef.raw(), sSize);
|
---|
1278 | }
|
---|
1279 | template<>
|
---|
1280 | inline void com::SafeArray<BYTE>::initFrom(const BYTE* aPtr, size_t aSize)
|
---|
1281 | {
|
---|
1282 | resize(aSize);
|
---|
1283 | ::memcpy(raw(), aPtr, aSize);
|
---|
1284 | }
|
---|
1285 |
|
---|
1286 |
|
---|
1287 | template<>
|
---|
1288 | inline void com::SafeArray<SHORT>::initFrom(const com::SafeArray<SHORT> & aRef)
|
---|
1289 | {
|
---|
1290 | size_t sSize = aRef.size();
|
---|
1291 | resize(sSize);
|
---|
1292 | ::memcpy(raw(), aRef.raw(), sSize * sizeof(SHORT));
|
---|
1293 | }
|
---|
1294 | template<>
|
---|
1295 | inline void com::SafeArray<SHORT>::initFrom(const SHORT* aPtr, size_t aSize)
|
---|
1296 | {
|
---|
1297 | resize(aSize);
|
---|
1298 | ::memcpy(raw(), aPtr, aSize * sizeof(SHORT));
|
---|
1299 | }
|
---|
1300 |
|
---|
1301 | template<>
|
---|
1302 | inline void com::SafeArray<USHORT>::initFrom(const com::SafeArray<USHORT> & aRef)
|
---|
1303 | {
|
---|
1304 | size_t sSize = aRef.size();
|
---|
1305 | resize(sSize);
|
---|
1306 | ::memcpy(raw(), aRef.raw(), sSize * sizeof(USHORT));
|
---|
1307 | }
|
---|
1308 | template<>
|
---|
1309 | inline void com::SafeArray<USHORT>::initFrom(const USHORT* aPtr, size_t aSize)
|
---|
1310 | {
|
---|
1311 | resize(aSize);
|
---|
1312 | ::memcpy(raw(), aPtr, aSize * sizeof(USHORT));
|
---|
1313 | }
|
---|
1314 |
|
---|
1315 | template<>
|
---|
1316 | inline void com::SafeArray<LONG>::initFrom(const com::SafeArray<LONG> & aRef)
|
---|
1317 | {
|
---|
1318 | size_t sSize = aRef.size();
|
---|
1319 | resize(sSize);
|
---|
1320 | ::memcpy(raw(), aRef.raw(), sSize * sizeof(LONG));
|
---|
1321 | }
|
---|
1322 | template<>
|
---|
1323 | inline void com::SafeArray<LONG>::initFrom(const LONG* aPtr, size_t aSize)
|
---|
1324 | {
|
---|
1325 | resize(aSize);
|
---|
1326 | ::memcpy(raw(), aPtr, aSize * sizeof(LONG));
|
---|
1327 | }
|
---|
1328 |
|
---|
1329 |
|
---|
1330 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1331 |
|
---|
1332 | #ifdef VBOX_WITH_XPCOM
|
---|
1333 |
|
---|
1334 | /**
|
---|
1335 | * Version of com::SafeArray for arrays of GUID.
|
---|
1336 | *
|
---|
1337 | * In MS COM, GUID arrays store GUIDs by value and therefore input arrays are
|
---|
1338 | * represented using |GUID *| and out arrays -- using |GUID **|. In XPCOM,
|
---|
1339 | * GUID arrays store pointers to nsID so that input arrays are |const nsID **|
|
---|
1340 | * and out arrays are |nsID ***|. Due to this difference, it is impossible to
|
---|
1341 | * work with arrays of GUID on both platforms by simply using com::SafeArray
|
---|
1342 | * <GUID>. This class is intended to provide some level of cross-platform
|
---|
1343 | * behavior.
|
---|
1344 | *
|
---|
1345 | * The basic usage pattern is basically similar to com::SafeArray<> except that
|
---|
1346 | * you use ComSafeGUIDArrayIn* and ComSafeGUIDArrayOut* macros instead of
|
---|
1347 | * ComSafeArrayIn* and ComSafeArrayOut*. Another important nuance is that the
|
---|
1348 | * raw() array type is different (nsID **, or GUID ** on XPCOM and GUID * on MS
|
---|
1349 | * COM) so it is recommended to use operator[] instead which always returns a
|
---|
1350 | * GUID by value.
|
---|
1351 | *
|
---|
1352 | * Note that due to const modifiers, you cannot use SafeGUIDArray for input GUID
|
---|
1353 | * arrays. Please use SafeConstGUIDArray for this instead.
|
---|
1354 | *
|
---|
1355 | * Other than mentioned above, the functionality of this class is equivalent to
|
---|
1356 | * com::SafeArray<>. See the description of that template and its methods for
|
---|
1357 | * more information.
|
---|
1358 | *
|
---|
1359 | * Output GUID arrays are handled by a separate class, SafeGUIDArrayOut, since
|
---|
1360 | * this class cannot handle them because of const modifiers.
|
---|
1361 | */
|
---|
1362 | class SafeGUIDArray : public SafeArray<nsID *>
|
---|
1363 | {
|
---|
1364 | public:
|
---|
1365 |
|
---|
1366 | typedef SafeArray<nsID *> Base;
|
---|
1367 |
|
---|
1368 | class nsIDRef
|
---|
1369 | {
|
---|
1370 | public:
|
---|
1371 |
|
---|
1372 | nsIDRef(nsID * &aVal) : mVal(aVal) {}
|
---|
1373 |
|
---|
1374 | operator const nsID &() const { return mVal ? *mVal : *Empty; }
|
---|
1375 | operator nsID() const { return mVal ? *mVal : *Empty; }
|
---|
1376 |
|
---|
1377 | const nsID *operator&() const { return mVal ? mVal : Empty; }
|
---|
1378 |
|
---|
1379 | nsIDRef &operator= (const nsID &aThat)
|
---|
1380 | {
|
---|
1381 | if (mVal == NULL)
|
---|
1382 | Copy(&aThat, mVal);
|
---|
1383 | else
|
---|
1384 | *mVal = aThat;
|
---|
1385 | return *this;
|
---|
1386 | }
|
---|
1387 |
|
---|
1388 | private:
|
---|
1389 |
|
---|
1390 | nsID * &mVal;
|
---|
1391 |
|
---|
1392 | static const nsID *Empty;
|
---|
1393 |
|
---|
1394 | friend class SafeGUIDArray;
|
---|
1395 | };
|
---|
1396 |
|
---|
1397 | /** See SafeArray<>::SafeArray(). */
|
---|
1398 | SafeGUIDArray() {}
|
---|
1399 |
|
---|
1400 | /** See SafeArray<>::SafeArray(size_t). */
|
---|
1401 | SafeGUIDArray(size_t aSize) : Base(aSize) {}
|
---|
1402 |
|
---|
1403 | /**
|
---|
1404 | * Array access operator that returns an array element by reference. As a
|
---|
1405 | * special case, the return value of this operator on XPCOM is an nsID (GUID)
|
---|
1406 | * reference, instead of an nsID pointer (the actual SafeArray template
|
---|
1407 | * argument), for compatibility with the MS COM version.
|
---|
1408 | *
|
---|
1409 | * The rest is equivalent to SafeArray<>::operator[].
|
---|
1410 | */
|
---|
1411 | nsIDRef operator[] (size_t aIdx)
|
---|
1412 | {
|
---|
1413 | Assert(m.arr != NULL);
|
---|
1414 | Assert(aIdx < size());
|
---|
1415 | return nsIDRef(m.arr[aIdx]);
|
---|
1416 | }
|
---|
1417 |
|
---|
1418 | /**
|
---|
1419 | * Const version of #operator[] that returns an array element by value.
|
---|
1420 | */
|
---|
1421 | const nsID &operator[] (size_t aIdx) const
|
---|
1422 | {
|
---|
1423 | Assert(m.arr != NULL);
|
---|
1424 | Assert(aIdx < size());
|
---|
1425 | return m.arr[aIdx] ? *m.arr[aIdx] : *nsIDRef::Empty;
|
---|
1426 | }
|
---|
1427 | };
|
---|
1428 |
|
---|
1429 | /**
|
---|
1430 | * Version of com::SafeArray for const arrays of GUID.
|
---|
1431 | *
|
---|
1432 | * This class is used to work with input GUID array parameters in method
|
---|
1433 | * implementations. See SafeGUIDArray for more details.
|
---|
1434 | */
|
---|
1435 | class SafeConstGUIDArray : public SafeArray<const nsID *,
|
---|
1436 | SafeArrayTraits<nsID *> >
|
---|
1437 | {
|
---|
1438 | public:
|
---|
1439 |
|
---|
1440 | typedef SafeArray<const nsID *, SafeArrayTraits<nsID *> > Base;
|
---|
1441 |
|
---|
1442 | /** See SafeArray<>::SafeArray(). */
|
---|
1443 | SafeConstGUIDArray() {}
|
---|
1444 |
|
---|
1445 | /* See SafeArray<>::SafeArray(ComSafeArrayIn(T, aArg)). */
|
---|
1446 | SafeConstGUIDArray(ComSafeGUIDArrayIn(aArg))
|
---|
1447 | : Base(ComSafeGUIDArrayInArg(aArg)) {}
|
---|
1448 |
|
---|
1449 | /**
|
---|
1450 | * Array access operator that returns an array element by reference. As a
|
---|
1451 | * special case, the return value of this operator on XPCOM is nsID (GUID)
|
---|
1452 | * instead of nsID *, for compatibility with the MS COM version.
|
---|
1453 | *
|
---|
1454 | * The rest is equivalent to SafeArray<>::operator[].
|
---|
1455 | */
|
---|
1456 | const nsID &operator[] (size_t aIdx) const
|
---|
1457 | {
|
---|
1458 | AssertReturn(m.arr != NULL, **((const nsID * *)1));
|
---|
1459 | AssertReturn(aIdx < size(), **((const nsID * *)1));
|
---|
1460 | return *m.arr[aIdx];
|
---|
1461 | }
|
---|
1462 |
|
---|
1463 | private:
|
---|
1464 |
|
---|
1465 | /* These are disabled because of const. */
|
---|
1466 | bool reset(size_t aNewSize) { NOREF(aNewSize); return false; }
|
---|
1467 | };
|
---|
1468 |
|
---|
1469 | #else /* !VBOX_WITH_XPCOM */
|
---|
1470 |
|
---|
1471 | typedef SafeArray<GUID> SafeGUIDArray;
|
---|
1472 | typedef SafeArray<const GUID, SafeArrayTraits<GUID> > SafeConstGUIDArray;
|
---|
1473 |
|
---|
1474 | #endif /* !VBOX_WITH_XPCOM */
|
---|
1475 |
|
---|
1476 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1477 |
|
---|
1478 | #ifdef VBOX_WITH_XPCOM
|
---|
1479 |
|
---|
1480 | template<class I>
|
---|
1481 | struct SafeIfaceArrayTraits
|
---|
1482 | {
|
---|
1483 | protected:
|
---|
1484 |
|
---|
1485 | static void Init(I * &aElem) { aElem = NULL; }
|
---|
1486 | static void Uninit(I * &aElem)
|
---|
1487 | {
|
---|
1488 | if (aElem)
|
---|
1489 | {
|
---|
1490 | aElem->Release();
|
---|
1491 | aElem = NULL;
|
---|
1492 | }
|
---|
1493 | }
|
---|
1494 |
|
---|
1495 | static void Copy(I * aFrom, I * &aTo)
|
---|
1496 | {
|
---|
1497 | if (aFrom != NULL)
|
---|
1498 | {
|
---|
1499 | aTo = aFrom;
|
---|
1500 | aTo->AddRef();
|
---|
1501 | }
|
---|
1502 | else
|
---|
1503 | aTo = NULL;
|
---|
1504 | }
|
---|
1505 |
|
---|
1506 | public:
|
---|
1507 |
|
---|
1508 | /* Magic to workaround strict rules of par. 4.4.4 of the C++ standard. */
|
---|
1509 | static I **__asInParam_Arr(I **aArr) { return aArr; }
|
---|
1510 | static I **__asInParam_Arr(const I **aArr) { return const_cast<I **>(aArr); }
|
---|
1511 | };
|
---|
1512 |
|
---|
1513 | #else /* !VBOX_WITH_XPCOM */
|
---|
1514 |
|
---|
1515 | template<class I>
|
---|
1516 | struct SafeIfaceArrayTraits
|
---|
1517 | {
|
---|
1518 | protected:
|
---|
1519 |
|
---|
1520 | static VARTYPE VarType() { return VT_DISPATCH; }
|
---|
1521 | static ULONG VarCount(size_t aSize) { return (ULONG)aSize; }
|
---|
1522 | static size_t Size(ULONG aVarCount) { return (size_t)aVarCount; }
|
---|
1523 |
|
---|
1524 | static void Copy(I * aFrom, I * &aTo)
|
---|
1525 | {
|
---|
1526 | if (aFrom != NULL)
|
---|
1527 | {
|
---|
1528 | aTo = aFrom;
|
---|
1529 | aTo->AddRef();
|
---|
1530 | }
|
---|
1531 | else
|
---|
1532 | aTo = NULL;
|
---|
1533 | }
|
---|
1534 |
|
---|
1535 | static SAFEARRAY *CreateSafeArray(VARTYPE aVarType, SAFEARRAYBOUND *aBound)
|
---|
1536 | {
|
---|
1537 | NOREF(aVarType);
|
---|
1538 | return SafeArrayCreateEx(VT_DISPATCH, 1, aBound, (PVOID)&COM_IIDOF(I));
|
---|
1539 | }
|
---|
1540 | };
|
---|
1541 |
|
---|
1542 | #endif /* !VBOX_WITH_XPCOM */
|
---|
1543 |
|
---|
1544 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1545 |
|
---|
1546 | /**
|
---|
1547 | * Version of com::SafeArray for arrays of interface pointers.
|
---|
1548 | *
|
---|
1549 | * Except that it manages arrays of interface pointers, the usage of this class
|
---|
1550 | * is identical to com::SafeArray.
|
---|
1551 | *
|
---|
1552 | * @param I Interface class (no asterisk).
|
---|
1553 | */
|
---|
1554 | template<class I>
|
---|
1555 | class SafeIfaceArray : public SafeArray<I *, SafeIfaceArrayTraits<I> >
|
---|
1556 | {
|
---|
1557 | public:
|
---|
1558 |
|
---|
1559 | typedef SafeArray<I *, SafeIfaceArrayTraits<I> > Base;
|
---|
1560 |
|
---|
1561 | /**
|
---|
1562 | * Creates a null array.
|
---|
1563 | */
|
---|
1564 | SafeIfaceArray() {}
|
---|
1565 |
|
---|
1566 | /**
|
---|
1567 | * Creates a new array of the given size. All elements of the newly created
|
---|
1568 | * array initialized with null values.
|
---|
1569 | *
|
---|
1570 | * @param aSize Initial number of elements in the array. Must be greater
|
---|
1571 | * than 0.
|
---|
1572 | *
|
---|
1573 | * @note If this object remains null after construction it means that there
|
---|
1574 | * was not enough memory for creating an array of the requested size.
|
---|
1575 | * The constructor will also assert in this case.
|
---|
1576 | */
|
---|
1577 | SafeIfaceArray(size_t aSize) { Base::resize(aSize); }
|
---|
1578 |
|
---|
1579 | /**
|
---|
1580 | * Weakly attaches this instance to the existing array passed in a method
|
---|
1581 | * parameter declared using the ComSafeArrayIn macro. When using this call,
|
---|
1582 | * always wrap the parameter name in the ComSafeArrayOutArg macro call like
|
---|
1583 | * this:
|
---|
1584 | * <pre>
|
---|
1585 | * SafeArray safeArray(ComSafeArrayInArg(aArg));
|
---|
1586 | * </pre>
|
---|
1587 | *
|
---|
1588 | * Note that this constructor doesn't take the ownership of the array. In
|
---|
1589 | * particular, this means that operations that operate on the ownership
|
---|
1590 | * (e.g. #detachTo()) are forbidden and will assert.
|
---|
1591 | *
|
---|
1592 | * @param aArg Input method parameter to attach to.
|
---|
1593 | */
|
---|
1594 | SafeIfaceArray(ComSafeArrayIn(I *, aArg))
|
---|
1595 | {
|
---|
1596 | if (aArg)
|
---|
1597 | {
|
---|
1598 | #ifdef VBOX_WITH_XPCOM
|
---|
1599 |
|
---|
1600 | Base::m.size = aArgSize;
|
---|
1601 | Base::m.arr = aArg;
|
---|
1602 | Base::m.isWeak = true;
|
---|
1603 |
|
---|
1604 | #else /* !VBOX_WITH_XPCOM */
|
---|
1605 |
|
---|
1606 | SAFEARRAY *arg = aArg;
|
---|
1607 |
|
---|
1608 | AssertReturnVoid(arg->cDims == 1);
|
---|
1609 |
|
---|
1610 | VARTYPE vt;
|
---|
1611 | HRESULT rc = SafeArrayGetVartype(arg, &vt);
|
---|
1612 | AssertComRCReturnVoid(rc);
|
---|
1613 | AssertMsgReturnVoid(vt == VT_UNKNOWN || vt == VT_DISPATCH,
|
---|
1614 | ("Expected vartype VT_UNKNOWN or VT_DISPATCH, got %d.\n",
|
---|
1615 | vt));
|
---|
1616 | GUID guid;
|
---|
1617 | rc = SafeArrayGetIID(arg, &guid);
|
---|
1618 | AssertComRCReturnVoid(rc);
|
---|
1619 | AssertMsgReturnVoid(InlineIsEqualGUID(COM_IIDOF(I), guid),
|
---|
1620 | ("Expected IID {%RTuuid}, got {%RTuuid}.\n",
|
---|
1621 | &COM_IIDOF(I), &guid));
|
---|
1622 |
|
---|
1623 | rc = SafeArrayAccessData(arg, (void HUGEP **)&m.raw);
|
---|
1624 | AssertComRCReturnVoid(rc);
|
---|
1625 |
|
---|
1626 | m.arr = arg;
|
---|
1627 | m.isWeak = true;
|
---|
1628 |
|
---|
1629 | #endif /* !VBOX_WITH_XPCOM */
|
---|
1630 | }
|
---|
1631 | }
|
---|
1632 |
|
---|
1633 | /**
|
---|
1634 | * Creates a deep copy of the given standard C++ container that stores
|
---|
1635 | * interface pointers as objects of the ComPtr\<I\> class.
|
---|
1636 | *
|
---|
1637 | * @param aCntr Container object to copy.
|
---|
1638 | *
|
---|
1639 | * @tparam C Standard C++ container template class (normally deduced from
|
---|
1640 | * @c aCntr).
|
---|
1641 | * @tparam A Standard C++ allocator class (deduced from @c aCntr).
|
---|
1642 | * @tparam OI Argument to the ComPtr template (deduced from @c aCntr).
|
---|
1643 | */
|
---|
1644 | template<template<typename, typename> class C, class A, class OI>
|
---|
1645 | SafeIfaceArray(const C<ComPtr<OI>, A> & aCntr)
|
---|
1646 | {
|
---|
1647 | typedef C<ComPtr<OI>, A> List;
|
---|
1648 |
|
---|
1649 | Base::resize(aCntr.size());
|
---|
1650 | AssertReturnVoid(!Base::isNull());
|
---|
1651 |
|
---|
1652 | int i = 0;
|
---|
1653 | for (typename List::const_iterator it = aCntr.begin();
|
---|
1654 | it != aCntr.end(); ++ it, ++ i)
|
---|
1655 | #ifdef VBOX_WITH_XPCOM
|
---|
1656 | this->Copy(*it, Base::m.arr[i]);
|
---|
1657 | #else
|
---|
1658 | Copy(*it, Base::m.raw[i]);
|
---|
1659 | #endif
|
---|
1660 | }
|
---|
1661 |
|
---|
1662 | /**
|
---|
1663 | * Creates a deep copy of the given standard C++ container that stores
|
---|
1664 | * interface pointers as objects of the ComObjPtr\<I\> class.
|
---|
1665 | *
|
---|
1666 | * @param aCntr Container object to copy.
|
---|
1667 | *
|
---|
1668 | * @tparam C Standard C++ container template class (normally deduced from
|
---|
1669 | * @c aCntr).
|
---|
1670 | * @tparam A Standard C++ allocator class (deduced from @c aCntr).
|
---|
1671 | * @tparam OI Argument to the ComObjPtr template (deduced from @c aCntr).
|
---|
1672 | */
|
---|
1673 | template<template<typename, typename> class C, class A, class OI>
|
---|
1674 | SafeIfaceArray(const C<ComObjPtr<OI>, A> & aCntr)
|
---|
1675 | {
|
---|
1676 | typedef C<ComObjPtr<OI>, A> List;
|
---|
1677 |
|
---|
1678 | Base::resize(aCntr.size());
|
---|
1679 | AssertReturnVoid(!Base::isNull());
|
---|
1680 |
|
---|
1681 | int i = 0;
|
---|
1682 | for (typename List::const_iterator it = aCntr.begin();
|
---|
1683 | it != aCntr.end(); ++ it, ++ i)
|
---|
1684 | #ifdef VBOX_WITH_XPCOM
|
---|
1685 | SafeIfaceArray::Copy(*it, Base::m.arr[i]);
|
---|
1686 | #else
|
---|
1687 | Copy(*it, Base::m.raw[i]);
|
---|
1688 | #endif
|
---|
1689 | }
|
---|
1690 |
|
---|
1691 | /**
|
---|
1692 | * Creates a deep copy of the given standard C++ map whose values are
|
---|
1693 | * interface pointers stored as objects of the ComPtr\<I\> class.
|
---|
1694 | *
|
---|
1695 | * @param aMap Map object to copy.
|
---|
1696 | *
|
---|
1697 | * @tparam C Standard C++ map template class (normally deduced from
|
---|
1698 | * @c aCntr).
|
---|
1699 | * @tparam L Standard C++ compare class (deduced from @c aCntr).
|
---|
1700 | * @tparam A Standard C++ allocator class (deduced from @c aCntr).
|
---|
1701 | * @tparam K Map key class (deduced from @c aCntr).
|
---|
1702 | * @tparam OI Argument to the ComPtr template (deduced from @c aCntr).
|
---|
1703 | */
|
---|
1704 | template<template<typename, typename, typename, typename>
|
---|
1705 | class C, class L, class A, class K, class OI>
|
---|
1706 | SafeIfaceArray(const C<K, ComPtr<OI>, L, A> & aMap)
|
---|
1707 | {
|
---|
1708 | typedef C<K, ComPtr<OI>, L, A> Map;
|
---|
1709 |
|
---|
1710 | Base::resize(aMap.size());
|
---|
1711 | AssertReturnVoid(!Base::isNull());
|
---|
1712 |
|
---|
1713 | int i = 0;
|
---|
1714 | for (typename Map::const_iterator it = aMap.begin();
|
---|
1715 | it != aMap.end(); ++ it, ++ i)
|
---|
1716 | #ifdef VBOX_WITH_XPCOM
|
---|
1717 | SafeIfaceArray::Copy(it->second, Base::m.arr[i]);
|
---|
1718 | #else
|
---|
1719 | Copy(it->second, Base::m.raw[i]);
|
---|
1720 | #endif
|
---|
1721 | }
|
---|
1722 |
|
---|
1723 | /**
|
---|
1724 | * Creates a deep copy of the given standard C++ map whose values are
|
---|
1725 | * interface pointers stored as objects of the ComObjPtr\<I\> class.
|
---|
1726 | *
|
---|
1727 | * @param aMap Map object to copy.
|
---|
1728 | *
|
---|
1729 | * @tparam C Standard C++ map template class (normally deduced from
|
---|
1730 | * @c aCntr).
|
---|
1731 | * @tparam L Standard C++ compare class (deduced from @c aCntr).
|
---|
1732 | * @tparam A Standard C++ allocator class (deduced from @c aCntr).
|
---|
1733 | * @tparam K Map key class (deduced from @c aCntr).
|
---|
1734 | * @tparam OI Argument to the ComObjPtr template (deduced from @c aCntr).
|
---|
1735 | */
|
---|
1736 | template<template<typename, typename, typename, typename>
|
---|
1737 | class C, class L, class A, class K, class OI>
|
---|
1738 | SafeIfaceArray(const C<K, ComObjPtr<OI>, L, A> & aMap)
|
---|
1739 | {
|
---|
1740 | typedef C<K, ComObjPtr<OI>, L, A> Map;
|
---|
1741 |
|
---|
1742 | Base::resize(aMap.size());
|
---|
1743 | AssertReturnVoid(!Base::isNull());
|
---|
1744 |
|
---|
1745 | int i = 0;
|
---|
1746 | for (typename Map::const_iterator it = aMap.begin();
|
---|
1747 | it != aMap.end(); ++ it, ++ i)
|
---|
1748 | #ifdef VBOX_WITH_XPCOM
|
---|
1749 | SafeIfaceArray::Copy(it->second, Base::m.arr[i]);
|
---|
1750 | #else
|
---|
1751 | Copy(it->second, Base::m.raw[i]);
|
---|
1752 | #endif
|
---|
1753 | }
|
---|
1754 |
|
---|
1755 | void setElement(size_t iIdx, I* obj)
|
---|
1756 | {
|
---|
1757 | #ifdef VBOX_WITH_XPCOM
|
---|
1758 | SafeIfaceArray::Copy(obj, Base::m.arr[iIdx]);
|
---|
1759 | #else
|
---|
1760 | Copy(obj, Base::m.raw[iIdx]);
|
---|
1761 | #endif
|
---|
1762 | }
|
---|
1763 | };
|
---|
1764 |
|
---|
1765 | } /* namespace com */
|
---|
1766 |
|
---|
1767 | /** @} */
|
---|
1768 |
|
---|
1769 | #endif /* !___VBox_com_array_h */
|
---|
1770 |
|
---|