VirtualBox

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

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

Main: back out r63429

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

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