1 | /** @file
|
---|
2 | * MS COM / XPCOM Abstraction Layer:
|
---|
3 | * Safe array helper class declaration
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | *
|
---|
26 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 | #ifndef ___VBox_com_array_h
|
---|
32 | #define ___VBox_com_array_h
|
---|
33 |
|
---|
34 | #include <VBox/com/ptr.h>
|
---|
35 |
|
---|
36 | /** @defgroup grp_COM_arrays COM/XPCOM Arrays
|
---|
37 | * @{
|
---|
38 | *
|
---|
39 | * The COM/XPCOM array support layer provides a cross-platform way to pass
|
---|
40 | * arrays to and from COM interface methods and consists of the com::SafeArray
|
---|
41 | * template and a set of ComSafeArray* macros part of which is defined in
|
---|
42 | * VBox/com/defs.h.
|
---|
43 | *
|
---|
44 | * This layer works with interface attributes and method parameters that have
|
---|
45 | * the 'safearray="yes"' attribute in the XIDL definition:
|
---|
46 | * @code
|
---|
47 |
|
---|
48 | <interface name="ISomething" ...>
|
---|
49 |
|
---|
50 | <method name="testArrays">
|
---|
51 | <param name="inArr" type="long" dir="in" safearray="yes"/>
|
---|
52 | <param name="outArr" type="long" dir="out" safearray="yes"/>
|
---|
53 | <param name="retArr" type="long" dir="return" safearray="yes"/>
|
---|
54 | </method>
|
---|
55 |
|
---|
56 | </interface>
|
---|
57 |
|
---|
58 | * @endcode
|
---|
59 | *
|
---|
60 | * Methods generated from this and similar definitions are implemented in
|
---|
61 | * component classes using the following declarations:
|
---|
62 | * @code
|
---|
63 |
|
---|
64 | STDMETHOD(TestArrays) (ComSafeArrayIn (LONG, aIn),
|
---|
65 | ComSafeArrayOut (LONG, aOut),
|
---|
66 | ComSafeArrayOut (LONG, aRet));
|
---|
67 |
|
---|
68 | * @endcode
|
---|
69 | *
|
---|
70 | * And the following function bodies:
|
---|
71 | * @code
|
---|
72 |
|
---|
73 | STDMETHODIMP Component::TestArrays (ComSafeArrayIn (LONG, aIn),
|
---|
74 | ComSafeArrayOut (LONG, aOut),
|
---|
75 | ComSafeArrayOut (LONG, aRet))
|
---|
76 | {
|
---|
77 | if (ComSafeArrayInIsNull (aIn))
|
---|
78 | return E_INVALIDARG;
|
---|
79 | if (ComSafeArrayOutIsNull (aOut))
|
---|
80 | return E_POINTER;
|
---|
81 | if (ComSafeArrayOutIsNull (aRet))
|
---|
82 | return E_POINTER;
|
---|
83 |
|
---|
84 | // Use SafeArray to access the input array parameter
|
---|
85 |
|
---|
86 | com::SafeArray <LONG> in (ComSafeArrayInArg (aIn));
|
---|
87 |
|
---|
88 | for (size_t i = 0; i < in.size(); ++ i)
|
---|
89 | LogFlow (("*** in[%u]=%d\n", i, in [i]));
|
---|
90 |
|
---|
91 | // Use SafeArray to create the return array (the same technique is used
|
---|
92 | // for output array paramters)
|
---|
93 |
|
---|
94 | SafeArray <LONG> ret (in.size() * 2);
|
---|
95 | for (size_t i = 0; i < in.size(); ++ i)
|
---|
96 | {
|
---|
97 | ret [i] = in [i];
|
---|
98 | ret [i + in.size()] = in [i] * 10;
|
---|
99 | }
|
---|
100 |
|
---|
101 | ret.detachTo (ComSafeArrayOutArg (aRet));
|
---|
102 |
|
---|
103 | return S_OK;
|
---|
104 | }
|
---|
105 |
|
---|
106 | * @endcode
|
---|
107 | *
|
---|
108 | * Such methods can be called from the client code using the following pattern:
|
---|
109 | * @code
|
---|
110 |
|
---|
111 | ComPtr <ISomething> component;
|
---|
112 |
|
---|
113 | // ...
|
---|
114 |
|
---|
115 | com::SafeArray <LONG> in (3);
|
---|
116 | in [0] = -1;
|
---|
117 | in [1] = -2;
|
---|
118 | in [2] = -3;
|
---|
119 |
|
---|
120 | com::SafeArray <LONG> out;
|
---|
121 | com::SafeArray <LONG> ret;
|
---|
122 |
|
---|
123 | HRESULT rc = component->TestArrays (ComSafeArrayAsInParam (in),
|
---|
124 | ComSafeArrayAsOutParam (out),
|
---|
125 | ComSafeArrayAsOutParam (ret));
|
---|
126 |
|
---|
127 | if (SUCCEEDED (rc))
|
---|
128 | for (size_t i = 0; i < ret.size(); ++ i)
|
---|
129 | printf ("*** ret[%u]=%d\n", i, ret [i]);
|
---|
130 |
|
---|
131 | * @endcode
|
---|
132 | *
|
---|
133 | * For interoperability with standard C++ containers, there is a template
|
---|
134 | * constructor that takes such a container as argument and performs a deep copy
|
---|
135 | * of its contents. This can be used in method implementations like this:
|
---|
136 | * @code
|
---|
137 |
|
---|
138 | STDMETHODIMP Component::COMGETTER(Values) (ComSafeArrayOut (int, aValues))
|
---|
139 | {
|
---|
140 | // ... assume there is a |std::list <int> mValues| data member
|
---|
141 |
|
---|
142 | com::SafeArray <int> values (mValues);
|
---|
143 | values.detachTo (ComSafeArrayOutArg (aValues));
|
---|
144 |
|
---|
145 | return S_OK;
|
---|
146 | }
|
---|
147 |
|
---|
148 | * @endcode
|
---|
149 | *
|
---|
150 | * The current implementation of the SafeArray layer supports all types normally
|
---|
151 | * allowed in XIDL as array element types (including 'wstring' and 'uuid').
|
---|
152 | * However, 'pointer-to-...' types (e.g. 'long *', 'wstring *') are not
|
---|
153 | * supported and therefore cannot be used as element types.
|
---|
154 | *
|
---|
155 | * Note that for GUID arrays you should use SafeGUIDArray and
|
---|
156 | * SafeConstGUIDArray, customized SafeArray<> specializations.
|
---|
157 | *
|
---|
158 | * Also note that in order to pass input BSTR array parameters delcared
|
---|
159 | * using the ComSafeArrayIn (INPTR BSTR, aParam) macro to the SafeArray<>
|
---|
160 | * constructor using the ComSafeArrayInArg() macro, you should use INPTR BSTR
|
---|
161 | * as the SafeArray<> template argument, not just BSTR.
|
---|
162 | *
|
---|
163 | * Arrays of interface pointers are also supported but they require to use a
|
---|
164 | * special SafeArray implementation, com::SafeIfacePointer, which takes the
|
---|
165 | * interface class name as a template argument (e.g. com::SafeIfacePointer
|
---|
166 | * <IUnknown>). This implementation functions identically to com::SafeArray.
|
---|
167 | */
|
---|
168 |
|
---|
169 | #if defined (VBOX_WITH_XPCOM)
|
---|
170 | #include <nsMemory.h>
|
---|
171 | #endif
|
---|
172 |
|
---|
173 | #include "VBox/com/defs.h"
|
---|
174 | #include "VBox/com/assert.h"
|
---|
175 |
|
---|
176 | #include "iprt/cpputils.h"
|
---|
177 |
|
---|
178 | #if defined (VBOX_WITH_XPCOM)
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Wraps the given com::SafeArray instance to generate an expression that is
|
---|
182 | * suitable for passing it to functions that take input safearray parameters
|
---|
183 | * declared using the ComSafeArrayIn marco.
|
---|
184 | *
|
---|
185 | * @param aArray com::SafeArray instance to pass as an input parameter.
|
---|
186 | */
|
---|
187 | #define ComSafeArrayAsInParam(aArray) \
|
---|
188 | (aArray).size(), (aArray).__asInParam_Arr ((aArray).raw())
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * Wraps the given com::SafeArray instance to generate an expression that is
|
---|
192 | * suitable for passing it to functions that take output safearray parameters
|
---|
193 | * declared using the ComSafeArrayOut marco.
|
---|
194 | *
|
---|
195 | * @param aArray com::SafeArray instance to pass as an output parameter.
|
---|
196 | */
|
---|
197 | #define ComSafeArrayAsOutParam(aArray) \
|
---|
198 | (aArray).__asOutParam_Size(), (aArray).__asOutParam_Arr()
|
---|
199 |
|
---|
200 | #else /* defined (VBOX_WITH_XPCOM) */
|
---|
201 |
|
---|
202 | #define ComSafeArrayAsInParam(aArray) (aArray).__asInParam()
|
---|
203 |
|
---|
204 | #define ComSafeArrayAsOutParam(aArray) (aArray).__asOutParam()
|
---|
205 |
|
---|
206 | #endif /* defined (VBOX_WITH_XPCOM) */
|
---|
207 |
|
---|
208 | /**
|
---|
209 | *
|
---|
210 | */
|
---|
211 | namespace com
|
---|
212 | {
|
---|
213 |
|
---|
214 | #if defined (VBOX_WITH_XPCOM)
|
---|
215 |
|
---|
216 | ////////////////////////////////////////////////////////////////////////////////
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * Provides various helpers for SafeArray.
|
---|
220 | *
|
---|
221 | * @param T Type of array elements.
|
---|
222 | */
|
---|
223 | template <typename T>
|
---|
224 | struct SafeArrayTraits
|
---|
225 | {
|
---|
226 | protected:
|
---|
227 |
|
---|
228 | /** Initializes memory for aElem. */
|
---|
229 | static void Init (T &aElem) { aElem = 0; }
|
---|
230 |
|
---|
231 | /** Initializes memory occupied by aElem. */
|
---|
232 | static void Uninit (T &aElem) { aElem = 0; }
|
---|
233 |
|
---|
234 | /** Creates a deep copy of aFrom and stores it in aTo. */
|
---|
235 | static void Copy (const T &aFrom, T &aTo) { aTo = aFrom; }
|
---|
236 |
|
---|
237 | public:
|
---|
238 |
|
---|
239 | /* Magic to workaround strict rules of par. 4.4.4 of the C++ standard (that
|
---|
240 | * in particular forbid casts of 'char **' to 'const char **'). Then initial
|
---|
241 | * reason for this magic is that XPIDL declares input strings
|
---|
242 | * (char/PRUnichar pointers) as const but doesn't do so for pointers to
|
---|
243 | * arrays. */
|
---|
244 | static T *__asInParam_Arr (T *aArr) { return aArr; }
|
---|
245 | static T *__asInParam_Arr (const T *aArr) { return const_cast <T *> (aArr); }
|
---|
246 | };
|
---|
247 |
|
---|
248 | template <typename T>
|
---|
249 | struct SafeArrayTraits <T *>
|
---|
250 | {
|
---|
251 | // Arbitrary pointers are not supported
|
---|
252 | };
|
---|
253 |
|
---|
254 | template<>
|
---|
255 | struct SafeArrayTraits <PRUnichar *>
|
---|
256 | {
|
---|
257 | protected:
|
---|
258 |
|
---|
259 | static void Init (PRUnichar * &aElem) { aElem = NULL; }
|
---|
260 |
|
---|
261 | static void Uninit (PRUnichar * &aElem)
|
---|
262 | {
|
---|
263 | if (aElem)
|
---|
264 | {
|
---|
265 | ::SysFreeString (aElem);
|
---|
266 | aElem = NULL;
|
---|
267 | }
|
---|
268 | }
|
---|
269 |
|
---|
270 | static void Copy (const PRUnichar * aFrom, PRUnichar * &aTo)
|
---|
271 | {
|
---|
272 | AssertCompile (sizeof (PRUnichar) == sizeof (OLECHAR));
|
---|
273 | aTo = aFrom ? ::SysAllocString ((const OLECHAR *) aFrom) : NULL;
|
---|
274 | }
|
---|
275 |
|
---|
276 | public:
|
---|
277 |
|
---|
278 | /* Magic to workaround strict rules of par. 4.4.4 of the C++ standard */
|
---|
279 | static const PRUnichar **__asInParam_Arr (PRUnichar **aArr)
|
---|
280 | {
|
---|
281 | return const_cast <const PRUnichar **> (aArr);
|
---|
282 | }
|
---|
283 | static const PRUnichar **__asInParam_Arr (const PRUnichar **aArr) { return aArr; }
|
---|
284 | };
|
---|
285 |
|
---|
286 | template<>
|
---|
287 | struct SafeArrayTraits <const PRUnichar *>
|
---|
288 | {
|
---|
289 | protected:
|
---|
290 |
|
---|
291 | static void Init (const PRUnichar * &aElem) { aElem = NULL; }
|
---|
292 | static void Uninit (const PRUnichar * &aElem)
|
---|
293 | {
|
---|
294 | if (aElem)
|
---|
295 | {
|
---|
296 | ::SysFreeString (const_cast <PRUnichar *> (aElem));
|
---|
297 | aElem = NULL;
|
---|
298 | }
|
---|
299 | }
|
---|
300 |
|
---|
301 | static void Copy (const PRUnichar * aFrom, const PRUnichar * &aTo)
|
---|
302 | {
|
---|
303 | AssertCompile (sizeof (PRUnichar) == sizeof (OLECHAR));
|
---|
304 | aTo = aFrom ? ::SysAllocString ((const OLECHAR *) aFrom) : NULL;
|
---|
305 | }
|
---|
306 |
|
---|
307 | public:
|
---|
308 |
|
---|
309 | /* Magic to workaround strict rules of par. 4.4.4 of the C++ standard */
|
---|
310 | static const PRUnichar **__asInParam_Arr (const PRUnichar **aArr) { return aArr; }
|
---|
311 | };
|
---|
312 |
|
---|
313 | template<>
|
---|
314 | struct SafeArrayTraits <nsID *>
|
---|
315 | {
|
---|
316 | protected:
|
---|
317 |
|
---|
318 | static void Init (nsID * &aElem) { aElem = NULL; }
|
---|
319 |
|
---|
320 | static void Uninit (nsID * &aElem)
|
---|
321 | {
|
---|
322 | if (aElem)
|
---|
323 | {
|
---|
324 | ::nsMemory::Free (aElem);
|
---|
325 | aElem = NULL;
|
---|
326 | }
|
---|
327 | }
|
---|
328 |
|
---|
329 | static void Copy (const nsID * aFrom, nsID * &aTo)
|
---|
330 | {
|
---|
331 | if (aFrom)
|
---|
332 | {
|
---|
333 | aTo = (nsID *) ::nsMemory::Alloc (sizeof (nsID));
|
---|
334 | if (aTo)
|
---|
335 | *aTo = *aFrom;
|
---|
336 | }
|
---|
337 | else
|
---|
338 | aTo = NULL;
|
---|
339 | }
|
---|
340 |
|
---|
341 | /* This specification is also reused for SafeConstGUIDArray, so provide a
|
---|
342 | * no-op Init() and Uninit() which are necessary for SafeArray<> but should
|
---|
343 | * be never called in context of SafeConstGUIDArray. */
|
---|
344 |
|
---|
345 | static void Init (const nsID * &aElem) { NOREF (aElem); AssertFailed(); }
|
---|
346 | static void Uninit (const nsID * &aElem) { NOREF (aElem); AssertFailed(); }
|
---|
347 |
|
---|
348 | public:
|
---|
349 |
|
---|
350 | /** Magic to workaround strict rules of par. 4.4.4 of the C++ standard. */
|
---|
351 | static const nsID **__asInParam_Arr (nsID **aArr)
|
---|
352 | {
|
---|
353 | return const_cast <const nsID **> (aArr);
|
---|
354 | }
|
---|
355 | static const nsID **__asInParam_Arr (const nsID **aArr) { return aArr; }
|
---|
356 | };
|
---|
357 |
|
---|
358 | #else /* defined (VBOX_WITH_XPCOM) */
|
---|
359 |
|
---|
360 | ////////////////////////////////////////////////////////////////////////////////
|
---|
361 |
|
---|
362 | /**
|
---|
363 | * Provides various helpers for SafeArray.
|
---|
364 | *
|
---|
365 | * @param T Type of array elements.
|
---|
366 | *
|
---|
367 | * Specializations of this template must provide the following methods:
|
---|
368 | *
|
---|
369 | // Returns the VARTYPE of COM SafeArray elements to be used for T
|
---|
370 | static VARTYPE VarType();
|
---|
371 |
|
---|
372 | // Returns the number of VarType() elements necessary for aSize
|
---|
373 | // elements of T
|
---|
374 | static ULONG VarCount (size_t aSize);
|
---|
375 |
|
---|
376 | // Returns the number of elements of T that occupy the given number of
|
---|
377 | // VarType() elements (opposite to VarCount (size_t aSize)).
|
---|
378 | static size_t Size (ULONG aVarCount);
|
---|
379 |
|
---|
380 | // Creates a deep copy of aFrom and stores it in aTo
|
---|
381 | static void Copy (ULONG aFrom, ULONG &aTo);
|
---|
382 | */
|
---|
383 | template <typename T>
|
---|
384 | struct SafeArrayTraits
|
---|
385 | {
|
---|
386 | // Arbitrary types are not supported -- no helpers
|
---|
387 | };
|
---|
388 |
|
---|
389 | template<>
|
---|
390 | struct SafeArrayTraits <LONG>
|
---|
391 | {
|
---|
392 | protected:
|
---|
393 |
|
---|
394 | static VARTYPE VarType() { return VT_I4; }
|
---|
395 | static ULONG VarCount (size_t aSize) { return (ULONG) aSize; }
|
---|
396 | static size_t Size (ULONG aVarCount) { return (size_t) aVarCount; }
|
---|
397 |
|
---|
398 | static void Copy (LONG aFrom, LONG &aTo) { aTo = aFrom; }
|
---|
399 | };
|
---|
400 |
|
---|
401 | template<>
|
---|
402 | struct SafeArrayTraits <ULONG>
|
---|
403 | {
|
---|
404 | protected:
|
---|
405 |
|
---|
406 | static VARTYPE VarType() { return VT_UI4; }
|
---|
407 | static ULONG VarCount (size_t aSize) { return (ULONG) aSize; }
|
---|
408 | static size_t Size (ULONG aVarCount) { return (size_t) aVarCount; }
|
---|
409 |
|
---|
410 | static void Copy (ULONG aFrom, ULONG &aTo) { aTo = aFrom; }
|
---|
411 | };
|
---|
412 |
|
---|
413 | template<>
|
---|
414 | struct SafeArrayTraits <LONG64>
|
---|
415 | {
|
---|
416 | protected:
|
---|
417 |
|
---|
418 | static VARTYPE VarType() { return VT_I8; }
|
---|
419 | static ULONG VarCount (size_t aSize) { return (ULONG) aSize; }
|
---|
420 | static size_t Size (ULONG aVarCount) { return (size_t) aVarCount; }
|
---|
421 |
|
---|
422 | static void Copy (LONG64 aFrom, LONG64 &aTo) { aTo = aFrom; }
|
---|
423 | };
|
---|
424 |
|
---|
425 | template<>
|
---|
426 | struct SafeArrayTraits <ULONG64>
|
---|
427 | {
|
---|
428 | protected:
|
---|
429 |
|
---|
430 | static VARTYPE VarType() { return VT_UI8; }
|
---|
431 | static ULONG VarCount (size_t aSize) { return (ULONG) aSize; }
|
---|
432 | static size_t Size (ULONG aVarCount) { return (size_t) aVarCount; }
|
---|
433 |
|
---|
434 | static void Copy (ULONG64 aFrom, ULONG64 &aTo) { aTo = aFrom; }
|
---|
435 | };
|
---|
436 |
|
---|
437 | template<>
|
---|
438 | struct SafeArrayTraits <BSTR>
|
---|
439 | {
|
---|
440 | protected:
|
---|
441 |
|
---|
442 | static VARTYPE VarType() { return VT_BSTR; }
|
---|
443 | static ULONG VarCount (size_t aSize) { return (ULONG) aSize; }
|
---|
444 | static size_t Size (ULONG aVarCount) { return (size_t) aVarCount; }
|
---|
445 |
|
---|
446 | static void Copy (BSTR aFrom, BSTR &aTo)
|
---|
447 | {
|
---|
448 | aTo = aFrom ? ::SysAllocString ((const OLECHAR *) aFrom) : NULL;
|
---|
449 | }
|
---|
450 | };
|
---|
451 |
|
---|
452 | template<>
|
---|
453 | struct SafeArrayTraits <GUID>
|
---|
454 | {
|
---|
455 | protected:
|
---|
456 |
|
---|
457 | /* Use the 64-bit unsigned integer type for GUID */
|
---|
458 | static VARTYPE VarType() { return VT_UI8; }
|
---|
459 |
|
---|
460 | /* GUID is 128 bit, so we need two VT_UI8 */
|
---|
461 | static ULONG VarCount (size_t aSize)
|
---|
462 | {
|
---|
463 | AssertCompileSize (GUID, 16);
|
---|
464 | return (ULONG) (aSize * 2);
|
---|
465 | }
|
---|
466 |
|
---|
467 | static size_t Size (ULONG aVarCount) { return (size_t) aVarCount / 2; }
|
---|
468 |
|
---|
469 | static void Copy (GUID aFrom, GUID &aTo) { aTo = aFrom; }
|
---|
470 | };
|
---|
471 |
|
---|
472 | #endif /* defined (VBOX_WITH_XPCOM) */
|
---|
473 |
|
---|
474 | ////////////////////////////////////////////////////////////////////////////////
|
---|
475 |
|
---|
476 | /**
|
---|
477 | * The SafeArray class represents the safe array type used in COM to pass arrays
|
---|
478 | * to/from interface methods.
|
---|
479 | *
|
---|
480 | * This helper class hides all MSCOM/XPCOM specific implementation details and,
|
---|
481 | * together with ComSafeArrayIn, ComSafeArrayOut and ComSafeArrayRet macros,
|
---|
482 | * provides a platform-neutral way to handle safe arrays in the method
|
---|
483 | * implementation.
|
---|
484 | *
|
---|
485 | * When an instance of this class is destroyed, it automatically frees all
|
---|
486 | * resources occupied by individual elements of the array as well as by the
|
---|
487 | * array itself. However, when the value of an element is manually changed
|
---|
488 | * using #operator[] or by acessing array data through the #raw() pointer, it is
|
---|
489 | * the caller's responsibility to free resources occupied by the previous
|
---|
490 | * element's value.
|
---|
491 | *
|
---|
492 | * Also, objects of this class do not support copy and assignment operations and
|
---|
493 | * therefore cannot be returned from functions by value. In other words, this
|
---|
494 | * class is just a temporary storage for handling interface method calls and not
|
---|
495 | * intended to be used to store arrays as data members and such -- you should
|
---|
496 | * use normal list/vector classes for that.
|
---|
497 | *
|
---|
498 | * @note The current implementation supports only one-dimentional arrays.
|
---|
499 | *
|
---|
500 | * @note This class is not thread-safe.
|
---|
501 | */
|
---|
502 | template <typename T, class Traits = SafeArrayTraits <T> >
|
---|
503 | class SafeArray : public Traits
|
---|
504 | {
|
---|
505 | public:
|
---|
506 |
|
---|
507 | /**
|
---|
508 | * Creates a null array.
|
---|
509 | */
|
---|
510 | SafeArray() {}
|
---|
511 |
|
---|
512 | /**
|
---|
513 | * Creates a new array of the given size. All elements of the newly created
|
---|
514 | * array initialized with null values.
|
---|
515 | *
|
---|
516 | * @param aSize Initial number of elements in the array. Must be greater
|
---|
517 | * than 0.
|
---|
518 | *
|
---|
519 | * @note If this object remains null after construction it means that there
|
---|
520 | * was not enough memory for creating an array of the requested size.
|
---|
521 | * The constructor will also assert in this case.
|
---|
522 | */
|
---|
523 | SafeArray (size_t aSize) { reset (aSize); }
|
---|
524 |
|
---|
525 | /**
|
---|
526 | * Weakly attaches this instance to the existing array passed in a method
|
---|
527 | * parameter declared using the ComSafeArrayIn macro. When using this call,
|
---|
528 | * always wrap the parameter name in the ComSafeArrayInArg macro call like
|
---|
529 | * this:
|
---|
530 | * <pre>
|
---|
531 | * SafeArray safeArray (ComSafeArrayInArg (aArg));
|
---|
532 | * </pre>
|
---|
533 | *
|
---|
534 | * Note that this constructor doesn't take the ownership of the array. In
|
---|
535 | * particular, it means that operations that operate on the ownership (e.g.
|
---|
536 | * #detachTo()) are forbidden and will assert.
|
---|
537 | *
|
---|
538 | * @param aArg Input method parameter to attach to.
|
---|
539 | */
|
---|
540 | SafeArray (ComSafeArrayIn (T, aArg))
|
---|
541 | {
|
---|
542 | #if defined (VBOX_WITH_XPCOM)
|
---|
543 |
|
---|
544 | AssertReturnVoid (aArg != NULL);
|
---|
545 |
|
---|
546 | m.size = aArgSize;
|
---|
547 | m.arr = aArg;
|
---|
548 | m.isWeak = true;
|
---|
549 |
|
---|
550 | #else /* defined (VBOX_WITH_XPCOM) */
|
---|
551 |
|
---|
552 | AssertReturnVoid (aArg != NULL);
|
---|
553 | SAFEARRAY *arg = *aArg;
|
---|
554 |
|
---|
555 | if (arg)
|
---|
556 | {
|
---|
557 | AssertReturnVoid (arg->cDims == 1);
|
---|
558 |
|
---|
559 | VARTYPE vt;
|
---|
560 | HRESULT rc = SafeArrayGetVartype (arg, &vt);
|
---|
561 | AssertComRCReturnVoid (rc);
|
---|
562 | AssertMsgReturnVoid (vt == VarType(),
|
---|
563 | ("Expected vartype %d, got %d.\n",
|
---|
564 | VarType(), vt));
|
---|
565 | }
|
---|
566 |
|
---|
567 | m.arr = arg;
|
---|
568 | m.isWeak = true;
|
---|
569 |
|
---|
570 | AssertReturnVoid (m.arr == NULL || accessRaw() != NULL);
|
---|
571 |
|
---|
572 | #endif /* defined (VBOX_WITH_XPCOM) */
|
---|
573 | }
|
---|
574 |
|
---|
575 | /**
|
---|
576 | * Creates a deep copy of the given standard C++ container.
|
---|
577 | *
|
---|
578 | * @param aCntr Container object to copy.
|
---|
579 | *
|
---|
580 | * @param C Standard C++ container template class (normally deduced from
|
---|
581 | * @c aCntr).
|
---|
582 | */
|
---|
583 | template <template <class> class C>
|
---|
584 | SafeArray (const C <T> & aCntr)
|
---|
585 | {
|
---|
586 | reset (aCntr.size());
|
---|
587 | AssertReturnVoid (!isNull());
|
---|
588 |
|
---|
589 | size_t i = 0;
|
---|
590 | for (typename C <T>::const_iterator it = aCntr.begin();
|
---|
591 | it != aCntr.end(); ++ it, ++ i)
|
---|
592 | #if defined (VBOX_WITH_XPCOM)
|
---|
593 | Copy (*it, m.arr [i]);
|
---|
594 | #else
|
---|
595 | Copy (*it, m.raw [i]);
|
---|
596 | #endif
|
---|
597 | }
|
---|
598 |
|
---|
599 | /**
|
---|
600 | * Destroys this instance after calling #setNull() to release allocated
|
---|
601 | * resources. See #setNull() for more details.
|
---|
602 | */
|
---|
603 | virtual ~SafeArray() { setNull(); }
|
---|
604 |
|
---|
605 | /**
|
---|
606 | * Returns @c true if this instance represents a null array.
|
---|
607 | */
|
---|
608 | bool isNull() const { return m.arr == NULL; }
|
---|
609 |
|
---|
610 | /**
|
---|
611 | * Resets this instance to null and, if this instance is not a weak one,
|
---|
612 | * releases any resources ocuppied by the array data.
|
---|
613 | *
|
---|
614 | * @note This method destroys (cleans up) all elements of the array using
|
---|
615 | * the corresponding cleanup routine for the element type before the
|
---|
616 | * array itself is destroyed.
|
---|
617 | */
|
---|
618 | virtual void setNull() { m.uninit(); }
|
---|
619 |
|
---|
620 | /**
|
---|
621 | * Returns @c true if this instance is weak. A weak instance doesn't own the
|
---|
622 | * array data and therefore operations manipulating the ownership (e.g.
|
---|
623 | * #detachTo()) are forbidden and will assert.
|
---|
624 | */
|
---|
625 | bool isWeak() const { return m.isWeak; }
|
---|
626 |
|
---|
627 | /** Number of elements in the array. */
|
---|
628 | size_t size() const
|
---|
629 | {
|
---|
630 | #if defined (VBOX_WITH_XPCOM)
|
---|
631 | if (m.arr)
|
---|
632 | return m.size;
|
---|
633 | return 0;
|
---|
634 | #else
|
---|
635 | if (m.arr)
|
---|
636 | return Size (m.arr->rgsabound [0].cElements);
|
---|
637 | return 0;
|
---|
638 | #endif
|
---|
639 | }
|
---|
640 |
|
---|
641 | /**
|
---|
642 | * Resizes the array preserving its contents when possible. If the new size
|
---|
643 | * is bigger than the old size, new elements are initialized with null
|
---|
644 | * values. If the new size is smaller than the old size, the contents of the
|
---|
645 | * array above the new size is lost.
|
---|
646 | *
|
---|
647 | * @param aNewSize New number of elements in the array.
|
---|
648 | * @return @c true on success and false if there is not enough
|
---|
649 | * memory for resizing.
|
---|
650 | */
|
---|
651 | virtual bool resize (size_t aNewSize)
|
---|
652 | {
|
---|
653 | /// @todo Implement me!
|
---|
654 | NOREF (aNewSize);
|
---|
655 | AssertFailedReturn (false);
|
---|
656 | }
|
---|
657 |
|
---|
658 | /**
|
---|
659 | * Reinitializes this instance by preallocating space for the given number
|
---|
660 | * of elements. The previous array contents is lost.
|
---|
661 | *
|
---|
662 | * @param aNewSize New number of elements in the array.
|
---|
663 | * @return @c true on success and false if there is not enough
|
---|
664 | * memory for resizing.
|
---|
665 | */
|
---|
666 | virtual bool reset (size_t aNewSize)
|
---|
667 | {
|
---|
668 | m.uninit();
|
---|
669 |
|
---|
670 | #if defined (VBOX_WITH_XPCOM)
|
---|
671 |
|
---|
672 | /* Note: for zero-sized arrays, we use the size of 1 because whether
|
---|
673 | * malloc(0) returns a null pointer or not (which is used in isNull())
|
---|
674 | * is implementation-dependent according to the C standard. */
|
---|
675 |
|
---|
676 | m.arr = (T *) nsMemory::Alloc (RT_MAX (aNewSize, 1) * sizeof (T));
|
---|
677 | AssertReturn (m.arr != NULL, false);
|
---|
678 |
|
---|
679 | m.size = aNewSize;
|
---|
680 |
|
---|
681 | for (size_t i = 0; i < m.size; ++ i)
|
---|
682 | Init (m.arr [i]);
|
---|
683 |
|
---|
684 | #else
|
---|
685 |
|
---|
686 | SAFEARRAYBOUND bound = { VarCount (aNewSize), 0 };
|
---|
687 | m.arr = SafeArrayCreate (VarType(), 1, &bound);
|
---|
688 | AssertReturn (m.arr != NULL, false);
|
---|
689 |
|
---|
690 | AssertReturn (accessRaw() != NULL, false);
|
---|
691 |
|
---|
692 | #endif
|
---|
693 | return true;
|
---|
694 | }
|
---|
695 |
|
---|
696 | /**
|
---|
697 | * Returns a pointer to the raw array data. Use this raw pointer with care
|
---|
698 | * as no type or bound checking is done for you in this case.
|
---|
699 | *
|
---|
700 | * @note This method returns @c NULL when this instance is null.
|
---|
701 | * @see #operator[]
|
---|
702 | */
|
---|
703 | T *raw()
|
---|
704 | {
|
---|
705 | #if defined (VBOX_WITH_XPCOM)
|
---|
706 | return m.arr;
|
---|
707 | #else
|
---|
708 | return accessRaw();
|
---|
709 | #endif
|
---|
710 | }
|
---|
711 |
|
---|
712 | /**
|
---|
713 | * Const version of #raw().
|
---|
714 | */
|
---|
715 | const T *raw() const
|
---|
716 | {
|
---|
717 | #if defined (VBOX_WITH_XPCOM)
|
---|
718 | return m.arr;
|
---|
719 | #else
|
---|
720 | return accessRaw();
|
---|
721 | #endif
|
---|
722 | }
|
---|
723 |
|
---|
724 | /**
|
---|
725 | * Array access operator that returns an array element by reference. A bit
|
---|
726 | * safer than #raw(): asserts and returns an invalid reference if this
|
---|
727 | * instance is null or if the index is out of bounds.
|
---|
728 | *
|
---|
729 | * @note For weak instances, this call will succeed but the beiavior of
|
---|
730 | * changing the contents of an element of the weak array instance is
|
---|
731 | * undefined and may lead to a program crash on some platforms.
|
---|
732 | */
|
---|
733 | T &operator[] (size_t aIdx)
|
---|
734 | {
|
---|
735 | AssertReturn (m.arr != NULL, *((T *) NULL));
|
---|
736 | AssertReturn (aIdx < size(), *((T *) NULL));
|
---|
737 | #if defined (VBOX_WITH_XPCOM)
|
---|
738 | return m.arr [aIdx];
|
---|
739 | #else
|
---|
740 |
|
---|
741 | AssertReturn (accessRaw() != NULL, *((T *) NULL));
|
---|
742 | return m.raw [aIdx];
|
---|
743 | #endif
|
---|
744 | }
|
---|
745 |
|
---|
746 | /**
|
---|
747 | * Const version of #operator[] that returns an array element by value.
|
---|
748 | */
|
---|
749 | const T operator[] (size_t aIdx) const
|
---|
750 | {
|
---|
751 | AssertReturn (m.arr != NULL, *((T *) NULL));
|
---|
752 | AssertReturn (aIdx < size(), *((T *) NULL));
|
---|
753 | #if defined (VBOX_WITH_XPCOM)
|
---|
754 | return m.arr [aIdx];
|
---|
755 | #else
|
---|
756 | AssertReturn (unconst (this)->accessRaw() != NULL, *((T *) NULL));
|
---|
757 | return m.raw [aIdx];
|
---|
758 | #endif
|
---|
759 | }
|
---|
760 |
|
---|
761 | /**
|
---|
762 | * Creates a copy of this array and stores it in a method parameter declared
|
---|
763 | * using the ComSafeArrayOut macro. When using this call, always wrap the
|
---|
764 | * parameter name in the ComSafeArrayOutArg macro call like this:
|
---|
765 | * <pre>
|
---|
766 | * safeArray.cloneTo (ComSafeArrayOutArg (aArg));
|
---|
767 | * </pre>
|
---|
768 | *
|
---|
769 | * @note It is assumed that the ownership of the returned copy is
|
---|
770 | * transferred to the caller of the method and he is responsible to free the
|
---|
771 | * array data when it is no more necessary.
|
---|
772 | *
|
---|
773 | * @param aArg Output method parameter to clone to.
|
---|
774 | */
|
---|
775 | virtual const SafeArray &cloneTo (ComSafeArrayOut (T, aArg)) const
|
---|
776 | {
|
---|
777 | /// @todo Implement me!
|
---|
778 | #if defined (VBOX_WITH_XPCOM)
|
---|
779 | NOREF (aArgSize);
|
---|
780 | NOREF (aArg);
|
---|
781 | #else
|
---|
782 | NOREF (aArg);
|
---|
783 | #endif
|
---|
784 | AssertFailedReturn (*this);
|
---|
785 | }
|
---|
786 |
|
---|
787 | /**
|
---|
788 | * Transfers the ownership of this array's data to the specified location
|
---|
789 | * declared using the ComSafeArrayOut macro and makes this array a null
|
---|
790 | * array. When using this call, always wrap the parameter name in the
|
---|
791 | * ComSafeArrayOutArg macro call like this:
|
---|
792 | * <pre>
|
---|
793 | * safeArray.detachTo (ComSafeArrayOutArg (aArg));
|
---|
794 | * </pre>
|
---|
795 | *
|
---|
796 | * Detaching the null array is also possible in which case the location will
|
---|
797 | * receive NULL.
|
---|
798 | *
|
---|
799 | * @note Since the ownership of the array data is transferred to the
|
---|
800 | * caller of the method, he is responsible to free the array data when it is
|
---|
801 | * no more necessary.
|
---|
802 | *
|
---|
803 | * @param aArg Location to detach to.
|
---|
804 | */
|
---|
805 | virtual SafeArray &detachTo (ComSafeArrayOut (T, aArg))
|
---|
806 | {
|
---|
807 | AssertReturn (m.isWeak == false, *this);
|
---|
808 |
|
---|
809 | #if defined (VBOX_WITH_XPCOM)
|
---|
810 |
|
---|
811 | AssertReturn (aArgSize != NULL, *this);
|
---|
812 | AssertReturn (aArg != NULL, *this);
|
---|
813 |
|
---|
814 | *aArgSize = m.size;
|
---|
815 | *aArg = m.arr;
|
---|
816 |
|
---|
817 | m.isWeak = false;
|
---|
818 | m.size = 0;
|
---|
819 | m.arr = NULL;
|
---|
820 |
|
---|
821 | #else /* defined (VBOX_WITH_XPCOM) */
|
---|
822 |
|
---|
823 | AssertReturn (aArg != NULL, *this);
|
---|
824 | *aArg = m.arr;
|
---|
825 |
|
---|
826 | if (m.raw)
|
---|
827 | {
|
---|
828 | HRESULT rc = SafeArrayUnaccessData (m.arr);
|
---|
829 | AssertComRCReturn (rc, *this);
|
---|
830 | m.raw = NULL;
|
---|
831 | }
|
---|
832 |
|
---|
833 | m.isWeak = false;
|
---|
834 | m.arr = NULL;
|
---|
835 |
|
---|
836 | #endif /* defined (VBOX_WITH_XPCOM) */
|
---|
837 |
|
---|
838 | return *this;
|
---|
839 | }
|
---|
840 |
|
---|
841 | // public methods for internal purposes only
|
---|
842 |
|
---|
843 | #if defined (VBOX_WITH_XPCOM)
|
---|
844 |
|
---|
845 | /** Internal funciton. Never call it directly. */
|
---|
846 | PRUint32 *__asOutParam_Size() { setNull(); return &m.size; }
|
---|
847 |
|
---|
848 | /** Internal funciton. Never call it directly. */
|
---|
849 | T **__asOutParam_Arr() { Assert (isNull()); return &m.arr; }
|
---|
850 |
|
---|
851 | #else /* defined (VBOX_WITH_XPCOM) */
|
---|
852 |
|
---|
853 | /** Internal funciton. Never call it directly. */
|
---|
854 | SAFEARRAY ** __asInParam() { return &m.arr; }
|
---|
855 |
|
---|
856 | /** Internal funciton. Never call it directly. */
|
---|
857 | SAFEARRAY ** __asOutParam() { setNull(); return &m.arr; }
|
---|
858 |
|
---|
859 | #endif /* defined (VBOX_WITH_XPCOM) */
|
---|
860 |
|
---|
861 | static const SafeArray Null;
|
---|
862 |
|
---|
863 | protected:
|
---|
864 |
|
---|
865 | DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP(SafeArray)
|
---|
866 |
|
---|
867 | #if defined (VBOX_WITH_XPCOM)
|
---|
868 | #else /* defined (VBOX_WITH_XPCOM) */
|
---|
869 |
|
---|
870 | /** Requests access to the raw data pointer. */
|
---|
871 | T *accessRaw()
|
---|
872 | {
|
---|
873 | if (m.arr && m.raw == NULL)
|
---|
874 | {
|
---|
875 | HRESULT rc = SafeArrayAccessData (m.arr, (void HUGEP **) &m.raw);
|
---|
876 | AssertComRCReturn (rc, NULL);
|
---|
877 | }
|
---|
878 | return m.raw;
|
---|
879 | }
|
---|
880 |
|
---|
881 | #endif /* defined (VBOX_WITH_XPCOM) */
|
---|
882 |
|
---|
883 | struct Data
|
---|
884 | {
|
---|
885 | Data()
|
---|
886 | : isWeak (false)
|
---|
887 | #if defined (VBOX_WITH_XPCOM)
|
---|
888 | , size (0), arr (NULL)
|
---|
889 | #else
|
---|
890 | , arr (NULL), raw (NULL)
|
---|
891 | #endif
|
---|
892 | {}
|
---|
893 |
|
---|
894 | ~Data() { uninit(); }
|
---|
895 |
|
---|
896 | void uninit()
|
---|
897 | {
|
---|
898 | #if defined (VBOX_WITH_XPCOM)
|
---|
899 |
|
---|
900 | if (arr)
|
---|
901 | {
|
---|
902 | if (!isWeak)
|
---|
903 | {
|
---|
904 | for (size_t i = 0; i < size; ++ i)
|
---|
905 | Uninit (arr [i]);
|
---|
906 |
|
---|
907 | nsMemory::Free ((void *) arr);
|
---|
908 |
|
---|
909 | isWeak = false;
|
---|
910 | }
|
---|
911 | arr = NULL;
|
---|
912 | }
|
---|
913 |
|
---|
914 | #else /* defined (VBOX_WITH_XPCOM) */
|
---|
915 |
|
---|
916 | if (arr)
|
---|
917 | {
|
---|
918 | if (raw)
|
---|
919 | {
|
---|
920 | SafeArrayUnaccessData (arr);
|
---|
921 | raw = NULL;
|
---|
922 | }
|
---|
923 |
|
---|
924 | if (!isWeak)
|
---|
925 | {
|
---|
926 | HRESULT rc = SafeArrayDestroy (arr);
|
---|
927 | AssertComRCReturnVoid (rc);
|
---|
928 |
|
---|
929 | isWeak = false;
|
---|
930 | }
|
---|
931 | arr = NULL;
|
---|
932 | }
|
---|
933 |
|
---|
934 | #endif /* defined (VBOX_WITH_XPCOM) */
|
---|
935 | }
|
---|
936 |
|
---|
937 | bool isWeak : 1;
|
---|
938 |
|
---|
939 | #if defined (VBOX_WITH_XPCOM)
|
---|
940 | PRUint32 size;
|
---|
941 | T *arr;
|
---|
942 | #else
|
---|
943 | SAFEARRAY *arr;
|
---|
944 | T *raw;
|
---|
945 | #endif
|
---|
946 | };
|
---|
947 |
|
---|
948 | Data m;
|
---|
949 | };
|
---|
950 |
|
---|
951 | ////////////////////////////////////////////////////////////////////////////////
|
---|
952 |
|
---|
953 | #if defined (VBOX_WITH_XPCOM)
|
---|
954 |
|
---|
955 | /**
|
---|
956 | * Version of com::SafeArray for arrays of GUID.
|
---|
957 | *
|
---|
958 | * In MS COM, GUID arrays store GUIDs by value and therefore input arrays are
|
---|
959 | * represented using |GUID *| and out arrays -- using |GUID **|. In XPCOM,
|
---|
960 | * GUID arrays store pointers to nsID so that input arrays are |const nsID **|
|
---|
961 | * and out arrays are |nsID ***|. Due to this difference, it is impossible to
|
---|
962 | * work with arrays of GUID on both platforms by simply using com::SafeArray
|
---|
963 | * <GUID>. This class is intended to provide some leve of cross-platform
|
---|
964 | * behavior.
|
---|
965 | *
|
---|
966 | * The basic usage pattern is basically similar to com::SafeArray<> except that
|
---|
967 | * you use ComSafeGUIDArrayIn* and ComSafeGUIDArrayOut* macros instead of
|
---|
968 | * ComSafeArrayIn* and ComSafeArrayOut*. Another important nuance is that the
|
---|
969 | * raw() array type is different (nsID **, or GUID ** on XPCOM and GUID * on MS
|
---|
970 | * COM) so it is recommended to use operator[] instead that always returns a
|
---|
971 | * GUID by value.
|
---|
972 | *
|
---|
973 | * Note that due to const modifiers, you cannot use SafeGUIDArray for input GUID
|
---|
974 | * arrays. Please use SafeConstGUIDArray for this instead.
|
---|
975 | *
|
---|
976 | * Other than mentioned above, the functionality of this class is equivalent to
|
---|
977 | * com::SafeArray<>. See the description of that template and its methods for
|
---|
978 | * more information.
|
---|
979 | *
|
---|
980 | * Output GUID arrays are handled by a separate class, SafeGUIDArrayOut, since
|
---|
981 | * this class cannot handle them because of const modifiers.
|
---|
982 | */
|
---|
983 | class SafeGUIDArray : public SafeArray <nsID *>
|
---|
984 | {
|
---|
985 | public:
|
---|
986 |
|
---|
987 | typedef SafeArray <nsID *> Base;
|
---|
988 |
|
---|
989 | class nsIDRef
|
---|
990 | {
|
---|
991 | public:
|
---|
992 |
|
---|
993 | nsIDRef (nsID * &aVal) : mVal (aVal) {}
|
---|
994 |
|
---|
995 | operator const nsID &() const { return mVal ? *mVal : *Empty; }
|
---|
996 | operator nsID() const { return mVal ? *mVal : *Empty; }
|
---|
997 |
|
---|
998 | const nsID *operator&() const { return mVal ? mVal : Empty; }
|
---|
999 |
|
---|
1000 | nsIDRef &operator= (const nsID &aThat)
|
---|
1001 | {
|
---|
1002 | if (mVal == NULL)
|
---|
1003 | Copy (&aThat, mVal);
|
---|
1004 | else
|
---|
1005 | *mVal = aThat;
|
---|
1006 | return *this;
|
---|
1007 | }
|
---|
1008 |
|
---|
1009 | private:
|
---|
1010 |
|
---|
1011 | nsID * &mVal;
|
---|
1012 |
|
---|
1013 | static const nsID *Empty;
|
---|
1014 |
|
---|
1015 | friend class SafeGUIDArray;
|
---|
1016 | };
|
---|
1017 |
|
---|
1018 | /** See SafeArray<>::SafeArray(). */
|
---|
1019 | SafeGUIDArray() {}
|
---|
1020 |
|
---|
1021 | /** See SafeArray<>::SafeArray (size_t). */
|
---|
1022 | SafeGUIDArray (size_t aSize) : Base (aSize) {}
|
---|
1023 |
|
---|
1024 | /**
|
---|
1025 | * Array access operator that returns an array element by reference. As a
|
---|
1026 | * special case, the return value of this operator on XPCOM is a nsID (GUID)
|
---|
1027 | * reference, instead of a nsID pointer (the actual SafeArray template
|
---|
1028 | * argument), for compatibility with the MS COM version.
|
---|
1029 | *
|
---|
1030 | * The rest is equivalent to SafeArray<>::operator[].
|
---|
1031 | */
|
---|
1032 | nsIDRef operator[] (size_t aIdx)
|
---|
1033 | {
|
---|
1034 | Assert (m.arr != NULL);
|
---|
1035 | Assert (aIdx < size());
|
---|
1036 | return nsIDRef (m.arr [aIdx]);
|
---|
1037 | }
|
---|
1038 |
|
---|
1039 | /**
|
---|
1040 | * Const version of #operator[] that returns an array element by value.
|
---|
1041 | */
|
---|
1042 | const nsID &operator[] (size_t aIdx) const
|
---|
1043 | {
|
---|
1044 | Assert (m.arr != NULL);
|
---|
1045 | Assert (aIdx < size());
|
---|
1046 | return m.arr [aIdx] ? *m.arr [aIdx] : *nsIDRef::Empty;
|
---|
1047 | }
|
---|
1048 | };
|
---|
1049 |
|
---|
1050 | /**
|
---|
1051 | * Version of com::SafeArray for const arrays of GUID.
|
---|
1052 | *
|
---|
1053 | * This class is used to work with input GUID array parameters in method
|
---|
1054 | * implementaitons. See SafeGUIDArray for more details.
|
---|
1055 | */
|
---|
1056 | class SafeConstGUIDArray : public SafeArray <const nsID *,
|
---|
1057 | SafeArrayTraits <nsID *> >
|
---|
1058 | {
|
---|
1059 | public:
|
---|
1060 |
|
---|
1061 | typedef SafeArray <const nsID *, SafeArrayTraits <nsID *> > Base;
|
---|
1062 |
|
---|
1063 | /** See SafeArray<>::SafeArray(). */
|
---|
1064 | SafeConstGUIDArray() {}
|
---|
1065 |
|
---|
1066 | /* See SafeArray<>::SafeArray (ComSafeArrayIn (T, aArg)). */
|
---|
1067 | SafeConstGUIDArray (ComSafeGUIDArrayIn (aArg))
|
---|
1068 | : Base (ComSafeGUIDArrayInArg (aArg)) {}
|
---|
1069 |
|
---|
1070 | /**
|
---|
1071 | * Array access operator that returns an array element by reference. As a
|
---|
1072 | * special case, the return value of this operator on XPCOM is nsID (GUID)
|
---|
1073 | * instead of nsID *, for compatibility with the MS COM version.
|
---|
1074 | *
|
---|
1075 | * The rest is equivalent to SafeArray<>::operator[].
|
---|
1076 | */
|
---|
1077 | const nsID &operator[] (size_t aIdx) const
|
---|
1078 | {
|
---|
1079 | AssertReturn (m.arr != NULL, **((const nsID * *) NULL));
|
---|
1080 | AssertReturn (aIdx < size(), **((const nsID * *) NULL));
|
---|
1081 | return *m.arr [aIdx];
|
---|
1082 | }
|
---|
1083 |
|
---|
1084 | private:
|
---|
1085 |
|
---|
1086 | /* These are disabled because of const */
|
---|
1087 | bool reset (size_t aNewSize) { NOREF (aNewSize); return false; }
|
---|
1088 | };
|
---|
1089 |
|
---|
1090 | #else /* defined (VBOX_WITH_XPCOM) */
|
---|
1091 |
|
---|
1092 | typedef SafeArray <GUID> SafeGUIDArray;
|
---|
1093 | typedef SafeArray <const GUID, SafeArrayTraits <GUID> > SafeConstGUIDArray;
|
---|
1094 |
|
---|
1095 | #endif /* defined (VBOX_WITH_XPCOM) */
|
---|
1096 |
|
---|
1097 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1098 |
|
---|
1099 | #if defined (VBOX_WITH_XPCOM)
|
---|
1100 |
|
---|
1101 | template <class I>
|
---|
1102 | struct SafeIfaceArrayTraits
|
---|
1103 | {
|
---|
1104 | protected:
|
---|
1105 |
|
---|
1106 | static void Init (I * &aElem) { aElem = NULL; }
|
---|
1107 | static void Uninit (I * &aElem)
|
---|
1108 | {
|
---|
1109 | if (aElem)
|
---|
1110 | {
|
---|
1111 | aElem->Release();
|
---|
1112 | aElem = NULL;
|
---|
1113 | }
|
---|
1114 | }
|
---|
1115 |
|
---|
1116 | static void Copy (I * aFrom, I * &aTo)
|
---|
1117 | {
|
---|
1118 | if (aFrom != NULL)
|
---|
1119 | {
|
---|
1120 | aTo = aFrom;
|
---|
1121 | aTo->AddRef();
|
---|
1122 | }
|
---|
1123 | else
|
---|
1124 | aTo = NULL;
|
---|
1125 | }
|
---|
1126 |
|
---|
1127 | public:
|
---|
1128 |
|
---|
1129 | /* Magic to workaround strict rules of par. 4.4.4 of the C++ standard. */
|
---|
1130 | static I **__asInParam_Arr (I **aArr) { return aArr; }
|
---|
1131 | static I **__asInParam_Arr (const I **aArr) { return const_cast <I **> (aArr); }
|
---|
1132 | };
|
---|
1133 |
|
---|
1134 | #else /* defined (VBOX_WITH_XPCOM) */
|
---|
1135 |
|
---|
1136 | template <class I>
|
---|
1137 | struct SafeIfaceArrayTraits
|
---|
1138 | {
|
---|
1139 | protected:
|
---|
1140 |
|
---|
1141 | static VARTYPE VarType() { return VT_UNKNOWN; }
|
---|
1142 | static ULONG VarCount (size_t aSize) { return (ULONG) aSize; }
|
---|
1143 | static size_t Size (ULONG aVarCount) { return (size_t) aVarCount; }
|
---|
1144 |
|
---|
1145 | static void Copy (I * aFrom, I * &aTo)
|
---|
1146 | {
|
---|
1147 | if (aFrom != NULL)
|
---|
1148 | {
|
---|
1149 | aTo = aFrom;
|
---|
1150 | aTo->AddRef();
|
---|
1151 | }
|
---|
1152 | else
|
---|
1153 | aTo = NULL;
|
---|
1154 | }
|
---|
1155 | };
|
---|
1156 |
|
---|
1157 | #endif /* defined (VBOX_WITH_XPCOM) */
|
---|
1158 |
|
---|
1159 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1160 |
|
---|
1161 | /**
|
---|
1162 | * Version of com::SafeArray for arrays of interface pointers.
|
---|
1163 | *
|
---|
1164 | * Except that it manages arrays of interface pointers, the usage of this class
|
---|
1165 | * is identical to com::SafeArray.
|
---|
1166 | *
|
---|
1167 | * @param I Interface class (no asterisk).
|
---|
1168 | */
|
---|
1169 | template <class I>
|
---|
1170 | class SafeIfaceArray : public SafeArray <I *, SafeIfaceArrayTraits <I> >
|
---|
1171 | {
|
---|
1172 | public:
|
---|
1173 |
|
---|
1174 | typedef SafeArray <I *, SafeIfaceArrayTraits <I> > Base;
|
---|
1175 |
|
---|
1176 | /**
|
---|
1177 | * Creates a null array.
|
---|
1178 | */
|
---|
1179 | SafeIfaceArray() {}
|
---|
1180 |
|
---|
1181 | /**
|
---|
1182 | * Creates a new array of the given size. All elements of the newly created
|
---|
1183 | * array initialized with null values.
|
---|
1184 | *
|
---|
1185 | * @param aSize Initial number of elements in the array. Must be greater
|
---|
1186 | * than 0.
|
---|
1187 | *
|
---|
1188 | * @note If this object remains null after construction it means that there
|
---|
1189 | * was not enough memory for creating an array of the requested size.
|
---|
1190 | * The constructor will also assert in this case.
|
---|
1191 | */
|
---|
1192 | SafeIfaceArray (size_t aSize) { reset (aSize); }
|
---|
1193 |
|
---|
1194 | /**
|
---|
1195 | * Weakly attaches this instance to the existing array passed in a method
|
---|
1196 | * parameter declared using the ComSafeArrayIn macro. When using this call,
|
---|
1197 | * always wrap the parameter name in the ComSafeArrayOutArg macro call like
|
---|
1198 | * this:
|
---|
1199 | * <pre>
|
---|
1200 | * SafeArray safeArray (ComSafeArrayInArg (aArg));
|
---|
1201 | * </pre>
|
---|
1202 | *
|
---|
1203 | * Note that this constructor doesn't take the ownership of the array. In
|
---|
1204 | * particular, it means that operations that operate on the ownership (e.g.
|
---|
1205 | * #detachTo()) are forbidden and will assert.
|
---|
1206 | *
|
---|
1207 | * @param aArg Input method parameter to attach to.
|
---|
1208 | */
|
---|
1209 | SafeIfaceArray (ComSafeArrayIn (I *, aArg))
|
---|
1210 | {
|
---|
1211 | #if defined (VBOX_WITH_XPCOM)
|
---|
1212 |
|
---|
1213 | AssertReturnVoid (aArg != NULL);
|
---|
1214 |
|
---|
1215 | Base::m.size = aArgSize;
|
---|
1216 | Base::m.arr = aArg;
|
---|
1217 | Base::m.isWeak = true;
|
---|
1218 |
|
---|
1219 | #else /* defined (VBOX_WITH_XPCOM) */
|
---|
1220 |
|
---|
1221 | AssertReturnVoid (aArg != NULL);
|
---|
1222 | SAFEARRAY *arg = *aArg;
|
---|
1223 |
|
---|
1224 | if (arg)
|
---|
1225 | {
|
---|
1226 | AssertReturnVoid (arg->cDims == 1);
|
---|
1227 |
|
---|
1228 | VARTYPE vt;
|
---|
1229 | HRESULT rc = SafeArrayGetVartype (arg, &vt);
|
---|
1230 | AssertComRCReturnVoid (rc);
|
---|
1231 | AssertMsgReturnVoid (vt == VT_UNKNOWN,
|
---|
1232 | ("Expected vartype VT_UNKNOWN, got %d.\n",
|
---|
1233 | VarType(), vt));
|
---|
1234 | GUID guid;
|
---|
1235 | rc = SafeArrayGetIID (arg, &guid);
|
---|
1236 | AssertComRCReturnVoid (rc);
|
---|
1237 | AssertMsgReturnVoid (InlineIsEqualGUID (_ATL_IIDOF (I), guid),
|
---|
1238 | ("Expected IID {%Vuuid}, got {%Vuuid}.\n",
|
---|
1239 | &_ATL_IIDOF (I), &guid));
|
---|
1240 | }
|
---|
1241 |
|
---|
1242 | m.arr = arg;
|
---|
1243 | m.isWeak = true;
|
---|
1244 |
|
---|
1245 | AssertReturnVoid (accessRaw() != NULL);
|
---|
1246 |
|
---|
1247 | #endif /* defined (VBOX_WITH_XPCOM) */
|
---|
1248 | }
|
---|
1249 |
|
---|
1250 | /**
|
---|
1251 | * Creates a deep copy of the given standard C++ container that stores
|
---|
1252 | * interface pointers as objects of the ComPtr <I> class.
|
---|
1253 | *
|
---|
1254 | * @param aCntr Container object to copy.
|
---|
1255 | *
|
---|
1256 | * @param C Standard C++ container template class (normally deduced from
|
---|
1257 | * @c aCntr).
|
---|
1258 | * @param A Standard C++ allocator class (deduced from @c aCntr).
|
---|
1259 | * @param OI Argument to the ComPtr template (deduced from @c aCntr).
|
---|
1260 | */
|
---|
1261 | template <template <typename, typename> class C, class A, class OI>
|
---|
1262 | SafeIfaceArray (const C <ComPtr <OI>, A> & aCntr)
|
---|
1263 | {
|
---|
1264 | typedef C <ComPtr <OI>, A> List;
|
---|
1265 |
|
---|
1266 | reset (aCntr.size());
|
---|
1267 | AssertReturnVoid (!Base::isNull());
|
---|
1268 |
|
---|
1269 | int i = 0;
|
---|
1270 | for (typename List::const_iterator it = aCntr.begin();
|
---|
1271 | it != aCntr.end(); ++ it, ++ i)
|
---|
1272 | #if defined (VBOX_WITH_XPCOM)
|
---|
1273 | Copy (*it, Base::m.arr [i]);
|
---|
1274 | #else
|
---|
1275 | Copy (*it, Base::m.raw [i]);
|
---|
1276 | #endif
|
---|
1277 | }
|
---|
1278 |
|
---|
1279 | /**
|
---|
1280 | * Creates a deep copy of the given standard C++ container that stores
|
---|
1281 | * interface pointers as objects of the ComObjPtr <I> class.
|
---|
1282 | *
|
---|
1283 | * @param aCntr Container object to copy.
|
---|
1284 | *
|
---|
1285 | * @param C Standard C++ container template class (normally deduced from
|
---|
1286 | * @c aCntr).
|
---|
1287 | * @param A Standard C++ allocator class (deduced from @c aCntr).
|
---|
1288 | * @param OI Argument to the ComObjPtr template (deduced from @c aCntr).
|
---|
1289 | */
|
---|
1290 | template <template <typename, typename> class C, class A, class OI>
|
---|
1291 | SafeIfaceArray (const C <ComObjPtr <OI>, A> & aCntr)
|
---|
1292 | {
|
---|
1293 | typedef C <ComObjPtr <OI>, A> List;
|
---|
1294 |
|
---|
1295 | reset (aCntr.size());
|
---|
1296 | AssertReturnVoid (!Base::isNull());
|
---|
1297 |
|
---|
1298 | int i = 0;
|
---|
1299 | for (typename List::const_iterator it = aCntr.begin();
|
---|
1300 | it != aCntr.end(); ++ it, ++ i)
|
---|
1301 | #if defined (VBOX_WITH_XPCOM)
|
---|
1302 | Copy (*it, Base::m.arr [i]);
|
---|
1303 | #else
|
---|
1304 | Copy (*it, Base::m.raw [i]);
|
---|
1305 | #endif
|
---|
1306 | }
|
---|
1307 |
|
---|
1308 | /**
|
---|
1309 | * Reinitializes this instance by preallocating space for the given number
|
---|
1310 | * of elements. The previous array contents is lost.
|
---|
1311 | *
|
---|
1312 | * @param aNewSize New number of elements in the array.
|
---|
1313 | * @return @c true on success and false if there is not enough
|
---|
1314 | * memory for resizing.
|
---|
1315 | */
|
---|
1316 | virtual bool reset (size_t aNewSize)
|
---|
1317 | {
|
---|
1318 | Base::m.uninit();
|
---|
1319 |
|
---|
1320 | #if defined (VBOX_WITH_XPCOM)
|
---|
1321 |
|
---|
1322 | /* Note: for zero-sized arrays, we use the size of 1 because whether
|
---|
1323 | * malloc(0) returns a null pointer or not (which is used in isNull())
|
---|
1324 | * is implementation-dependent according to the C standard. */
|
---|
1325 |
|
---|
1326 | Base::m.arr = (I **) nsMemory::Alloc (RT_MAX (aNewSize, 1) * sizeof (I *));
|
---|
1327 | AssertReturn (Base::m.arr != NULL, false);
|
---|
1328 |
|
---|
1329 | Base::m.size = aNewSize;
|
---|
1330 |
|
---|
1331 | for (size_t i = 0; i < Base::m.size; ++ i)
|
---|
1332 | Init (Base::m.arr [i]);
|
---|
1333 |
|
---|
1334 | #else
|
---|
1335 |
|
---|
1336 | SAFEARRAYBOUND bound = { (ULONG)aNewSize, 0 };
|
---|
1337 | m.arr = SafeArrayCreateEx (VT_UNKNOWN, 1, &bound,
|
---|
1338 | (PVOID) &_ATL_IIDOF (I));
|
---|
1339 | AssertReturn (m.arr != NULL, false);
|
---|
1340 |
|
---|
1341 | AssertReturn (accessRaw() != NULL, false);
|
---|
1342 |
|
---|
1343 | #endif
|
---|
1344 | return true;
|
---|
1345 | }
|
---|
1346 | };
|
---|
1347 |
|
---|
1348 | } /* namespace com */
|
---|
1349 |
|
---|
1350 | /** @} */
|
---|
1351 |
|
---|
1352 | #endif /* ___VBox_com_array_h */
|
---|