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.uno;
26 using unoidl.com.sun.star.lang;
27 
28 namespace uno.util
29 {
30 
31 /** An XAdapter implementation that holds a weak reference
32     (System.WeakReference) to an object.
33     Clients can register listeners (unoidl.com.sun.star.lang.XReference)
34     which are notified when the object (the one which is kept weak) is
35     being finalized.  That is, that object is being destroyed because there
36     are not any hard references to it.
37 */
38 public class WeakAdapter : XAdapter
39 {
40     // references the XWeak implementation
41     private WeakReference m_weakRef;
42     // contains XReference objects registered by addReference
XReference_dispose()43     private delegate void XReference_dispose();
44     private XReference_dispose m_XReference_dispose;
45 
46     /** ctor.
47 
48         @param obj the object that is to be held weakly
49     */
WeakAdapter( Object obj )50     public WeakAdapter( Object obj )
51     {
52         m_weakRef = new WeakReference( obj );
53         m_XReference_dispose = null;
54     }
55 
56     /** Called by the XWeak implementation (WeakBase) when it is being
57         finalized.  It is only being called once.
58         The registererd XReference listeners are notified. On notification
59         they are  to unregister themselves. The notification is thread-safe.
60         However, it is possible to add a listener during the notification
61         process, which will never receive a notification.
62         To prevent this, one would have to synchronize this method with
63         the addReference method. But this can result in deadlocks in a
64         multithreaded environment.
65     */
referentDying()66     internal /* non-virtual */ void referentDying()
67     {
68         XReference_dispose call;
69         lock (this)
70         {
71             call = m_XReference_dispose;
72             m_XReference_dispose = null;
73         }
74         if (null != call)
75             call();
76     }
77 
78     // XAdapter impl
79 
80     /** Called to obtain a hard reference o the object which is kept weakly
81         by this instance.
82 
83         @return hard reference to the object
84     */
queryAdapted()85     public Object queryAdapted()
86     {
87         return m_weakRef.Target;
88     }
89     /** Called by clients to register listener which are notified when the
90         weak object is dying.
91 
92         @param xReference a listener
93     */
removeReference( XReference xReference )94     public void removeReference( XReference xReference )
95     {
96         lock (this)
97         {
98             m_XReference_dispose -=
99                 new XReference_dispose( xReference.dispose );
100         }
101     }
102     /** Called by clients to unregister listeners.
103 
104         @param xReference a listener
105     */
addReference( XReference xReference )106     public void addReference( XReference xReference )
107     {
108         lock (this)
109         {
110             m_XReference_dispose +=
111                 new XReference_dispose( xReference.dispose );
112         }
113     }
114 }
115 
116 }
117