array.h revision a49dafa13549ac5db76a3c7f5e44ba83485a1971
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync/** @file
17bbfe820c22c3e599276890d8b7baa48c32abbbvboxsync * MS COM / XPCOM Abstraction Layer - Safe array helper class declaration.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync */
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync/*
a49dafa13549ac5db76a3c7f5e44ba83485a1971vboxsync * Copyright (C) 2006-2014 Oracle Corporation
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * available from http://www.virtualbox.org. This file is free software;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * you can redistribute it and/or modify it under the terms of the GNU
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * General Public License (GPL) as published by the Free Software
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * The contents of this file may alternatively be used under the terms
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * of the Common Development and Distribution License Version 1.0
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * VirtualBox OSE distribution, in which case the provisions of the
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * CDDL are applicable instead of those of the GPL.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * You may elect to license modified versions of this file under the
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * terms and conditions of either the GPL or the CDDL or both.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync */
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync#ifndef ___VBox_com_array_h
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync#define ___VBox_com_array_h
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync/** @defgroup grp_COM_arrays COM/XPCOM Arrays
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * @{
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * The COM/XPCOM array support layer provides a cross-platform way to pass
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * arrays to and from COM interface methods and consists of the com::SafeArray
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * template and a set of ComSafeArray* macros part of which is defined in
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * VBox/com/defs.h.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * This layer works with interface attributes and method parameters that have
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * the 'safearray="yes"' attribute in the XIDL definition:
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * @code
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync <interface name="ISomething" ...>
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync <method name="testArrays">
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync <param name="inArr" type="long" dir="in" safearray="yes"/>
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync <param name="outArr" type="long" dir="out" safearray="yes"/>
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync <param name="retArr" type="long" dir="return" safearray="yes"/>
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync </method>
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync </interface>
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * @endcode
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * Methods generated from this and similar definitions are implemented in
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * component classes using the following declarations:
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * @code
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync STDMETHOD(TestArrays)(ComSafeArrayIn(LONG, aIn),
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync ComSafeArrayOut(LONG, aOut),
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync ComSafeArrayOut(LONG, aRet));
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * @endcode
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * And the following function bodies:
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * @code
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync STDMETHODIMP Component::TestArrays(ComSafeArrayIn(LONG, aIn),
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync ComSafeArrayOut(LONG, aOut),
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync ComSafeArrayOut(LONG, aRet))
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync if (ComSafeArrayInIsNull(aIn))
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync return E_INVALIDARG;
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync if (ComSafeArrayOutIsNull(aOut))
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync return E_POINTER;
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync if (ComSafeArrayOutIsNull(aRet))
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync return E_POINTER;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync // Use SafeArray to access the input array parameter
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync com::SafeArray<LONG> in(ComSafeArrayInArg(aIn));
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync for (size_t i = 0; i < in.size(); ++ i)
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync LogFlow(("*** in[%u]=%d\n", i, in[i]));
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync // Use SafeArray to create the return array (the same technique is used
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync // for output array parameters)
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync SafeArray<LONG> ret(in.size() * 2);
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync for (size_t i = 0; i < in.size(); ++ i)
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync ret[i] = in[i];
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync ret[i + in.size()] = in[i] * 10;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync ret.detachTo(ComSafeArrayOutArg(aRet));
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync return S_OK;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * @endcode
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * Such methods can be called from the client code using the following pattern:
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * @code
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync ComPtr<ISomething> component;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync // ...
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync com::SafeArray<LONG> in(3);
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync in[0] = -1;
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync in[1] = -2;
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync in[2] = -3;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync com::SafeArray<LONG> out;
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync com::SafeArray<LONG> ret;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync HRESULT rc = component->TestArrays(ComSafeArrayAsInParam(in),
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync ComSafeArrayAsOutParam(out),
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync ComSafeArrayAsOutParam(ret));
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync if (SUCCEEDED(rc))
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync for (size_t i = 0; i < ret.size(); ++ i)
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync printf("*** ret[%u]=%d\n", i, ret[i]);
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * @endcode
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * For interoperability with standard C++ containers, there is a template
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * constructor that takes such a container as argument and performs a deep copy
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * of its contents. This can be used in method implementations like this:
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * @code
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync STDMETHODIMP Component::COMGETTER(Values)(ComSafeArrayOut(int, aValues))
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync // ... assume there is a |std::list<int> mValues| data member
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync com::SafeArray<int> values(mValues);
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync values.detachTo(ComSafeArrayOutArg(aValues));
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync return S_OK;
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync }
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * @endcode
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * The current implementation of the SafeArray layer supports all types normally
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * allowed in XIDL as array element types (including 'wstring' and 'uuid').
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * However, 'pointer-to-...' types (e.g. 'long *', 'wstring *') are not
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * supported and therefore cannot be used as element types.
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync *
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * Note that for GUID arrays you should use SafeGUIDArray and
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * SafeConstGUIDArray, customized SafeArray<> specializations.
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync *
dbea238999d6a4b34b9d1bdd1292e29717dd14bevboxsync * Also note that in order to pass input BSTR array parameters declared
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync * using the ComSafeArrayIn(IN_BSTR, aParam) macro to the SafeArray<>
f72cbd6a549c34992fa79cce84600fe2b92b3299vboxsync * constructor using the ComSafeArrayInArg() macro, you should use IN_BSTR
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * as the SafeArray<> template argument, not just BSTR.
66ea7017a123b8b854e76bbfd919a320815058b3vboxsync *
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * Arrays of interface pointers are also supported but they require to use a
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * special SafeArray implementation, com::SafeIfacePointer, which takes the
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * interface class name as a template argument (e.g. com::SafeIfacePointer
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * <IUnknown>). This implementation functions identically to com::SafeArray.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync */
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#ifdef VBOX_WITH_XPCOM
628ddfbd43ad5365d69fddda4007598242956577vboxsync# include <nsMemory.h>
5364452eae1726b30b3af49a60c33b189da75494vboxsync#endif
5364452eae1726b30b3af49a60c33b189da75494vboxsync
5364452eae1726b30b3af49a60c33b189da75494vboxsync /* Type traits are a C++ 11 feature, so not available everywhere (yet). */
5364452eae1726b30b3af49a60c33b189da75494vboxsync /* Only GCC 4.6 or newer. */
5364452eae1726b30b3af49a60c33b189da75494vboxsync#if (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 406) \
af94231fb372dbcb6413fb0e4a6d8a82d0412fb3vboxsync /* Only MSVC++ 16.0 (Visual Studio 2010) or newer. */ \
5364452eae1726b30b3af49a60c33b189da75494vboxsync || (defined(_MSC_VER) && (_MSC_VER >= 1600))
5364452eae1726b30b3af49a60c33b189da75494vboxsync #define VBOX_WITH_TYPE_TRAITS
5364452eae1726b30b3af49a60c33b189da75494vboxsync#endif
5364452eae1726b30b3af49a60c33b189da75494vboxsync
5364452eae1726b30b3af49a60c33b189da75494vboxsync#ifdef VBOX_WITH_TYPE_TRAITS
5364452eae1726b30b3af49a60c33b189da75494vboxsync# include <type_traits>
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync#endif
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync#include "VBox/com/defs.h"
628ddfbd43ad5365d69fddda4007598242956577vboxsync#include "VBox/com/ptr.h"
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync#include "VBox/com/assert.h"
e081d2eefdae89dce06cd6451f0ce45bf023e6e3vboxsync#include "iprt/cpp/list.h"
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#ifdef VBOX_WITH_XPCOM
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync/**
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * Wraps the given com::SafeArray instance to generate an expression that is
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * suitable for passing it to functions that take input safearray parameters
dbea238999d6a4b34b9d1bdd1292e29717dd14bevboxsync * declared using the ComSafeArrayIn macro.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * @param aArray com::SafeArray instance to pass as an input parameter.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync */
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync#define ComSafeArrayAsInParam(aArray) \
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync (aArray).size(), (aArray).__asInParam_Arr((aArray).raw())
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync/**
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * Wraps the given com::SafeArray instance to generate an expression that is
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * suitable for passing it to functions that take output safearray parameters
dbea238999d6a4b34b9d1bdd1292e29717dd14bevboxsync * declared using the ComSafeArrayOut macro.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * @param aArray com::SafeArray instance to pass as an output parameter.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync */
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync#define ComSafeArrayAsOutParam(aArray) \
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync (aArray).__asOutParam_Size(), (aArray).__asOutParam_Arr()
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#else /* !VBOX_WITH_XPCOM */
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync#define ComSafeArrayAsInParam(aArray) (aArray).__asInParam()
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync#define ComSafeArrayAsOutParam(aArray) (aArray).__asOutParam()
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#endif /* !VBOX_WITH_XPCOM */
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync/**
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync */
f9108665f82b59c99ac444815d75af51a14e46c7vboxsyncnamespace com
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync{
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#ifdef VBOX_WITH_XPCOM
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync////////////////////////////////////////////////////////////////////////////////
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync/**
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * Provides various helpers for SafeArray.
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync *
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * @param T Type of array elements.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsynctemplate<typename T>
f9108665f82b59c99ac444815d75af51a14e46c7vboxsyncstruct SafeArrayTraits
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync{
29830933677ee9718064e760adf8462b6e79ffdevboxsyncprotected:
29830933677ee9718064e760adf8462b6e79ffdevboxsync
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync /** Initializes memory for aElem. */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static void Init(T &aElem) { aElem = 0; }
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync /** Initializes memory occupied by aElem. */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static void Uninit(T &aElem) { aElem = 0; }
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync /** Creates a deep copy of aFrom and stores it in aTo. */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static void Copy(const T &aFrom, T &aTo) { aTo = aFrom; }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
29830933677ee9718064e760adf8462b6e79ffdevboxsyncpublic:
29830933677ee9718064e760adf8462b6e79ffdevboxsync
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync /* Magic to workaround strict rules of par. 4.4.4 of the C++ standard (that
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * in particular forbid casts of 'char **' to 'const char **'). Then initial
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * reason for this magic is that XPIDL declares input strings
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * (char/PRUnichar pointers) as const but doesn't do so for pointers to
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * arrays. */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static T *__asInParam_Arr(T *aArr) { return aArr; }
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static T *__asInParam_Arr(const T *aArr) { return const_cast<T *>(aArr); }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync};
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsynctemplate<typename T>
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsyncstruct SafeArrayTraits<T *>
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync{
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync // Arbitrary pointers are not supported
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync};
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsynctemplate<>
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsyncstruct SafeArrayTraits<PRUnichar *>
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync{
29830933677ee9718064e760adf8462b6e79ffdevboxsyncprotected:
29830933677ee9718064e760adf8462b6e79ffdevboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static void Init(PRUnichar * &aElem) { aElem = NULL; }
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static void Uninit(PRUnichar * &aElem)
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync {
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync if (aElem)
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync ::SysFreeString(aElem);
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync aElem = NULL;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static void Copy(const PRUnichar * aFrom, PRUnichar * &aTo)
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertCompile(sizeof(PRUnichar) == sizeof(OLECHAR));
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync aTo = aFrom ? ::SysAllocString((const OLECHAR *)aFrom) : NULL;
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync }
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
29830933677ee9718064e760adf8462b6e79ffdevboxsyncpublic:
29830933677ee9718064e760adf8462b6e79ffdevboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync /* Magic to workaround strict rules of par. 4.4.4 of the C++ standard */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static const PRUnichar **__asInParam_Arr(PRUnichar **aArr)
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync return const_cast<const PRUnichar **>(aArr);
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync }
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static const PRUnichar **__asInParam_Arr(const PRUnichar **aArr) { return aArr; }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync};
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
66ea7017a123b8b854e76bbfd919a320815058b3vboxsynctemplate<>
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsyncstruct SafeArrayTraits<const PRUnichar *>
66ea7017a123b8b854e76bbfd919a320815058b3vboxsync{
66ea7017a123b8b854e76bbfd919a320815058b3vboxsyncprotected:
66ea7017a123b8b854e76bbfd919a320815058b3vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static void Init(const PRUnichar * &aElem) { aElem = NULL; }
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static void Uninit(const PRUnichar * &aElem)
66ea7017a123b8b854e76bbfd919a320815058b3vboxsync {
66ea7017a123b8b854e76bbfd919a320815058b3vboxsync if (aElem)
66ea7017a123b8b854e76bbfd919a320815058b3vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync ::SysFreeString(const_cast<PRUnichar *>(aElem));
66ea7017a123b8b854e76bbfd919a320815058b3vboxsync aElem = NULL;
66ea7017a123b8b854e76bbfd919a320815058b3vboxsync }
66ea7017a123b8b854e76bbfd919a320815058b3vboxsync }
66ea7017a123b8b854e76bbfd919a320815058b3vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static void Copy(const PRUnichar * aFrom, const PRUnichar * &aTo)
66ea7017a123b8b854e76bbfd919a320815058b3vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertCompile(sizeof(PRUnichar) == sizeof(OLECHAR));
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync aTo = aFrom ? ::SysAllocString((const OLECHAR *)aFrom) : NULL;
66ea7017a123b8b854e76bbfd919a320815058b3vboxsync }
66ea7017a123b8b854e76bbfd919a320815058b3vboxsync
66ea7017a123b8b854e76bbfd919a320815058b3vboxsyncpublic:
66ea7017a123b8b854e76bbfd919a320815058b3vboxsync
66ea7017a123b8b854e76bbfd919a320815058b3vboxsync /* Magic to workaround strict rules of par. 4.4.4 of the C++ standard */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static const PRUnichar **__asInParam_Arr(const PRUnichar **aArr) { return aArr; }
66ea7017a123b8b854e76bbfd919a320815058b3vboxsync};
66ea7017a123b8b854e76bbfd919a320815058b3vboxsync
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsynctemplate<>
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsyncstruct SafeArrayTraits<nsID *>
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync{
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsyncprotected:
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static void Init(nsID * &aElem) { aElem = NULL; }
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static void Uninit(nsID * &aElem)
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync {
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync if (aElem)
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync ::nsMemory::Free(aElem);
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync aElem = NULL;
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync }
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync }
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static void Copy(const nsID * aFrom, nsID * &aTo)
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync {
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync if (aFrom)
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync aTo = (nsID *) ::nsMemory::Alloc(sizeof(nsID));
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync if (aTo)
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync *aTo = *aFrom;
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync }
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync else
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync aTo = NULL;
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync }
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync /* This specification is also reused for SafeConstGUIDArray, so provide a
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * no-op Init() and Uninit() which are necessary for SafeArray<> but should
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * be never called in context of SafeConstGUIDArray. */
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static void Init(const nsID * &aElem) { NOREF(aElem); AssertFailed(); }
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static void Uninit(const nsID * &aElem) { NOREF(aElem); AssertFailed(); }
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsyncpublic:
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync /** Magic to workaround strict rules of par. 4.4.4 of the C++ standard. */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static const nsID **__asInParam_Arr(nsID **aArr)
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync return const_cast<const nsID **>(aArr);
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync }
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static const nsID **__asInParam_Arr(const nsID **aArr) { return aArr; }
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync};
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#else /* !VBOX_WITH_XPCOM */
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync////////////////////////////////////////////////////////////////////////////////
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsyncstruct SafeArrayTraitsBase
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync{
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsyncprotected:
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static SAFEARRAY *CreateSafeArray(VARTYPE aVarType, SAFEARRAYBOUND *aBound)
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync { return SafeArrayCreate(aVarType, 1, aBound); }
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync};
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync/**
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * Provides various helpers for SafeArray.
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync *
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * @param T Type of array elements.
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync *
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * Specializations of this template must provide the following methods:
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync *
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync // Returns the VARTYPE of COM SafeArray elements to be used for T
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync static VARTYPE VarType();
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync // Returns the number of VarType() elements necessary for aSize
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync // elements of T
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static ULONG VarCount(size_t aSize);
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
f13f1ed69a96abc07232826cd40e8b08ae66afeavboxsync // Returns the number of elements of T that fit into the given number of
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync // VarType() elements (opposite to VarCount(size_t aSize)).
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static size_t Size(ULONG aVarCount);
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync // Creates a deep copy of aFrom and stores it in aTo
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static void Copy(ULONG aFrom, ULONG &aTo);
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsynctemplate<typename T>
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsyncstruct SafeArrayTraits : public SafeArrayTraitsBase
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync{
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsyncprotected:
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync
f13f1ed69a96abc07232826cd40e8b08ae66afeavboxsync // Arbitrary types are treated as passed by value and each value is
f13f1ed69a96abc07232826cd40e8b08ae66afeavboxsync // represented by a number of VT_Ix type elements where VT_Ix has the
f13f1ed69a96abc07232826cd40e8b08ae66afeavboxsync // biggest possible bitness necessary to represent T w/o a gap. COM enums
f13f1ed69a96abc07232826cd40e8b08ae66afeavboxsync // fall into this category.
f13f1ed69a96abc07232826cd40e8b08ae66afeavboxsync
f13f1ed69a96abc07232826cd40e8b08ae66afeavboxsync static VARTYPE VarType()
f13f1ed69a96abc07232826cd40e8b08ae66afeavboxsync {
5364452eae1726b30b3af49a60c33b189da75494vboxsync#ifdef VBOX_WITH_TYPE_TRAITS
5364452eae1726b30b3af49a60c33b189da75494vboxsync if ( std::is_integral<T>::value
5364452eae1726b30b3af49a60c33b189da75494vboxsync && !std::is_signed<T>::value)
5364452eae1726b30b3af49a60c33b189da75494vboxsync {
5364452eae1726b30b3af49a60c33b189da75494vboxsync if (sizeof(T) % 8 == 0) return VT_UI8;
5364452eae1726b30b3af49a60c33b189da75494vboxsync if (sizeof(T) % 4 == 0) return VT_UI4;
5364452eae1726b30b3af49a60c33b189da75494vboxsync if (sizeof(T) % 2 == 0) return VT_UI2;
5364452eae1726b30b3af49a60c33b189da75494vboxsync return VT_UI1;
5364452eae1726b30b3af49a60c33b189da75494vboxsync }
5364452eae1726b30b3af49a60c33b189da75494vboxsync#endif
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync if (sizeof(T) % 8 == 0) return VT_I8;
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync if (sizeof(T) % 4 == 0) return VT_I4;
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync if (sizeof(T) % 2 == 0) return VT_I2;
f13f1ed69a96abc07232826cd40e8b08ae66afeavboxsync return VT_I1;
f13f1ed69a96abc07232826cd40e8b08ae66afeavboxsync }
f13f1ed69a96abc07232826cd40e8b08ae66afeavboxsync
8164b4484483bf790b14bd94e37d743f93d9fec4vboxsync /*
8164b4484483bf790b14bd94e37d743f93d9fec4vboxsync * Fallback method in case type traits (VBOX_WITH_TYPE_TRAITS)
8164b4484483bf790b14bd94e37d743f93d9fec4vboxsync * are not available. Always returns unsigned types.
8164b4484483bf790b14bd94e37d743f93d9fec4vboxsync */
8164b4484483bf790b14bd94e37d743f93d9fec4vboxsync static VARTYPE VarTypeUnsigned()
8164b4484483bf790b14bd94e37d743f93d9fec4vboxsync {
8164b4484483bf790b14bd94e37d743f93d9fec4vboxsync if (sizeof(T) % 8 == 0) return VT_UI8;
8164b4484483bf790b14bd94e37d743f93d9fec4vboxsync if (sizeof(T) % 4 == 0) return VT_UI4;
8164b4484483bf790b14bd94e37d743f93d9fec4vboxsync if (sizeof(T) % 2 == 0) return VT_UI2;
8164b4484483bf790b14bd94e37d743f93d9fec4vboxsync return VT_UI1;
8164b4484483bf790b14bd94e37d743f93d9fec4vboxsync }
8164b4484483bf790b14bd94e37d743f93d9fec4vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static ULONG VarCount(size_t aSize)
f13f1ed69a96abc07232826cd40e8b08ae66afeavboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync if (sizeof(T) % 8 == 0) return (ULONG)((sizeof(T) / 8) * aSize);
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync if (sizeof(T) % 4 == 0) return (ULONG)((sizeof(T) / 4) * aSize);
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync if (sizeof(T) % 2 == 0) return (ULONG)((sizeof(T) / 2) * aSize);
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync return (ULONG)(sizeof(T) * aSize);
f13f1ed69a96abc07232826cd40e8b08ae66afeavboxsync }
f13f1ed69a96abc07232826cd40e8b08ae66afeavboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static size_t Size(ULONG aVarCount)
f13f1ed69a96abc07232826cd40e8b08ae66afeavboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync if (sizeof(T) % 8 == 0) return (size_t)(aVarCount * 8) / sizeof(T);
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync if (sizeof(T) % 4 == 0) return (size_t)(aVarCount * 4) / sizeof(T);
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync if (sizeof(T) % 2 == 0) return (size_t)(aVarCount * 2) / sizeof(T);
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync return (size_t) aVarCount / sizeof(T);
f13f1ed69a96abc07232826cd40e8b08ae66afeavboxsync }
f13f1ed69a96abc07232826cd40e8b08ae66afeavboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static void Copy(T aFrom, T &aTo) { aTo = aFrom; }
f13f1ed69a96abc07232826cd40e8b08ae66afeavboxsync};
f13f1ed69a96abc07232826cd40e8b08ae66afeavboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsynctemplate<typename T>
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsyncstruct SafeArrayTraits<T *>
f13f1ed69a96abc07232826cd40e8b08ae66afeavboxsync{
f13f1ed69a96abc07232826cd40e8b08ae66afeavboxsync // Arbitrary pointer types are not supported
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync};
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f13f1ed69a96abc07232826cd40e8b08ae66afeavboxsync/* Although the generic SafeArrayTraits template would work for all integers,
f13f1ed69a96abc07232826cd40e8b08ae66afeavboxsync * we specialize it for some of them in order to use the correct VT_ type */
f13f1ed69a96abc07232826cd40e8b08ae66afeavboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsynctemplate<>
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsyncstruct SafeArrayTraits<LONG> : public SafeArrayTraitsBase
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync{
29830933677ee9718064e760adf8462b6e79ffdevboxsyncprotected:
29830933677ee9718064e760adf8462b6e79ffdevboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync static VARTYPE VarType() { return VT_I4; }
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static ULONG VarCount(size_t aSize) { return (ULONG)aSize; }
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static size_t Size(ULONG aVarCount) { return (size_t)aVarCount; }
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static void Copy(LONG aFrom, LONG &aTo) { aTo = aFrom; }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync};
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsynctemplate<>
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsyncstruct SafeArrayTraits<ULONG> : public SafeArrayTraitsBase
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync{
29830933677ee9718064e760adf8462b6e79ffdevboxsyncprotected:
29830933677ee9718064e760adf8462b6e79ffdevboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync static VARTYPE VarType() { return VT_UI4; }
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static ULONG VarCount(size_t aSize) { return (ULONG)aSize; }
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static size_t Size(ULONG aVarCount) { return (size_t)aVarCount; }
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static void Copy(ULONG aFrom, ULONG &aTo) { aTo = aFrom; }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync};
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
a74cfe6f5b8ca6b317ff91593bcc5b113a78f54fvboxsynctemplate<>
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsyncstruct SafeArrayTraits<LONG64> : public SafeArrayTraitsBase
a74cfe6f5b8ca6b317ff91593bcc5b113a78f54fvboxsync{
a74cfe6f5b8ca6b317ff91593bcc5b113a78f54fvboxsyncprotected:
a74cfe6f5b8ca6b317ff91593bcc5b113a78f54fvboxsync
a74cfe6f5b8ca6b317ff91593bcc5b113a78f54fvboxsync static VARTYPE VarType() { return VT_I8; }
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static ULONG VarCount(size_t aSize) { return (ULONG)aSize; }
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static size_t Size(ULONG aVarCount) { return (size_t)aVarCount; }
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static void Copy(LONG64 aFrom, LONG64 &aTo) { aTo = aFrom; }
a74cfe6f5b8ca6b317ff91593bcc5b113a78f54fvboxsync};
a74cfe6f5b8ca6b317ff91593bcc5b113a78f54fvboxsync
a74cfe6f5b8ca6b317ff91593bcc5b113a78f54fvboxsynctemplate<>
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsyncstruct SafeArrayTraits<ULONG64> : public SafeArrayTraitsBase
a74cfe6f5b8ca6b317ff91593bcc5b113a78f54fvboxsync{
a74cfe6f5b8ca6b317ff91593bcc5b113a78f54fvboxsyncprotected:
a74cfe6f5b8ca6b317ff91593bcc5b113a78f54fvboxsync
a74cfe6f5b8ca6b317ff91593bcc5b113a78f54fvboxsync static VARTYPE VarType() { return VT_UI8; }
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static ULONG VarCount(size_t aSize) { return (ULONG)aSize; }
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static size_t Size(ULONG aVarCount) { return (size_t)aVarCount; }
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static void Copy(ULONG64 aFrom, ULONG64 &aTo) { aTo = aFrom; }
a74cfe6f5b8ca6b317ff91593bcc5b113a78f54fvboxsync};
a74cfe6f5b8ca6b317ff91593bcc5b113a78f54fvboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsynctemplate<>
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsyncstruct SafeArrayTraits<BSTR> : public SafeArrayTraitsBase
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync{
29830933677ee9718064e760adf8462b6e79ffdevboxsyncprotected:
29830933677ee9718064e760adf8462b6e79ffdevboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync static VARTYPE VarType() { return VT_BSTR; }
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static ULONG VarCount(size_t aSize) { return (ULONG)aSize; }
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static size_t Size(ULONG aVarCount) { return (size_t)aVarCount; }
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static void Copy(BSTR aFrom, BSTR &aTo)
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync aTo = aFrom ? ::SysAllocString((const OLECHAR *)aFrom) : NULL;
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync};
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsynctemplate<>
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsyncstruct SafeArrayTraits<GUID> : public SafeArrayTraitsBase
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync{
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsyncprotected:
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync /* Use the 64-bit unsigned integer type for GUID */
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync static VARTYPE VarType() { return VT_UI8; }
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync /* GUID is 128 bit, so we need two VT_UI8 */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static ULONG VarCount(size_t aSize)
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertCompileSize(GUID, 16);
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync return (ULONG)(aSize * 2);
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync }
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static size_t Size(ULONG aVarCount) { return (size_t)aVarCount / 2; }
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static void Copy(GUID aFrom, GUID &aTo) { aTo = aFrom; }
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync};
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync/**
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * Helper for SafeArray::__asOutParam() that automatically updates m.raw after a
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * non-NULL m.arr assignment.
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync */
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsyncclass OutSafeArrayDipper
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync{
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync OutSafeArrayDipper(SAFEARRAY **aArr, void **aRaw)
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync : arr(aArr), raw(aRaw) { Assert(*aArr == NULL && *aRaw == NULL); }
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync SAFEARRAY **arr;
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync void **raw;
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync template<class, class> friend class SafeArray;
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsyncpublic:
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync ~OutSafeArrayDipper()
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync {
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync if (*arr != NULL)
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync HRESULT rc = SafeArrayAccessData(*arr, raw);
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertComRC(rc);
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync }
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync }
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync operator SAFEARRAY **() { return arr; }
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync};
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#endif /* !VBOX_WITH_XPCOM */
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync////////////////////////////////////////////////////////////////////////////////
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync/**
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * The SafeArray class represents the safe array type used in COM to pass arrays
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * to/from interface methods.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * This helper class hides all MSCOM/XPCOM specific implementation details and,
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * together with ComSafeArrayIn, ComSafeArrayOut and ComSafeArrayRet macros,
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * provides a platform-neutral way to handle safe arrays in the method
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * implementation.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * When an instance of this class is destroyed, it automatically frees all
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * resources occupied by individual elements of the array as well as by the
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * array itself. However, when the value of an element is manually changed
dbea238999d6a4b34b9d1bdd1292e29717dd14bevboxsync * using #operator[] or by accessing array data through the #raw() pointer, it is
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * the caller's responsibility to free resources occupied by the previous
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * element's value.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * Also, objects of this class do not support copy and assignment operations and
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * therefore cannot be returned from functions by value. In other words, this
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * class is just a temporary storage for handling interface method calls and not
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * intended to be used to store arrays as data members and such -- you should
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * use normal list/vector classes for that.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *
dbea238999d6a4b34b9d1bdd1292e29717dd14bevboxsync * @note The current implementation supports only one-dimensional arrays.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * @note This class is not thread-safe.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsynctemplate<typename T, class Traits = SafeArrayTraits<T> >
29830933677ee9718064e760adf8462b6e79ffdevboxsyncclass SafeArray : public Traits
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync{
f9108665f82b59c99ac444815d75af51a14e46c7vboxsyncpublic:
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync /**
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * Creates a null array.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync */
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync SafeArray() {}
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync /**
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * Creates a new array of the given size. All elements of the newly created
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * array initialized with null values.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * @param aSize Initial number of elements in the array.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * @note If this object remains null after construction it means that there
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * was not enough memory for creating an array of the requested size.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * The constructor will also assert in this case.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync SafeArray(size_t aSize) { resize(aSize); }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync /**
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * Weakly attaches this instance to the existing array passed in a method
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * parameter declared using the ComSafeArrayIn macro. When using this call,
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * always wrap the parameter name in the ComSafeArrayInArg macro call like
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * this:
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * <pre>
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync * SafeArray safeArray(ComSafeArrayInArg(aArg));
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * </pre>
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * Note that this constructor doesn't take the ownership of the array. In
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * particular, it means that operations that operate on the ownership (e.g.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * #detachTo()) are forbidden and will assert.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * @param aArg Input method parameter to attach to.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync SafeArray(ComSafeArrayIn(T, aArg))
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync {
a49dafa13549ac5db76a3c7f5e44ba83485a1971vboxsync if (aArg)
a49dafa13549ac5db76a3c7f5e44ba83485a1971vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#ifdef VBOX_WITH_XPCOM
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
a49dafa13549ac5db76a3c7f5e44ba83485a1971vboxsync m.size = aArgSize;
a49dafa13549ac5db76a3c7f5e44ba83485a1971vboxsync m.arr = aArg;
a49dafa13549ac5db76a3c7f5e44ba83485a1971vboxsync m.isWeak = true;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#else /* !VBOX_WITH_XPCOM */
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
a49dafa13549ac5db76a3c7f5e44ba83485a1971vboxsync SAFEARRAY *arg = aArg;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertReturnVoid(arg->cDims == 1);
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync VARTYPE vt;
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync HRESULT rc = SafeArrayGetVartype(arg, &vt);
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertComRCReturnVoid(rc);
8164b4484483bf790b14bd94e37d743f93d9fec4vboxsync# ifndef VBOX_WITH_TYPE_TRAITS
8164b4484483bf790b14bd94e37d743f93d9fec4vboxsync AssertMsgReturnVoid(
8164b4484483bf790b14bd94e37d743f93d9fec4vboxsync vt == VarType()
8164b4484483bf790b14bd94e37d743f93d9fec4vboxsync || vt == VarTypeUnsigned(),
8164b4484483bf790b14bd94e37d743f93d9fec4vboxsync ("Expected vartype %d or %d, got %d.\n",
8164b4484483bf790b14bd94e37d743f93d9fec4vboxsync VarType(), VarTypeUnsigned(), vt));
8164b4484483bf790b14bd94e37d743f93d9fec4vboxsync# else /* !VBOX_WITH_TYPE_TRAITS */
8164b4484483bf790b14bd94e37d743f93d9fec4vboxsync AssertMsgReturnVoid(
8164b4484483bf790b14bd94e37d743f93d9fec4vboxsync vt == VarType(),
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync ("Expected vartype %d, got %d.\n",
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync VarType(), vt));
8164b4484483bf790b14bd94e37d743f93d9fec4vboxsync# endif
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync rc = SafeArrayAccessData(arg, (void HUGEP **)&m.raw);
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertComRCReturnVoid(rc);
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
a49dafa13549ac5db76a3c7f5e44ba83485a1971vboxsync m.arr = arg;
a49dafa13549ac5db76a3c7f5e44ba83485a1971vboxsync m.isWeak = true;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#endif /* !VBOX_WITH_XPCOM */
a49dafa13549ac5db76a3c7f5e44ba83485a1971vboxsync }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync /**
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync * Creates a deep copy of the given standard C++ container that stores
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync * T objects.
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync *
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * @param aCntr Container object to copy.
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync *
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * @param C Standard C++ container template class (normally deduced from
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * @c aCntr).
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync template<template<typename, typename> class C, class A>
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync SafeArray(const C<T, A> & aCntr)
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync resize(aCntr.size());
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertReturnVoid(!isNull());
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync size_t i = 0;
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync for (typename C<T, A>::const_iterator it = aCntr.begin();
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync it != aCntr.end(); ++ it, ++ i)
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#ifdef VBOX_WITH_XPCOM
9ccb4775161b8c4cd987a13ed61be6e3c62a27e3vboxsync SafeArray::Copy(*it, m.arr[i]);
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync#else
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync Copy(*it, m.raw[i]);
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync#endif
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync }
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync /**
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync * Creates a deep copy of the given standard C++ map that stores T objects
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync * as values.
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync *
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync * @param aMap Map object to copy.
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync *
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync * @param C Standard C++ map template class (normally deduced from
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync * @c aCntr).
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync * @param L Standard C++ compare class (deduced from @c aCntr).
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync * @param A Standard C++ allocator class (deduced from @c aCntr).
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync * @param K Map key class (deduced from @c aCntr).
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync template<template<typename, typename, typename, typename>
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync class C, class L, class A, class K>
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync SafeArray(const C<K, T, L, A> & aMap)
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync typedef C<K, T, L, A> Map;
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync resize(aMap.size());
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertReturnVoid(!isNull());
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync int i = 0;
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync for (typename Map::const_iterator it = aMap.begin();
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync it != aMap.end(); ++ it, ++ i)
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#ifdef VBOX_WITH_XPCOM
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync Copy(it->second, m.arr[i]);
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync#else
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync Copy(it->second, m.raw[i]);
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync#endif
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync }
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync /**
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * Destroys this instance after calling #setNull() to release allocated
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * resources. See #setNull() for more details.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync */
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync virtual ~SafeArray() { setNull(); }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync /**
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * Returns @c true if this instance represents a null array.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync */
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync bool isNull() const { return m.arr == NULL; }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
169f826dd7697156030b4a121548552f9b135b98vboxsync /**
169f826dd7697156030b4a121548552f9b135b98vboxsync * Returns @c true if this instance does not represents a null array.
169f826dd7697156030b4a121548552f9b135b98vboxsync */
169f826dd7697156030b4a121548552f9b135b98vboxsync bool isNotNull() const { return m.arr != NULL; }
169f826dd7697156030b4a121548552f9b135b98vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync /**
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * Resets this instance to null and, if this instance is not a weak one,
dbea238999d6a4b34b9d1bdd1292e29717dd14bevboxsync * releases any resources occupied by the array data.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * @note This method destroys (cleans up) all elements of the array using
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * the corresponding cleanup routine for the element type before the
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * array itself is destroyed.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync */
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync virtual void setNull() { m.uninit(); }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync /**
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * Returns @c true if this instance is weak. A weak instance doesn't own the
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * array data and therefore operations manipulating the ownership (e.g.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * #detachTo()) are forbidden and will assert.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync */
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync bool isWeak() const { return m.isWeak; }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync /** Number of elements in the array. */
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync size_t size() const
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#ifdef VBOX_WITH_XPCOM
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync if (m.arr)
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync return m.size;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync return 0;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync#else
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync if (m.arr)
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync return Size(m.arr->rgsabound[0].cElements);
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync return 0;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync#endif
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync /**
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * Appends a copy of the given element at the end of the array.
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync *
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * The array size is increased by one by this method and the additional
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * space is allocated as needed.
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync *
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * This method is handy in cases where you want to assign a copy of the
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * existing value to the array element, for example:
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync * <tt>Bstr string; array.push_back(string);</tt>. If you create a string
869259a9bb3c1b0f5f98519ca67e0b948bbd52a4vboxsync * just to put it in the array, you may find #appendedRaw() more useful.
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync *
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * @param aElement Element to append.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *
869259a9bb3c1b0f5f98519ca67e0b948bbd52a4vboxsync * @return @c true on success and @c false if there is not enough
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * memory for resizing.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync bool push_back(const T &aElement)
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync if (!ensureCapacity(size() + 1))
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync return false;
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#ifdef VBOX_WITH_XPCOM
9ccb4775161b8c4cd987a13ed61be6e3c62a27e3vboxsync SafeArray::Copy(aElement, m.arr[m.size]);
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync ++ m.size;
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync#else
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync Copy(aElement, m.raw[size() - 1]);
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync#endif
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync return true;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync /**
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * Appends an empty element at the end of the array and returns a raw
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * pointer to it suitable for assigning a raw value (w/o constructing a
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * copy).
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync *
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * The array size is increased by one by this method and the additional
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * space is allocated as needed.
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync *
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * Note that in case of raw assignment, value ownership (for types with
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * dynamically allocated data and for interface pointers) is transferred to
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * the safe array object.
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync *
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * This method is handy for operations like
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync * <tt>Bstr("foo").detachTo(array.appendedRaw());</tt>. Don't use it as
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync * an l-value (<tt>array.appendedRaw() = SysAllocString(L"tralala");</tt>)
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * since this doesn't check for a NULL condition; use #resize() and
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * #setRawAt() instead. If you need to assign a copy of the existing value
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * instead of transferring the ownership, look at #push_back().
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync *
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * @return Raw pointer to the added element or NULL if no memory.
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync */
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync T *appendedRaw()
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync if (!ensureCapacity(size() + 1))
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync return NULL;
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#ifdef VBOX_WITH_XPCOM
9ccb4775161b8c4cd987a13ed61be6e3c62a27e3vboxsync SafeArray::Init(m.arr[m.size]);
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync ++ m.size;
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync return &m.arr[m.size - 1];
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync#else
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync /* nothing to do here, SafeArrayCreate() has performed element
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * initialization */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync return &m.raw[size() - 1];
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync#endif
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync }
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync /**
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * Resizes the array preserving its contents when possible. If the new size
869259a9bb3c1b0f5f98519ca67e0b948bbd52a4vboxsync * is larger than the old size, new elements are initialized with null
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * values. If the new size is less than the old size, the contents of the
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * array beyond the new size is lost.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * @param aNewSize New number of elements in the array.
869259a9bb3c1b0f5f98519ca67e0b948bbd52a4vboxsync * @return @c true on success and @c false if there is not enough
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * memory for resizing.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync bool resize(size_t aNewSize)
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync if (!ensureCapacity(aNewSize))
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync return false;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#ifdef VBOX_WITH_XPCOM
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync if (m.size < aNewSize)
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync {
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync /* initialize the new elements */
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync for (size_t i = m.size; i < aNewSize; ++ i)
9ccb4775161b8c4cd987a13ed61be6e3c62a27e3vboxsync SafeArray::Init(m.arr[i]);
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync m.size = aNewSize;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync#else
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync /* nothing to do here, SafeArrayCreate() has performed element
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * initialization */
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync#endif
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync return true;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync /**
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * Reinitializes this instance by preallocating space for the given number
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * of elements. The previous array contents is lost.
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync *
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * @param aNewSize New number of elements in the array.
869259a9bb3c1b0f5f98519ca67e0b948bbd52a4vboxsync * @return @c true on success and @c false if there is not enough
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * memory for resizing.
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync bool reset(size_t aNewSize)
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync {
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync m.uninit();
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync return resize(aNewSize);
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync }
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync /**
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * Returns a pointer to the raw array data. Use this raw pointer with care
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * as no type or bound checking is done for you in this case.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * @note This method returns @c NULL when this instance is null.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * @see #operator[]
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync */
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync T *raw()
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#ifdef VBOX_WITH_XPCOM
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync return m.arr;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync#else
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync return m.raw;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync#endif
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync /**
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * Const version of #raw().
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync */
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync const T *raw() const
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#ifdef VBOX_WITH_XPCOM
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync return m.arr;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync#else
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync return m.raw;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync#endif
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync /**
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * Array access operator that returns an array element by reference. A bit
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * safer than #raw(): asserts and returns an invalid reference if this
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * instance is null or if the index is out of bounds.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *
dbea238999d6a4b34b9d1bdd1292e29717dd14bevboxsync * @note For weak instances, this call will succeed but the behavior of
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * changing the contents of an element of the weak array instance is
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * undefined and may lead to a program crash on some platforms.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync */
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync T &operator[] (size_t aIdx)
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertReturn(m.arr != NULL, *((T *)NULL));
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertReturn(aIdx < size(), *((T *)NULL));
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#ifdef VBOX_WITH_XPCOM
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync return m.arr[aIdx];
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync#else
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertReturn(m.raw != NULL, *((T *)NULL));
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync return m.raw[aIdx];
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync#endif
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync /**
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * Const version of #operator[] that returns an array element by value.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync */
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync const T operator[] (size_t aIdx) const
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertReturn(m.arr != NULL, *((T *)NULL));
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertReturn(aIdx < size(), *((T *)NULL));
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#ifdef VBOX_WITH_XPCOM
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync return m.arr[aIdx];
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync#else
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertReturn(m.raw != NULL, *((T *)NULL));
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync return m.raw[aIdx];
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync#endif
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync /**
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * Creates a copy of this array and stores it in a method parameter declared
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * using the ComSafeArrayOut macro. When using this call, always wrap the
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * parameter name in the ComSafeArrayOutArg macro call like this:
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * <pre>
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync * safeArray.cloneTo(ComSafeArrayOutArg(aArg));
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * </pre>
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * @note It is assumed that the ownership of the returned copy is
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * transferred to the caller of the method and he is responsible to free the
869259a9bb3c1b0f5f98519ca67e0b948bbd52a4vboxsync * array data when it is no longer needed.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * @param aArg Output method parameter to clone to.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync virtual const SafeArray &cloneTo(ComSafeArrayOut(T, aArg)) const
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync {
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync /// @todo Implement me!
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#ifdef VBOX_WITH_XPCOM
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync NOREF(aArgSize);
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync NOREF(aArg);
856a3d5f769f353598828838646be53f74258c90vboxsync#else
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync NOREF(aArg);
856a3d5f769f353598828838646be53f74258c90vboxsync#endif
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertFailedReturn(*this);
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync void cloneTo(SafeArray<T>& aOther) const
18dbcbb8555ee72f93ae6e7c08904d066f3d45d4vboxsync {
18dbcbb8555ee72f93ae6e7c08904d066f3d45d4vboxsync aOther.reset(size());
18dbcbb8555ee72f93ae6e7c08904d066f3d45d4vboxsync aOther.initFrom(*this);
18dbcbb8555ee72f93ae6e7c08904d066f3d45d4vboxsync }
18dbcbb8555ee72f93ae6e7c08904d066f3d45d4vboxsync
3f37f0cad4422f7703bf013668d4fce693e8a802vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync /**
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * Transfers the ownership of this array's data to the specified location
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * declared using the ComSafeArrayOut macro and makes this array a null
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * array. When using this call, always wrap the parameter name in the
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * ComSafeArrayOutArg macro call like this:
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * <pre>
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync * safeArray.detachTo(ComSafeArrayOutArg(aArg));
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * </pre>
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * Detaching the null array is also possible in which case the location will
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * receive NULL.
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync *
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * @note Since the ownership of the array data is transferred to the
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync * caller of the method, he is responsible to free the array data when it is
869259a9bb3c1b0f5f98519ca67e0b948bbd52a4vboxsync * no longer needed.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * @param aArg Location to detach to.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync virtual SafeArray &detachTo(ComSafeArrayOut(T, aArg))
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync {
a49dafa13549ac5db76a3c7f5e44ba83485a1971vboxsync AssertReturn(!m.isWeak, *this);
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#ifdef VBOX_WITH_XPCOM
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertReturn(aArgSize != NULL, *this);
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertReturn(aArg != NULL, *this);
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *aArgSize = m.size;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *aArg = m.arr;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync m.isWeak = false;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync m.size = 0;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync m.arr = NULL;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#else /* !VBOX_WITH_XPCOM */
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertReturn(aArg != NULL, *this);
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync *aArg = m.arr;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync if (m.raw)
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync HRESULT rc = SafeArrayUnaccessData(m.arr);
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertComRCReturn(rc, *this);
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync m.raw = NULL;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync m.isWeak = false;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync m.arr = NULL;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#endif /* !VBOX_WITH_XPCOM */
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync return *this;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
e081d2eefdae89dce06cd6451f0ce45bf023e6e3vboxsync /**
e081d2eefdae89dce06cd6451f0ce45bf023e6e3vboxsync * Returns a copy of this SafeArray as RTCList<T>.
e081d2eefdae89dce06cd6451f0ce45bf023e6e3vboxsync */
bd05e0cd7996ffdf46920daa2575635139e023b3vboxsync RTCList<T> toList()
bd05e0cd7996ffdf46920daa2575635139e023b3vboxsync {
bd05e0cd7996ffdf46920daa2575635139e023b3vboxsync RTCList<T> list(size());
bd05e0cd7996ffdf46920daa2575635139e023b3vboxsync for (size_t i = 0; i < size(); ++i)
e081d2eefdae89dce06cd6451f0ce45bf023e6e3vboxsync#ifdef VBOX_WITH_XPCOM
bd05e0cd7996ffdf46920daa2575635139e023b3vboxsync list.append(m.arr[i]);
e081d2eefdae89dce06cd6451f0ce45bf023e6e3vboxsync#else
bd05e0cd7996ffdf46920daa2575635139e023b3vboxsync list.append(m.raw[i]);
e081d2eefdae89dce06cd6451f0ce45bf023e6e3vboxsync#endif
bd05e0cd7996ffdf46920daa2575635139e023b3vboxsync return list;
bd05e0cd7996ffdf46920daa2575635139e023b3vboxsync }
e081d2eefdae89dce06cd6451f0ce45bf023e6e3vboxsync
18dbcbb8555ee72f93ae6e7c08904d066f3d45d4vboxsync inline void initFrom(const com::SafeArray<T> & aRef);
6d31d5f5af28ee2dbff98cceb06b5e42d1fa4134vboxsync inline void initFrom(const T* aPtr, size_t aSize);
3f37f0cad4422f7703bf013668d4fce693e8a802vboxsync
869259a9bb3c1b0f5f98519ca67e0b948bbd52a4vboxsync // Public methods for internal purposes only.
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#ifdef VBOX_WITH_XPCOM
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
dbea238999d6a4b34b9d1bdd1292e29717dd14bevboxsync /** Internal function. Never call it directly. */
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync PRUint32 *__asOutParam_Size() { setNull(); return &m.size; }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
dbea238999d6a4b34b9d1bdd1292e29717dd14bevboxsync /** Internal function Never call it directly. */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync T **__asOutParam_Arr() { Assert(isNull()); return &m.arr; }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#else /* !VBOX_WITH_XPCOM */
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
dbea238999d6a4b34b9d1bdd1292e29717dd14bevboxsync /** Internal function Never call it directly. */
25ae9d4809d959f11c3a69f743d3d9677507ad56vboxsync SAFEARRAY * __asInParam() { return m.arr; }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
dbea238999d6a4b34b9d1bdd1292e29717dd14bevboxsync /** Internal function Never call it directly. */
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync OutSafeArrayDipper __asOutParam()
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync { setNull(); return OutSafeArrayDipper(&m.arr, (void **)&m.raw); }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#endif /* !VBOX_WITH_XPCOM */
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync static const SafeArray Null;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsyncprotected:
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP(SafeArray)
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync /**
869259a9bb3c1b0f5f98519ca67e0b948bbd52a4vboxsync * Ensures that the array is big enough to contain aNewSize elements.
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync *
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * If the new size is greater than the current capacity, a new array is
869259a9bb3c1b0f5f98519ca67e0b948bbd52a4vboxsync * allocated and elements from the old array are copied over. The size of
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * the array doesn't change, only the capacity increases (which is always
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * greater than the size). Note that the additionally allocated elements are
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * left uninitialized by this method.
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync *
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * If the new size is less than the current size, the existing array is
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * truncated to the specified size and the elements outside the new array
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * boundary are freed.
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync *
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * If the new size is the same as the current size, nothing happens.
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync *
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * @param aNewSize New size of the array.
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync *
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * @return @c true on success and @c false if not enough memory.
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync bool ensureCapacity(size_t aNewSize)
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertReturn(!m.isWeak, false);
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#ifdef VBOX_WITH_XPCOM
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync /* Note: we distinguish between a null array and an empty (zero
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * elements) array. Therefore we never use zero in malloc (even if
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync * aNewSize is zero) to make sure we get a non-null pointer. */
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync if (m.size == aNewSize && m.arr != NULL)
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync return true;
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync
869259a9bb3c1b0f5f98519ca67e0b948bbd52a4vboxsync /* Allocate in 16-byte pieces. */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync size_t newCapacity = RT_MAX((aNewSize + 15) / 16 * 16, 16);
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync if (m.capacity != newCapacity)
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync T *newArr = (T *)nsMemory::Alloc(RT_MAX(newCapacity, 1) * sizeof(T));
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertReturn(newArr != NULL, false);
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync if (m.arr != NULL)
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync {
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync if (m.size > aNewSize)
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync {
869259a9bb3c1b0f5f98519ca67e0b948bbd52a4vboxsync /* Truncation takes place, uninit exceeding elements and
869259a9bb3c1b0f5f98519ca67e0b948bbd52a4vboxsync * shrink the size. */
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync for (size_t i = aNewSize; i < m.size; ++ i)
bb3ad91d3349be7a71137cc821beb3b76f094667vboxsync SafeArray::Uninit(m.arr[i]);
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync m.size = aNewSize;
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync }
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync
869259a9bb3c1b0f5f98519ca67e0b948bbd52a4vboxsync /* Copy the old contents. */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync memcpy(newArr, m.arr, m.size * sizeof(T));
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync nsMemory::Free((void *)m.arr);
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync }
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync m.arr = newArr;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync }
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync else
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync {
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync if (m.size > aNewSize)
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync {
869259a9bb3c1b0f5f98519ca67e0b948bbd52a4vboxsync /* Truncation takes place, uninit exceeding elements and
869259a9bb3c1b0f5f98519ca67e0b948bbd52a4vboxsync * shrink the size. */
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync for (size_t i = aNewSize; i < m.size; ++ i)
bb3ad91d3349be7a71137cc821beb3b76f094667vboxsync SafeArray::Uninit(m.arr[i]);
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync m.size = aNewSize;
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync }
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync }
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync m.capacity = newCapacity;
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync#else
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync SAFEARRAYBOUND bound = { VarCount(aNewSize), 0 };
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync HRESULT rc;
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync if (m.arr == NULL)
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync m.arr = CreateSafeArray(VarType(), &bound);
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertReturn(m.arr != NULL, false);
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync }
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync else
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync SafeArrayUnaccessData(m.arr);
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync rc = SafeArrayRedim(m.arr, &bound);
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertComRCReturn(rc == S_OK, false);
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync }
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync rc = SafeArrayAccessData(m.arr, (void HUGEP **)&m.raw);
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertComRCReturn(rc, false);
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync#endif
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync return true;
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync struct Data
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync {
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync Data()
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync : isWeak(false)
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#ifdef VBOX_WITH_XPCOM
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync , capacity(0), size(0), arr(NULL)
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync#else
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync , arr(NULL), raw(NULL)
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync#endif
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync {}
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync ~Data() { uninit(); }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync void uninit()
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#ifdef VBOX_WITH_XPCOM
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync if (arr)
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync {
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync if (!isWeak)
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync {
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync for (size_t i = 0; i < size; ++ i)
9ccb4775161b8c4cd987a13ed61be6e3c62a27e3vboxsync SafeArray::Uninit(arr[i]);
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync nsMemory::Free((void *)arr);
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync }
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync else
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync isWeak = false;
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync arr = NULL;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync size = capacity = 0;
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#else /* !VBOX_WITH_XPCOM */
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync if (arr)
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync {
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync if (raw)
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync SafeArrayUnaccessData(arr);
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync raw = NULL;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync if (!isWeak)
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync HRESULT rc = SafeArrayDestroy(arr);
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertComRCReturnVoid(rc);
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync }
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync else
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync isWeak = false;
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync arr = NULL;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#endif /* !VBOX_WITH_XPCOM */
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync }
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync bool isWeak : 1;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#ifdef VBOX_WITH_XPCOM
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync PRUint32 capacity;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync PRUint32 size;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync T *arr;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync#else
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync SAFEARRAY *arr;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync T *raw;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync#endif
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync };
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync Data m;
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync};
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
18dbcbb8555ee72f93ae6e7c08904d066f3d45d4vboxsync/* Few fast specializations for primitive array types */
3f37f0cad4422f7703bf013668d4fce693e8a802vboxsynctemplate<>
18dbcbb8555ee72f93ae6e7c08904d066f3d45d4vboxsyncinline void com::SafeArray<BYTE>::initFrom(const com::SafeArray<BYTE> & aRef)
3f37f0cad4422f7703bf013668d4fce693e8a802vboxsync{
3f37f0cad4422f7703bf013668d4fce693e8a802vboxsync size_t sSize = aRef.size();
3f37f0cad4422f7703bf013668d4fce693e8a802vboxsync resize(sSize);
3f37f0cad4422f7703bf013668d4fce693e8a802vboxsync ::memcpy(raw(), aRef.raw(), sSize);
3f37f0cad4422f7703bf013668d4fce693e8a802vboxsync}
6d31d5f5af28ee2dbff98cceb06b5e42d1fa4134vboxsynctemplate<>
6d31d5f5af28ee2dbff98cceb06b5e42d1fa4134vboxsyncinline void com::SafeArray<BYTE>::initFrom(const BYTE* aPtr, size_t aSize)
6d31d5f5af28ee2dbff98cceb06b5e42d1fa4134vboxsync{
6d31d5f5af28ee2dbff98cceb06b5e42d1fa4134vboxsync resize(aSize);
6d31d5f5af28ee2dbff98cceb06b5e42d1fa4134vboxsync ::memcpy(raw(), aPtr, aSize);
6d31d5f5af28ee2dbff98cceb06b5e42d1fa4134vboxsync}
6d31d5f5af28ee2dbff98cceb06b5e42d1fa4134vboxsync
3f37f0cad4422f7703bf013668d4fce693e8a802vboxsync
9171169d99a08b8dba6b409f0fbc598f01b5527fvboxsynctemplate<>
9171169d99a08b8dba6b409f0fbc598f01b5527fvboxsyncinline void com::SafeArray<SHORT>::initFrom(const com::SafeArray<SHORT> & aRef)
9171169d99a08b8dba6b409f0fbc598f01b5527fvboxsync{
9171169d99a08b8dba6b409f0fbc598f01b5527fvboxsync size_t sSize = aRef.size();
9171169d99a08b8dba6b409f0fbc598f01b5527fvboxsync resize(sSize);
9171169d99a08b8dba6b409f0fbc598f01b5527fvboxsync ::memcpy(raw(), aRef.raw(), sSize * sizeof(SHORT));
9171169d99a08b8dba6b409f0fbc598f01b5527fvboxsync}
9171169d99a08b8dba6b409f0fbc598f01b5527fvboxsynctemplate<>
9171169d99a08b8dba6b409f0fbc598f01b5527fvboxsyncinline void com::SafeArray<SHORT>::initFrom(const SHORT* aPtr, size_t aSize)
9171169d99a08b8dba6b409f0fbc598f01b5527fvboxsync{
9171169d99a08b8dba6b409f0fbc598f01b5527fvboxsync resize(aSize);
9171169d99a08b8dba6b409f0fbc598f01b5527fvboxsync ::memcpy(raw(), aPtr, aSize * sizeof(SHORT));
9171169d99a08b8dba6b409f0fbc598f01b5527fvboxsync}
9171169d99a08b8dba6b409f0fbc598f01b5527fvboxsync
9171169d99a08b8dba6b409f0fbc598f01b5527fvboxsynctemplate<>
9171169d99a08b8dba6b409f0fbc598f01b5527fvboxsyncinline void com::SafeArray<USHORT>::initFrom(const com::SafeArray<USHORT> & aRef)
9171169d99a08b8dba6b409f0fbc598f01b5527fvboxsync{
9171169d99a08b8dba6b409f0fbc598f01b5527fvboxsync size_t sSize = aRef.size();
9171169d99a08b8dba6b409f0fbc598f01b5527fvboxsync resize(sSize);
9171169d99a08b8dba6b409f0fbc598f01b5527fvboxsync ::memcpy(raw(), aRef.raw(), sSize * sizeof(USHORT));
9171169d99a08b8dba6b409f0fbc598f01b5527fvboxsync}
9171169d99a08b8dba6b409f0fbc598f01b5527fvboxsynctemplate<>
9171169d99a08b8dba6b409f0fbc598f01b5527fvboxsyncinline void com::SafeArray<USHORT>::initFrom(const USHORT* aPtr, size_t aSize)
9171169d99a08b8dba6b409f0fbc598f01b5527fvboxsync{
9171169d99a08b8dba6b409f0fbc598f01b5527fvboxsync resize(aSize);
9171169d99a08b8dba6b409f0fbc598f01b5527fvboxsync ::memcpy(raw(), aPtr, aSize * sizeof(USHORT));
9171169d99a08b8dba6b409f0fbc598f01b5527fvboxsync}
9171169d99a08b8dba6b409f0fbc598f01b5527fvboxsync
1068802cd5ebd6495e27178002fbefa977ca7154vboxsynctemplate<>
1068802cd5ebd6495e27178002fbefa977ca7154vboxsyncinline void com::SafeArray<LONG>::initFrom(const com::SafeArray<LONG> & aRef)
1068802cd5ebd6495e27178002fbefa977ca7154vboxsync{
1068802cd5ebd6495e27178002fbefa977ca7154vboxsync size_t sSize = aRef.size();
1068802cd5ebd6495e27178002fbefa977ca7154vboxsync resize(sSize);
1068802cd5ebd6495e27178002fbefa977ca7154vboxsync ::memcpy(raw(), aRef.raw(), sSize * sizeof(LONG));
1068802cd5ebd6495e27178002fbefa977ca7154vboxsync}
1068802cd5ebd6495e27178002fbefa977ca7154vboxsynctemplate<>
1068802cd5ebd6495e27178002fbefa977ca7154vboxsyncinline void com::SafeArray<LONG>::initFrom(const LONG* aPtr, size_t aSize)
1068802cd5ebd6495e27178002fbefa977ca7154vboxsync{
1068802cd5ebd6495e27178002fbefa977ca7154vboxsync resize(aSize);
1068802cd5ebd6495e27178002fbefa977ca7154vboxsync ::memcpy(raw(), aPtr, aSize * sizeof(LONG));
1068802cd5ebd6495e27178002fbefa977ca7154vboxsync}
1068802cd5ebd6495e27178002fbefa977ca7154vboxsync
1068802cd5ebd6495e27178002fbefa977ca7154vboxsync
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync////////////////////////////////////////////////////////////////////////////////
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#ifdef VBOX_WITH_XPCOM
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync/**
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * Version of com::SafeArray for arrays of GUID.
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync *
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * In MS COM, GUID arrays store GUIDs by value and therefore input arrays are
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * represented using |GUID *| and out arrays -- using |GUID **|. In XPCOM,
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * GUID arrays store pointers to nsID so that input arrays are |const nsID **|
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * and out arrays are |nsID ***|. Due to this difference, it is impossible to
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * work with arrays of GUID on both platforms by simply using com::SafeArray
dbea238999d6a4b34b9d1bdd1292e29717dd14bevboxsync * <GUID>. This class is intended to provide some level of cross-platform
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * behavior.
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync *
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * The basic usage pattern is basically similar to com::SafeArray<> except that
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * you use ComSafeGUIDArrayIn* and ComSafeGUIDArrayOut* macros instead of
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * ComSafeArrayIn* and ComSafeArrayOut*. Another important nuance is that the
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * raw() array type is different (nsID **, or GUID ** on XPCOM and GUID * on MS
869259a9bb3c1b0f5f98519ca67e0b948bbd52a4vboxsync * COM) so it is recommended to use operator[] instead which always returns a
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * GUID by value.
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync *
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * Note that due to const modifiers, you cannot use SafeGUIDArray for input GUID
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * arrays. Please use SafeConstGUIDArray for this instead.
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync *
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * Other than mentioned above, the functionality of this class is equivalent to
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * com::SafeArray<>. See the description of that template and its methods for
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * more information.
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync *
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * Output GUID arrays are handled by a separate class, SafeGUIDArrayOut, since
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * this class cannot handle them because of const modifiers.
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsyncclass SafeGUIDArray : public SafeArray<nsID *>
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync{
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsyncpublic:
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync typedef SafeArray<nsID *> Base;
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync class nsIDRef
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync {
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync public:
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync nsIDRef(nsID * &aVal) : mVal(aVal) {}
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync operator const nsID &() const { return mVal ? *mVal : *Empty; }
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync operator nsID() const { return mVal ? *mVal : *Empty; }
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync const nsID *operator&() const { return mVal ? mVal : Empty; }
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync nsIDRef &operator= (const nsID &aThat)
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync {
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync if (mVal == NULL)
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync Copy(&aThat, mVal);
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync else
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync *mVal = aThat;
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync return *this;
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync }
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync private:
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync nsID * &mVal;
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync static const nsID *Empty;
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync friend class SafeGUIDArray;
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync };
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync /** See SafeArray<>::SafeArray(). */
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync SafeGUIDArray() {}
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync /** See SafeArray<>::SafeArray(size_t). */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync SafeGUIDArray(size_t aSize) : Base(aSize) {}
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync /**
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * Array access operator that returns an array element by reference. As a
869259a9bb3c1b0f5f98519ca67e0b948bbd52a4vboxsync * special case, the return value of this operator on XPCOM is an nsID (GUID)
869259a9bb3c1b0f5f98519ca67e0b948bbd52a4vboxsync * reference, instead of an nsID pointer (the actual SafeArray template
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * argument), for compatibility with the MS COM version.
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync *
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * The rest is equivalent to SafeArray<>::operator[].
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync */
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync nsIDRef operator[] (size_t aIdx)
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync Assert(m.arr != NULL);
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync Assert(aIdx < size());
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync return nsIDRef(m.arr[aIdx]);
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync }
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync /**
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * Const version of #operator[] that returns an array element by value.
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync */
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync const nsID &operator[] (size_t aIdx) const
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync Assert(m.arr != NULL);
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync Assert(aIdx < size());
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync return m.arr[aIdx] ? *m.arr[aIdx] : *nsIDRef::Empty;
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync }
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync};
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync/**
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * Version of com::SafeArray for const arrays of GUID.
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync *
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * This class is used to work with input GUID array parameters in method
dbea238999d6a4b34b9d1bdd1292e29717dd14bevboxsync * implementations. See SafeGUIDArray for more details.
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsyncclass SafeConstGUIDArray : public SafeArray<const nsID *,
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync SafeArrayTraits<nsID *> >
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync{
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsyncpublic:
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync typedef SafeArray<const nsID *, SafeArrayTraits<nsID *> > Base;
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync /** See SafeArray<>::SafeArray(). */
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync SafeConstGUIDArray() {}
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync /* See SafeArray<>::SafeArray(ComSafeArrayIn(T, aArg)). */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync SafeConstGUIDArray(ComSafeGUIDArrayIn(aArg))
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync : Base(ComSafeGUIDArrayInArg(aArg)) {}
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync /**
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * Array access operator that returns an array element by reference. As a
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * special case, the return value of this operator on XPCOM is nsID (GUID)
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * instead of nsID *, for compatibility with the MS COM version.
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync *
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync * The rest is equivalent to SafeArray<>::operator[].
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync */
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync const nsID &operator[] (size_t aIdx) const
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertReturn(m.arr != NULL, **((const nsID * *)NULL));
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertReturn(aIdx < size(), **((const nsID * *)NULL));
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync return *m.arr[aIdx];
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync }
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsyncprivate:
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
869259a9bb3c1b0f5f98519ca67e0b948bbd52a4vboxsync /* These are disabled because of const. */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync bool reset(size_t aNewSize) { NOREF(aNewSize); return false; }
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync};
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#else /* !VBOX_WITH_XPCOM */
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsynctypedef SafeArray<GUID> SafeGUIDArray;
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsynctypedef SafeArray<const GUID, SafeArrayTraits<GUID> > SafeConstGUIDArray;
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#endif /* !VBOX_WITH_XPCOM */
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync////////////////////////////////////////////////////////////////////////////////
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#ifdef VBOX_WITH_XPCOM
dcea3eadf1ecc8f4ac868185a05a63be42a199e4vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsynctemplate<class I>
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsyncstruct SafeIfaceArrayTraits
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync{
29830933677ee9718064e760adf8462b6e79ffdevboxsyncprotected:
29830933677ee9718064e760adf8462b6e79ffdevboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static void Init(I * &aElem) { aElem = NULL; }
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static void Uninit(I * &aElem)
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync {
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync if (aElem)
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync {
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync aElem->Release();
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync aElem = NULL;
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync }
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync }
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static void Copy(I * aFrom, I * &aTo)
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync {
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync if (aFrom != NULL)
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync {
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync aTo = aFrom;
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync aTo->AddRef();
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync }
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync else
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync aTo = NULL;
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync }
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
29830933677ee9718064e760adf8462b6e79ffdevboxsyncpublic:
29830933677ee9718064e760adf8462b6e79ffdevboxsync
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync /* Magic to workaround strict rules of par. 4.4.4 of the C++ standard. */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static I **__asInParam_Arr(I **aArr) { return aArr; }
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static I **__asInParam_Arr(const I **aArr) { return const_cast<I **>(aArr); }
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync};
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#else /* !VBOX_WITH_XPCOM */
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsynctemplate<class I>
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsyncstruct SafeIfaceArrayTraits
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync{
29830933677ee9718064e760adf8462b6e79ffdevboxsyncprotected:
29830933677ee9718064e760adf8462b6e79ffdevboxsync
bf3c19723b48ea41e15dcd037d3693ec1b1a9a41vboxsync static VARTYPE VarType() { return VT_DISPATCH; }
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static ULONG VarCount(size_t aSize) { return (ULONG)aSize; }
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static size_t Size(ULONG aVarCount) { return (size_t)aVarCount; }
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static void Copy(I * aFrom, I * &aTo)
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync {
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync if (aFrom != NULL)
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync {
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync aTo = aFrom;
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync aTo->AddRef();
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync }
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync else
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync aTo = NULL;
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync }
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync static SAFEARRAY *CreateSafeArray(VARTYPE aVarType, SAFEARRAYBOUND *aBound)
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync NOREF(aVarType);
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync return SafeArrayCreateEx(VT_DISPATCH, 1, aBound, (PVOID)&_ATL_IIDOF(I));
f2a38a61f58a9252205f92a1b17f1e3eba4a91a1vboxsync }
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync};
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#endif /* !VBOX_WITH_XPCOM */
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync////////////////////////////////////////////////////////////////////////////////
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync/**
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * Version of com::SafeArray for arrays of interface pointers.
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync *
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * Except that it manages arrays of interface pointers, the usage of this class
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * is identical to com::SafeArray.
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync *
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * @param I Interface class (no asterisk).
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsynctemplate<class I>
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsyncclass SafeIfaceArray : public SafeArray<I *, SafeIfaceArrayTraits<I> >
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync{
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsyncpublic:
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync typedef SafeArray<I *, SafeIfaceArrayTraits<I> > Base;
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync /**
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * Creates a null array.
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync */
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync SafeIfaceArray() {}
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync /**
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * Creates a new array of the given size. All elements of the newly created
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * array initialized with null values.
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync *
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * @param aSize Initial number of elements in the array. Must be greater
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * than 0.
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync *
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * @note If this object remains null after construction it means that there
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * was not enough memory for creating an array of the requested size.
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * The constructor will also assert in this case.
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync SafeIfaceArray(size_t aSize) { Base::resize(aSize); }
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync /**
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * Weakly attaches this instance to the existing array passed in a method
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * parameter declared using the ComSafeArrayIn macro. When using this call,
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * always wrap the parameter name in the ComSafeArrayOutArg macro call like
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * this:
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * <pre>
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync * SafeArray safeArray(ComSafeArrayInArg(aArg));
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * </pre>
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync *
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * Note that this constructor doesn't take the ownership of the array. In
869259a9bb3c1b0f5f98519ca67e0b948bbd52a4vboxsync * particular, this means that operations that operate on the ownership
869259a9bb3c1b0f5f98519ca67e0b948bbd52a4vboxsync * (e.g. #detachTo()) are forbidden and will assert.
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync *
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * @param aArg Input method parameter to attach to.
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync SafeIfaceArray(ComSafeArrayIn(I *, aArg))
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync {
a49dafa13549ac5db76a3c7f5e44ba83485a1971vboxsync if (aArg)
a49dafa13549ac5db76a3c7f5e44ba83485a1971vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#ifdef VBOX_WITH_XPCOM
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
a49dafa13549ac5db76a3c7f5e44ba83485a1971vboxsync Base::m.size = aArgSize;
a49dafa13549ac5db76a3c7f5e44ba83485a1971vboxsync Base::m.arr = aArg;
a49dafa13549ac5db76a3c7f5e44ba83485a1971vboxsync Base::m.isWeak = true;
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#else /* !VBOX_WITH_XPCOM */
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
a49dafa13549ac5db76a3c7f5e44ba83485a1971vboxsync SAFEARRAY *arg = aArg;
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertReturnVoid(arg->cDims == 1);
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync VARTYPE vt;
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync HRESULT rc = SafeArrayGetVartype(arg, &vt);
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertComRCReturnVoid(rc);
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertMsgReturnVoid(vt == VT_UNKNOWN || vt == VT_DISPATCH,
8164b4484483bf790b14bd94e37d743f93d9fec4vboxsync ("Expected vartype VT_UNKNOWN or VT_DISPATCH, got %d.\n",
8164b4484483bf790b14bd94e37d743f93d9fec4vboxsync vt));
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync GUID guid;
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync rc = SafeArrayGetIID(arg, &guid);
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertComRCReturnVoid(rc);
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertMsgReturnVoid(InlineIsEqualGUID(_ATL_IIDOF(I), guid),
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync ("Expected IID {%RTuuid}, got {%RTuuid}.\n",
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync &_ATL_IIDOF(I), &guid));
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync rc = SafeArrayAccessData(arg, (void HUGEP **)&m.raw);
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertComRCReturnVoid(rc);
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
a49dafa13549ac5db76a3c7f5e44ba83485a1971vboxsync m.arr = arg;
a49dafa13549ac5db76a3c7f5e44ba83485a1971vboxsync m.isWeak = true;
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#endif /* !VBOX_WITH_XPCOM */
a49dafa13549ac5db76a3c7f5e44ba83485a1971vboxsync }
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync }
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync /**
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * Creates a deep copy of the given standard C++ container that stores
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync * interface pointers as objects of the ComPtr<I> class.
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync *
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * @param aCntr Container object to copy.
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync *
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * @param C Standard C++ container template class (normally deduced from
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * @c aCntr).
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * @param A Standard C++ allocator class (deduced from @c aCntr).
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * @param OI Argument to the ComPtr template (deduced from @c aCntr).
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync template<template<typename, typename> class C, class A, class OI>
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync SafeIfaceArray(const C<ComPtr<OI>, A> & aCntr)
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync typedef C<ComPtr<OI>, A> List;
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync Base::resize(aCntr.size());
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertReturnVoid(!Base::isNull());
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync int i = 0;
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync for (typename List::const_iterator it = aCntr.begin();
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync it != aCntr.end(); ++ it, ++ i)
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#ifdef VBOX_WITH_XPCOM
7e43d80769d96006c785b9883e449d0cff3b7973vboxsync this->Copy(*it, Base::m.arr[i]);
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync#else
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync Copy(*it, Base::m.raw[i]);
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync#endif
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync }
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync /**
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * Creates a deep copy of the given standard C++ container that stores
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync * interface pointers as objects of the ComObjPtr<I> class.
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync *
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * @param aCntr Container object to copy.
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync *
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * @param C Standard C++ container template class (normally deduced from
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * @c aCntr).
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * @param A Standard C++ allocator class (deduced from @c aCntr).
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync * @param OI Argument to the ComObjPtr template (deduced from @c aCntr).
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync template<template<typename, typename> class C, class A, class OI>
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync SafeIfaceArray(const C<ComObjPtr<OI>, A> & aCntr)
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync typedef C<ComObjPtr<OI>, A> List;
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync Base::resize(aCntr.size());
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertReturnVoid(!Base::isNull());
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync int i = 0;
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync for (typename List::const_iterator it = aCntr.begin();
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync it != aCntr.end(); ++ it, ++ i)
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#ifdef VBOX_WITH_XPCOM
9ccb4775161b8c4cd987a13ed61be6e3c62a27e3vboxsync SafeIfaceArray::Copy(*it, Base::m.arr[i]);
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync#else
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync Copy(*it, Base::m.raw[i]);
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync#endif
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync }
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync /**
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync * Creates a deep copy of the given standard C++ map whose values are
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync * interface pointers stored as objects of the ComPtr<I> class.
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync *
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync * @param aMap Map object to copy.
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync *
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync * @param C Standard C++ map template class (normally deduced from
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync * @c aCntr).
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync * @param L Standard C++ compare class (deduced from @c aCntr).
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync * @param A Standard C++ allocator class (deduced from @c aCntr).
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync * @param K Map key class (deduced from @c aCntr).
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync * @param OI Argument to the ComPtr template (deduced from @c aCntr).
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync template<template<typename, typename, typename, typename>
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync class C, class L, class A, class K, class OI>
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync SafeIfaceArray(const C<K, ComPtr<OI>, L, A> & aMap)
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync typedef C<K, ComPtr<OI>, L, A> Map;
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync Base::resize(aMap.size());
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertReturnVoid(!Base::isNull());
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync int i = 0;
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync for (typename Map::const_iterator it = aMap.begin();
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync it != aMap.end(); ++ it, ++ i)
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#ifdef VBOX_WITH_XPCOM
9ccb4775161b8c4cd987a13ed61be6e3c62a27e3vboxsync SafeIfaceArray::Copy(it->second, Base::m.arr[i]);
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync#else
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync Copy(it->second, Base::m.raw[i]);
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync#endif
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync }
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync /**
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync * Creates a deep copy of the given standard C++ map whose values are
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync * interface pointers stored as objects of the ComObjPtr<I> class.
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync *
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync * @param aMap Map object to copy.
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync *
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync * @param C Standard C++ map template class (normally deduced from
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync * @c aCntr).
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync * @param L Standard C++ compare class (deduced from @c aCntr).
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync * @param A Standard C++ allocator class (deduced from @c aCntr).
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync * @param K Map key class (deduced from @c aCntr).
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync * @param OI Argument to the ComObjPtr template (deduced from @c aCntr).
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync */
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync template<template<typename, typename, typename, typename>
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync class C, class L, class A, class K, class OI>
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync SafeIfaceArray(const C<K, ComObjPtr<OI>, L, A> & aMap)
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync typedef C<K, ComObjPtr<OI>, L, A> Map;
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync Base::resize(aMap.size());
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync AssertReturnVoid(!Base::isNull());
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync int i = 0;
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync for (typename Map::const_iterator it = aMap.begin();
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync it != aMap.end(); ++ it, ++ i)
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#ifdef VBOX_WITH_XPCOM
9ccb4775161b8c4cd987a13ed61be6e3c62a27e3vboxsync SafeIfaceArray::Copy(it->second, Base::m.arr[i]);
6009ff7dbed47f682276143b4483fa417e7211f1vboxsync#else
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync Copy(it->second, Base::m.raw[i]);
cb708a92b0b801600e8fcc7012a36fcb74908b67vboxsync#endif
cb708a92b0b801600e8fcc7012a36fcb74908b67vboxsync }
cb708a92b0b801600e8fcc7012a36fcb74908b67vboxsync
cb708a92b0b801600e8fcc7012a36fcb74908b67vboxsync void setElement(size_t iIdx, I* obj)
cb708a92b0b801600e8fcc7012a36fcb74908b67vboxsync {
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync#ifdef VBOX_WITH_XPCOM
9ccb4775161b8c4cd987a13ed61be6e3c62a27e3vboxsync SafeIfaceArray::Copy(obj, Base::m.arr[iIdx]);
cb708a92b0b801600e8fcc7012a36fcb74908b67vboxsync#else
9fe7370f586fb762a5387f0efa192b1fd9d20af2vboxsync Copy(obj, Base::m.raw[iIdx]);
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync#endif
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync }
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync};
7a18d4bae4d9ff1216be13970ae7815604b3365bvboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync} /* namespace com */
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync/** @} */
f9108665f82b59c99ac444815d75af51a14e46c7vboxsync
17bbfe820c22c3e599276890d8b7baa48c32abbbvboxsync#endif /* !___VBox_com_array_h */
17bbfe820c22c3e599276890d8b7baa48c32abbbvboxsync