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