VirtualBox

source: vbox/trunk/src/VBox/Main/include/EventImpl.h@ 33959

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

Main: code for generic listener creation

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.1 KB
Line 
1/** @file
2 *
3 * VirtualBox COM IEvent implementation
4 */
5
6/*
7 * Copyright (C) 2010 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#ifndef ____H_EVENTIMPL
19#define ____H_EVENTIMPL
20
21#include "VirtualBoxBase.h"
22#include <iprt/asm.h>
23
24class ATL_NO_VTABLE VBoxEvent :
25 public VirtualBoxBase,
26 VBOX_SCRIPTABLE_IMPL(IEvent)
27{
28public:
29 VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT(VBoxEvent, IEvent)
30
31 DECLARE_NOT_AGGREGATABLE(VBoxEvent)
32
33 DECLARE_PROTECT_FINAL_CONSTRUCT()
34
35 BEGIN_COM_MAP(VBoxEvent)
36 COM_INTERFACE_ENTRY(ISupportErrorInfo)
37 COM_INTERFACE_ENTRY(IEvent)
38 COM_INTERFACE_ENTRY(IDispatch)
39 END_COM_MAP()
40
41 VBoxEvent() {}
42 virtual ~VBoxEvent() {}
43
44 HRESULT FinalConstruct();
45 void FinalRelease();
46
47 // public initializer/uninitializer for internal purposes only
48 HRESULT init (IEventSource *aSource, VBoxEventType_T aType, BOOL aWaitable);
49 void uninit();
50
51 // IEvent properties
52 STDMETHOD(COMGETTER(Type)) (VBoxEventType_T *aType);
53 STDMETHOD(COMGETTER(Source)) (IEventSource * *aSource);
54 STDMETHOD(COMGETTER(Waitable)) (BOOL *aWaitable);
55
56 // IEvent methods
57 STDMETHOD(SetProcessed)();
58 STDMETHOD(WaitProcessed)(LONG aTimeout, BOOL *aResult);
59
60private:
61 struct Data;
62
63 Data* m;
64};
65
66class ATL_NO_VTABLE VBoxVetoEvent :
67 public VBoxEvent,
68 VBOX_SCRIPTABLE_IMPL(IVetoEvent)
69{
70public:
71 VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT(VBoxVetoEvent, IVetoEvent)
72
73 DECLARE_NOT_AGGREGATABLE(VBoxVetoEvent)
74
75 DECLARE_PROTECT_FINAL_CONSTRUCT()
76
77 BEGIN_COM_MAP(VBoxVetoEvent)
78 COM_INTERFACE_ENTRY(ISupportErrorInfo)
79 COM_INTERFACE_ENTRY2(IEvent, IVetoEvent)
80 COM_INTERFACE_ENTRY(IVetoEvent)
81 COM_INTERFACE_ENTRY2(IDispatch, IVetoEvent)
82 END_COM_MAP()
83
84 VBoxVetoEvent() {}
85 virtual ~VBoxVetoEvent() {}
86
87 HRESULT FinalConstruct();
88 void FinalRelease();
89
90 // public initializer/uninitializer for internal purposes only
91 HRESULT init (IEventSource *aSource, VBoxEventType_T aType);
92 void uninit();
93
94 // IEvent properties
95 STDMETHOD(COMGETTER(Type)) (VBoxEventType_T *aType)
96 {
97 return VBoxEvent::COMGETTER(Type)(aType);
98 }
99 STDMETHOD(COMGETTER(Source)) (IEventSource * *aSource)
100 {
101 return VBoxEvent::COMGETTER(Source)(aSource);
102 }
103 STDMETHOD(COMGETTER(Waitable)) (BOOL *aWaitable)
104 {
105 return VBoxEvent::COMGETTER(Waitable)(aWaitable);
106 }
107
108 // IEvent methods
109 STDMETHOD(SetProcessed)()
110 {
111 return VBoxEvent::SetProcessed();
112 }
113 STDMETHOD(WaitProcessed)(LONG aTimeout, BOOL *aResult)
114 {
115 return VBoxEvent::WaitProcessed(aTimeout, aResult);
116 }
117
118 // IVetoEvent methods
119 STDMETHOD(AddVeto)(IN_BSTR aVeto);
120 STDMETHOD(IsVetoed)(BOOL *aResult);
121 STDMETHOD(GetVetos)(ComSafeArrayOut(BSTR, aVetos));
122
123private:
124 struct Data;
125
126 Data* m;
127};
128
129class ATL_NO_VTABLE EventSource :
130 public VirtualBoxBase,
131 VBOX_SCRIPTABLE_IMPL(IEventSource)
132{
133public:
134
135 VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT(EventSource, IEventSource)
136
137 DECLARE_NOT_AGGREGATABLE(EventSource)
138
139 DECLARE_PROTECT_FINAL_CONSTRUCT()
140
141 BEGIN_COM_MAP(EventSource)
142 COM_INTERFACE_ENTRY(ISupportErrorInfo)
143 COM_INTERFACE_ENTRY(IEventSource)
144 COM_INTERFACE_ENTRY(IDispatch)
145 END_COM_MAP()
146
147 DECLARE_EMPTY_CTOR_DTOR (EventSource)
148
149 HRESULT FinalConstruct();
150 void FinalRelease();
151
152 // public initializer/uninitializer for internal purposes only
153 HRESULT init (IUnknown * aParent);
154 void uninit();
155
156 // IEventSource methods
157 STDMETHOD(CreateListener)(IEventListener ** aListener);
158 STDMETHOD(CreateAggregator)(ComSafeArrayIn(IEventSource*, aSubordinates),
159 IEventSource ** aAggregator);
160 STDMETHOD(RegisterListener)(IEventListener * aListener,
161 ComSafeArrayIn(VBoxEventType_T, aInterested),
162 BOOL aActive);
163 STDMETHOD(UnregisterListener)(IEventListener * aListener);
164 STDMETHOD(FireEvent)(IEvent * aEvent,
165 LONG aTimeout,
166 BOOL *aProcessed);
167 STDMETHOD(GetEvent)(IEventListener * aListener,
168 LONG aTimeout,
169 IEvent * *aEvent);
170 STDMETHOD(EventProcessed)(IEventListener * aListener,
171 IEvent * aEvent);
172
173private:
174 struct Data;
175
176 Data* m;
177
178 friend class ListenerRecord;
179};
180
181class VBoxEventDesc
182{
183public:
184 VBoxEventDesc()
185 : mEvent(0), mEventSource(0)
186 {}
187 ~VBoxEventDesc()
188 {}
189
190 /**
191 * This function to be used with some care, as arguments order must match attribute declaration order
192 * event class and its superclasses up to IEvent. If unsure, consult implementation in
193 * generated VBoxEvents.cpp.
194 */
195 HRESULT init(IEventSource* aSource, VBoxEventType_T aType, ...);
196
197 /**
198 * Function similar to the above, but assumes that init() for this type already called once,
199 * so no need to allocate memory, and only reinit fields. Assumes event is subtype of
200 * IReusableEvent, asserts otherwise.
201 */
202 HRESULT reinit(VBoxEventType_T aType, ...);
203
204 void uninit()
205 {
206 mEvent.setNull();
207 mEventSource.setNull();
208 }
209
210 void getEvent(IEvent ** aEvent)
211 {
212 mEvent.queryInterfaceTo(aEvent);
213 }
214
215 BOOL fire(LONG aTimeout)
216 {
217 if (mEventSource && mEvent)
218 {
219 BOOL fDelivered = FALSE;
220 int rc = mEventSource->FireEvent(mEvent, aTimeout, &fDelivered);
221 AssertRCReturn(rc, FALSE);
222 return fDelivered;
223 }
224 return FALSE;
225 }
226
227private:
228 ComPtr<IEvent> mEvent;
229 ComPtr<IEventSource> mEventSource;
230};
231
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
305#endif // ____H_EVENTIMPL
Note: See TracBrowser for help on using the repository browser.

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