VirtualBox

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

Last change on this file since 40023 was 40023, checked in by vboxsync, 13 years ago

gcc-4.7 fixes

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