1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_comphelper.hxx"
26 #include <comphelper/weakeventlistener.hxx>
27 #include <osl/diagnose.h>
28 
29 
30 //.........................................................................
31 namespace comphelper
32 {
33 //.........................................................................
34 
35 	using namespace ::com::sun::star::uno;
36 	using namespace ::com::sun::star::lang;
37 
38 	//=====================================================================
39 	//= OWeakListenerAdapter
40 	//=====================================================================
41 	//---------------------------------------------------------------------
~OWeakListenerAdapterBase()42 	OWeakListenerAdapterBase::~OWeakListenerAdapterBase()
43 	{
44 	}
45 
46 	//=====================================================================
47 	//= OWeakEventListenerAdapter
48 	//=====================================================================
49 	//---------------------------------------------------------------------
OWeakEventListenerAdapter(Reference<XWeak> _rxListener,Reference<XComponent> _rxBroadcaster)50 	OWeakEventListenerAdapter::OWeakEventListenerAdapter( Reference< XWeak > _rxListener, Reference< XComponent > _rxBroadcaster )
51 		:OWeakEventListenerAdapter_Base( _rxListener, _rxBroadcaster )
52 	{
53 		// add ourself as listener to the broadcaster
54 		OSL_ENSURE( _rxBroadcaster.is(), "OWeakEventListenerAdapter::OWeakEventListenerAdapter: invalid broadcaster!" );
55 		if ( _rxBroadcaster.is() )
56 		{
57 			osl_incrementInterlockedCount( &m_refCount );
58 			{
59 				_rxBroadcaster->addEventListener( this );
60 			}
61 			osl_decrementInterlockedCount( &m_refCount );
62 			OSL_ENSURE( m_refCount > 0, "OWeakEventListenerAdapter::OWeakEventListenerAdapter: oops - not to be used with implementations which hold their listeners weak!" );
63 				// the one and only reason for this adapter class (A) is to add as listener to a component (C) which
64 				// holds its listeners hard, and forward all calls then to another listener (L) which is
65 				// held weak by A.
66 				// Now if C holds listeners weak, then we do not need A, we can add L directly to C.
67 		}
68 
69 		OSL_ENSURE( getListener().is(), "OWeakEventListenerAdapter::OWeakEventListenerAdapter: invalid listener (does not support the XEventListener interface)!" );
70 	}
71 
72 	//---------------------------------------------------------------------
disposing()73 	void SAL_CALL OWeakEventListenerAdapter::disposing( )
74 	{
75 		Reference< XComponent > xBroadcaster( getBroadcaster( ), UNO_QUERY );
76 		OSL_ENSURE( xBroadcaster.is(), "OWeakEventListenerAdapter::disposing: broadcaster is invalid in the meantime! How this?" );
77 		if ( xBroadcaster.is() )
78 		{
79 			xBroadcaster->removeEventListener( this );
80 		}
81 
82 		resetListener();
83 	}
84 
85 //.........................................................................
86 }	// namespace comphelper
87 //.........................................................................
88 
89 
90