Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / stoc / source / corereflection / criface.cxx
blobd5a3a2192d607746379f87f184aa54089dbb31aa
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 .
20 #include <sal/config.h>
21 #ifdef SAL_UNX
22 #include <sal/alloca.h>
23 #endif
24 #if !(defined(MACOSX) || defined(IOS) || defined(FREEBSD))
25 #include <malloc.h>
26 #endif
27 #include <o3tl/any.hxx>
28 #include <rtl/alloc.h>
29 #include <typelib/typedescription.hxx>
30 #include <uno/data.h>
32 #include "base.hxx"
34 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
35 #include <com/sun/star/uno/RuntimeException.hpp>
36 #include <cppuhelper/queryinterface.hxx>
37 #include <cppuhelper/exc_hlp.hxx>
39 using namespace css::lang;
40 using namespace css::reflection;
41 using namespace css::uno;
43 namespace stoc_corefl
47 class IdlAttributeFieldImpl
48 : public IdlMemberImpl
49 , public XIdlField
50 , public XIdlField2
52 public:
53 typelib_InterfaceAttributeTypeDescription * getAttributeTypeDescr()
54 { return reinterpret_cast<typelib_InterfaceAttributeTypeDescription *>(getTypeDescr()); }
56 IdlAttributeFieldImpl( IdlReflectionServiceImpl * pReflection, const OUString & rName,
57 typelib_TypeDescription * pTypeDescr, typelib_TypeDescription * pDeclTypeDescr )
58 : IdlMemberImpl( pReflection, rName, pTypeDescr, pDeclTypeDescr )
61 // XInterface
62 virtual Any SAL_CALL queryInterface( const Type & rType ) override;
63 virtual void SAL_CALL acquire() throw() override;
64 virtual void SAL_CALL release() throw() override;
66 // XTypeProvider
67 virtual Sequence< Type > SAL_CALL getTypes() override;
68 virtual Sequence< sal_Int8 > SAL_CALL getImplementationId() override;
70 // XIdlMember
71 virtual Reference< XIdlClass > SAL_CALL getDeclaringClass() override;
72 virtual OUString SAL_CALL getName() override;
73 // XIdlField
74 virtual Reference< XIdlClass > SAL_CALL getType() override;
75 virtual FieldAccessMode SAL_CALL getAccessMode() override;
76 virtual Any SAL_CALL get( const Any & rObj ) override;
77 virtual void SAL_CALL set( const Any & rObj, const Any & rValue ) override;
78 // XIdlField2: getType, getAccessMode and get are equal to XIdlField
79 virtual void SAL_CALL set( Any & rObj, const Any & rValue ) override;
81 private:
82 void checkException(
83 uno_Any * exception, Reference< XInterface > const & context);
86 // XInterface
88 Any IdlAttributeFieldImpl::queryInterface( const Type & rType )
90 Any aRet( ::cppu::queryInterface( rType,
91 static_cast< XIdlField * >( this ),
92 static_cast< XIdlField2 * >( this ) ) );
93 return (aRet.hasValue() ? aRet : IdlMemberImpl::queryInterface( rType ));
96 void IdlAttributeFieldImpl::acquire() throw()
98 IdlMemberImpl::acquire();
101 void IdlAttributeFieldImpl::release() throw()
103 IdlMemberImpl::release();
106 // XTypeProvider
108 Sequence< Type > IdlAttributeFieldImpl::getTypes()
110 static ::cppu::OTypeCollection * s_pTypes = nullptr;
111 if (! s_pTypes)
113 ::osl::MutexGuard aGuard( getMutexAccess() );
114 if (! s_pTypes)
116 static ::cppu::OTypeCollection s_aTypes(
117 cppu::UnoType<XIdlField2>::get(),
118 cppu::UnoType<XIdlField>::get(),
119 IdlMemberImpl::getTypes() );
120 s_pTypes = &s_aTypes;
123 return s_pTypes->getTypes();
126 Sequence< sal_Int8 > IdlAttributeFieldImpl::getImplementationId()
128 return css::uno::Sequence<sal_Int8>();
131 // XIdlMember
133 Reference< XIdlClass > IdlAttributeFieldImpl::getDeclaringClass()
135 if (! _xDeclClass.is())
137 ::osl::MutexGuard aGuard( getMutexAccess() );
138 if (! _xDeclClass.is())
140 OUString aName(getAttributeTypeDescr()->aBase.aBase.pTypeName);
141 sal_Int32 i = aName.indexOf(':');
142 OSL_ASSERT(i >= 0);
143 _xDeclClass = getReflection()->forName(aName.copy(0, i));
146 return _xDeclClass;
149 OUString IdlAttributeFieldImpl::getName()
151 return IdlMemberImpl::getName();
154 // XIdlField
156 Reference< XIdlClass > IdlAttributeFieldImpl::getType()
158 return getReflection()->forType(
159 getAttributeTypeDescr()->pAttributeTypeRef );
162 FieldAccessMode IdlAttributeFieldImpl::getAccessMode()
164 return ((getAttributeTypeDescr())->bReadOnly
165 ? FieldAccessMode_READONLY : FieldAccessMode_READWRITE);
168 Any IdlAttributeFieldImpl::get( const Any & rObj )
170 uno_Interface * pUnoI = getReflection()->mapToUno(
171 rObj, reinterpret_cast<typelib_InterfaceTypeDescription *>(getDeclTypeDescr()) );
172 OSL_ENSURE( pUnoI, "### illegal destination object given!" );
173 if (pUnoI)
175 TypeDescription aTD( getAttributeTypeDescr()->pAttributeTypeRef );
176 typelib_TypeDescription * pTD = aTD.get();
178 uno_Any aExc;
179 uno_Any * pExc = &aExc;
180 void * pReturn = alloca( pTD->nSize );
182 (*pUnoI->pDispatcher)( pUnoI, getTypeDescr(), pReturn, nullptr, &pExc );
183 (*pUnoI->release)( pUnoI );
185 checkException(pExc, *o3tl::doAccess<Reference<XInterface>>(rObj));
186 Any aRet;
187 uno_any_destruct(
188 &aRet, reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
189 uno_any_constructAndConvert( &aRet, pReturn, pTD, getReflection()->getUno2Cpp().get() );
190 uno_destructData( pReturn, pTD, nullptr );
191 return aRet;
193 throw IllegalArgumentException(
194 "illegal object given!",
195 static_cast<XWeak *>(static_cast<OWeakObject *>(this)), 0 );
198 void IdlAttributeFieldImpl::set( Any & rObj, const Any & rValue )
200 if (getAttributeTypeDescr()->bReadOnly)
202 throw IllegalAccessException(
203 "cannot set readonly attribute!",
204 static_cast<XWeak *>(static_cast<OWeakObject *>(this)) );
207 uno_Interface * pUnoI = getReflection()->mapToUno(
208 rObj, reinterpret_cast<typelib_InterfaceTypeDescription *>(getDeclTypeDescr()) );
209 OSL_ENSURE( pUnoI, "### illegal destination object given!" );
210 if (pUnoI)
212 TypeDescription aTD( getAttributeTypeDescr()->pAttributeTypeRef );
213 typelib_TypeDescription * pTD = aTD.get();
215 // construct uno value to be set
216 void * pArgs[1];
217 void * pArg = pArgs[0] = alloca( pTD->nSize );
219 bool bAssign;
220 if (pTD->eTypeClass == typelib_TypeClass_ANY)
222 uno_copyAndConvertData( pArg, (const_cast< Any * >(&rValue)),
223 pTD, getReflection()->getCpp2Uno().get() );
224 bAssign = true;
226 else if (typelib_typedescriptionreference_equals( rValue.getValueTypeRef(), pTD->pWeakRef ))
228 uno_copyAndConvertData( pArg, (const_cast< void * >(rValue.getValue()) ),
229 pTD, getReflection()->getCpp2Uno().get() );
230 bAssign = true;
232 else if (pTD->eTypeClass == typelib_TypeClass_INTERFACE)
234 Reference< XInterface > xObj;
235 bAssign = extract(
236 rValue, reinterpret_cast<typelib_InterfaceTypeDescription *>(pTD), xObj,
237 getReflection() );
238 if (bAssign)
240 *static_cast<void **>(pArg) = getReflection()->getCpp2Uno().mapInterface(
241 xObj.get(), reinterpret_cast<typelib_InterfaceTypeDescription *>(pTD) );
244 else
246 typelib_TypeDescription * pValueTD = nullptr;
247 TYPELIB_DANGER_GET( &pValueTD, rValue.getValueTypeRef() );
248 // construct temp uno val to do proper assignment: todo opt
249 void * pTemp = alloca( pValueTD->nSize );
250 uno_copyAndConvertData(
251 pTemp, const_cast<void *>(rValue.getValue()), pValueTD, getReflection()->getCpp2Uno().get() );
252 uno_constructData(
253 pArg, pTD );
254 // assignment does simple conversion
255 bAssign = uno_assignData(
256 pArg, pTD, pTemp, pValueTD, nullptr, nullptr, nullptr );
257 uno_destructData(
258 pTemp, pValueTD, nullptr );
259 TYPELIB_DANGER_RELEASE( pValueTD );
262 if (bAssign)
264 uno_Any aExc;
265 uno_Any * pExc = &aExc;
266 (*pUnoI->pDispatcher)( pUnoI, getTypeDescr(), nullptr, pArgs, &pExc );
267 (*pUnoI->release)( pUnoI );
269 uno_destructData( pArg, pTD, nullptr );
270 checkException(pExc, *o3tl::doAccess<Reference<XInterface>>(rObj));
271 return;
273 (*pUnoI->release)( pUnoI );
275 throw IllegalArgumentException(
276 "illegal value given!",
277 *o3tl::doAccess<Reference<XInterface>>(rObj), 1 );
279 throw IllegalArgumentException(
280 "illegal destination object given!",
281 static_cast<XWeak *>(static_cast<OWeakObject *>(this)), 0 );
284 void IdlAttributeFieldImpl::set( const Any & rObj, const Any & rValue )
286 IdlAttributeFieldImpl::set( const_cast< Any & >( rObj ), rValue );
289 void IdlAttributeFieldImpl::checkException(
290 uno_Any * exception, Reference< XInterface > const & context)
292 if (exception != nullptr) {
293 Any e;
294 uno_any_destruct(&e, reinterpret_cast< uno_ReleaseFunc >(cpp_release));
295 uno_type_any_constructAndConvert(
296 &e, exception->pData, exception->pType,
297 getReflection()->getUno2Cpp().get());
298 uno_any_destruct(exception, nullptr);
299 if (e.isExtractableTo(
300 cppu::UnoType<RuntimeException>::get()))
302 cppu::throwException(e);
303 } else {
304 throw WrappedTargetRuntimeException(
305 "non-RuntimeException occurred when accessing an"
306 " interface type attribute",
307 context, e);
313 class IdlInterfaceMethodImpl
314 : public IdlMemberImpl
315 , public XIdlMethod
317 Sequence< Reference< XIdlClass > > * _pExceptionTypes;
318 Sequence< Reference< XIdlClass > > * _pParamTypes;
319 Sequence< ParamInfo > * _pParamInfos;
321 public:
322 typelib_InterfaceMethodTypeDescription * getMethodTypeDescr()
323 { return reinterpret_cast<typelib_InterfaceMethodTypeDescription *>(getTypeDescr()); }
325 IdlInterfaceMethodImpl( IdlReflectionServiceImpl * pReflection, const OUString & rName,
326 typelib_TypeDescription * pTypeDescr, typelib_TypeDescription * pDeclTypeDescr )
327 : IdlMemberImpl( pReflection, rName, pTypeDescr, pDeclTypeDescr )
328 , _pExceptionTypes( nullptr )
329 , _pParamTypes( nullptr )
330 , _pParamInfos( nullptr )
332 virtual ~IdlInterfaceMethodImpl() override;
334 // XInterface
335 virtual Any SAL_CALL queryInterface( const Type & rType ) override;
336 virtual void SAL_CALL acquire() throw() override;
337 virtual void SAL_CALL release() throw() override;
339 // XTypeProvider
340 virtual Sequence< Type > SAL_CALL getTypes() override;
341 virtual Sequence< sal_Int8 > SAL_CALL getImplementationId() override;
343 // XIdlMember
344 virtual Reference< XIdlClass > SAL_CALL getDeclaringClass() override;
345 virtual OUString SAL_CALL getName() override;
346 // XIdlMethod
347 virtual Reference< XIdlClass > SAL_CALL getReturnType() override;
348 virtual Sequence< Reference< XIdlClass > > SAL_CALL getParameterTypes() override;
349 virtual Sequence< ParamInfo > SAL_CALL getParameterInfos() override;
350 virtual Sequence< Reference< XIdlClass > > SAL_CALL getExceptionTypes() override;
351 virtual MethodMode SAL_CALL getMode() override;
352 virtual Any SAL_CALL invoke( const Any & rObj, Sequence< Any > & rArgs ) override;
355 IdlInterfaceMethodImpl::~IdlInterfaceMethodImpl()
357 delete _pParamInfos;
358 delete _pParamTypes;
359 delete _pExceptionTypes;
362 // XInterface
364 Any IdlInterfaceMethodImpl::queryInterface( const Type & rType )
366 Any aRet( ::cppu::queryInterface( rType, static_cast< XIdlMethod * >( this ) ) );
367 return (aRet.hasValue() ? aRet : IdlMemberImpl::queryInterface( rType ));
370 void IdlInterfaceMethodImpl::acquire() throw()
372 IdlMemberImpl::acquire();
375 void IdlInterfaceMethodImpl::release() throw()
377 IdlMemberImpl::release();
380 // XTypeProvider
382 Sequence< Type > IdlInterfaceMethodImpl::getTypes()
384 static ::cppu::OTypeCollection * s_pTypes = nullptr;
385 if (! s_pTypes)
387 ::osl::MutexGuard aGuard( getMutexAccess() );
388 if (! s_pTypes)
390 static ::cppu::OTypeCollection s_aTypes(
391 cppu::UnoType<XIdlMethod>::get(),
392 IdlMemberImpl::getTypes() );
393 s_pTypes = &s_aTypes;
396 return s_pTypes->getTypes();
399 Sequence< sal_Int8 > IdlInterfaceMethodImpl::getImplementationId()
401 return css::uno::Sequence<sal_Int8>();
404 // XIdlMember
406 Reference< XIdlClass > IdlInterfaceMethodImpl::getDeclaringClass()
408 if (! _xDeclClass.is())
410 ::osl::MutexGuard aGuard( getMutexAccess() );
411 if (! _xDeclClass.is())
413 OUString aName(getMethodTypeDescr()->aBase.aBase.pTypeName);
414 sal_Int32 i = aName.indexOf(':');
415 OSL_ASSERT(i >= 0);
416 _xDeclClass = getReflection()->forName(aName.copy(0, i));
419 return _xDeclClass;
422 OUString IdlInterfaceMethodImpl::getName()
424 return IdlMemberImpl::getName();
427 // XIdlMethod
429 Reference< XIdlClass > SAL_CALL IdlInterfaceMethodImpl::getReturnType()
431 return getReflection()->forType( getMethodTypeDescr()->pReturnTypeRef );
434 Sequence< Reference< XIdlClass > > IdlInterfaceMethodImpl::getExceptionTypes()
436 if (! _pExceptionTypes)
438 ::osl::MutexGuard aGuard( getMutexAccess() );
439 if (! _pExceptionTypes)
441 sal_Int32 nExc = getMethodTypeDescr()->nExceptions;
442 Sequence< Reference< XIdlClass > > * pTempExceptionTypes =
443 new Sequence< Reference< XIdlClass > >( nExc );
444 Reference< XIdlClass > * pExceptionTypes = pTempExceptionTypes->getArray();
446 typelib_TypeDescriptionReference ** ppExc =
447 getMethodTypeDescr()->ppExceptions;
448 IdlReflectionServiceImpl * pRefl = getReflection();
450 while (nExc--)
451 pExceptionTypes[nExc] = pRefl->forType( ppExc[nExc] );
453 _pExceptionTypes = pTempExceptionTypes;
456 return *_pExceptionTypes;
459 Sequence< Reference< XIdlClass > > IdlInterfaceMethodImpl::getParameterTypes()
461 if (! _pParamTypes)
463 ::osl::MutexGuard aGuard( getMutexAccess() );
464 if (! _pParamTypes)
466 sal_Int32 nParams = getMethodTypeDescr()->nParams;
467 Sequence< Reference< XIdlClass > > * pTempParamTypes =
468 new Sequence< Reference< XIdlClass > >( nParams );
469 Reference< XIdlClass > * pParamTypes = pTempParamTypes->getArray();
471 typelib_MethodParameter * pTypelibParams =
472 getMethodTypeDescr()->pParams;
473 IdlReflectionServiceImpl * pRefl = getReflection();
475 while (nParams--)
476 pParamTypes[nParams] = pRefl->forType( pTypelibParams[nParams].pTypeRef );
478 _pParamTypes = pTempParamTypes;
481 return *_pParamTypes;
484 Sequence< ParamInfo > IdlInterfaceMethodImpl::getParameterInfos()
486 if (! _pParamInfos)
488 ::osl::MutexGuard aGuard( getMutexAccess() );
489 if (! _pParamInfos)
491 sal_Int32 nParams = getMethodTypeDescr()->nParams;
492 Sequence< ParamInfo > * pTempParamInfos = new Sequence< ParamInfo >( nParams );
493 ParamInfo * pParamInfos = pTempParamInfos->getArray();
495 typelib_MethodParameter * pTypelibParams =
496 getMethodTypeDescr()->pParams;
498 if (_pParamTypes) // use param types
500 const Reference< XIdlClass > * pParamTypes = _pParamTypes->getConstArray();
502 while (nParams--)
504 const typelib_MethodParameter & rParam = pTypelibParams[nParams];
505 ParamInfo & rInfo = pParamInfos[nParams];
506 rInfo.aName = rParam.pName;
507 if (rParam.bIn)
508 rInfo.aMode = (rParam.bOut ? ParamMode_INOUT : ParamMode_IN);
509 else
510 rInfo.aMode = ParamMode_OUT;
511 rInfo.aType = pParamTypes[nParams];
514 else // make also param types sequence if not already initialized
516 Sequence< Reference< XIdlClass > > * pTempParamTypes =
517 new Sequence< Reference< XIdlClass > >( nParams );
518 Reference< XIdlClass > * pParamTypes = pTempParamTypes->getArray();
520 IdlReflectionServiceImpl * pRefl = getReflection();
522 while (nParams--)
524 const typelib_MethodParameter & rParam = pTypelibParams[nParams];
525 ParamInfo & rInfo = pParamInfos[nParams];
526 rInfo.aName = rParam.pName;
527 if (rParam.bIn)
528 rInfo.aMode = (rParam.bOut ? ParamMode_INOUT : ParamMode_IN);
529 else
530 rInfo.aMode = ParamMode_OUT;
531 rInfo.aType = pParamTypes[nParams] = pRefl->forType( rParam.pTypeRef );
534 _pParamTypes = pTempParamTypes;
537 _pParamInfos = pTempParamInfos;
540 return *_pParamInfos;
543 MethodMode SAL_CALL IdlInterfaceMethodImpl::getMode()
545 return
546 getMethodTypeDescr()->bOneWay ? MethodMode_ONEWAY : MethodMode_TWOWAY;
549 Any SAL_CALL IdlInterfaceMethodImpl::invoke( const Any & rObj, Sequence< Any > & rArgs )
551 if (auto ifc = o3tl::tryAccess<css::uno::Reference<css::uno::XInterface>>(
552 rObj))
554 // acquire()/ release()
555 if (rtl_ustr_ascii_compare( getTypeDescr()->pTypeName->buffer,
556 "com.sun.star.uno.XInterface::acquire" ) == 0)
558 (*ifc)->acquire();
559 return Any();
561 else if (rtl_ustr_ascii_compare( getTypeDescr()->pTypeName->buffer,
562 "com.sun.star.uno.XInterface::release" ) == 0)
564 (*ifc)->release();
565 return Any();
569 uno_Interface * pUnoI = getReflection()->mapToUno(
570 rObj, reinterpret_cast<typelib_InterfaceTypeDescription *>(getDeclTypeDescr()) );
571 OSL_ENSURE( pUnoI, "### illegal destination object given!" );
572 if (pUnoI)
574 sal_Int32 nParams = getMethodTypeDescr()->nParams;
575 if (rArgs.getLength() != nParams)
577 (*pUnoI->release)( pUnoI );
578 throw IllegalArgumentException(
579 "expected " + OUString::number(nParams) +
580 " arguments, got " + OUString::number(rArgs.getLength()),
581 *o3tl::doAccess<Reference<XInterface>>(rObj), 1 );
584 Any * pCppArgs = rArgs.getArray();
585 typelib_MethodParameter * pParams = getMethodTypeDescr()->pParams;
586 typelib_TypeDescription * pReturnType = nullptr;
587 TYPELIB_DANGER_GET(
588 &pReturnType, getMethodTypeDescr()->pReturnTypeRef );
590 void * pUnoReturn = alloca( pReturnType->nSize );
591 void ** ppUnoArgs = static_cast<void **>(alloca( sizeof(void *) * nParams *2 ));
592 typelib_TypeDescription ** ppParamTypes = reinterpret_cast<typelib_TypeDescription **>(ppUnoArgs + nParams);
594 // convert arguments
595 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
597 ppParamTypes[nPos] = nullptr;
598 TYPELIB_DANGER_GET( ppParamTypes + nPos, pParams[nPos].pTypeRef );
599 typelib_TypeDescription * pTD = ppParamTypes[nPos];
601 ppUnoArgs[nPos] = alloca( pTD->nSize );
602 if (pParams[nPos].bIn)
604 bool bAssign;
605 if (typelib_typedescriptionreference_equals(
606 pCppArgs[nPos].getValueTypeRef(), pTD->pWeakRef ))
608 uno_type_copyAndConvertData(
609 ppUnoArgs[nPos], const_cast<void *>(pCppArgs[nPos].getValue()),
610 pCppArgs[nPos].getValueTypeRef(), getReflection()->getCpp2Uno().get() );
611 bAssign = true;
613 else if (pTD->eTypeClass == typelib_TypeClass_ANY)
615 uno_type_any_constructAndConvert(
616 static_cast<uno_Any *>(ppUnoArgs[nPos]), const_cast<void *>(pCppArgs[nPos].getValue()),
617 pCppArgs[nPos].getValueTypeRef(), getReflection()->getCpp2Uno().get() );
618 bAssign = true;
620 else if (pTD->eTypeClass == typelib_TypeClass_INTERFACE)
622 Reference< XInterface > xDest;
623 bAssign = extract(
624 pCppArgs[nPos], reinterpret_cast<typelib_InterfaceTypeDescription *>(pTD),
625 xDest, getReflection() );
626 if (bAssign)
628 *static_cast<void **>(ppUnoArgs[nPos]) = getReflection()->getCpp2Uno().mapInterface(
629 xDest.get(), reinterpret_cast<typelib_InterfaceTypeDescription *>(pTD) );
632 else
634 typelib_TypeDescription * pValueTD = nullptr;
635 TYPELIB_DANGER_GET( &pValueTD, pCppArgs[nPos].getValueTypeRef() );
636 // construct temp uno val to do proper assignment: todo opt
637 void * pTemp = alloca( pValueTD->nSize );
638 uno_copyAndConvertData(
639 pTemp, const_cast<void *>(pCppArgs[nPos].getValue()), pValueTD,
640 getReflection()->getCpp2Uno().get() );
641 uno_constructData(
642 ppUnoArgs[nPos], pTD );
643 // assignment does simple conversion
644 bAssign = uno_assignData(
645 ppUnoArgs[nPos], pTD, pTemp, pValueTD, nullptr, nullptr, nullptr );
646 uno_destructData(
647 pTemp, pValueTD, nullptr );
648 TYPELIB_DANGER_RELEASE( pValueTD );
651 if (! bAssign)
653 IllegalArgumentException aExc(
654 "cannot coerce argument type during corereflection call:"
655 "\narg no.: " + OUString::number(nPos)
656 + " expected: \"" + OUString(pTD->pTypeName)
657 + "\" actual: \"" + OUString(pCppArgs[nPos].getValueTypeRef()->pTypeName)
658 + "\"",
659 *o3tl::doAccess<Reference<XInterface>>(rObj), (sal_Int16)nPos );
661 // cleanup
662 while (nPos--)
664 if (pParams[nPos].bIn)
665 uno_destructData( ppUnoArgs[nPos], ppParamTypes[nPos], nullptr );
666 TYPELIB_DANGER_RELEASE( ppParamTypes[nPos] );
668 TYPELIB_DANGER_RELEASE( pReturnType );
669 (*pUnoI->release)( pUnoI );
671 throw aExc;
676 uno_Any aUnoExc;
677 uno_Any * pUnoExc = &aUnoExc;
679 (*pUnoI->pDispatcher)(
680 pUnoI, getTypeDescr(), pUnoReturn, ppUnoArgs, &pUnoExc );
681 (*pUnoI->release)( pUnoI );
683 Any aRet;
684 if (pUnoExc)
686 // cleanup
687 while (nParams--)
689 if (pParams[nParams].bIn)
690 uno_destructData( ppUnoArgs[nParams], ppParamTypes[nParams], nullptr );
691 TYPELIB_DANGER_RELEASE( ppParamTypes[nParams] );
693 TYPELIB_DANGER_RELEASE( pReturnType );
695 InvocationTargetException aExc;
696 aExc.Context = *o3tl::doAccess<Reference<XInterface>>(rObj);
697 aExc.Message = "exception occurred during invocation!";
698 uno_any_destruct(
699 &aExc.TargetException,
700 reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
701 uno_type_copyAndConvertData(
702 &aExc.TargetException, pUnoExc, cppu::UnoType<Any>::get().getTypeLibType(),
703 getReflection()->getUno2Cpp().get() );
704 uno_any_destruct( pUnoExc, nullptr );
705 throw aExc;
707 else
709 // reconvert arguments and cleanup
710 while (nParams--)
712 if (pParams[nParams].bOut) // write back
714 uno_any_destruct(
715 &pCppArgs[nParams],
716 reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
717 uno_any_constructAndConvert(
718 &pCppArgs[nParams], ppUnoArgs[nParams], ppParamTypes[nParams],
719 getReflection()->getUno2Cpp().get() );
721 uno_destructData( ppUnoArgs[nParams], ppParamTypes[nParams], nullptr );
722 TYPELIB_DANGER_RELEASE( ppParamTypes[nParams] );
724 uno_any_destruct(
725 &aRet, reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
726 uno_any_constructAndConvert(
727 &aRet, pUnoReturn, pReturnType,
728 getReflection()->getUno2Cpp().get() );
729 uno_destructData( pUnoReturn, pReturnType, nullptr );
730 TYPELIB_DANGER_RELEASE( pReturnType );
732 return aRet;
734 throw IllegalArgumentException(
735 "illegal destination object given!",
736 static_cast<XWeak *>(static_cast<OWeakObject *>(this)), 0 );
740 InterfaceIdlClassImpl::~InterfaceIdlClassImpl()
742 for ( sal_Int32 nPos = _nMethods + _nAttributes; nPos--; )
743 typelib_typedescription_release( _pSortedMemberInit[nPos].second );
745 delete [] _pSortedMemberInit;
749 Sequence< Reference< XIdlClass > > InterfaceIdlClassImpl::getSuperclasses()
751 ::osl::MutexGuard aGuard(getMutexAccess());
752 if (_xSuperClasses.getLength() == 0) {
753 typelib_InterfaceTypeDescription * pType = getTypeDescr();
754 _xSuperClasses.realloc(pType->nBaseTypes);
755 for (sal_Int32 i = 0; i < pType->nBaseTypes; ++i) {
756 _xSuperClasses[i] = getReflection()->forType(
757 &pType->ppBaseTypes[i]->aBase);
758 OSL_ASSERT(_xSuperClasses[i].is());
761 return Sequence< Reference< XIdlClass > >(_xSuperClasses);
764 void InterfaceIdlClassImpl::initMembers()
766 sal_Int32 nAll = getTypeDescr()->nAllMembers;
767 MemberInit * pSortedMemberInit = new MemberInit[nAll];
768 typelib_TypeDescriptionReference ** ppAllMembers = getTypeDescr()->ppAllMembers;
770 for ( sal_Int32 nPos = 0; nPos < nAll; ++nPos )
772 sal_Int32 nIndex;
773 if (ppAllMembers[nPos]->eTypeClass == typelib_TypeClass_INTERFACE_METHOD)
775 // methods to front
776 nIndex = _nMethods;
777 ++_nMethods;
779 else
781 ++_nAttributes;
782 nIndex = (nAll - _nAttributes);
783 // attributes at the back
786 typelib_TypeDescription * pTD = nullptr;
787 typelib_typedescriptionreference_getDescription( &pTD, ppAllMembers[nPos] );
788 assert(pTD && "### cannot get type description!");
789 pSortedMemberInit[nIndex].first = reinterpret_cast<typelib_InterfaceMemberTypeDescription *>(pTD)->pMemberName;
790 pSortedMemberInit[nIndex].second = pTD;
793 _pSortedMemberInit = pSortedMemberInit;
796 sal_Bool InterfaceIdlClassImpl::isAssignableFrom( const Reference< XIdlClass > & xType )
798 if (xType.is() && xType->getTypeClass() == TypeClass_INTERFACE)
800 if (equals( xType ))
801 return true;
802 else
804 const Sequence< Reference< XIdlClass > > & rSeq = xType->getSuperclasses();
805 for (sal_Int32 i = 0; i < rSeq.getLength(); ++i) {
806 if (isAssignableFrom(rSeq[i])) {
807 return true;
812 return false;
815 Uik InterfaceIdlClassImpl::getUik()
817 return Uik(0, 0, 0, 0, 0);
818 // Uiks are deprecated and this function must not be called
821 Sequence< Reference< XIdlMethod > > InterfaceIdlClassImpl::getMethods()
823 ::osl::MutexGuard aGuard( getMutexAccess() );
824 if (! _pSortedMemberInit)
825 initMembers();
827 // create methods sequence
828 Sequence< Reference< XIdlMethod > > aRet( _nMethods );
829 Reference< XIdlMethod > * pRet = aRet.getArray();
830 for ( sal_Int32 nPos = _nMethods; nPos--; )
833 /*_aName2Method[_pSortedMemberInit[nPos].first] = */pRet[nPos] = new IdlInterfaceMethodImpl(
834 getReflection(), _pSortedMemberInit[nPos].first,
835 _pSortedMemberInit[nPos].second, IdlClassImpl::getTypeDescr() );
837 return aRet;
840 Sequence< Reference< XIdlField > > InterfaceIdlClassImpl::getFields()
842 ::osl::MutexGuard aGuard( getMutexAccess() );
843 if (! _pSortedMemberInit)
844 initMembers();
846 // create fields sequence
847 Sequence< Reference< XIdlField > > aRet( _nAttributes );
848 Reference< XIdlField > * pRet = aRet.getArray();
849 for ( sal_Int32 nPos = _nAttributes; nPos--; )
851 /*_aName2Field[_pSortedMemberInit[_nMethods+nPos].first] = */pRet[_nAttributes-nPos-1] =
852 new IdlAttributeFieldImpl(
853 getReflection(), _pSortedMemberInit[_nMethods+nPos].first,
854 _pSortedMemberInit[_nMethods+nPos].second, IdlClassImpl::getTypeDescr() );
856 return aRet;
859 Reference< XIdlMethod > InterfaceIdlClassImpl::getMethod( const OUString & rName )
861 ::osl::MutexGuard aGuard( getMutexAccess() );
862 if (! _pSortedMemberInit)
863 initMembers();
865 Reference< XIdlMethod > xRet;
867 // try weak map
868 const OUString2Method::const_iterator iFind( _aName2Method.find( rName ) );
869 if (iFind != _aName2Method.end())
870 xRet = (*iFind).second; // harden ref
872 if (! xRet.is())
874 for ( sal_Int32 nPos = _nMethods; nPos--; )
876 if (_pSortedMemberInit[nPos].first == rName)
878 _aName2Method[rName] = xRet = new IdlInterfaceMethodImpl(
879 getReflection(), rName,
880 _pSortedMemberInit[nPos].second, IdlClassImpl::getTypeDescr() );
881 break;
885 return xRet;
888 Reference< XIdlField > InterfaceIdlClassImpl::getField( const OUString & rName )
890 ::osl::MutexGuard aGuard( getMutexAccess() );
891 if (! _pSortedMemberInit)
892 initMembers();
894 Reference< XIdlField > xRet;
896 // try weak map
897 const OUString2Field::const_iterator iFind( _aName2Field.find( rName ) );
898 if (iFind != _aName2Field.end())
899 xRet = (*iFind).second; // harden ref
901 if (! xRet.is())
903 for ( sal_Int32 nPos = _nAttributes; nPos--; )
905 if (_pSortedMemberInit[_nMethods+nPos].first == rName)
907 _aName2Field[rName] = xRet = new IdlAttributeFieldImpl(
908 getReflection(), rName,
909 _pSortedMemberInit[_nMethods+nPos].second, IdlClassImpl::getTypeDescr() );
910 break;
914 return xRet;
917 void InterfaceIdlClassImpl::createObject( Any & rObj )
919 // interfaces cannot be constructed
920 rObj.clear();
926 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */