VirtualBox

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

Last change on this file since 30681 was 30681, checked in by vboxsync, 14 years ago

Main: COM header cleanup (remove obscure and unused templates), second try

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