Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / cli_ure / source / ure / uno / util / WeakBase.cs
blob561d2e577ff29bb4fb608095a5299373f8cf342e
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 System.Collections;
21 using unoidl.com.sun.star.uno;
22 using unoidl.com.sun.star.lang;
24 namespace uno.util
27 /** This class can be used as a base class for UNO objects.
28 It implements the capability to be kept weakly
29 (unoidl.com.sun.star.uno.XWeak) and it implements
30 unoidl.com.sun.star.lang.XTypeProvider which is necessary for
31 using the object from StarBasic.
33 public class WeakBase : XWeak, XTypeProvider
35 // Contains all WeakAdapter which have been created in this class
36 // They have to be notified when this object dies
37 private WeakAdapter m_adapter = null;
39 protected static Hashtable s_types = new Hashtable();
40 protected static Hashtable s_impl_ids = new Hashtable();
42 // XWeak impl
43 /** The returned XAdapter implementation can be used to keap a
44 weak reference to this object.
46 @return a weak adapter
48 public XAdapter queryAdapter()
50 if (null == m_adapter)
52 lock (this)
54 if (null == m_adapter)
55 m_adapter = new WeakAdapter( this );
58 return m_adapter;
61 /** Overrides of Object.Finalize method.
62 When there are no references to this object anymore, then the
63 garbage collector calls this method, thereby causing the adapter
64 object to be notified. The adapter, in turn, notifies all
65 listeners (unoidl.com.sun.star.uno.XReference).
67 ~WeakBase()
69 if (null != m_adapter)
70 m_adapter.referentDying();
73 // XTypeProvider impl
75 /** Returns an array of Type objects which represent all implemented
76 UNO interfaces of this object.
78 @return Type objects of all implemented interfaces.
80 public Type [] getTypes()
82 Type [] types;
83 Type type = GetType();
84 lock (s_types)
86 types = (Type []) s_types[ type ];
87 if (null == types)
89 Type [] interfaces = type.GetInterfaces();
90 ArrayList list = new ArrayList( interfaces.Length );
91 for ( Int32 pos = 0; pos < interfaces.Length; ++pos )
93 Type iface = interfaces[ pos ];
94 // xxx todo: as long as the bridge cannot introduce
95 // native CTS types into UNO on the fly
96 if (iface.FullName.StartsWith( "unoidl." ))
98 list.Add( iface );
101 Int32 len = list.Count;
102 Type [] ar = new Type [ len ];
103 for ( Int32 pos = 0; pos < len; ++pos )
104 ar[ pos ] = (Type) list[ pos ];
105 s_types[ type ] = ar;
106 types = ar;
109 return types;
112 public byte [] getImplementationId()
114 return new byte[0];
117 // System.Object
118 public override String ToString()
120 System.Text.StringBuilder buf =
121 new System.Text.StringBuilder( base.ToString(), 256 );
122 buf.Append( "\nUNO Object Implementation:\n\tInterfaces: " );
123 Type [] types = getTypes();
124 for ( Int32 pos = 0; pos < types.Length; ++pos )
126 buf.Append( types[ pos ].FullName );
127 if (pos < (types.Length -1))
128 buf.Append( ", " );
130 return buf.ToString();