Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / cli_ure / source / ure / uno / util / WeakAdapter.cs
blob508a4b0f15acf1f3b6178edb72a0926576ff66c1
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.uno;
21 using unoidl.com.sun.star.lang;
23 namespace uno.util
26 /** An XAdapter implementation that holds a weak reference
27 (System.WeakReference) to an object.
28 Clients can register listeners (unoidl.com.sun.star.lang.XReference)
29 which are notified when the object (the one which is kept weak) is
30 being finalized. That is, that object is being destroyed because there
31 are not any hard references to it.
33 public class WeakAdapter : XAdapter
35 // references the XWeak implementation
36 private WeakReference m_weakRef;
37 // contains XReference objects registered by addReference
38 private delegate void XReference_dispose();
39 private XReference_dispose m_XReference_dispose;
41 /** ctor.
43 @param obj the object that is to be held weakly
45 public WeakAdapter( Object obj )
47 m_weakRef = new WeakReference( obj );
48 m_XReference_dispose = null;
51 /** Called by the XWeak implementation (WeakBase) when it is being
52 finalized. It is only being called once.
53 The registererd XReference listeners are notified. On notification
54 they are to unregister themselves. The notification is thread-safe.
55 However, it is possible to add a listener during the notification
56 process, which will never receive a notification.
57 To prevent this, one would have to synchronize this method with
58 the addReference method. But this can result in deadlocks in a
59 multithreaded environment.
61 internal /* non-virtual */ void referentDying()
63 XReference_dispose call;
64 lock (this)
66 call = m_XReference_dispose;
67 m_XReference_dispose = null;
69 if (null != call)
70 call();
73 // XAdapter impl
75 /** Called to obtain a hard reference o the object which is kept weakly
76 by this instance.
78 @return hard reference to the object
80 public Object queryAdapted()
82 return m_weakRef.Target;
84 /** Called by clients to register listener which are notified when the
85 weak object is dying.
87 @param xReference a listener
89 public void removeReference( XReference xReference )
91 lock (this)
93 m_XReference_dispose -=
94 new XReference_dispose( xReference.dispose );
97 /** Called by clients to unregister listeners.
99 @param xReference a listener
101 public void addReference( XReference xReference )
103 lock (this)
105 m_XReference_dispose +=
106 new XReference_dispose( xReference.dispose );