VirtualBox

Ignore:
Timestamp:
Jul 12, 2020 11:08:50 PM (5 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
139263
Message:

Main: Refactored the generated event code implementation as Clang 11 claims va_start() on an VBoxEventType_T may trigger undefined behaviour due to type promotion (or something to that effect), but also because that variadict approach was horrible from the start. Refactored code as a dedicated factory method for each event, removing a dependency on VBoxEventDesc in veto cases. The fireXxxxEvent functions now return a HRESULT are no longer DECLINLINE (no need to compile all of them all the time). Reusable events got a ReinitXxxxxEvent function for helping with that. The VirtualBox::CallbackEvent stuff was a horrid misdesign from the start, it could've been done with a single class carrying a VBoxEventDesc member that the i_onXxxx methods would init() with all the event specific parameters and stuff. Refactored code passes a IEvent around, as that's even easier. I've left much of the old code in place for reference, but will remove once I've had a chance to test it a bit more (still struggling building VBox on the mac). bugref:9790

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/idl/comimpl.xsl

    r82972 r85286  
    127127      <xsl:choose>
    128128        <xsl:when test="$mod='ptr'">
    129           <xsl:value-of select="'BYTE*'" />
     129          <xsl:value-of select="'BYTE *'" />
    130130        </xsl:when>
    131131        <xsl:when test="(($type='wstring') or ($type='uuid'))">
     
    148148          <xsl:choose>
    149149            <xsl:when test="$param">
    150               <xsl:value-of select="concat($type,'*')"/>
     150              <xsl:value-of select="concat($type,' *')"/>
    151151            </xsl:when>
    152152            <xsl:otherwise>
     
    186186      </xsl:choose>
    187187      <xsl:if test="$dir='out'">
    188         <xsl:value-of select="'*'"/>
     188        <xsl:value-of select="' *'"/>
    189189      </xsl:if>
    190190      <xsl:if test="$param and not($param='_')">
     
    258258  <xsl:param name="name" />
    259259  <xsl:param name="obj" />
     260  <xsl:param name="va" select="true" />
    260261  <xsl:variable name="extends">
    261262    <xsl:value-of select="key('G_keyInterfacesByName', $name)/@extends" />
     
    271272        <xsl:with-param name="name" select="$extends" />
    272273        <xsl:with-param name="obj" select="$obj" />
     274        <xsl:with-param name="va" select="$va" />
    273275      </xsl:call-template>
    274276    </xsl:when>
     
    310312          </xsl:call-template>
    311313        </xsl:variable>
    312         <xsl:value-of select="       '#ifdef RT_OS_WINDOWS&#10;'"/>
    313         <xsl:value-of select="concat('              SAFEARRAY *aPtr_', @name, ' = va_arg(args, SAFEARRAY *);&#10;')"/>
    314         <xsl:value-of select="concat('              com::SafeArray&lt;', $elemtype,'&gt;   aArr_', @name, '(aPtr_', @name, ');&#10;')"/>
    315         <xsl:value-of select="       '#else&#10;'"/>
    316         <xsl:value-of select="concat('              PRUint32 aArrSize_', @name, ' = va_arg(args, PRUint32);&#10;')"/>
    317         <xsl:value-of select="concat('              void*    aPtr_', @name, ' = va_arg(args, void*);&#10;')"/>
    318         <xsl:value-of select="concat('              com::SafeArray&lt;', $elemtype,'&gt;   aArr_', @name, '(aArrSize_', @name, ', (', $elemtype,'*)aPtr_', @name, ');&#10;')"/>
    319         <xsl:value-of select="       '#endif&#10;'"/>
    320         <xsl:value-of select="concat('              ',$obj, '->set_', @name, '(ComSafeArrayAsInParam(aArr_', @name, '));&#10;')"/>
     314        <xsl:if test="$va">
     315          <xsl:value-of select="       '#ifdef RT_OS_WINDOWS&#10;'"/>
     316          <xsl:value-of select="concat('            SAFEARRAY *aPtr_', @name, ' = va_arg(args, SAFEARRAY *);&#10;')"/>
     317          <xsl:value-of select="concat('            com::SafeArray&lt;', $elemtype,'&gt;   aArr_', @name, '(aPtr_', @name, ');&#10;')"/>
     318          <xsl:value-of select="       '#else&#10;'"/>
     319          <xsl:value-of select="concat('            PRUint32 aArrSize_', @name, ' = va_arg(args, PRUint32);&#10;')"/>
     320          <xsl:value-of select="concat('            void*    aPtr_', @name, ' = va_arg(args, void*);&#10;')"/>
     321          <xsl:value-of select="concat('            com::SafeArray&lt;', $elemtype,'&gt;   aArr_', @name, '(aArrSize_', @name, ', (', $elemtype,'*)aPtr_', @name, ');&#10;')"/>
     322          <xsl:value-of select="       '#endif&#10;'"/>
     323          <xsl:value-of select="concat('            ',$obj, '->set_', @name, '(ComSafeArrayAsInParam(aArr_', @name, '));&#10;')"/>
     324        </xsl:if>
     325        <xsl:if test="not($va)">
     326          <xsl:value-of select="concat('            ',$obj, '->set_', @name, '(ComSafeArrayInArg(a_', @name, '));&#10;')"/>
     327        </xsl:if>
    321328      </xsl:when>
    322329      <xsl:when test="substring($aType, string-length($aType) - 1) = '_T'"> <!-- To avoid pedantic gcc warnings/errors. -->
    323         <xsl:value-of select="       '#ifdef VBOX_WITH_XPCOM_CPP_ENUM_HACK&#10;'"/>
    324         <xsl:value-of select="concat('              ',$aTypeName, ' = (',$aType,')va_arg(args, int);&#10;')"/>
    325         <xsl:value-of select="       '#else&#10;'"/>
    326         <xsl:value-of select="concat('              ',$aTypeName, ' = va_arg(args, ',$aType,');&#10;')"/>
    327         <xsl:value-of select="       '#endif&#10;'"/>
    328         <xsl:value-of select="concat('              ',$obj, '->set_', @name, '(',$aName, ');&#10;')"/>
     330        <xsl:if test="$va">
     331          <xsl:value-of select="       '#ifdef VBOX_WITH_XPCOM_CPP_ENUM_HACK&#10;'"/>
     332          <xsl:value-of select="concat('            ',$aTypeName, ' = (',$aType,')va_arg(args, int);&#10;')"/>
     333          <xsl:value-of select="       '#else&#10;'"/>
     334          <xsl:value-of select="concat('            ',$aTypeName, ' = va_arg(args, ',$aType,');&#10;')"/>
     335          <xsl:value-of select="       '#endif&#10;'"/>
     336        </xsl:if>
     337        <xsl:value-of select="concat('            ',$obj, '->set_', @name, '(',$aName, ');&#10;')"/>
    329338      </xsl:when>
    330339      <xsl:otherwise>
    331         <xsl:value-of select="concat('              ',$aTypeName, ' = va_arg(args, ',$aType,');&#10;')"/>
    332         <xsl:value-of select="concat('              ',$obj, '->set_', @name, '(',$aName, ');&#10;')"/>
     340        <xsl:if test="$va">
     341          <xsl:value-of select="concat('            ',$aTypeName, ' = va_arg(args, ',$aType,');&#10;')"/>
     342        </xsl:if>
     343        <xsl:value-of select="concat('            ',$obj, '->set_', @name, '(',$aName, ');&#10;')"/>
    333344      </xsl:otherwise>
    334345    </xsl:choose>
     
    489500
    490501  <xsl:value-of select="concat('class ATL_NO_VTABLE ',$implName,
    491                         '&#10;    : public VirtualBoxBase,&#10;      VBOX_SCRIPTABLE_IMPL(',
     502                        '&#10;    : public VirtualBoxBase&#10;    , VBOX_SCRIPTABLE_IMPL(',
    492503                        @name, ')&#10;{&#10;')" />
    493504  <xsl:value-of select="'public:&#10;'" />
     
    550561    <xsl:when test="$isVeto='yes'">
    551562<xsl:text><![CDATA[
    552     HRESULT init(IEventSource* aSource, VBoxEventType_T aType, BOOL aWaitable = TRUE)
     563    HRESULT init(IEventSource *aSource, VBoxEventType_T aType, BOOL aWaitable = TRUE)
    553564    {
    554565        NOREF(aWaitable);
     
    586597      <xsl:text>
    587598<![CDATA[
    588     HRESULT init(IEventSource* aSource, VBoxEventType_T aType, BOOL aWaitable = FALSE)
     599    HRESULT init(IEventSource *aSource, VBoxEventType_T aType, BOOL aWaitable = FALSE)
    589600    {
    590601        mGeneration = 1;
     
    593604    STDMETHOD(COMGETTER(Generation))(ULONG *aGeneration)
    594605    {
    595       *aGeneration = mGeneration;
    596       return S_OK;
     606        *aGeneration = mGeneration;
     607        return S_OK;
    597608    }
    598609    STDMETHOD(Reuse)()
    599610    {
    600        ASMAtomicIncU32((volatile uint32_t*)&mGeneration);
    601        return S_OK;
     611        ASMAtomicIncU32((volatile uint32_t *)&mGeneration);
     612        return S_OK;
    602613    }
    603614private:
     
    608619    <xsl:otherwise>
    609620<xsl:text><![CDATA[
    610     HRESULT init(IEventSource* aSource, VBoxEventType_T aType, BOOL aWaitable)
     621    HRESULT init(IEventSource *aSource, VBoxEventType_T aType, BOOL aWaitable)
    611622    {
    612623        return mEvent->init(aSource, aType, aWaitable);
     
    637648    <xsl:with-param name="parents" select="''" />
    638649  </xsl:call-template>
     650
     651  <!-- Split off the remainer into separate template? -->
     652  <xsl:variable name="evname">
     653    <xsl:value-of select="substring(@name, 2)" />
     654  </xsl:variable>
     655  <xsl:variable name="evid">
     656    <xsl:value-of select="concat('On', substring(@name, 2, string-length(@name)-6))" />
     657  </xsl:variable>
     658  <xsl:variable name="ifname">
     659    <xsl:value-of select="@name" />
     660  </xsl:variable>
     661  <xsl:variable name="waitable">
     662    <xsl:choose>
     663      <xsl:when test="@waitable='yes'">
     664        <xsl:value-of select="'TRUE'"/>
     665      </xsl:when>
     666      <xsl:otherwise>
     667        <xsl:value-of select="'FALSE'"/>
     668      </xsl:otherwise>
     669    </xsl:choose>
     670  </xsl:variable>
     671
     672  <!-- Generate ReinitXxxxEvent functions if reusable. -->
     673  <xsl:if test="$isReusable='yes'">
     674    <xsl:value-of select="concat('DECLHIDDEN(HRESULT) Reinit', $evname, '(IEvent *aEvent')"/>
     675    <xsl:call-template name="genFormalParams">
     676      <xsl:with-param name="name" select="$ifname" />
     677    </xsl:call-template>
     678    <xsl:text>)&#10;</xsl:text>
     679    <xsl:text>{&#10;</xsl:text>
     680    <xsl:text>    </xsl:text><xsl:value-of select="$implName"/><xsl:text> *pEvtImpl = dynamic_cast&lt;</xsl:text>
     681    <xsl:value-of select="$implName"/><xsl:text> *&gt;(aEvent);&#10;</xsl:text>
     682    <xsl:text>    if (pEvtImpl)&#10;</xsl:text>
     683    <xsl:text>    {&#10;</xsl:text>
     684    <xsl:text>        pEvtImpl->Reuse();&#10;</xsl:text>
     685    <xsl:text>        {&#10;</xsl:text>
     686    <xsl:call-template name="genAttrInitCode">
     687      <xsl:with-param name="name" select="@name" />
     688      <xsl:with-param name="obj" select="'pEvtImpl'" />
     689      <xsl:with-param name="va" select="false" />
     690    </xsl:call-template>
     691    <xsl:text>        }&#10;</xsl:text>
     692    <xsl:text>        return S_OK;&#10;</xsl:text>
     693    <xsl:text>    }&#10;</xsl:text>
     694    <xsl:text>    return E_INVALIDARG;&#10;</xsl:text>
     695    <xsl:text>}&#10;</xsl:text>
     696    <xsl:text>&#10;</xsl:text>
     697  </xsl:if>
     698
     699  <!-- Generate the CreateXxxxEvent function. -->
     700  <xsl:value-of select="concat('DECLHIDDEN(HRESULT) Create', $evname, '(IEvent **aEvent, IEventSource *aSource')"/>
     701  <xsl:call-template name="genFormalParams">
     702    <xsl:with-param name="name" select="$ifname" />
     703  </xsl:call-template>
     704  <xsl:text>)&#10;</xsl:text>
     705  <xsl:text>{&#10;</xsl:text>
     706  <xsl:text>    ComObjPtr&lt;</xsl:text><xsl:value-of select="$implName"/><xsl:text>&gt; EvtObj;&#10;</xsl:text>
     707  <xsl:text>    HRESULT hrc = EvtObj.createObject();&#10;</xsl:text>
     708  <xsl:text>    if (SUCCEEDED(hrc))&#10;</xsl:text>
     709  <xsl:text>    {&#10;</xsl:text>
     710  <xsl:text>        hrc = EvtObj-&gt;init(aSource, VBoxEventType_</xsl:text><xsl:value-of select="$evid"/>
     711  <xsl:text>, </xsl:text><xsl:value-of select="$waitable" /><xsl:text> /*waitable*/);&#10;</xsl:text>
     712  <xsl:text>        if (SUCCEEDED(hrc))&#10;</xsl:text>
     713  <xsl:text>        {&#10;</xsl:text>
     714  <xsl:call-template name="genAttrInitCode">
     715    <xsl:with-param name="name" select="@name" />
     716    <xsl:with-param name="obj" select="'EvtObj'" />
     717    <xsl:with-param name="va" select="false" />
     718  </xsl:call-template>
     719  <xsl:text>            hrc = EvtObj.queryInterfaceTo(aEvent);&#10;</xsl:text>
     720  <xsl:text>        }&#10;</xsl:text>
     721  <xsl:text>    }&#10;</xsl:text>
     722  <xsl:text>    return hrc;&#10;</xsl:text>
     723  <xsl:text>}&#10;</xsl:text>
     724  <xsl:text>&#10;</xsl:text>
     725
     726  <!-- Generate the fireXxxxEvent function. -->
     727  <xsl:value-of select="concat('DECLHIDDEN(HRESULT) fire', $evname, '(IEventSource *aSource')"/>
     728  <xsl:call-template name="genFormalParams">
     729    <xsl:with-param name="name" select="$ifname" />
     730  </xsl:call-template>
     731  <xsl:text>)&#10;</xsl:text>
     732  <xsl:text>{&#10;</xsl:text>
     733  <xsl:text>    ComPtr&lt;IEvent&gt; ptrEvent;&#10;</xsl:text>
     734  <xsl:text>    HRESULT hrc = </xsl:text>
     735  <xsl:value-of select="concat('Create', $evname, '(ptrEvent.asOutParam(), aSource')"/>
     736  <xsl:call-template name="genFactParams">
     737    <xsl:with-param name="name" select="$ifname" />
     738  </xsl:call-template>
     739  <xsl:text>);&#10;</xsl:text>
     740  <xsl:text>    if (SUCCEEDED(hrc))&#10;</xsl:text>
     741  <xsl:text>    {&#10;</xsl:text>
     742  <xsl:text>        VBoxEventDesc EvtDesc(ptrEvent, aSource);&#10;</xsl:text>
     743  <xsl:text>        EvtDesc.fire(/* do not wait for delivery */ 0);&#10;</xsl:text>
     744  <xsl:text>    }&#10;</xsl:text>
     745  <xsl:text>    return hrc;&#10;</xsl:text>
     746  <xsl:text>}&#10;</xsl:text>
     747  <xsl:text>&#10;</xsl:text>
    639748
    640749</xsl:template>
     
    691800#include <iprt/asm.h>
    692801#include "EventImpl.h"
     802#include "VBoxEvents.h"
    693803]]></xsl:text>
    694804
     
    723833
    724834  <xsl:text><![CDATA[
     835#if 0
    725836HRESULT VBoxEventDesc::init(IEventSource *aSource, VBoxEventType_T aType, ...)
    726837{
     
    752863    return S_OK;
    753864}
     865#endif
    754866]]></xsl:text>
    755867
    756868 <xsl:text><![CDATA[
     869#if 0
    757870HRESULT VBoxEventDesc::reinit(VBoxEventType_T aType, ...)
    758871{
     
    783896    return S_OK;
    784897}
     898#endif
    785899]]></xsl:text>
    786900
     
    868982<xsl:text><![CDATA[
    869983#include "EventImpl.h"
     984
    870985]]></xsl:text>
    871986
    872   <!-- Interfaces -->
     987  <!-- Simple methods for firing off events. -->
     988 <xsl:text>/** @name Fire off events&#10;</xsl:text>
     989 <xsl:text> * @{ */&#10;</xsl:text>
    873990  <xsl:for-each select="//interface[@autogen='VBoxEvent']">
    874     <xsl:value-of select="concat('// ', @name,  ' generation routine &#10;')" />
     991    <xsl:value-of select="concat('/** Fire an ', @name,  ' event. */&#10;')" />
    875992    <xsl:variable name="evname">
    876993      <xsl:value-of select="substring(@name, 2)" />
     
    8841001    </xsl:variable>
    8851002
    886     <xsl:value-of select="concat('DECLINLINE(void) fire', $evname, '(IEventSource* aSource')"/>
     1003    <!--
     1004    OLD:
     1005    <xsl:value-of select="concat('DECLINLINE(void) fire', $evname, '(IEventSource *aSource')"/>
    8871006    <xsl:call-template name="genFormalParams">
    8881007      <xsl:with-param name="name" select="$ifname" />
     
    8981017    <xsl:value-of select="       '    evDesc.fire(/* do not wait for delivery */ 0);&#10;'"/>
    8991018    <xsl:value-of select="       '}&#10;'"/>
     1019
     1020    NEW:
     1021    -->
     1022    <xsl:value-of select="concat('DECLHIDDEN(HRESULT) fire', $evname, '(IEventSource *aSource')"/>
     1023    <xsl:call-template name="genFormalParams">
     1024      <xsl:with-param name="name" select="$ifname" />
     1025    </xsl:call-template>
     1026    <xsl:text>);&#10;</xsl:text>
    9001027  </xsl:for-each>
     1028  <xsl:text>/** @} */&#10;&#10;</xsl:text>
     1029
     1030  <!-- Event instantiation methods. -->
     1031  <xsl:text>/** @name Instantiate events&#10;</xsl:text>
     1032  <xsl:text> * @{ */&#10;</xsl:text>
     1033  <xsl:for-each select="//interface[@autogen='VBoxEvent']">
     1034    <xsl:value-of select="concat('/** Create an ', @name,  ' event. */&#10;')" />
     1035    <xsl:variable name="evname">
     1036      <xsl:value-of select="substring(@name, 2)" />
     1037    </xsl:variable>
     1038    <xsl:variable name="evid">
     1039      <xsl:value-of select="concat('On', substring(@name, 2, string-length(@name)-6))" />
     1040    </xsl:variable>
     1041    <xsl:variable name="ifname">
     1042      <xsl:value-of select="@name" />
     1043    </xsl:variable>
     1044
     1045    <xsl:value-of select="concat('DECLHIDDEN(HRESULT) Create', $evname, '(IEvent **aEvent, IEventSource *aSource')"/>
     1046    <xsl:call-template name="genFormalParams">
     1047      <xsl:with-param name="name" select="$ifname" />
     1048    </xsl:call-template>
     1049    <xsl:text>);&#10;</xsl:text>
     1050  </xsl:for-each>
     1051  <xsl:text>/** @} */&#10;</xsl:text>
     1052  <xsl:text>&#10;</xsl:text>
     1053
     1054  <!-- Reinitialization methods for reusable events. -->
     1055  <xsl:text>/** @name Re-init reusable events&#10;</xsl:text>
     1056  <xsl:text> * @{ */&#10;</xsl:text>
     1057  <xsl:for-each select="//interface[@autogen='VBoxEvent']">
     1058    <xsl:if test="@extends='IReusableEvent'">
     1059      <xsl:value-of select="concat('/** Re-init an ', @name,  ' event. */&#10;')" />
     1060      <xsl:variable name="evname">
     1061        <xsl:value-of select="substring(@name, 2)" />
     1062      </xsl:variable>
     1063      <xsl:variable name="ifname">
     1064        <xsl:value-of select="@name" />
     1065      </xsl:variable>
     1066
     1067      <xsl:value-of select="concat('DECLHIDDEN(HRESULT) Reinit', $evname, '(IEvent *aEvent')"/>
     1068      <xsl:call-template name="genFormalParams">
     1069        <xsl:with-param name="name" select="$ifname" />
     1070      </xsl:call-template>
     1071      <xsl:text>);&#10;</xsl:text>
     1072    </xsl:if>
     1073  </xsl:for-each>
     1074  <xsl:text>/** @} */&#10;</xsl:text>
     1075
    9011076</xsl:template>
    9021077
Note: See TracChangeset for help on using the changeset viewer.

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