VirtualBox

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

Last change on this file was 101967, checked in by vboxsync, 6 months ago

libs/xpcom/xpcom: Convert PR_Atomic* to ASMAtomic*, bugref:10545

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

© 2023 Oracle
ContactPrivacy policyTerms of Use