1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: WeakBase.cs,v $
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 ************************************************************************/
32 using System
.Collections
;
33 using unoidl
.com
.sun
.star
.uno
;
34 using unoidl
.com
.sun
.star
.lang
;
39 /** This class can be used as a base class for UNO objects.
40 It implements the capability to be kept weakly
41 (unoidl.com.sun.star.uno.XWeak) and it implements
42 unoidl.com.sun.star.lang.XTypeProvider which is necessary for
43 using the object from StarBasic.
45 public class WeakBase
: XWeak
, XTypeProvider
47 // Contains all WeakAdapter which have been created in this class
48 // They have to be notified when this object dies
49 private WeakAdapter m_adapter
= null;
51 protected static Hashtable s_types
= new Hashtable();
52 protected static Hashtable s_impl_ids
= new Hashtable();
55 /** The returned XAdapter implementation can be used to keap a
56 weak reference to this object.
58 @return a weak adapter
60 public XAdapter
queryAdapter()
62 if (null == m_adapter
)
66 if (null == m_adapter
)
67 m_adapter
= new WeakAdapter( this );
73 /** Overrides of Object.Finalize method.
74 When there are no references to this object anymore, then the
75 garbage collector calls this method, thereby causing the adapter
76 object to be notified. The adapter, in turn, notifies all
77 listeners (unoidl.com.sun.star.uno.XReference).
81 if (null != m_adapter
)
82 m_adapter
.referentDying();
87 /** Returns an array of Type objects which represent all implemented
88 UNO interfaces of this object.
90 @return Type objects of all implemented interfaces.
92 public Type
[] getTypes()
95 Type type
= GetType();
98 types
= (Type
[]) s_types
[ type
];
101 Type
[] interfaces
= type
.GetInterfaces();
102 ArrayList list
= new ArrayList( interfaces
.Length
);
103 for ( Int32 pos
= 0; pos
< interfaces
.Length
; ++pos
)
105 Type iface
= interfaces
[ pos
];
106 // xxx todo: as long as the bridge cannot introduce
107 // native CTS types into UNO on the fly
108 if (iface
.FullName
.StartsWith( "unoidl." ))
113 Int32 len
= list
.Count
;
114 Type
[] ar
= new Type
[ len
];
115 for ( Int32 pos
= 0; pos
< len
; ++pos
)
116 ar
[ pos
] = (Type
) list
[ pos
];
117 s_types
[ type
] = ar
;
124 /** Provides an identifier that represents the set of UNO interfaces
125 implemented by this class. All instances of this class which run
126 in the same CLR return the same array.
128 @return identifier as array of bytes
130 public byte [] getImplementationId()
133 Type type
= GetType();
136 id
= (byte []) s_impl_ids
[ type
];
139 Int32 hash
= GetHashCode();
140 String name
= type
.FullName
;
141 Int32 len
= name
.Length
;
143 id
= new byte[ 4 + (2 * len
) ];
144 id
[ 0 ]= (byte) (hash
& 0xff);
145 id
[ 1 ]= (byte) ((hash
>> 8) & 0xff);
146 id
[ 2 ]= (byte) ((hash
>> 16) & 0xff);
147 id
[ 3 ]= (byte) ((hash
>> 24) & 0xff);
149 for ( Int32 pos
= 0; pos
< len
; ++pos
)
151 UInt16 c
= Convert
.ToUInt16( name
[ pos
] );
152 id
[ 4 + (2 * pos
) ] = (byte) (c
& 0xff);
153 id
[ 4 + (2 * pos
) +1 ] = (byte) ((c
>> 8) & 0xff);
155 s_impl_ids
[ type
] = id
;
162 public override String
ToString()
164 System
.Text
.StringBuilder buf
=
165 new System
.Text
.StringBuilder( base.ToString(), 256 );
166 buf
.Append( "\nUNO Object Implementation:\n\tImplementationId: " );
167 buf
.Append( getImplementationId() );
168 buf
.Append( "\n\tInterfaces: " );
169 Type
[] types
= getTypes();
170 for ( Int32 pos
= 0; pos
< types
.Length
; ++pos
)
172 buf
.Append( types
[ pos
].FullName
);
173 if (pos
< (types
.Length
-1))
176 return buf
.ToString();