merge the formfield patch from ooo-build
[ooovba.git] / cli_ure / source / ure / uno / util / WeakAdapter.cs
blob50f39a9f5fff8dd33319f5ead8b0f13997455f20
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: WeakAdapter.cs,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 using System;
32 using unoidl.com.sun.star.uno;
33 using unoidl.com.sun.star.lang;
35 namespace uno.util
38 /** An XAdapter implementation that holds a weak reference
39 (System.WeakReference) to an object.
40 Clients can register listeners (unoidl.com.sun.star.lang.XReference)
41 which are notified when the object (the one which is kept weak) is
42 being finalized. That is, that object is being destroyed because there
43 are not any hard references to it.
45 public class WeakAdapter : XAdapter
47 // references the XWeak implementation
48 private WeakReference m_weakRef;
49 // contains XReference objects registered by addReference
50 private delegate void XReference_dispose();
51 private XReference_dispose m_XReference_dispose;
53 /** ctor.
55 @param obj the object that is to be held weakly
57 public WeakAdapter( Object obj )
59 m_weakRef = new WeakReference( obj );
60 m_XReference_dispose = null;
63 /** Called by the XWeak implementation (WeakBase) when it is being
64 finalized. It is only being called once.
65 The registererd XReference listeners are notified. On notification
66 they are to unregister themselves. The notification is thread-safe.
67 However, it is possible to add a listener during the notification
68 process, which will never receive a notification.
69 To prevent this, one would have to synchronize this method with
70 the addReference method. But this can result in deadlocks in a
71 multithreaded environment.
73 internal /* non-virtual */ void referentDying()
75 XReference_dispose call;
76 lock (this)
78 call = m_XReference_dispose;
79 m_XReference_dispose = null;
81 if (null != call)
82 call();
85 // XAdapter impl
87 /** Called to obtain a hard reference o the object which is kept weakly
88 by this instance.
90 @return hard reference to the object
92 public Object queryAdapted()
94 return m_weakRef.Target;
96 /** Called by clients to register listener which are notified when the
97 weak object is dying.
99 @param xReference a listener
101 public void removeReference( XReference xReference )
103 lock (this)
105 m_XReference_dispose -=
106 new XReference_dispose( xReference.dispose );
109 /** Called by clients to unregister listeners.
111 @param xReference a listener
113 public void addReference( XReference xReference )
115 lock (this)
117 m_XReference_dispose +=
118 new XReference_dispose( xReference.dispose );