xref: /AOO42X/main/comphelper/source/container/containermultiplexer.cxx (revision e2530cf997e32d49427a30bc8b11ebe297a9a57e)
1*dde7d3faSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*dde7d3faSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*dde7d3faSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*dde7d3faSAndrew Rist  * distributed with this work for additional information
6*dde7d3faSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*dde7d3faSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*dde7d3faSAndrew Rist  * "License"); you may not use this file except in compliance
9*dde7d3faSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*dde7d3faSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*dde7d3faSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*dde7d3faSAndrew Rist  * software distributed under the License is distributed on an
15*dde7d3faSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*dde7d3faSAndrew Rist  * KIND, either express or implied.  See the License for the
17*dde7d3faSAndrew Rist  * specific language governing permissions and limitations
18*dde7d3faSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*dde7d3faSAndrew Rist  *************************************************************/
21*dde7d3faSAndrew Rist 
22*dde7d3faSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_comphelper.hxx"
26cdf0e10cSrcweir #include "comphelper/containermultiplexer.hxx"
27cdf0e10cSrcweir #include "comphelper/uno3.hxx"
28cdf0e10cSrcweir #include <osl/diagnose.h>
29cdf0e10cSrcweir //.........................................................................
30cdf0e10cSrcweir namespace comphelper
31cdf0e10cSrcweir {
32cdf0e10cSrcweir //.........................................................................
33cdf0e10cSrcweir 
34cdf0e10cSrcweir     using namespace ::com::sun::star::uno;
35cdf0e10cSrcweir     using namespace ::com::sun::star::lang;
36cdf0e10cSrcweir     using namespace ::com::sun::star::container;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir     //=====================================================================
39cdf0e10cSrcweir     //= OContainerListener
40cdf0e10cSrcweir     //=====================================================================
41cdf0e10cSrcweir     //---------------------------------------------------------------------
OContainerListener(::osl::Mutex & _rMutex)42cdf0e10cSrcweir     OContainerListener::OContainerListener(::osl::Mutex& _rMutex)
43cdf0e10cSrcweir         :m_pAdapter(NULL)
44cdf0e10cSrcweir         ,m_rMutex(_rMutex)
45cdf0e10cSrcweir     {
46cdf0e10cSrcweir     }
47cdf0e10cSrcweir 
48cdf0e10cSrcweir     //---------------------------------------------------------------------
~OContainerListener()49cdf0e10cSrcweir     OContainerListener::~OContainerListener()
50cdf0e10cSrcweir     {
51cdf0e10cSrcweir         if (m_pAdapter)
52cdf0e10cSrcweir         {
53cdf0e10cSrcweir             m_pAdapter->dispose();
54cdf0e10cSrcweir             m_pAdapter = NULL;
55cdf0e10cSrcweir         }
56cdf0e10cSrcweir     }
57cdf0e10cSrcweir 
58cdf0e10cSrcweir     //---------------------------------------------------------------------
_elementInserted(const ContainerEvent &)59cdf0e10cSrcweir     void OContainerListener::_elementInserted( const ContainerEvent& /*_rEvent*/ ) throw(RuntimeException)
60cdf0e10cSrcweir     {
61cdf0e10cSrcweir     }
62cdf0e10cSrcweir 
63cdf0e10cSrcweir     //---------------------------------------------------------------------
_elementRemoved(const ContainerEvent &)64cdf0e10cSrcweir     void OContainerListener::_elementRemoved( const ContainerEvent& ) throw(RuntimeException)
65cdf0e10cSrcweir     {
66cdf0e10cSrcweir     }
67cdf0e10cSrcweir 
68cdf0e10cSrcweir     //---------------------------------------------------------------------
_elementReplaced(const ContainerEvent &)69cdf0e10cSrcweir     void OContainerListener::_elementReplaced( const ContainerEvent& /*_rEvent*/ ) throw(RuntimeException)
70cdf0e10cSrcweir     {
71cdf0e10cSrcweir     }
72cdf0e10cSrcweir 
73cdf0e10cSrcweir     //---------------------------------------------------------------------
_disposing(const EventObject &)74cdf0e10cSrcweir     void OContainerListener::_disposing(const EventObject& ) throw( RuntimeException)
75cdf0e10cSrcweir     {
76cdf0e10cSrcweir     }
77cdf0e10cSrcweir 
78cdf0e10cSrcweir     //------------------------------------------------------------------
setAdapter(OContainerListenerAdapter * pAdapter)79cdf0e10cSrcweir     void OContainerListener::setAdapter(OContainerListenerAdapter* pAdapter)
80cdf0e10cSrcweir     {
81cdf0e10cSrcweir         if (m_pAdapter)
82cdf0e10cSrcweir         {
83cdf0e10cSrcweir             ::osl::MutexGuard aGuard(m_rMutex);
84cdf0e10cSrcweir             m_pAdapter->release();
85cdf0e10cSrcweir             m_pAdapter = NULL;
86cdf0e10cSrcweir         }
87cdf0e10cSrcweir 
88cdf0e10cSrcweir         if (pAdapter)
89cdf0e10cSrcweir         {
90cdf0e10cSrcweir             ::osl::MutexGuard aGuard(m_rMutex);
91cdf0e10cSrcweir             m_pAdapter = pAdapter;
92cdf0e10cSrcweir             m_pAdapter->acquire();
93cdf0e10cSrcweir         }
94cdf0e10cSrcweir     }
95cdf0e10cSrcweir 
96cdf0e10cSrcweir     //=====================================================================
97cdf0e10cSrcweir     //= OContainerListenerAdapter
98cdf0e10cSrcweir     //=====================================================================
99cdf0e10cSrcweir     //---------------------------------------------------------------------
OContainerListenerAdapter(OContainerListener * _pListener,const Reference<XContainer> & _rxContainer)100cdf0e10cSrcweir     OContainerListenerAdapter::OContainerListenerAdapter(OContainerListener* _pListener,
101cdf0e10cSrcweir             const Reference< XContainer >& _rxContainer)
102cdf0e10cSrcweir         :m_xContainer(_rxContainer)
103cdf0e10cSrcweir         ,m_pListener(_pListener)
104cdf0e10cSrcweir         ,m_nLockCount(0)
105cdf0e10cSrcweir     {
106cdf0e10cSrcweir         if (m_pListener)
107cdf0e10cSrcweir             m_pListener->setAdapter(this);
108cdf0e10cSrcweir 
109cdf0e10cSrcweir         ::comphelper::increment(m_refCount);
110cdf0e10cSrcweir         try
111cdf0e10cSrcweir         {
112cdf0e10cSrcweir             m_xContainer->addContainerListener(this);
113cdf0e10cSrcweir         }
114cdf0e10cSrcweir         catch(const Exception&)
115cdf0e10cSrcweir         {
116cdf0e10cSrcweir             OSL_ENSURE(0,"Exceptiopn catched!");
117cdf0e10cSrcweir         }
118cdf0e10cSrcweir         ::comphelper::decrement(m_refCount);
119cdf0e10cSrcweir     }
120cdf0e10cSrcweir 
121cdf0e10cSrcweir     //---------------------------------------------------------------------
~OContainerListenerAdapter()122cdf0e10cSrcweir     OContainerListenerAdapter::~OContainerListenerAdapter()
123cdf0e10cSrcweir     {
124cdf0e10cSrcweir     }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir     //------------------------------------------------------------------
lock()127cdf0e10cSrcweir     void OContainerListenerAdapter::lock()
128cdf0e10cSrcweir     {
129cdf0e10cSrcweir         ++m_nLockCount;
130cdf0e10cSrcweir     }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir     //------------------------------------------------------------------
unlock()133cdf0e10cSrcweir     void OContainerListenerAdapter::unlock()
134cdf0e10cSrcweir     {
135cdf0e10cSrcweir         --m_nLockCount;
136cdf0e10cSrcweir     }
137cdf0e10cSrcweir 
138cdf0e10cSrcweir     //------------------------------------------------------------------
dispose()139cdf0e10cSrcweir     void OContainerListenerAdapter::dispose()
140cdf0e10cSrcweir     {
141cdf0e10cSrcweir         if (m_xContainer.is())
142cdf0e10cSrcweir         {
143cdf0e10cSrcweir             try
144cdf0e10cSrcweir             {
145cdf0e10cSrcweir                 Reference< XContainerListener > xPreventDelete(this);
146cdf0e10cSrcweir                 m_xContainer->removeContainerListener(xPreventDelete);
147cdf0e10cSrcweir                 m_pListener->setAdapter(NULL);
148cdf0e10cSrcweir             }
149cdf0e10cSrcweir             catch(const Exception&)
150cdf0e10cSrcweir             {
151cdf0e10cSrcweir                 OSL_ENSURE(0,"Exception catched!");
152cdf0e10cSrcweir             }
153cdf0e10cSrcweir             m_xContainer = NULL;
154cdf0e10cSrcweir             m_pListener = NULL;
155cdf0e10cSrcweir         }
156cdf0e10cSrcweir     }
157cdf0e10cSrcweir 
158cdf0e10cSrcweir     //------------------------------------------------------------------
disposing(const EventObject & _rSource)159cdf0e10cSrcweir     void SAL_CALL OContainerListenerAdapter::disposing( const EventObject& _rSource) throw(RuntimeException)
160cdf0e10cSrcweir     {
161cdf0e10cSrcweir         if (m_pListener)
162cdf0e10cSrcweir         {
163cdf0e10cSrcweir              // tell the listener
164cdf0e10cSrcweir             if (!locked())
165cdf0e10cSrcweir                 m_pListener->_disposing(_rSource);
166cdf0e10cSrcweir             // disconnect the listener
167cdf0e10cSrcweir             if ( m_pListener )
168cdf0e10cSrcweir                 m_pListener->setAdapter(NULL);
169cdf0e10cSrcweir         }
170cdf0e10cSrcweir 
171cdf0e10cSrcweir         m_xContainer = NULL;
172cdf0e10cSrcweir         m_pListener = NULL;
173cdf0e10cSrcweir     }
174cdf0e10cSrcweir 
175cdf0e10cSrcweir     //------------------------------------------------------------------
elementInserted(const ContainerEvent & _rEvent)176cdf0e10cSrcweir     void SAL_CALL OContainerListenerAdapter::elementInserted( const ContainerEvent& _rEvent ) throw(RuntimeException)
177cdf0e10cSrcweir     {
178cdf0e10cSrcweir         if (m_pListener && !locked())
179cdf0e10cSrcweir             m_pListener->_elementInserted(_rEvent);
180cdf0e10cSrcweir     }
181cdf0e10cSrcweir 
182cdf0e10cSrcweir     //------------------------------------------------------------------
elementRemoved(const ContainerEvent & _rEvent)183cdf0e10cSrcweir     void SAL_CALL OContainerListenerAdapter::elementRemoved( const ContainerEvent& _rEvent ) throw(RuntimeException)
184cdf0e10cSrcweir     {
185cdf0e10cSrcweir         if (m_pListener && !locked())
186cdf0e10cSrcweir             m_pListener->_elementRemoved(_rEvent);
187cdf0e10cSrcweir     }
188cdf0e10cSrcweir 
189cdf0e10cSrcweir     //------------------------------------------------------------------
elementReplaced(const ContainerEvent & _rEvent)190cdf0e10cSrcweir     void SAL_CALL OContainerListenerAdapter::elementReplaced( const ContainerEvent& _rEvent ) throw(RuntimeException)
191cdf0e10cSrcweir     {
192cdf0e10cSrcweir         if (m_pListener && !locked())
193cdf0e10cSrcweir             m_pListener->_elementReplaced(_rEvent);
194cdf0e10cSrcweir     }
195cdf0e10cSrcweir 
196cdf0e10cSrcweir //.........................................................................
197cdf0e10cSrcweir }   // namespace comphelper
198cdf0e10cSrcweir //.........................................................................
199