VirtualBox

source: vbox/trunk/include/VBox/com/listeners.h@ 73768

Last change on this file since 73768 was 69107, checked in by vboxsync, 7 years ago

include/VBox/: (C) year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.4 KB
Line 
1/* $Id: listeners.h 69107 2017-10-17 10:53:48Z vboxsync $ */
2/** @file
3 * MS COM / XPCOM Abstraction Layer - Listener helpers.
4 */
5
6/*
7 * Copyright (C) 2010-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27#ifndef ___VBox_com_listeners_h
28#define ___VBox_com_listeners_h
29
30#include <VBox/com/com.h>
31#include <VBox/com/VirtualBox.h>
32
33
34/** @defgroup grp_com_listeners Listener Helpers
35 * @ingroup grp_com
36 * @{
37 */
38
39
40#ifdef VBOX_WITH_XPCOM
41# define NS_IMPL_QUERY_HEAD_INLINE() \
42NS_IMETHODIMP QueryInterface(REFNSIID aIID, void **aInstancePtr) \
43{ \
44 NS_ASSERTION(aInstancePtr, "QueryInterface requires a non-NULL destination!"); \
45 nsISupports *foundInterface;
46
47# define NS_INTERFACE_MAP_BEGIN_INLINE() NS_IMPL_QUERY_HEAD_INLINE()
48
49# define NS_IMPL_QUERY_INTERFACE1_INLINE(a_i1) \
50 NS_INTERFACE_MAP_BEGIN_INLINE() \
51 NS_INTERFACE_MAP_ENTRY(a_i1) \
52 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, a_i1) \
53 NS_INTERFACE_MAP_END
54#endif
55
56template <class T, class TParam = void *>
57class ListenerImpl :
58 public ATL::CComObjectRootEx<ATL::CComMultiThreadModel>,
59 VBOX_SCRIPTABLE_IMPL(IEventListener)
60{
61 T* mListener;
62
63#ifdef RT_OS_WINDOWS
64 /* FTM stuff */
65 ComPtr<IUnknown> m_pUnkMarshaler;
66#else
67 nsAutoRefCnt mRefCnt;
68 NS_DECL_OWNINGTHREAD
69#endif
70
71public:
72 ListenerImpl()
73 {
74 }
75
76 virtual ~ListenerImpl()
77 {
78 }
79
80 HRESULT init(T* aListener, TParam param)
81 {
82 mListener = aListener;
83 return mListener->init(param);
84 }
85
86 HRESULT init(T* aListener)
87 {
88 mListener = aListener;
89 return mListener->init();
90 }
91
92 void uninit()
93 {
94 if (mListener)
95 {
96 mListener->uninit();
97 delete mListener;
98 mListener = 0;
99 }
100 }
101
102 HRESULT FinalConstruct()
103 {
104#ifdef RT_OS_WINDOWS
105 return CoCreateFreeThreadedMarshaler(this, &m_pUnkMarshaler.m_p);
106#else
107 return S_OK;
108#endif
109 }
110
111 void FinalRelease()
112 {
113 uninit();
114#ifdef RT_OS_WINDOWS
115 m_pUnkMarshaler.setNull();
116#endif
117 }
118
119 T* getWrapped()
120 {
121 return mListener;
122 }
123
124 DECLARE_NOT_AGGREGATABLE(ListenerImpl)
125
126 DECLARE_PROTECT_FINAL_CONSTRUCT()
127
128#ifdef RT_OS_WINDOWS
129 BEGIN_COM_MAP(ListenerImpl)
130 COM_INTERFACE_ENTRY(IEventListener)
131 COM_INTERFACE_ENTRY2(IDispatch, IEventListener)
132 COM_INTERFACE_ENTRY_AGGREGATE(IID_IMarshal, m_pUnkMarshaler.m_p)
133 END_COM_MAP()
134#else
135 NS_IMETHOD_(nsrefcnt) AddRef(void)
136 {
137 NS_PRECONDITION(PRInt32(mRefCnt) >= 0, "illegal refcnt");
138 nsrefcnt count;
139 count = PR_AtomicIncrement((PRInt32*)&mRefCnt);
140 NS_LOG_ADDREF(this, count, "ListenerImpl", sizeof(*this));
141 return count;
142 }
143
144 NS_IMETHOD_(nsrefcnt) Release(void)
145 {
146 nsrefcnt count;
147 NS_PRECONDITION(0 != mRefCnt, "dup release");
148 count = PR_AtomicDecrement((PRInt32 *)&mRefCnt);
149 NS_LOG_RELEASE(this, count, "ListenerImpl");
150 if (0 == count) {
151 mRefCnt = 1; /* stabilize */
152 /* enable this to find non-threadsafe destructors: */
153 /* NS_ASSERT_OWNINGTHREAD(_class); */
154 NS_DELETEXPCOM(this);
155 return 0;
156 }
157 return count;
158 }
159
160 NS_IMPL_QUERY_INTERFACE1_INLINE(IEventListener)
161#endif
162
163
164 STDMETHOD(HandleEvent)(IEvent * aEvent)
165 {
166 VBoxEventType_T aType = VBoxEventType_Invalid;
167 aEvent->COMGETTER(Type)(&aType);
168 return mListener->HandleEvent(aType, aEvent);
169 }
170};
171
172#ifdef VBOX_WITH_XPCOM
173# define VBOX_LISTENER_DECLARE(klazz) NS_DECL_CLASSINFO(klazz)
174#else
175# define VBOX_LISTENER_DECLARE(klazz)
176#endif
177
178/** @} */
179#endif
180
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use