VirtualBox

source: vbox/trunk/include/VBox/com/array.h@ 49076

Last change on this file since 49076 was 47848, checked in by vboxsync, 11 years ago

Main: API event for multitouch input.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette