update dev300-m58
[ooovba.git] / cppuhelper / source / tdmgr.cxx
blob73896ded6e408c571b438224cbb24c7715df4df8
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: tdmgr.cxx,v $
10 * $Revision: 1.15 $
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 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_cppuhelper.hxx"
33 #include <sal/alloca.h>
35 #include <osl/diagnose.h>
36 #include <rtl/alloc.h>
37 #include <rtl/ustring.hxx>
39 #include <uno/mapping.hxx>
41 #include <cppuhelper/bootstrap.hxx>
42 #include <cppuhelper/implbase1.hxx>
43 #include <typelib/typedescription.h>
45 #include <com/sun/star/lang/XComponent.hpp>
46 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
47 #include <com/sun/star/reflection/XTypeDescription.hpp>
48 #include <com/sun/star/reflection/XEnumTypeDescription.hpp>
49 #include <com/sun/star/reflection/XIndirectTypeDescription.hpp>
50 #include <com/sun/star/reflection/XInterfaceMemberTypeDescription.hpp>
51 #include <com/sun/star/reflection/XInterfaceAttributeTypeDescription.hpp>
52 #include <com/sun/star/reflection/XMethodParameter.hpp>
53 #include <com/sun/star/reflection/XInterfaceMethodTypeDescription.hpp>
54 #include <com/sun/star/reflection/XInterfaceTypeDescription2.hpp>
55 #include <com/sun/star/reflection/XCompoundTypeDescription.hpp>
56 #include <com/sun/star/reflection/XStructTypeDescription.hpp>
57 #include <com/sun/star/reflection/XUnionTypeDescription.hpp>
58 #include "com/sun/star/uno/RuntimeException.hpp"
60 #include "boost/scoped_array.hpp"
62 using namespace ::rtl;
63 using namespace ::com::sun::star;
64 using namespace ::com::sun::star::uno;
65 using namespace ::com::sun::star::reflection;
68 namespace cppu
71 static typelib_TypeDescription * createCTD(
72 Reference< container::XHierarchicalNameAccess > const & access,
73 const Reference< XTypeDescription > & xType );
75 //==================================================================================================
76 inline static sal_Int64 coerceToInt64( const Any & rVal )
78 switch (rVal.getValueTypeClass())
80 case TypeClass_CHAR:
81 return *(sal_Unicode *)rVal.getValue();
82 case TypeClass_BOOLEAN:
83 return (*(sal_Bool *)rVal.getValue() ? 1 : 0);
84 case TypeClass_BYTE:
85 return *(sal_Int8 *)rVal.getValue();
86 case TypeClass_SHORT:
87 return *(sal_Int16 *)rVal.getValue();
88 case TypeClass_UNSIGNED_SHORT:
89 return *(sal_uInt16 *)rVal.getValue();
90 case TypeClass_LONG:
91 return *(sal_Int32 *)rVal.getValue();
92 case TypeClass_UNSIGNED_LONG:
93 return *(sal_uInt32 *)rVal.getValue();
94 case TypeClass_HYPER:
95 return *(sal_Int64 *)rVal.getValue();
96 case TypeClass_UNSIGNED_HYPER:
97 return *(sal_uInt64 *)rVal.getValue();
98 case TypeClass_ENUM:
99 return *(int *)rVal.getValue();
100 default:
101 OSL_ASSERT(false);
102 return 0;
105 //==================================================================================================
106 inline static typelib_TypeDescription * createCTD(
107 const Reference< XUnionTypeDescription > & xType )
109 typelib_TypeDescription * pRet = 0;
110 if (xType.is())
112 OUString aTypeName( xType->getName() );
114 // discriminant type
115 Reference< XTypeDescription > xDiscrTD( xType->getDiscriminantType() );
116 OUString aDiscrTypeName( xDiscrTD->getName() );
117 typelib_TypeDescriptionReference * pDiscrTypeRef = 0;
118 typelib_typedescriptionreference_new( &pDiscrTypeRef,
119 (typelib_TypeClass)xDiscrTD->getTypeClass(),
120 aDiscrTypeName.pData );
121 // default member type
122 Reference< XTypeDescription > xDefaultMemberTD( xType->getDefaultMemberType() );
123 OUString aDefMemberTypeName( xDefaultMemberTD->getName() );
124 typelib_TypeDescriptionReference * pDefMemberTypeRef = 0;
125 typelib_typedescriptionreference_new( &pDefMemberTypeRef,
126 (typelib_TypeClass)xDefaultMemberTD->getTypeClass(),
127 aDefMemberTypeName.pData );
128 // init array
129 Sequence< Any > aDiscriminants( xType->getDiscriminants() );
130 Sequence< Reference< XTypeDescription > > aMemberTypes( xType->getMemberTypes() );
131 Sequence< OUString > aMemberNames( xType->getMemberNames() );
132 sal_Int32 nMembers = aDiscriminants.getLength();
133 OSL_ASSERT( nMembers == aMemberNames.getLength() && nMembers == aMemberTypes.getLength() );
135 const Any * pDiscriminants = aDiscriminants.getConstArray();
136 const Reference< XTypeDescription > * pMemberTypes = aMemberTypes.getConstArray();
137 const OUString * pMemberNames = aMemberNames.getConstArray();
139 typelib_Union_Init * pMembers = (typelib_Union_Init *)alloca( nMembers * sizeof(typelib_Union_Init) );
141 sal_Int32 nPos;
142 for ( nPos = nMembers; nPos--; )
144 typelib_Union_Init & rEntry = pMembers[nPos];
145 // member discriminant
146 rEntry.nDiscriminant = coerceToInt64( pDiscriminants[nPos] );
147 // member type
148 OUString aMemberTypeName( pMemberTypes[nPos]->getName() );
149 rEntry.pTypeRef = 0;
150 typelib_typedescriptionreference_new( &rEntry.pTypeRef,
151 (typelib_TypeClass)pMemberTypes[nPos]->getTypeClass(),
152 aMemberTypeName.pData );
153 // member name
154 rEntry.pMemberName = pMemberNames[nPos].pData;
157 typelib_typedescription_newUnion( &pRet, aTypeName.pData,
158 pDiscrTypeRef,
159 coerceToInt64( xType->getDefaultDiscriminant() ),
160 pDefMemberTypeRef,
161 nMembers, pMembers );
163 for ( nPos = nMembers; nPos--; )
165 typelib_typedescriptionreference_release( pMembers[nPos].pTypeRef );
168 typelib_typedescriptionreference_release( pDiscrTypeRef );
169 typelib_typedescriptionreference_release( pDefMemberTypeRef );
171 return pRet;
173 //==================================================================================================
174 inline static typelib_TypeDescription * createCTD(
175 const Reference< XCompoundTypeDescription > & xType )
177 typelib_TypeDescription * pRet = 0;
178 if (xType.is())
180 typelib_TypeDescription * pBaseType = createCTD(
181 Reference< XCompoundTypeDescription >::query( xType->getBaseType() ) );
182 if (pBaseType)
183 typelib_typedescription_register( &pBaseType );
185 // construct member init array
186 const Sequence<Reference< XTypeDescription > > & rMemberTypes = xType->getMemberTypes();
187 const Sequence< OUString > & rMemberNames = xType->getMemberNames();
189 const Reference< XTypeDescription > * pMemberTypes = rMemberTypes.getConstArray();
190 const OUString * pMemberNames = rMemberNames.getConstArray();
192 sal_Int32 nMembers = rMemberTypes.getLength();
193 OSL_ENSURE( nMembers == rMemberNames.getLength(), "### lens differ!" );
195 OUString aTypeName( xType->getName() );
197 typelib_CompoundMember_Init * pMemberInits = (typelib_CompoundMember_Init *)alloca(
198 sizeof(typelib_CompoundMember_Init) * nMembers );
200 sal_Int32 nPos;
201 for ( nPos = nMembers; nPos--; )
203 typelib_CompoundMember_Init & rInit = pMemberInits[nPos];
204 rInit.eTypeClass = (typelib_TypeClass)pMemberTypes[nPos]->getTypeClass();
206 OUString aMemberTypeName( pMemberTypes[nPos]->getName() );
207 rtl_uString_acquire( rInit.pTypeName = aMemberTypeName.pData );
209 // string is held by rMemberNames
210 rInit.pMemberName = pMemberNames[nPos].pData;
213 typelib_typedescription_new(
214 &pRet,
215 (typelib_TypeClass)xType->getTypeClass(),
216 aTypeName.pData,
217 (pBaseType ? pBaseType->pWeakRef : 0),
218 nMembers, pMemberInits );
220 // cleanup
221 for ( nPos = nMembers; nPos--; )
223 rtl_uString_release( pMemberInits[nPos].pTypeName );
225 if (pBaseType)
226 typelib_typedescription_release( pBaseType );
228 return pRet;
230 //==================================================================================================
231 inline static typelib_TypeDescription * createCTD(
232 Reference< container::XHierarchicalNameAccess > const & access,
233 const Reference< XStructTypeDescription > & xType )
235 typelib_TypeDescription * pRet = 0;
236 if (xType.is() && xType->getTypeParameters().getLength() == 0)
238 typelib_TypeDescription * pBaseType = createCTD(
239 access, xType->getBaseType() );
240 if (pBaseType)
241 typelib_typedescription_register( &pBaseType );
243 // construct member init array
244 const Sequence<Reference< XTypeDescription > > & rMemberTypes = xType->getMemberTypes();
245 const Sequence< OUString > & rMemberNames = xType->getMemberNames();
247 const Reference< XTypeDescription > * pMemberTypes = rMemberTypes.getConstArray();
248 const OUString * pMemberNames = rMemberNames.getConstArray();
250 sal_Int32 nMembers = rMemberTypes.getLength();
251 OSL_ENSURE( nMembers == rMemberNames.getLength(), "### lens differ!" );
253 OUString aTypeName( xType->getName() );
255 typelib_StructMember_Init * pMemberInits = (typelib_StructMember_Init *)alloca(
256 sizeof(typelib_StructMember_Init) * nMembers );
258 Sequence< Reference< XTypeDescription > > templateMemberTypes;
259 sal_Int32 i = aTypeName.indexOf('<');
260 if (i >= 0) {
261 Reference< XStructTypeDescription > templateDesc(
262 access->getByHierarchicalName(aTypeName.copy(0, i)),
263 UNO_QUERY_THROW);
264 OSL_ASSERT(
265 templateDesc->getTypeParameters().getLength()
266 == xType->getTypeArguments().getLength());
267 templateMemberTypes = templateDesc->getMemberTypes();
268 OSL_ASSERT(templateMemberTypes.getLength() == nMembers);
271 sal_Int32 nPos;
272 for ( nPos = nMembers; nPos--; )
274 typelib_StructMember_Init & rInit = pMemberInits[nPos];
275 rInit.aBase.eTypeClass
276 = (typelib_TypeClass)pMemberTypes[nPos]->getTypeClass();
278 OUString aMemberTypeName( pMemberTypes[nPos]->getName() );
279 rtl_uString_acquire(
280 rInit.aBase.pTypeName = aMemberTypeName.pData );
282 // string is held by rMemberNames
283 rInit.aBase.pMemberName = pMemberNames[nPos].pData;
285 rInit.bParameterizedType = templateMemberTypes.getLength() != 0
286 && (templateMemberTypes[nPos]->getTypeClass()
287 == TypeClass_UNKNOWN);
290 typelib_typedescription_newStruct(
291 &pRet,
292 aTypeName.pData,
293 (pBaseType ? pBaseType->pWeakRef : 0),
294 nMembers, pMemberInits );
296 // cleanup
297 for ( nPos = nMembers; nPos--; )
299 rtl_uString_release( pMemberInits[nPos].aBase.pTypeName );
301 if (pBaseType)
302 typelib_typedescription_release( pBaseType );
304 return pRet;
306 //==================================================================================================
307 inline static typelib_TypeDescription * createCTD(
308 const Reference< XInterfaceAttributeTypeDescription > & xAttribute )
310 typelib_TypeDescription * pRet = 0;
311 if (xAttribute.is())
313 OUString aMemberName( xAttribute->getName() );
314 Reference< XTypeDescription > xType( xAttribute->getType() );
315 OUString aMemberTypeName( xType->getName() );
317 typelib_typedescription_newInterfaceAttribute(
318 (typelib_InterfaceAttributeTypeDescription **)&pRet,
319 xAttribute->getPosition(),
320 aMemberName.pData, // name
321 (typelib_TypeClass)xType->getTypeClass(),
322 aMemberTypeName.pData, // type name
323 xAttribute->isReadOnly() );
325 return pRet;
327 //==================================================================================================
328 static typelib_TypeDescription * createCTD(
329 const Reference< XInterfaceMethodTypeDescription > & xMethod )
331 typelib_TypeDescription * pRet = 0;
332 if (xMethod.is())
334 Reference< XTypeDescription > xReturnType( xMethod->getReturnType() );
336 // init all params
337 const Sequence<Reference< XMethodParameter > > & rParams = xMethod->getParameters();
338 const Reference< XMethodParameter > * pParams = rParams.getConstArray();
339 sal_Int32 nParams = rParams.getLength();
341 typelib_Parameter_Init * pParamInit = (typelib_Parameter_Init *)alloca(
342 sizeof(typelib_Parameter_Init) * nParams );
344 sal_Int32 nPos;
345 for ( nPos = nParams; nPos--; )
347 const Reference< XMethodParameter > & xParam = pParams[nPos];
348 const Reference< XTypeDescription > & xType = xParam->getType();
349 typelib_Parameter_Init & rInit = pParamInit[xParam->getPosition()];
351 rInit.eTypeClass = (typelib_TypeClass)xType->getTypeClass();
352 OUString aParamTypeName( xType->getName() );
353 rtl_uString_acquire( rInit.pTypeName = aParamTypeName.pData );
354 OUString aParamName( xParam->getName() );
355 rtl_uString_acquire( rInit.pParamName = aParamName.pData );
356 rInit.bIn = xParam->isIn();
357 rInit.bOut = xParam->isOut();
360 // init all exception strings
361 const Sequence<Reference< XTypeDescription > > & rExceptions = xMethod->getExceptions();
362 const Reference< XTypeDescription > * pExceptions = rExceptions.getConstArray();
363 sal_Int32 nExceptions = rExceptions.getLength();
364 rtl_uString ** ppExceptionNames = (rtl_uString **)alloca(
365 sizeof(rtl_uString *) * nExceptions );
367 for ( nPos = nExceptions; nPos--; )
369 OUString aExceptionTypeName( pExceptions[nPos]->getName() );
370 rtl_uString_acquire( ppExceptionNames[nPos] = aExceptionTypeName.pData );
373 OUString aTypeName( xMethod->getName() );
374 OUString aReturnTypeName( xReturnType->getName() );
376 typelib_typedescription_newInterfaceMethod(
377 (typelib_InterfaceMethodTypeDescription **)&pRet,
378 xMethod->getPosition(),
379 xMethod->isOneway(),
380 aTypeName.pData,
381 (typelib_TypeClass)xReturnType->getTypeClass(),
382 aReturnTypeName.pData,
383 nParams, pParamInit,
384 nExceptions, ppExceptionNames );
386 for ( nPos = nParams; nPos--; )
388 rtl_uString_release( pParamInit[nPos].pTypeName );
389 rtl_uString_release( pParamInit[nPos].pParamName );
391 for ( nPos = nExceptions; nPos--; )
393 rtl_uString_release( ppExceptionNames[nPos] );
396 return pRet;
398 //==================================================================================================
399 inline static typelib_TypeDescription * createCTD(
400 Reference< container::XHierarchicalNameAccess > const & access,
401 const Reference< XInterfaceTypeDescription2 > & xType )
403 typelib_TypeDescription * pRet = 0;
404 if (xType.is())
406 Sequence< Reference< XTypeDescription > > aBases(xType->getBaseTypes());
407 sal_Int32 nBases = aBases.getLength();
408 // Exploit the fact that a typelib_TypeDescription for an interface type
409 // is also the typelib_TypeDescriptionReference for that type:
410 boost::scoped_array< typelib_TypeDescription * > aBaseTypes(
411 new typelib_TypeDescription *[nBases]);
412 {for (sal_Int32 i = 0; i < nBases; ++i) {
413 typelib_TypeDescription * p = createCTD(access, aBases[i]);
414 OSL_ASSERT(
415 !TYPELIB_TYPEDESCRIPTIONREFERENCE_ISREALLYWEAK(p->eTypeClass));
416 typelib_typedescription_register(&p);
417 aBaseTypes[i] = p;
419 typelib_TypeDescriptionReference ** pBaseTypeRefs
420 = reinterpret_cast< typelib_TypeDescriptionReference ** >(
421 aBaseTypes.get());
423 // construct all member refs
424 const Sequence<Reference< XInterfaceMemberTypeDescription > > & rMembers = xType->getMembers();
425 sal_Int32 nMembers = rMembers.getLength();
427 typelib_TypeDescriptionReference ** ppMemberRefs = (typelib_TypeDescriptionReference **)alloca(
428 sizeof(typelib_TypeDescriptionReference *) * nMembers );
430 const Reference< XInterfaceMemberTypeDescription > * pMembers = rMembers.getConstArray();
432 OUString aTypeName( xType->getName() );
434 sal_Int32 nPos;
435 for ( nPos = nMembers; nPos--; )
437 OUString aMemberTypeName( pMembers[nPos]->getName() );
438 ppMemberRefs[nPos] = 0;
439 typelib_typedescriptionreference_new(
440 ppMemberRefs + nPos,
441 (typelib_TypeClass)pMembers[nPos]->getTypeClass(),
442 aMemberTypeName.pData );
445 Uik uik = xType->getUik();
447 typelib_typedescription_newMIInterface(
448 (typelib_InterfaceTypeDescription **)&pRet,
449 aTypeName.pData,
450 uik.m_Data1, uik.m_Data2, uik.m_Data3, uik.m_Data4, uik.m_Data5,
451 nBases, pBaseTypeRefs,
452 nMembers, ppMemberRefs );
454 // cleanup refs and base type
455 {for (int i = 0; i < nBases; ++i) {
456 typelib_typedescription_release(aBaseTypes[i]);
459 for ( nPos = nMembers; nPos--; )
461 typelib_typedescriptionreference_release( ppMemberRefs[nPos] );
464 return pRet;
466 //==================================================================================================
467 inline static typelib_TypeDescription * createCTD( const Reference< XEnumTypeDescription > & xType )
469 typelib_TypeDescription * pRet = 0;
470 if (xType.is())
472 OUString aTypeName( xType->getName() );
473 Sequence< OUString > aNames( xType->getEnumNames() );
474 OSL_ASSERT( sizeof(OUString) == sizeof(rtl_uString *) ); // !!!
475 Sequence< sal_Int32 > aValues( xType->getEnumValues() );
477 typelib_typedescription_newEnum(
478 &pRet, aTypeName.pData, xType->getDefaultEnumValue(),
479 aNames.getLength(),
480 (rtl_uString **)aNames.getConstArray(),
481 const_cast< sal_Int32 * >( aValues.getConstArray() ) );
483 return pRet;
485 //==================================================================================================
486 inline static typelib_TypeDescription * createCTD(
487 Reference< container::XHierarchicalNameAccess > const & access,
488 const Reference< XIndirectTypeDescription > & xType )
490 typelib_TypeDescription * pRet = 0;
491 if (xType.is())
493 typelib_TypeDescription * pRefType = createCTD(
494 access, xType->getReferencedType() );
495 typelib_typedescription_register( &pRefType );
497 OUString aTypeName( xType->getName() );
499 typelib_typedescription_new(
500 &pRet,
501 (typelib_TypeClass)xType->getTypeClass(),
502 aTypeName.pData,
503 pRefType->pWeakRef,
504 0, 0 );
506 // cleanup
507 if (pRefType)
508 typelib_typedescription_release( pRefType );
510 return pRet;
513 //==================================================================================================
514 static typelib_TypeDescription * createCTD(
515 Reference< container::XHierarchicalNameAccess > const & access,
516 const Reference< XTypeDescription > & xType )
518 typelib_TypeDescription * pRet = 0;
520 if (xType.is())
522 switch (xType->getTypeClass())
524 // built in types
525 case TypeClass_VOID:
527 OUString aTypeName( RTL_CONSTASCII_USTRINGPARAM("void") );
528 typelib_typedescription_new( &pRet, typelib_TypeClass_VOID, aTypeName.pData, 0, 0, 0 );
529 break;
531 case TypeClass_CHAR:
533 OUString aTypeName( RTL_CONSTASCII_USTRINGPARAM("char") );
534 typelib_typedescription_new( &pRet, typelib_TypeClass_CHAR, aTypeName.pData, 0, 0, 0 );
535 break;
537 case TypeClass_BOOLEAN:
539 OUString aTypeName( RTL_CONSTASCII_USTRINGPARAM("boolean") );
540 typelib_typedescription_new( &pRet, typelib_TypeClass_BOOLEAN, aTypeName.pData, 0, 0, 0 );
541 break;
543 case TypeClass_BYTE:
545 OUString aTypeName( RTL_CONSTASCII_USTRINGPARAM("byte") );
546 typelib_typedescription_new( &pRet, typelib_TypeClass_BYTE, aTypeName.pData, 0, 0, 0 );
547 break;
549 case TypeClass_SHORT:
551 OUString aTypeName( RTL_CONSTASCII_USTRINGPARAM("short") );
552 typelib_typedescription_new( &pRet, typelib_TypeClass_SHORT, aTypeName.pData, 0, 0, 0 );
553 break;
555 case TypeClass_UNSIGNED_SHORT:
557 OUString aTypeName( RTL_CONSTASCII_USTRINGPARAM("unsigned short") );
558 typelib_typedescription_new( &pRet, typelib_TypeClass_UNSIGNED_SHORT, aTypeName.pData, 0, 0, 0 );
559 break;
561 case TypeClass_LONG:
563 OUString aTypeName( RTL_CONSTASCII_USTRINGPARAM("long") );
564 typelib_typedescription_new( &pRet, typelib_TypeClass_LONG, aTypeName.pData, 0, 0, 0 );
565 break;
567 case TypeClass_UNSIGNED_LONG:
569 OUString aTypeName( RTL_CONSTASCII_USTRINGPARAM("unsigned long") );
570 typelib_typedescription_new( &pRet, typelib_TypeClass_UNSIGNED_LONG, aTypeName.pData, 0, 0, 0 );
571 break;
573 case TypeClass_HYPER:
575 OUString aTypeName( RTL_CONSTASCII_USTRINGPARAM("hyper") );
576 typelib_typedescription_new( &pRet, typelib_TypeClass_HYPER, aTypeName.pData, 0, 0, 0 );
577 break;
579 case TypeClass_UNSIGNED_HYPER:
581 OUString aTypeName( RTL_CONSTASCII_USTRINGPARAM("unsigned hyper") );
582 typelib_typedescription_new( &pRet, typelib_TypeClass_UNSIGNED_HYPER, aTypeName.pData, 0, 0, 0 );
583 break;
585 case TypeClass_FLOAT:
587 OUString aTypeName( RTL_CONSTASCII_USTRINGPARAM("float") );
588 typelib_typedescription_new( &pRet, typelib_TypeClass_FLOAT, aTypeName.pData, 0, 0, 0 );
589 break;
591 case TypeClass_DOUBLE:
593 OUString aTypeName( RTL_CONSTASCII_USTRINGPARAM("double") );
594 typelib_typedescription_new( &pRet, typelib_TypeClass_DOUBLE, aTypeName.pData, 0, 0, 0 );
595 break;
597 case TypeClass_STRING:
599 OUString aTypeName( RTL_CONSTASCII_USTRINGPARAM("string") );
600 typelib_typedescription_new( &pRet, typelib_TypeClass_STRING, aTypeName.pData, 0, 0, 0 );
601 break;
603 case TypeClass_TYPE:
605 OUString aTypeName( RTL_CONSTASCII_USTRINGPARAM("type") );
606 typelib_typedescription_new( &pRet, typelib_TypeClass_TYPE, aTypeName.pData, 0, 0, 0 );
607 break;
609 case TypeClass_ANY:
611 OUString aTypeName( RTL_CONSTASCII_USTRINGPARAM("any") );
612 typelib_typedescription_new( &pRet, typelib_TypeClass_ANY, aTypeName.pData, 0, 0, 0 );
613 break;
616 case TypeClass_UNION:
617 pRet = createCTD( Reference< XUnionTypeDescription >::query( xType ) );
618 break;
619 case TypeClass_EXCEPTION:
620 pRet = createCTD( Reference< XCompoundTypeDescription >::query( xType ) );
621 break;
622 case TypeClass_STRUCT:
623 pRet = createCTD(
624 access, Reference< XStructTypeDescription >::query( xType ) );
625 break;
626 case TypeClass_ENUM:
627 pRet = createCTD( Reference< XEnumTypeDescription >::query( xType ) );
628 break;
629 case TypeClass_TYPEDEF:
631 Reference< XIndirectTypeDescription > xTypedef( xType, UNO_QUERY );
632 if (xTypedef.is())
633 pRet = createCTD( access, xTypedef->getReferencedType() );
634 break;
636 case TypeClass_SEQUENCE:
637 pRet = createCTD(
638 access, Reference< XIndirectTypeDescription >::query( xType ) );
639 break;
640 case TypeClass_INTERFACE:
641 pRet = createCTD(
642 access,
643 Reference< XInterfaceTypeDescription2 >::query( xType ) );
644 break;
645 case TypeClass_INTERFACE_METHOD:
646 pRet = createCTD( Reference< XInterfaceMethodTypeDescription >::query( xType ) );
647 break;
648 case TypeClass_INTERFACE_ATTRIBUTE:
649 pRet = createCTD( Reference< XInterfaceAttributeTypeDescription >::query( xType ) );
650 break;
651 default:
652 break;
656 return pRet;
660 //==================================================================================================
661 extern "C"
663 static void SAL_CALL typelib_callback(
664 void * pContext, typelib_TypeDescription ** ppRet, rtl_uString * pTypeName )
666 OSL_ENSURE( pContext && ppRet && pTypeName, "### null ptr!" );
667 if (ppRet)
669 if (*ppRet)
671 ::typelib_typedescription_release( *ppRet );
672 *ppRet = 0;
674 if (pContext && pTypeName)
676 Reference< container::XHierarchicalNameAccess > access(
677 reinterpret_cast< container::XHierarchicalNameAccess * >(
678 pContext));
681 OUString const & rTypeName = OUString::unacquired( &pTypeName );
682 Reference< XTypeDescription > xTD;
683 if (access->getByHierarchicalName(rTypeName ) >>= xTD)
685 *ppRet = createCTD( access, xTD );
688 catch (container::NoSuchElementException & exc)
690 (void) exc; // avoid warning about unused variable
691 OSL_ENSURE(
692 0, OUStringToOString(
693 OUString( RTL_CONSTASCII_USTRINGPARAM(
694 "typelibrary type not available: ") ) +
695 exc.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
697 catch (Exception & exc)
699 (void) exc; // avoid warning about unused variable
700 OSL_ENSURE(
701 0, OUStringToOString(
702 exc.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
709 //==================================================================================================
710 class EventListenerImpl
711 : public WeakImplHelper1< lang::XEventListener >
713 Reference< container::XHierarchicalNameAccess > m_xTDMgr;
715 public:
716 inline EventListenerImpl(
717 Reference< container::XHierarchicalNameAccess > const & xTDMgr )
718 SAL_THROW( () )
719 : m_xTDMgr( xTDMgr )
722 // XEventListener
723 virtual void SAL_CALL disposing( lang::EventObject const & rEvt )
724 throw (RuntimeException);
726 //__________________________________________________________________________________________________
727 void EventListenerImpl::disposing( lang::EventObject const & rEvt )
728 throw (RuntimeException)
730 if (rEvt.Source != m_xTDMgr) {
731 OSL_ASSERT(false);
733 // deregister of c typelib callback
734 ::typelib_typedescription_revokeCallback( m_xTDMgr.get(), typelib_callback );
737 //==================================================================================================
738 sal_Bool SAL_CALL installTypeDescriptionManager(
739 Reference< container::XHierarchicalNameAccess > const & xTDMgr_c )
740 SAL_THROW( () )
742 uno::Environment curr_env(Environment::getCurrent());
743 uno::Environment target_env(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV))));
745 uno::Mapping curr2target(curr_env, target_env);
748 Reference<container::XHierarchicalNameAccess> xTDMgr(
749 reinterpret_cast<container::XHierarchicalNameAccess *>(
750 curr2target.mapInterface(xTDMgr_c.get(), ::getCppuType(&xTDMgr_c))),
751 SAL_NO_ACQUIRE);
753 Reference< lang::XComponent > xComp( xTDMgr, UNO_QUERY );
754 if (xComp.is())
756 xComp->addEventListener( new EventListenerImpl( xTDMgr ) );
757 // register c typelib callback
758 ::typelib_typedescription_registerCallback( xTDMgr.get(), typelib_callback );
759 return sal_True;
761 return sal_False;
764 } // end namespace cppu