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 using System;
25 using unoidl.com.sun.star.lang;
26 
27 namespace uno.util
28 {
29 
30 /** This class can be used as a base class for UNO objects.
31     It implements the capability to be kept weakly
32     (unoidl.com.sun.star.uno.XWeak) and it implements
33     unoidl.com.sun.star.lang.XTypeProvider which is necessary for
34     using the object from StarBasic.
35     In addition, it implements the interface
36     unoidl.com.sun.star.lang.XComponent to be disposed explicitly.
37 */
38 public class WeakComponentBase : WeakBase, XComponent
39 {
t_disposing( EventObject evt )40     private delegate void t_disposing( EventObject evt );
41     private t_disposing m_disposing = null;
42     private bool m_inDispose = false;
43     private bool m_disposed = false;
44 
45     /** Indicates whether object is alrady disposed.
46 
47         @return
48                 true, if object has been disposed
49     */
isDisposed()50     protected bool isDisposed()
51     {
52         lock (this)
53         {
54             return m_disposed;
55         }
56     }
57 
58     /** Checks whether this object is disposed and throws a DisposedException
59         if it is already disposed.
60     */
checkUnDisposed()61     protected void checkUnDisposed()
62     {
63         if (! isDisposed())
64         {
65             throw new unoidl.com.sun.star.lang.DisposedException(
66                 "object already disposed!", this );
67         }
68     }
69 
~WeakComponentBase()70     ~WeakComponentBase()
71     {
72         bool doDispose;
73         lock (this)
74         {
75             doDispose = (!m_inDispose && !m_disposed);
76         }
77         if (doDispose)
78         {
79             dispose();
80         }
81     }
82 
83     /** Override to perform extra clean-up work. Provided for subclasses.
84         It is called during dispose()
85     */
preDisposing()86     protected void preDisposing()
87     {
88     }
89 
90     /** Override to become notified right before the disposing action is
91         performed.
92     */
postDisposing()93     protected void postDisposing()
94     {
95     }
96 
97     // XComponent impl
98     /** This method is called by the owner of this object to explicitly
99         dispose it.  This implementation of dispose() first notifies this object
100         via preDisposing(), then  all registered event listeners and
101         finally this object again calling postDisposing().
102     */
dispose()103     public void dispose()
104     {
105         // Determine in a thread-safe way if this is the first call to this
106         // method.  Only then we proceed with the notification of event
107         // listeners.  It is an error to call this method more then once.
108         bool doDispose = false;
109         t_disposing call = null;
110         lock (this)
111         {
112             if (! m_inDispose && !m_disposed)
113             {
114                 call = m_disposing;
115                 m_disposing = null;
116                 m_inDispose = true;
117                 doDispose = true;
118             }
119         }
120         // The notification occures in an unsynchronized block in order to avoid
121         // deadlocks if one of the listeners calls back in a different thread on
122         // a synchronized method which uses the same object.
123         if (doDispose)
124         {
125             try
126             {
127                 // call sub class
128                 preDisposing();
129                 // send disposing notifications to listeners
130                 if (null != call)
131                 {
132                     EventObject evt = new EventObject( this );
133                     call( evt );
134                 }
135                 // call sub class
136                 postDisposing();
137             }
138             finally
139             {
140                 // finally makes sure that the flags are set ensuring
141                 // that this function is only called once.
142                 m_disposed = true;
143                 m_inDispose = false;
144             }
145         }
146         else
147         {
148             // in a multithreaded environment, it can't be avoided,
149             // that dispose is called twice.
150             // However this condition is traced, because it MAY indicate an
151             // error.
152 #if DEBUG
153             Console.WriteLine(
154                 "WeakComponentBase.dispose() - dispose called twice" );
155 #endif
156 //             Debug.Fail( "WeakComponentBase.dispose() - dispose called twice" );
157         }
158     }
159     /** Registers an event listener being notified when this object is disposed.
160 
161         @param xListener event listener
162     */
addEventListener( XEventListener xListener )163     public void addEventListener( XEventListener xListener )
164     {
165         bool add;
166         lock (this)
167         {
168             add = (! m_inDispose && !m_disposed);
169             if (add)
170                 m_disposing += new t_disposing( xListener.disposing );
171         }
172         if (! add)
173             xListener.disposing( new EventObject( this ) );
174     }
175     /** Revokes an event listener from being notified when this object
176         is disposed.
177 
178         @param xListener event listener
179     */
removeEventListener( XEventListener xListener )180     public void removeEventListener( XEventListener xListener )
181     {
182         lock (this)
183         {
184             if (! m_inDispose && !m_disposed)
185                 m_disposing -= new t_disposing( xListener.disposing );
186         }
187     }
188 }
189 
190 }
191