1*cf279e26SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*cf279e26SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*cf279e26SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*cf279e26SAndrew Rist * distributed with this work for additional information 6*cf279e26SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*cf279e26SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*cf279e26SAndrew Rist * "License"); you may not use this file except in compliance 9*cf279e26SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*cf279e26SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*cf279e26SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*cf279e26SAndrew Rist * software distributed under the License is distributed on an 15*cf279e26SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*cf279e26SAndrew Rist * KIND, either express or implied. See the License for the 17*cf279e26SAndrew Rist * specific language governing permissions and limitations 18*cf279e26SAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*cf279e26SAndrew Rist *************************************************************/ 21*cf279e26SAndrew Rist 22*cf279e26SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir using System; 25cdf0e10cSrcweir using System.Collections; 26cdf0e10cSrcweir using unoidl.com.sun.star.uno; 27cdf0e10cSrcweir using unoidl.com.sun.star.lang; 28cdf0e10cSrcweir 29cdf0e10cSrcweir namespace uno.util 30cdf0e10cSrcweir { 31cdf0e10cSrcweir 32cdf0e10cSrcweir /** This class can be used as a base class for UNO objects. 33cdf0e10cSrcweir It implements the capability to be kept weakly 34cdf0e10cSrcweir (unoidl.com.sun.star.uno.XWeak) and it implements 35cdf0e10cSrcweir unoidl.com.sun.star.lang.XTypeProvider which is necessary for 36cdf0e10cSrcweir using the object from StarBasic. 37cdf0e10cSrcweir */ 38cdf0e10cSrcweir public class WeakBase : XWeak, XTypeProvider 39cdf0e10cSrcweir { 40cdf0e10cSrcweir // Contains all WeakAdapter which have been created in this class 41cdf0e10cSrcweir // They have to be notified when this object dies 42cdf0e10cSrcweir private WeakAdapter m_adapter = null; 43cdf0e10cSrcweir 44cdf0e10cSrcweir protected static Hashtable s_types = new Hashtable(); 45cdf0e10cSrcweir protected static Hashtable s_impl_ids = new Hashtable(); 46cdf0e10cSrcweir 47cdf0e10cSrcweir // XWeak impl 48cdf0e10cSrcweir /** The returned XAdapter implementation can be used to keap a 49cdf0e10cSrcweir weak reference to this object. 50cdf0e10cSrcweir 51cdf0e10cSrcweir @return a weak adapter 52cdf0e10cSrcweir */ queryAdapter()53cdf0e10cSrcweir public XAdapter queryAdapter() 54cdf0e10cSrcweir { 55cdf0e10cSrcweir if (null == m_adapter) 56cdf0e10cSrcweir { 57cdf0e10cSrcweir lock (this) 58cdf0e10cSrcweir { 59cdf0e10cSrcweir if (null == m_adapter) 60cdf0e10cSrcweir m_adapter = new WeakAdapter( this ); 61cdf0e10cSrcweir } 62cdf0e10cSrcweir } 63cdf0e10cSrcweir return m_adapter; 64cdf0e10cSrcweir } 65cdf0e10cSrcweir 66cdf0e10cSrcweir /** Overrides of Object.Finalize method. 67cdf0e10cSrcweir When there are no references to this object anymore, then the 68cdf0e10cSrcweir garbage collector calls this method, thereby causing the adapter 69cdf0e10cSrcweir object to be notified. The adapter, in turn, notifies all 70cdf0e10cSrcweir listeners (unoidl.com.sun.star.uno.XReference). 71cdf0e10cSrcweir */ ~WeakBase()72cdf0e10cSrcweir ~WeakBase() 73cdf0e10cSrcweir { 74cdf0e10cSrcweir if (null != m_adapter) 75cdf0e10cSrcweir m_adapter.referentDying(); 76cdf0e10cSrcweir } 77cdf0e10cSrcweir 78cdf0e10cSrcweir // XTypeProvider impl 79cdf0e10cSrcweir 80cdf0e10cSrcweir /** Returns an array of Type objects which represent all implemented 81cdf0e10cSrcweir UNO interfaces of this object. 82cdf0e10cSrcweir 83cdf0e10cSrcweir @return Type objects of all implemented interfaces. 84cdf0e10cSrcweir */ getTypes()85cdf0e10cSrcweir public Type [] getTypes() 86cdf0e10cSrcweir { 87cdf0e10cSrcweir Type [] types; 88cdf0e10cSrcweir Type type = GetType(); 89cdf0e10cSrcweir lock (s_types) 90cdf0e10cSrcweir { 91cdf0e10cSrcweir types = (Type []) s_types[ type ]; 92cdf0e10cSrcweir if (null == types) 93cdf0e10cSrcweir { 94cdf0e10cSrcweir Type [] interfaces = type.GetInterfaces(); 95cdf0e10cSrcweir ArrayList list = new ArrayList( interfaces.Length ); 96cdf0e10cSrcweir for ( Int32 pos = 0; pos < interfaces.Length; ++pos ) 97cdf0e10cSrcweir { 98cdf0e10cSrcweir Type iface = interfaces[ pos ]; 99cdf0e10cSrcweir // xxx todo: as long as the bridge cannot introduce 100cdf0e10cSrcweir // native CTS types into UNO on the fly 101cdf0e10cSrcweir if (iface.FullName.StartsWith( "unoidl." )) 102cdf0e10cSrcweir { 103cdf0e10cSrcweir list.Add( iface ); 104cdf0e10cSrcweir } 105cdf0e10cSrcweir } 106cdf0e10cSrcweir Int32 len = list.Count; 107cdf0e10cSrcweir Type [] ar = new Type [ len ]; 108cdf0e10cSrcweir for ( Int32 pos = 0; pos < len; ++pos ) 109cdf0e10cSrcweir ar[ pos ] = (Type) list[ pos ]; 110cdf0e10cSrcweir s_types[ type ] = ar; 111cdf0e10cSrcweir types = ar; 112cdf0e10cSrcweir } 113cdf0e10cSrcweir } 114cdf0e10cSrcweir return types; 115cdf0e10cSrcweir } 116cdf0e10cSrcweir 117cdf0e10cSrcweir /** Provides an identifier that represents the set of UNO interfaces 118cdf0e10cSrcweir implemented by this class. All instances of this class which run 119cdf0e10cSrcweir in the same CLR return the same array. 120cdf0e10cSrcweir 121cdf0e10cSrcweir @return identifier as array of bytes 122cdf0e10cSrcweir */ getImplementationId()123cdf0e10cSrcweir public byte [] getImplementationId() 124cdf0e10cSrcweir { 125cdf0e10cSrcweir byte [] id; 126cdf0e10cSrcweir Type type = GetType(); 127cdf0e10cSrcweir lock (s_impl_ids) 128cdf0e10cSrcweir { 129cdf0e10cSrcweir id = (byte []) s_impl_ids[ type ]; 130cdf0e10cSrcweir if (null == id) 131cdf0e10cSrcweir { 132cdf0e10cSrcweir Int32 hash = GetHashCode(); 133cdf0e10cSrcweir String name = type.FullName; 134cdf0e10cSrcweir Int32 len= name.Length; 135cdf0e10cSrcweir 136cdf0e10cSrcweir id = new byte[ 4 + (2 * len) ]; 137cdf0e10cSrcweir id[ 0 ]= (byte) (hash & 0xff); 138cdf0e10cSrcweir id[ 1 ]= (byte) ((hash >> 8) & 0xff); 139cdf0e10cSrcweir id[ 2 ]= (byte) ((hash >> 16) & 0xff); 140cdf0e10cSrcweir id[ 3 ]= (byte) ((hash >> 24) & 0xff); 141cdf0e10cSrcweir 142cdf0e10cSrcweir for ( Int32 pos = 0; pos < len; ++pos ) 143cdf0e10cSrcweir { 144cdf0e10cSrcweir UInt16 c = Convert.ToUInt16( name[ pos ] ); 145cdf0e10cSrcweir id[ 4 + (2 * pos) ] = (byte) (c & 0xff); 146cdf0e10cSrcweir id[ 4 + (2 * pos) +1 ] = (byte) ((c >> 8) & 0xff); 147cdf0e10cSrcweir } 148cdf0e10cSrcweir s_impl_ids[ type ] = id; 149cdf0e10cSrcweir } 150cdf0e10cSrcweir } 151cdf0e10cSrcweir return id; 152cdf0e10cSrcweir } 153cdf0e10cSrcweir 154cdf0e10cSrcweir // System.Object ToString()155cdf0e10cSrcweir public override String ToString() 156cdf0e10cSrcweir { 157cdf0e10cSrcweir System.Text.StringBuilder buf = 158cdf0e10cSrcweir new System.Text.StringBuilder( base.ToString(), 256 ); 159cdf0e10cSrcweir buf.Append( "\nUNO Object Implementation:\n\tImplementationId: " ); 160cdf0e10cSrcweir buf.Append( getImplementationId() ); 161cdf0e10cSrcweir buf.Append( "\n\tInterfaces: " ); 162cdf0e10cSrcweir Type [] types = getTypes(); 163cdf0e10cSrcweir for ( Int32 pos = 0; pos < types.Length; ++pos ) 164cdf0e10cSrcweir { 165cdf0e10cSrcweir buf.Append( types[ pos ].FullName ); 166cdf0e10cSrcweir if (pos < (types.Length -1)) 167cdf0e10cSrcweir buf.Append( ", " ); 168cdf0e10cSrcweir } 169cdf0e10cSrcweir return buf.ToString(); 170cdf0e10cSrcweir } 171cdf0e10cSrcweir } 172cdf0e10cSrcweir 173cdf0e10cSrcweir } 174cdf0e10cSrcweir 175