VirtualBox

Changeset 33959 in vbox


Ignore:
Timestamp:
Nov 11, 2010 9:41:52 AM (14 years ago)
Author:
vboxsync
Message:

Main: code for generic listener creation

Location:
trunk/src/VBox/Main
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/ConsoleImpl.cpp

    r33952 r33959  
    255255inline static const char *networkAdapterTypeToName(NetworkAdapterType_T adapterType);
    256256
    257 class VmEventListener :
    258   VBOX_SCRIPTABLE_IMPL(IEventListener)
    259 {
     257class VmEventListener {
    260258public:
    261259    VmEventListener(Console *aConsole)
    262260    {
    263 #ifndef VBOX_WITH_XPCOM
    264         refcnt = 0;
    265 #endif
    266261        mfNoLoggedInUsers = true;
    267262        mConsole = aConsole;
     
    272267    }
    273268
    274 #ifndef VBOX_WITH_XPCOM
    275     STDMETHOD_(ULONG, AddRef)()
    276     {
    277         return ::InterlockedIncrement(&refcnt);
    278     }
    279     STDMETHOD_(ULONG, Release)()
    280     {
    281         long cnt = ::InterlockedDecrement(&refcnt);
    282         if (cnt == 0)
    283             delete this;
    284         return cnt;
    285     }
    286 #endif
    287     VBOX_SCRIPTABLE_DISPATCH_IMPL(IEventListener)
    288 
    289     NS_DECL_ISUPPORTS
    290 
    291     STDMETHOD(HandleEvent)(IEvent * aEvent)
    292     {
    293         VBoxEventType_T aType = VBoxEventType_Invalid;
    294         aEvent->COMGETTER(Type)(&aType);
     269    STDMETHOD(HandleEvent)(VBoxEventType_T aType, IEvent * aEvent)
     270    {
    295271        switch(aType)
    296272        {
     
    337313    }
    338314private:
    339 #ifndef VBOX_WITH_XPCOM
    340     long refcnt;
    341 #endif
    342 
    343315    bool mfNoLoggedInUsers;
    344316    Console *mConsole;
    345317};
    346318
    347 #ifdef VBOX_WITH_XPCOM
    348 NS_DECL_CLASSINFO(VmEventListener)
    349 NS_IMPL_THREADSAFE_ISUPPORTS1_CI(VmEventListener, IEventListener)
    350 #endif
     319typedef ListenerImpl<VmEventListener, Console*> VmEventListenerImpl;
     320
     321VBOX_LISTENER_DECLARE(VmEventListenerImpl)
    351322
    352323// ConsoleCallbackRegistration
     
    600571        rc = virtualbox->COMGETTER(EventSource)(es.asOutParam());
    601572        AssertComRC(rc);
    602         mVmListner = new VmEventListener(this);
    603         mVmListner->AddRef();
     573        mVmListner = new VmEventListenerImpl(this);
    604574        com::SafeArray <VBoxEventType_T> eventTypes;
    605575        eventTypes.push_back(VBoxEventType_OnNATRedirectEvent);
  • trunk/src/VBox/Main/ConsoleVRDPServer.cpp

    r33676 r33959  
    3535#include <VBox/RemoteDesktop/VRDEOrders.h>
    3636
    37 class VRDPConsoleListener :
    38     VBOX_SCRIPTABLE_IMPL(IEventListener)
     37class VRDPConsoleListener
    3938{
    4039public:
     
    4241        : m_server(server)
    4342    {
    44 #ifndef VBOX_WITH_XPCOM
    45         refcnt = 0;
    46 #endif /* !VBOX_WITH_XPCOM */
    47     }
    48 
    49     virtual ~VRDPConsoleListener() {}
    50 
    51     NS_DECL_ISUPPORTS
    52 
    53 #ifndef VBOX_WITH_XPCOM
    54     STDMETHOD_(ULONG, AddRef)() {
    55         return ::InterlockedIncrement(&refcnt);
    56     }
    57     STDMETHOD_(ULONG, Release)()
    58     {
    59         long cnt = ::InterlockedDecrement(&refcnt);
    60         if (cnt == 0)
    61             delete this;
    62         return cnt;
    63     }
    64     STDMETHOD(QueryInterface)(REFIID riid , void **ppObj)
    65     {
    66         if (riid == IID_IUnknown) {
    67             *ppObj = (IUnknown*)this;
    68             AddRef();
    69             return S_OK;
    70         }
    71         if (riid == IID_IEventListener) {
    72             *ppObj = (IEventListener*)this;
    73             AddRef();
    74             return S_OK;
    75         }
    76         *ppObj = NULL;
    77         return E_NOINTERFACE;
    78     }
    79 #endif /* !VBOX_WITH_XPCOM */
    80 
    81 
    82     STDMETHOD(HandleEvent)(IEvent * aEvent)
    83     {
    84         VBoxEventType_T aType = VBoxEventType_Invalid;
    85 
    86         aEvent->COMGETTER(Type)(&aType);
     43    }
     44
     45    STDMETHOD(HandleEvent)(VBoxEventType_T aType, IEvent * aEvent)
     46    {
    8747        switch (aType)
    8848        {
     
    145105                                         ULONG width, ULONG height, ComSafeArrayIn(BYTE,shape));
    146106    ConsoleVRDPServer *m_server;
    147 #ifndef VBOX_WITH_XPCOM
    148     long refcnt;
    149 #endif /* !VBOX_WITH_XPCOM */
    150107};
    151108
    152 #ifdef VBOX_WITH_XPCOM
    153 #include <nsMemory.h>
    154 NS_DECL_CLASSINFO(VRDPConsoleListener)
    155 NS_IMPL_THREADSAFE_ISUPPORTS1_CI(VRDPConsoleListener, IEventListener)
    156 #endif /* VBOX_WITH_XPCOM */
     109typedef ListenerImpl<VRDPConsoleListener, ConsoleVRDPServer*> VRDPConsoleListenerImpl;
     110
     111VBOX_LISTENER_DECLARE(VRDPConsoleListenerImpl)
    157112
    158113#ifdef DEBUG_sunlover
     
    12201175        ComPtr<IEventSource> es;
    12211176        console->COMGETTER(EventSource)(es.asOutParam());
    1222         mConsoleListener = new VRDPConsoleListener(this);
    1223         mConsoleListener->AddRef();
     1177        mConsoleListener = new VRDPConsoleListenerImpl(this);
    12241178        com::SafeArray <VBoxEventType_T> eventTypes;
    12251179        eventTypes.push_back(VBoxEventType_OnMousePointerShapeChanged);
  • trunk/src/VBox/Main/idl/VirtualBox.xidl

    r33952 r33959  
    1559515595
    1559615596  <interface
    15597     name="INATRedirectEvent" extends="IEvent"
    15598     uuid="b566336e-ec24-11df-8fbd-f71b6f29dd03"
     15597    name="INATRedirectEvent" extends="IMachineEvent"
     15598    uuid="57DE97D7-3CBB-42A0-888F-610D5832D16B"
    1559915599    wsmap="managed" autogen="VBoxEvent" id="OnNATRedirectEvent"
    1560015600    >
     
    1560215602      Notification when NAT redirect rule added or removed.
    1560315603   </desc>
    15604      <attribute name="machineId" type="uuid" mod="string" readonly="yes">
    15605        <desc>
    15606         Machine id where redirect event is occured.
    15607        </desc>
    15608      </attribute>
    1560915604     <attribute name="slot"  type="unsigned long" readonly="yes">
    1561015605       <desc>
  • trunk/src/VBox/Main/include/EventImpl.h

    r33298 r33959  
    2020
    2121#include "VirtualBoxBase.h"
     22#include <iprt/asm.h>
    2223
    2324class ATL_NO_VTABLE VBoxEvent :
     
    229230};
    230231
     232#define NS_IMPL_QUERY_HEAD_INLINE()                                   \
     233NS_IMETHODIMP QueryInterface(REFNSIID aIID, void** aInstancePtr)      \
     234{                                                                     \
     235  NS_ASSERTION(aInstancePtr,                                          \
     236               "QueryInterface requires a non-NULL destination!");    \
     237  nsISupports* foundInterface;
     238
     239#ifndef RT_OS_WINDOWS
     240#define NS_INTERFACE_MAP_BEGIN_INLINE()      NS_IMPL_QUERY_HEAD_INLINE()
     241#define NS_IMPL_QUERY_INTERFACE1_INLINE(_i1)                                 \
     242  NS_INTERFACE_MAP_BEGIN_INLINE()                                            \
     243    NS_INTERFACE_MAP_ENTRY(_i1)                                              \
     244    NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, _i1)                       \
     245  NS_INTERFACE_MAP_END
     246#endif
     247
     248template <class T, class TParam = void* >
     249class ListenerImpl : VBOX_SCRIPTABLE_IMPL(IEventListener)
     250{
     251    T                 mListener;
     252    volatile uint32_t mRefCnt;
     253
     254    virtual ~ListenerImpl()
     255    {}
     256
     257public:
     258    ListenerImpl(TParam param)
     259    : mListener(param), mRefCnt(1)
     260    {
     261    }
     262
     263    /* On Windows QI implemented by VBOX_SCRIPTABLE_DISPATCH_IMPL */
     264#ifndef RT_OS_WINDOWS
     265    NS_IMPL_QUERY_INTERFACE1_INLINE(IEventListener)
     266#endif
     267
     268#ifdef RT_OS_WINDOWS
     269    STDMETHOD_(ULONG, AddRef)()
     270#else
     271    NS_IMETHOD_(nsrefcnt) AddRef(void)
     272#endif
     273    {
     274        return ASMAtomicIncU32(&mRefCnt);
     275    }
     276
     277#ifdef RT_OS_WINDOWS
     278    STDMETHOD_(ULONG, Release)()
     279#else
     280    NS_IMETHOD_(nsrefcnt) Release(void)
     281#endif
     282    {
     283        uint32_t cnt = ::ASMAtomicDecU32(&mRefCnt);
     284        if (cnt == 0)
     285            delete this;
     286        return cnt;
     287    }
     288
     289    VBOX_SCRIPTABLE_DISPATCH_IMPL(IEventListener)
     290
     291    STDMETHOD(HandleEvent)(IEvent * aEvent)
     292    {
     293        VBoxEventType_T aType = VBoxEventType_Invalid;
     294        aEvent->COMGETTER(Type)(&aType);
     295        return mListener.HandleEvent(aType, aEvent);
     296    }
     297};
     298
     299#ifdef VBOX_WITH_XPCOM
     300#define VBOX_LISTENER_DECLARE(klazz) NS_DECL_CLASSINFO(klazz)
     301#else
     302#define VBOX_LISTENER_DECLARE(klazz)
     303#endif
     304
    231305#endif // ____H_EVENTIMPL
Note: See TracChangeset for help on using the changeset viewer.

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