VirtualBox

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

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

Main: PNG screenshoting API

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