update credits
[LibreOffice.git] / cppuhelper / source / tdmgr.cxx
blobf838adfdd8c77df94e38803b35d74026514ebbb7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "sal/config.h"
23 #include <vector>
25 #include <sal/alloca.h>
27 #include <osl/diagnose.h>
28 #include <rtl/alloc.h>
29 #include <rtl/ustring.hxx>
31 #include <uno/mapping.hxx>
33 #include <cppuhelper/bootstrap.hxx>
34 #include <cppuhelper/implbase1.hxx>
35 #include <typelib/typedescription.h>
37 #include <com/sun/star/lang/XComponent.hpp>
38 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
39 #include <com/sun/star/reflection/XTypeDescription.hpp>
40 #include <com/sun/star/reflection/XEnumTypeDescription.hpp>
41 #include <com/sun/star/reflection/XIndirectTypeDescription.hpp>
42 #include <com/sun/star/reflection/XInterfaceMemberTypeDescription.hpp>
43 #include <com/sun/star/reflection/XInterfaceAttributeTypeDescription2.hpp>
44 #include <com/sun/star/reflection/XMethodParameter.hpp>
45 #include <com/sun/star/reflection/XInterfaceMethodTypeDescription.hpp>
46 #include <com/sun/star/reflection/XInterfaceTypeDescription2.hpp>
47 #include <com/sun/star/reflection/XCompoundTypeDescription.hpp>
48 #include <com/sun/star/reflection/XStructTypeDescription.hpp>
49 #include <com/sun/star/reflection/XUnionTypeDescription.hpp>
50 #include "com/sun/star/uno/RuntimeException.hpp"
52 #include "boost/scoped_array.hpp"
54 using namespace ::rtl;
55 using namespace ::com::sun::star;
56 using namespace ::com::sun::star::uno;
57 using namespace ::com::sun::star::reflection;
60 namespace cppu
63 static typelib_TypeDescription * createCTD(
64 Reference< container::XHierarchicalNameAccess > const & access,
65 const Reference< XTypeDescription > & xType );
67 //==================================================================================================
68 inline static sal_Int64 coerceToInt64( const Any & rVal )
70 switch (rVal.getValueTypeClass())
72 case TypeClass_CHAR:
73 return *(sal_Unicode *)rVal.getValue();
74 case TypeClass_BOOLEAN:
75 return (*(sal_Bool *)rVal.getValue() ? 1 : 0);
76 case TypeClass_BYTE:
77 return *(sal_Int8 *)rVal.getValue();
78 case TypeClass_SHORT:
79 return *(sal_Int16 *)rVal.getValue();
80 case TypeClass_UNSIGNED_SHORT:
81 return *(sal_uInt16 *)rVal.getValue();
82 case TypeClass_LONG:
83 return *(sal_Int32 *)rVal.getValue();
84 case TypeClass_UNSIGNED_LONG:
85 return *(sal_uInt32 *)rVal.getValue();
86 case TypeClass_HYPER:
87 return *(sal_Int64 *)rVal.getValue();
88 case TypeClass_UNSIGNED_HYPER:
89 return *(sal_uInt64 *)rVal.getValue();
90 case TypeClass_ENUM:
91 return *(int *)rVal.getValue();
92 default:
93 OSL_ASSERT(false);
94 return 0;
97 //==================================================================================================
98 inline static typelib_TypeDescription * createCTD(
99 const Reference< XUnionTypeDescription > & xType )
101 typelib_TypeDescription * pRet = 0;
102 if (xType.is())
104 OUString aTypeName( xType->getName() );
106 // discriminant type
107 Reference< XTypeDescription > xDiscrTD( xType->getDiscriminantType() );
108 OUString aDiscrTypeName( xDiscrTD->getName() );
109 typelib_TypeDescriptionReference * pDiscrTypeRef = 0;
110 typelib_typedescriptionreference_new( &pDiscrTypeRef,
111 (typelib_TypeClass)xDiscrTD->getTypeClass(),
112 aDiscrTypeName.pData );
113 // default member type
114 Reference< XTypeDescription > xDefaultMemberTD( xType->getDefaultMemberType() );
115 OUString aDefMemberTypeName( xDefaultMemberTD->getName() );
116 typelib_TypeDescriptionReference * pDefMemberTypeRef = 0;
117 typelib_typedescriptionreference_new( &pDefMemberTypeRef,
118 (typelib_TypeClass)xDefaultMemberTD->getTypeClass(),
119 aDefMemberTypeName.pData );
120 // init array
121 Sequence< Any > aDiscriminants( xType->getDiscriminants() );
122 Sequence< Reference< XTypeDescription > > aMemberTypes( xType->getMemberTypes() );
123 Sequence< OUString > aMemberNames( xType->getMemberNames() );
124 sal_Int32 nMembers = aDiscriminants.getLength();
125 OSL_ASSERT( nMembers == aMemberNames.getLength() && nMembers == aMemberTypes.getLength() );
127 const Any * pDiscriminants = aDiscriminants.getConstArray();
128 const Reference< XTypeDescription > * pMemberTypes = aMemberTypes.getConstArray();
129 const OUString * pMemberNames = aMemberNames.getConstArray();
131 typelib_Union_Init * pMembers = (typelib_Union_Init *)alloca( nMembers * sizeof(typelib_Union_Init) );
133 sal_Int32 nPos;
134 for ( nPos = nMembers; nPos--; )
136 typelib_Union_Init & rEntry = pMembers[nPos];
137 // member discriminant
138 rEntry.nDiscriminant = coerceToInt64( pDiscriminants[nPos] );
139 // member type
140 OUString aMemberTypeName( pMemberTypes[nPos]->getName() );
141 rEntry.pTypeRef = 0;
142 typelib_typedescriptionreference_new( &rEntry.pTypeRef,
143 (typelib_TypeClass)pMemberTypes[nPos]->getTypeClass(),
144 aMemberTypeName.pData );
145 // member name
146 rEntry.pMemberName = pMemberNames[nPos].pData;
149 typelib_typedescription_newUnion( &pRet, aTypeName.pData,
150 pDiscrTypeRef,
151 coerceToInt64( xType->getDefaultDiscriminant() ),
152 pDefMemberTypeRef,
153 nMembers, pMembers );
155 for ( nPos = nMembers; nPos--; )
157 typelib_typedescriptionreference_release( pMembers[nPos].pTypeRef );
160 typelib_typedescriptionreference_release( pDiscrTypeRef );
161 typelib_typedescriptionreference_release( pDefMemberTypeRef );
163 return pRet;
165 //==================================================================================================
166 inline static typelib_TypeDescription * createCTD(
167 const Reference< XCompoundTypeDescription > & xType )
169 typelib_TypeDescription * pRet = 0;
170 if (xType.is())
172 typelib_TypeDescription * pBaseType = createCTD(
173 Reference< XCompoundTypeDescription >::query( xType->getBaseType() ) );
174 if (pBaseType)
175 typelib_typedescription_register( &pBaseType );
177 // construct member init array
178 const Sequence<Reference< XTypeDescription > > & rMemberTypes = xType->getMemberTypes();
179 const Sequence< OUString > & rMemberNames = xType->getMemberNames();
181 const Reference< XTypeDescription > * pMemberTypes = rMemberTypes.getConstArray();
182 const OUString * pMemberNames = rMemberNames.getConstArray();
184 sal_Int32 nMembers = rMemberTypes.getLength();
185 OSL_ENSURE( nMembers == rMemberNames.getLength(), "### lens differ!" );
187 OUString aTypeName( xType->getName() );
189 typelib_CompoundMember_Init * pMemberInits = (typelib_CompoundMember_Init *)alloca(
190 sizeof(typelib_CompoundMember_Init) * nMembers );
192 sal_Int32 nPos;
193 for ( nPos = nMembers; nPos--; )
195 typelib_CompoundMember_Init & rInit = pMemberInits[nPos];
196 rInit.eTypeClass = (typelib_TypeClass)pMemberTypes[nPos]->getTypeClass();
198 OUString aMemberTypeName( pMemberTypes[nPos]->getName() );
199 rtl_uString_acquire( rInit.pTypeName = aMemberTypeName.pData );
201 // string is held by rMemberNames
202 rInit.pMemberName = pMemberNames[nPos].pData;
205 typelib_typedescription_new(
206 &pRet,
207 (typelib_TypeClass)xType->getTypeClass(),
208 aTypeName.pData,
209 (pBaseType ? pBaseType->pWeakRef : 0),
210 nMembers, pMemberInits );
212 // cleanup
213 for ( nPos = nMembers; nPos--; )
215 rtl_uString_release( pMemberInits[nPos].pTypeName );
217 if (pBaseType)
218 typelib_typedescription_release( pBaseType );
220 return pRet;
222 //==================================================================================================
223 inline static typelib_TypeDescription * createCTD(
224 Reference< container::XHierarchicalNameAccess > const & access,
225 const Reference< XStructTypeDescription > & xType )
227 typelib_TypeDescription * pRet = 0;
228 if (xType.is() && xType->getTypeParameters().getLength() == 0)
230 typelib_TypeDescription * pBaseType = createCTD(
231 access, xType->getBaseType() );
232 if (pBaseType)
233 typelib_typedescription_register( &pBaseType );
235 // construct member init array
236 const Sequence<Reference< XTypeDescription > > & rMemberTypes = xType->getMemberTypes();
237 const Sequence< OUString > & rMemberNames = xType->getMemberNames();
239 const Reference< XTypeDescription > * pMemberTypes = rMemberTypes.getConstArray();
240 const OUString * pMemberNames = rMemberNames.getConstArray();
242 sal_Int32 nMembers = rMemberTypes.getLength();
243 OSL_ENSURE( nMembers == rMemberNames.getLength(), "### lens differ!" );
245 OUString aTypeName( xType->getName() );
247 typelib_StructMember_Init * pMemberInits = (typelib_StructMember_Init *)alloca(
248 sizeof(typelib_StructMember_Init) * nMembers );
250 Sequence< Reference< XTypeDescription > > templateMemberTypes;
251 sal_Int32 i = aTypeName.indexOf('<');
252 if (i >= 0) {
253 Reference< XStructTypeDescription > templateDesc(
254 access->getByHierarchicalName(aTypeName.copy(0, i)),
255 UNO_QUERY_THROW);
256 OSL_ASSERT(
257 templateDesc->getTypeParameters().getLength()
258 == xType->getTypeArguments().getLength());
259 templateMemberTypes = templateDesc->getMemberTypes();
260 OSL_ASSERT(templateMemberTypes.getLength() == nMembers);
263 sal_Int32 nPos;
264 for ( nPos = nMembers; nPos--; )
266 typelib_StructMember_Init & rInit = pMemberInits[nPos];
267 rInit.aBase.eTypeClass
268 = (typelib_TypeClass)pMemberTypes[nPos]->getTypeClass();
270 OUString aMemberTypeName( pMemberTypes[nPos]->getName() );
271 rtl_uString_acquire(
272 rInit.aBase.pTypeName = aMemberTypeName.pData );
274 // string is held by rMemberNames
275 rInit.aBase.pMemberName = pMemberNames[nPos].pData;
277 rInit.bParameterizedType = templateMemberTypes.getLength() != 0
278 && (templateMemberTypes[nPos]->getTypeClass()
279 == TypeClass_UNKNOWN);
282 typelib_typedescription_newStruct(
283 &pRet,
284 aTypeName.pData,
285 (pBaseType ? pBaseType->pWeakRef : 0),
286 nMembers, pMemberInits );
288 // cleanup
289 for ( nPos = nMembers; nPos--; )
291 rtl_uString_release( pMemberInits[nPos].aBase.pTypeName );
293 if (pBaseType)
294 typelib_typedescription_release( pBaseType );
296 return pRet;
298 //==================================================================================================
299 inline static typelib_TypeDescription * createCTD(
300 const Reference< XInterfaceAttributeTypeDescription2 > & xAttribute )
302 typelib_TypeDescription * pRet = 0;
303 if (xAttribute.is())
305 OUString aMemberName( xAttribute->getName() );
306 Reference< XTypeDescription > xType( xAttribute->getType() );
307 OUString aMemberTypeName( xType->getName() );
308 std::vector< rtl_uString * > getExc;
309 Sequence< Reference< XCompoundTypeDescription > > getExcs(
310 xAttribute->getGetExceptions() );
311 for (sal_Int32 i = 0; i != getExcs.getLength(); ++i)
313 OSL_ASSERT( getExcs[i].is() );
314 getExc.push_back( getExcs[i]->getName().pData );
316 std::vector< rtl_uString * > setExc;
317 Sequence< Reference< XCompoundTypeDescription > > setExcs(
318 xAttribute->getSetExceptions() );
319 for (sal_Int32 i = 0; i != setExcs.getLength(); ++i)
321 OSL_ASSERT( setExcs[i].is() );
322 setExc.push_back( setExcs[i]->getName().pData );
324 typelib_typedescription_newExtendedInterfaceAttribute(
325 (typelib_InterfaceAttributeTypeDescription **)&pRet,
326 xAttribute->getPosition(),
327 aMemberName.pData, // name
328 (typelib_TypeClass)xType->getTypeClass(),
329 aMemberTypeName.pData, // type name
330 xAttribute->isReadOnly(),
331 getExc.size(), getExc.empty() ? 0 : &getExc[0],
332 setExc.size(), setExc.empty() ? 0 : &setExc[0] );
334 return pRet;
336 //==================================================================================================
337 static typelib_TypeDescription * createCTD(
338 const Reference< XInterfaceMethodTypeDescription > & xMethod )
340 typelib_TypeDescription * pRet = 0;
341 if (xMethod.is())
343 Reference< XTypeDescription > xReturnType( xMethod->getReturnType() );
345 // init all params
346 const Sequence<Reference< XMethodParameter > > & rParams = xMethod->getParameters();
347 const Reference< XMethodParameter > * pParams = rParams.getConstArray();
348 sal_Int32 nParams = rParams.getLength();
350 typelib_Parameter_Init * pParamInit = (typelib_Parameter_Init *)alloca(
351 sizeof(typelib_Parameter_Init) * nParams );
353 sal_Int32 nPos;
354 for ( nPos = nParams; nPos--; )
356 const Reference< XMethodParameter > & xParam = pParams[nPos];
357 const Reference< XTypeDescription > & xType = xParam->getType();
358 typelib_Parameter_Init & rInit = pParamInit[xParam->getPosition()];
360 rInit.eTypeClass = (typelib_TypeClass)xType->getTypeClass();
361 OUString aParamTypeName( xType->getName() );
362 rtl_uString_acquire( rInit.pTypeName = aParamTypeName.pData );
363 OUString aParamName( xParam->getName() );
364 rtl_uString_acquire( rInit.pParamName = aParamName.pData );
365 rInit.bIn = xParam->isIn();
366 rInit.bOut = xParam->isOut();
369 // init all exception strings
370 const Sequence<Reference< XTypeDescription > > & rExceptions = xMethod->getExceptions();
371 const Reference< XTypeDescription > * pExceptions = rExceptions.getConstArray();
372 sal_Int32 nExceptions = rExceptions.getLength();
373 rtl_uString ** ppExceptionNames = (rtl_uString **)alloca(
374 sizeof(rtl_uString *) * nExceptions );
376 for ( nPos = nExceptions; nPos--; )
378 OUString aExceptionTypeName( pExceptions[nPos]->getName() );
379 rtl_uString_acquire( ppExceptionNames[nPos] = aExceptionTypeName.pData );
382 OUString aTypeName( xMethod->getName() );
383 OUString aReturnTypeName( xReturnType->getName() );
385 typelib_typedescription_newInterfaceMethod(
386 (typelib_InterfaceMethodTypeDescription **)&pRet,
387 xMethod->getPosition(),
388 xMethod->isOneway(),
389 aTypeName.pData,
390 (typelib_TypeClass)xReturnType->getTypeClass(),
391 aReturnTypeName.pData,
392 nParams, pParamInit,
393 nExceptions, ppExceptionNames );
395 for ( nPos = nParams; nPos--; )
397 rtl_uString_release( pParamInit[nPos].pTypeName );
398 rtl_uString_release( pParamInit[nPos].pParamName );
400 for ( nPos = nExceptions; nPos--; )
402 rtl_uString_release( ppExceptionNames[nPos] );
405 return pRet;
407 //==================================================================================================
408 inline static typelib_TypeDescription * createCTD(
409 Reference< container::XHierarchicalNameAccess > const & access,
410 const Reference< XInterfaceTypeDescription2 > & xType )
412 typelib_TypeDescription * pRet = 0;
413 if (xType.is())
415 Sequence< Reference< XTypeDescription > > aBases(xType->getBaseTypes());
416 sal_Int32 nBases = aBases.getLength();
417 // Exploit the fact that a typelib_TypeDescription for an interface type
418 // is also the typelib_TypeDescriptionReference for that type:
419 boost::scoped_array< typelib_TypeDescription * > aBaseTypes(
420 new typelib_TypeDescription *[nBases]);
421 for (sal_Int32 i = 0; i < nBases; ++i) {
422 typelib_TypeDescription * p = createCTD(access, aBases[i]);
423 OSL_ASSERT(
424 !TYPELIB_TYPEDESCRIPTIONREFERENCE_ISREALLYWEAK(p->eTypeClass));
425 typelib_typedescription_register(&p);
426 aBaseTypes[i] = p;
428 typelib_TypeDescriptionReference ** pBaseTypeRefs
429 = reinterpret_cast< typelib_TypeDescriptionReference ** >(
430 aBaseTypes.get());
432 // construct all member refs
433 const Sequence<Reference< XInterfaceMemberTypeDescription > > & rMembers = xType->getMembers();
434 sal_Int32 nMembers = rMembers.getLength();
436 typelib_TypeDescriptionReference ** ppMemberRefs = (typelib_TypeDescriptionReference **)alloca(
437 sizeof(typelib_TypeDescriptionReference *) * nMembers );
439 const Reference< XInterfaceMemberTypeDescription > * pMembers = rMembers.getConstArray();
441 OUString aTypeName( xType->getName() );
443 sal_Int32 nPos;
444 for ( nPos = nMembers; nPos--; )
446 OUString aMemberTypeName( pMembers[nPos]->getName() );
447 ppMemberRefs[nPos] = 0;
448 typelib_typedescriptionreference_new(
449 ppMemberRefs + nPos,
450 (typelib_TypeClass)pMembers[nPos]->getTypeClass(),
451 aMemberTypeName.pData );
454 Uik uik = xType->getUik();
456 typelib_typedescription_newMIInterface(
457 (typelib_InterfaceTypeDescription **)&pRet,
458 aTypeName.pData,
459 uik.m_Data1, uik.m_Data2, uik.m_Data3, uik.m_Data4, uik.m_Data5,
460 nBases, pBaseTypeRefs,
461 nMembers, ppMemberRefs );
463 // cleanup refs and base type
464 for (int i = 0; i < nBases; ++i) {
465 typelib_typedescription_release(aBaseTypes[i]);
468 for ( nPos = nMembers; nPos--; )
470 typelib_typedescriptionreference_release( ppMemberRefs[nPos] );
473 return pRet;
475 //==================================================================================================
476 inline static typelib_TypeDescription * createCTD( const Reference< XEnumTypeDescription > & xType )
478 typelib_TypeDescription * pRet = 0;
479 if (xType.is())
481 OUString aTypeName( xType->getName() );
482 Sequence< OUString > aNames( xType->getEnumNames() );
483 OSL_ASSERT( sizeof(OUString) == sizeof(rtl_uString *) ); // !!!
484 Sequence< sal_Int32 > aValues( xType->getEnumValues() );
486 typelib_typedescription_newEnum(
487 &pRet, aTypeName.pData, xType->getDefaultEnumValue(),
488 aNames.getLength(),
489 (rtl_uString **)aNames.getConstArray(),
490 const_cast< sal_Int32 * >( aValues.getConstArray() ) );
492 return pRet;
494 //==================================================================================================
495 inline static typelib_TypeDescription * createCTD(
496 Reference< container::XHierarchicalNameAccess > const & access,
497 const Reference< XIndirectTypeDescription > & xType )
499 typelib_TypeDescription * pRet = 0;
500 if (xType.is())
502 typelib_TypeDescription * pRefType = createCTD(
503 access, xType->getReferencedType() );
504 typelib_typedescription_register( &pRefType );
506 OUString aTypeName( xType->getName() );
508 typelib_typedescription_new(
509 &pRet,
510 (typelib_TypeClass)xType->getTypeClass(),
511 aTypeName.pData,
512 pRefType->pWeakRef,
513 0, 0 );
515 // cleanup
516 typelib_typedescription_release( pRefType );
518 return pRet;
521 //==================================================================================================
522 static typelib_TypeDescription * createCTD(
523 Reference< container::XHierarchicalNameAccess > const & access,
524 const Reference< XTypeDescription > & xType )
526 typelib_TypeDescription * pRet = 0;
528 if (xType.is())
530 switch (xType->getTypeClass())
532 // built in types
533 case TypeClass_VOID:
535 OUString aTypeName("void");
536 typelib_typedescription_new( &pRet, typelib_TypeClass_VOID, aTypeName.pData, 0, 0, 0 );
537 break;
539 case TypeClass_CHAR:
541 OUString aTypeName("char");
542 typelib_typedescription_new( &pRet, typelib_TypeClass_CHAR, aTypeName.pData, 0, 0, 0 );
543 break;
545 case TypeClass_BOOLEAN:
547 OUString aTypeName("boolean");
548 typelib_typedescription_new( &pRet, typelib_TypeClass_BOOLEAN, aTypeName.pData, 0, 0, 0 );
549 break;
551 case TypeClass_BYTE:
553 OUString aTypeName("byte");
554 typelib_typedescription_new( &pRet, typelib_TypeClass_BYTE, aTypeName.pData, 0, 0, 0 );
555 break;
557 case TypeClass_SHORT:
559 OUString aTypeName("short");
560 typelib_typedescription_new( &pRet, typelib_TypeClass_SHORT, aTypeName.pData, 0, 0, 0 );
561 break;
563 case TypeClass_UNSIGNED_SHORT:
565 OUString aTypeName("unsigned short");
566 typelib_typedescription_new( &pRet, typelib_TypeClass_UNSIGNED_SHORT, aTypeName.pData, 0, 0, 0 );
567 break;
569 case TypeClass_LONG:
571 OUString aTypeName("long");
572 typelib_typedescription_new( &pRet, typelib_TypeClass_LONG, aTypeName.pData, 0, 0, 0 );
573 break;
575 case TypeClass_UNSIGNED_LONG:
577 OUString aTypeName("unsigned long");
578 typelib_typedescription_new( &pRet, typelib_TypeClass_UNSIGNED_LONG, aTypeName.pData, 0, 0, 0 );
579 break;
581 case TypeClass_HYPER:
583 OUString aTypeName("hyper");
584 typelib_typedescription_new( &pRet, typelib_TypeClass_HYPER, aTypeName.pData, 0, 0, 0 );
585 break;
587 case TypeClass_UNSIGNED_HYPER:
589 OUString aTypeName("unsigned hyper");
590 typelib_typedescription_new( &pRet, typelib_TypeClass_UNSIGNED_HYPER, aTypeName.pData, 0, 0, 0 );
591 break;
593 case TypeClass_FLOAT:
595 OUString aTypeName("float");
596 typelib_typedescription_new( &pRet, typelib_TypeClass_FLOAT, aTypeName.pData, 0, 0, 0 );
597 break;
599 case TypeClass_DOUBLE:
601 OUString aTypeName("double");
602 typelib_typedescription_new( &pRet, typelib_TypeClass_DOUBLE, aTypeName.pData, 0, 0, 0 );
603 break;
605 case TypeClass_STRING:
607 OUString aTypeName("string");
608 typelib_typedescription_new( &pRet, typelib_TypeClass_STRING, aTypeName.pData, 0, 0, 0 );
609 break;
611 case TypeClass_TYPE:
613 OUString aTypeName("type");
614 typelib_typedescription_new( &pRet, typelib_TypeClass_TYPE, aTypeName.pData, 0, 0, 0 );
615 break;
617 case TypeClass_ANY:
619 OUString aTypeName("any");
620 typelib_typedescription_new( &pRet, typelib_TypeClass_ANY, aTypeName.pData, 0, 0, 0 );
621 break;
624 case TypeClass_UNION:
625 pRet = createCTD( Reference< XUnionTypeDescription >::query( xType ) );
626 break;
627 case TypeClass_EXCEPTION:
628 pRet = createCTD( Reference< XCompoundTypeDescription >::query( xType ) );
629 break;
630 case TypeClass_STRUCT:
631 pRet = createCTD(
632 access, Reference< XStructTypeDescription >::query( xType ) );
633 break;
634 case TypeClass_ENUM:
635 pRet = createCTD( Reference< XEnumTypeDescription >::query( xType ) );
636 break;
637 case TypeClass_TYPEDEF:
639 Reference< XIndirectTypeDescription > xTypedef( xType, UNO_QUERY );
640 if (xTypedef.is())
641 pRet = createCTD( access, xTypedef->getReferencedType() );
642 break;
644 case TypeClass_SEQUENCE:
645 pRet = createCTD(
646 access, Reference< XIndirectTypeDescription >::query( xType ) );
647 break;
648 case TypeClass_INTERFACE:
649 pRet = createCTD(
650 access,
651 Reference< XInterfaceTypeDescription2 >::query( xType ) );
652 break;
653 case TypeClass_INTERFACE_METHOD:
654 pRet = createCTD( Reference< XInterfaceMethodTypeDescription >::query( xType ) );
655 break;
656 case TypeClass_INTERFACE_ATTRIBUTE:
657 pRet = createCTD( Reference< XInterfaceAttributeTypeDescription2 >::query( xType ) );
658 break;
659 default:
660 break;
664 return pRet;
668 //==================================================================================================
669 extern "C"
671 static void SAL_CALL typelib_callback(
672 void * pContext, typelib_TypeDescription ** ppRet, rtl_uString * pTypeName )
674 OSL_ENSURE( pContext && ppRet && pTypeName, "### null ptr!" );
675 if (ppRet)
677 if (*ppRet)
679 ::typelib_typedescription_release( *ppRet );
680 *ppRet = 0;
682 if (pContext && pTypeName)
684 Reference< container::XHierarchicalNameAccess > access(
685 reinterpret_cast< container::XHierarchicalNameAccess * >(
686 pContext));
689 OUString const & rTypeName = OUString::unacquired( &pTypeName );
690 Reference< XTypeDescription > xTD;
691 if (access->getByHierarchicalName(rTypeName ) >>= xTD)
693 *ppRet = createCTD( access, xTD );
696 catch (container::NoSuchElementException & exc)
698 (void) exc; // avoid warning about unused variable
699 OSL_TRACE(
700 "typelibrary type not available: %s",
701 OUStringToOString(
702 exc.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
704 catch (Exception & exc)
706 (void) exc; // avoid warning about unused variable
707 OSL_TRACE(
708 "%s",
709 OUStringToOString(
710 exc.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
717 //==================================================================================================
718 class EventListenerImpl
719 : public WeakImplHelper1< lang::XEventListener >
721 Reference< container::XHierarchicalNameAccess > m_xTDMgr;
723 public:
724 inline EventListenerImpl(
725 Reference< container::XHierarchicalNameAccess > const & xTDMgr )
726 SAL_THROW(())
727 : m_xTDMgr( xTDMgr )
730 // XEventListener
731 virtual void SAL_CALL disposing( lang::EventObject const & rEvt )
732 throw (RuntimeException);
734 //__________________________________________________________________________________________________
735 void EventListenerImpl::disposing( lang::EventObject const & rEvt )
736 throw (RuntimeException)
738 if (rEvt.Source != m_xTDMgr) {
739 OSL_ASSERT(false);
741 // deregister of c typelib callback
742 ::typelib_typedescription_revokeCallback( m_xTDMgr.get(), typelib_callback );
745 //==================================================================================================
746 sal_Bool SAL_CALL installTypeDescriptionManager(
747 Reference< container::XHierarchicalNameAccess > const & xTDMgr_c )
748 SAL_THROW(())
750 uno::Environment curr_env(Environment::getCurrent());
751 uno::Environment target_env(rtl::OUString(CPPU_STRINGIFY(CPPU_ENV)));
753 uno::Mapping curr2target(curr_env, target_env);
756 Reference<container::XHierarchicalNameAccess> xTDMgr(
757 reinterpret_cast<container::XHierarchicalNameAccess *>(
758 curr2target.mapInterface(xTDMgr_c.get(), ::getCppuType(&xTDMgr_c))),
759 SAL_NO_ACQUIRE);
761 Reference< lang::XComponent > xComp( xTDMgr, UNO_QUERY );
762 if (xComp.is())
764 xComp->addEventListener( new EventListenerImpl( xTDMgr ) );
765 // register c typelib callback
766 ::typelib_typedescription_registerCallback( xTDMgr.get(), typelib_callback );
767 return sal_True;
769 return sal_False;
772 } // end namespace cppu
774 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */