1*cf279e26SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*cf279e26SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*cf279e26SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*cf279e26SAndrew Rist  * distributed with this work for additional information
6*cf279e26SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*cf279e26SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*cf279e26SAndrew Rist  * "License"); you may not use this file except in compliance
9*cf279e26SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*cf279e26SAndrew Rist  *
11*cf279e26SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*cf279e26SAndrew Rist  *
13*cf279e26SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*cf279e26SAndrew Rist  * software distributed under the License is distributed on an
15*cf279e26SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*cf279e26SAndrew Rist  * KIND, either express or implied.  See the License for the
17*cf279e26SAndrew Rist  * specific language governing permissions and limitations
18*cf279e26SAndrew Rist  * under the License.
19*cf279e26SAndrew Rist  *
20*cf279e26SAndrew Rist  *************************************************************/
21*cf279e26SAndrew Rist 
22*cf279e26SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir using System;
25cdf0e10cSrcweir using unoidl.com.sun.star.lang;
26cdf0e10cSrcweir 
27cdf0e10cSrcweir namespace uno.util
28cdf0e10cSrcweir {
29cdf0e10cSrcweir 
30cdf0e10cSrcweir /** This class can be used as a base class for UNO objects.
31cdf0e10cSrcweir     It implements the capability to be kept weakly
32cdf0e10cSrcweir     (unoidl.com.sun.star.uno.XWeak) and it implements
33cdf0e10cSrcweir     unoidl.com.sun.star.lang.XTypeProvider which is necessary for
34cdf0e10cSrcweir     using the object from StarBasic.
35cdf0e10cSrcweir     In addition, it implements the interface
36cdf0e10cSrcweir     unoidl.com.sun.star.lang.XComponent to be disposed explicitly.
37cdf0e10cSrcweir */
38cdf0e10cSrcweir public class WeakComponentBase : WeakBase, XComponent
39cdf0e10cSrcweir {
40cdf0e10cSrcweir     private delegate void t_disposing( EventObject evt );
41cdf0e10cSrcweir     private t_disposing m_disposing = null;
42cdf0e10cSrcweir     private bool m_inDispose = false;
43cdf0e10cSrcweir     private bool m_disposed = false;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir     /** Indicates whether object is alrady disposed.
46cdf0e10cSrcweir 
47cdf0e10cSrcweir         @return
48cdf0e10cSrcweir                 true, if object has been disposed
49cdf0e10cSrcweir     */
50cdf0e10cSrcweir     protected bool isDisposed()
51cdf0e10cSrcweir     {
52cdf0e10cSrcweir         lock (this)
53cdf0e10cSrcweir         {
54cdf0e10cSrcweir             return m_disposed;
55cdf0e10cSrcweir         }
56cdf0e10cSrcweir     }
57cdf0e10cSrcweir 
58cdf0e10cSrcweir     /** Checks whether this object is disposed and throws a DisposedException
59cdf0e10cSrcweir         if it is already disposed.
60cdf0e10cSrcweir     */
61cdf0e10cSrcweir     protected void checkUnDisposed()
62cdf0e10cSrcweir     {
63cdf0e10cSrcweir         if (! isDisposed())
64cdf0e10cSrcweir         {
65cdf0e10cSrcweir             throw new unoidl.com.sun.star.lang.DisposedException(
66cdf0e10cSrcweir                 "object already disposed!", this );
67cdf0e10cSrcweir         }
68cdf0e10cSrcweir     }
69cdf0e10cSrcweir 
70cdf0e10cSrcweir     ~WeakComponentBase()
71cdf0e10cSrcweir     {
72cdf0e10cSrcweir         bool doDispose;
73cdf0e10cSrcweir         lock (this)
74cdf0e10cSrcweir         {
75cdf0e10cSrcweir             doDispose = (!m_inDispose && !m_disposed);
76cdf0e10cSrcweir         }
77cdf0e10cSrcweir         if (doDispose)
78cdf0e10cSrcweir         {
79cdf0e10cSrcweir             dispose();
80cdf0e10cSrcweir         }
81cdf0e10cSrcweir     }
82cdf0e10cSrcweir 
83cdf0e10cSrcweir     /** Override to perform extra clean-up work. Provided for subclasses.
84cdf0e10cSrcweir         It is called during dispose()
85cdf0e10cSrcweir     */
86cdf0e10cSrcweir     protected void preDisposing()
87cdf0e10cSrcweir     {
88cdf0e10cSrcweir     }
89cdf0e10cSrcweir 
90cdf0e10cSrcweir     /** Override to become notified right before the disposing action is
91cdf0e10cSrcweir         performed.
92cdf0e10cSrcweir     */
93cdf0e10cSrcweir     protected void postDisposing()
94cdf0e10cSrcweir     {
95cdf0e10cSrcweir     }
96cdf0e10cSrcweir 
97cdf0e10cSrcweir     // XComponent impl
98cdf0e10cSrcweir     /** This method is called by the owner of this object to explicitly
99cdf0e10cSrcweir         dispose it.  This implementation of dispose() first notifies this object
100cdf0e10cSrcweir         via preDisposing(), then  all registered event listeners and
101cdf0e10cSrcweir         finally this object again calling postDisposing().
102cdf0e10cSrcweir     */
103cdf0e10cSrcweir     public void dispose()
104cdf0e10cSrcweir     {
105cdf0e10cSrcweir         // Determine in a thread-safe way if this is the first call to this
106cdf0e10cSrcweir         // method.  Only then we proceed with the notification of event
107cdf0e10cSrcweir         // listeners.  It is an error to call this method more then once.
108cdf0e10cSrcweir         bool doDispose = false;
109cdf0e10cSrcweir         t_disposing call = null;
110cdf0e10cSrcweir         lock (this)
111cdf0e10cSrcweir         {
112cdf0e10cSrcweir             if (! m_inDispose && !m_disposed)
113cdf0e10cSrcweir             {
114cdf0e10cSrcweir                 call = m_disposing;
115cdf0e10cSrcweir                 m_disposing = null;
116cdf0e10cSrcweir                 m_inDispose = true;
117cdf0e10cSrcweir                 doDispose = true;
118cdf0e10cSrcweir             }
119cdf0e10cSrcweir         }
120cdf0e10cSrcweir         // The notification occures in an unsynchronized block in order to avoid
121cdf0e10cSrcweir         // deadlocks if one of the listeners calls back in a different thread on
122cdf0e10cSrcweir         // a synchronized method which uses the same object.
123cdf0e10cSrcweir         if (doDispose)
124cdf0e10cSrcweir         {
125cdf0e10cSrcweir             try
126cdf0e10cSrcweir             {
127cdf0e10cSrcweir                 // call sub class
128cdf0e10cSrcweir                 preDisposing();
129cdf0e10cSrcweir                 // send disposing notifications to listeners
130cdf0e10cSrcweir                 if (null != call)
131cdf0e10cSrcweir                 {
132cdf0e10cSrcweir                     EventObject evt = new EventObject( this );
133cdf0e10cSrcweir                     call( evt );
134cdf0e10cSrcweir                 }
135cdf0e10cSrcweir                 // call sub class
136cdf0e10cSrcweir                 postDisposing();
137cdf0e10cSrcweir             }
138cdf0e10cSrcweir             finally
139cdf0e10cSrcweir             {
140cdf0e10cSrcweir                 // finally makes sure that the flags are set ensuring
141cdf0e10cSrcweir                 // that this function is only called once.
142cdf0e10cSrcweir                 m_disposed = true;
143cdf0e10cSrcweir                 m_inDispose = false;
144cdf0e10cSrcweir             }
145cdf0e10cSrcweir         }
146cdf0e10cSrcweir         else
147cdf0e10cSrcweir         {
148cdf0e10cSrcweir             // in a multithreaded environment, it can't be avoided,
149cdf0e10cSrcweir             // that dispose is called twice.
150cdf0e10cSrcweir             // However this condition is traced, because it MAY indicate an
151cdf0e10cSrcweir             // error.
152cdf0e10cSrcweir #if DEBUG
153cdf0e10cSrcweir             Console.WriteLine(
154cdf0e10cSrcweir                 "WeakComponentBase.dispose() - dispose called twice" );
155cdf0e10cSrcweir #endif
156cdf0e10cSrcweir //             Debug.Fail( "WeakComponentBase.dispose() - dispose called twice" );
157cdf0e10cSrcweir         }
158cdf0e10cSrcweir     }
159cdf0e10cSrcweir     /** Registers an event listener being notified when this object is disposed.
160cdf0e10cSrcweir 
161cdf0e10cSrcweir         @param xListener event listener
162cdf0e10cSrcweir     */
163cdf0e10cSrcweir     public void addEventListener( XEventListener xListener )
164cdf0e10cSrcweir     {
165cdf0e10cSrcweir         bool add;
166cdf0e10cSrcweir         lock (this)
167cdf0e10cSrcweir         {
168cdf0e10cSrcweir             add = (! m_inDispose && !m_disposed);
169cdf0e10cSrcweir             if (add)
170cdf0e10cSrcweir                 m_disposing += new t_disposing( xListener.disposing );
171cdf0e10cSrcweir         }
172cdf0e10cSrcweir         if (! add)
173cdf0e10cSrcweir             xListener.disposing( new EventObject( this ) );
174cdf0e10cSrcweir     }
175cdf0e10cSrcweir     /** Revokes an event listener from being notified when this object
176cdf0e10cSrcweir         is disposed.
177cdf0e10cSrcweir 
178cdf0e10cSrcweir         @param xListener event listener
179cdf0e10cSrcweir     */
180cdf0e10cSrcweir     public void removeEventListener( XEventListener xListener )
181cdf0e10cSrcweir     {
182cdf0e10cSrcweir         lock (this)
183cdf0e10cSrcweir         {
184cdf0e10cSrcweir             if (! m_inDispose && !m_disposed)
185cdf0e10cSrcweir                 m_disposing -= new t_disposing( xListener.disposing );
186cdf0e10cSrcweir         }
187cdf0e10cSrcweir     }
188cdf0e10cSrcweir }
189cdf0e10cSrcweir 
190cdf0e10cSrcweir }
191