Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / cli_ure / source / ure / uno / util / WeakComponentBase.cs
blob48715afc6ae08d302397e28459b67f8f223df522
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 using System;
20 using unoidl.com.sun.star.lang;
22 namespace uno.util
25 /** This class can be used as a base class for UNO objects.
26 It implements the capability to be kept weakly
27 (unoidl.com.sun.star.uno.XWeak) and it implements
28 unoidl.com.sun.star.lang.XTypeProvider which is necessary for
29 using the object from StarBasic.
30 In addition, it implements the interface
31 unoidl.com.sun.star.lang.XComponent to be disposed explicitly.
33 public class WeakComponentBase : WeakBase, XComponent
35 private delegate void t_disposing( EventObject evt );
36 private t_disposing m_disposing = null;
37 private bool m_inDispose = false;
38 private bool m_disposed = false;
40 /** Indicates whether object is already disposed.
42 @return
43 true, if object has been disposed
45 protected bool isDisposed()
47 lock (this)
49 return m_disposed;
53 /** Checks whether this object is disposed and throws a DisposedException
54 if it is already disposed.
56 protected void checkUnDisposed()
58 if (! isDisposed())
60 throw new unoidl.com.sun.star.lang.DisposedException(
61 "object already disposed!", this );
65 ~WeakComponentBase()
67 bool doDispose;
68 lock (this)
70 doDispose = (!m_inDispose && !m_disposed);
72 if (doDispose)
74 dispose();
78 /** Override to perform extra clean-up work. Provided for subclasses.
79 It is called during dispose()
81 protected void preDisposing()
85 /** Override to become notified right before the disposing action is
86 performed.
88 protected void postDisposing()
92 // XComponent impl
93 /** This method is called by the owner of this object to explicitly
94 dispose it. This implementation of dispose() first notifies this object
95 via preDisposing(), then all registered event listeners and
96 finally this object again calling postDisposing().
98 public void dispose()
100 // Determine in a thread-safe way if this is the first call to this
101 // method. Only then we proceed with the notification of event
102 // listeners. It is an error to call this method more than once.
103 bool doDispose = false;
104 t_disposing call = null;
105 lock (this)
107 if (! m_inDispose && !m_disposed)
109 call = m_disposing;
110 m_disposing = null;
111 m_inDispose = true;
112 doDispose = true;
115 // The notification occurs in an unsynchronized block in order to avoid
116 // deadlocks if one of the listeners calls back in a different thread on
117 // a synchronized method which uses the same object.
118 if (doDispose)
122 // call sub class
123 preDisposing();
124 // send disposing notifications to listeners
125 if (null != call)
127 EventObject evt = new EventObject( this );
128 call( evt );
130 // call sub class
131 postDisposing();
133 finally
135 // finally makes sure that the flags are set ensuring
136 // that this function is only called once.
137 m_disposed = true;
138 m_inDispose = false;
141 else
143 // in a multithreaded environment, it can't be avoided,
144 // that dispose is called twice.
145 // However this condition is traced, because it MAY indicate an
146 // error.
147 #if DEBUG
148 Console.WriteLine(
149 "WeakComponentBase.dispose() - dispose called twice" );
150 #endif
151 // Debug.Fail( "WeakComponentBase.dispose() - dispose called twice" );
154 /** Registers an event listener being notified when this object is disposed.
156 @param xListener event listener
158 public void addEventListener( XEventListener xListener )
160 bool add;
161 lock (this)
163 add = (! m_inDispose && !m_disposed);
164 if (add)
165 m_disposing += new t_disposing( xListener.disposing );
167 if (! add)
168 xListener.disposing( new EventObject( this ) );
170 /** Revokes an event listener from being notified when this object
171 is disposed.
173 @param xListener event listener
175 public void removeEventListener( XEventListener xListener )
177 lock (this)
179 if (! m_inDispose && !m_disposed)
180 m_disposing -= new t_disposing( xListener.disposing );