VirtualBox

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

Last change on this file since 30254 was 29931, checked in by vboxsync, 15 years ago

com/ptr.h, com/array.h: Added isNotNull() methods. It is easier to read if (ptrSomething.isNotNull()) than if (!ptrSomething->isNull()) because of the double negation in the latter case. (Easier for people not used to double negation from their native tongue, anyway.)

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