Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / stoc / source / corereflection / criface.cxx
blob25839f741f5f2ec0891f71735f9e882b78e24fb1
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>
22 #include <cassert>
23 #include <cstddef>
24 #include <limits>
26 #ifdef SAL_UNX
27 #include <sal/alloca.h>
28 #endif
29 #if !(defined(MACOSX) || defined(IOS) || defined(FREEBSD))
30 #include <malloc.h>
31 #endif
32 #include <o3tl/any.hxx>
33 #include <rtl/alloc.h>
34 #include <typelib/typedescription.hxx>
35 #include <uno/data.h>
37 #include "base.hxx"
39 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
40 #include <com/sun/star/uno/RuntimeException.hpp>
41 #include <cppuhelper/queryinterface.hxx>
42 #include <cppuhelper/exc_hlp.hxx>
44 using namespace css::lang;
45 using namespace css::reflection;
46 using namespace css::uno;
48 namespace {
50 std::size_t multipleOf16(std::size_t n) {
51 assert(n <= std::numeric_limits<std::size_t>::max() - 15);
52 return (n + 15) & ~std::size_t(15);
57 namespace stoc_corefl
61 class IdlAttributeFieldImpl
62 : public IdlMemberImpl
63 , public XIdlField
64 , public XIdlField2
66 public:
67 typelib_InterfaceAttributeTypeDescription * getAttributeTypeDescr() const
68 { return reinterpret_cast<typelib_InterfaceAttributeTypeDescription *>(getTypeDescr()); }
70 IdlAttributeFieldImpl( IdlReflectionServiceImpl * pReflection, const OUString & rName,
71 typelib_TypeDescription * pTypeDescr, typelib_TypeDescription * pDeclTypeDescr )
72 : IdlMemberImpl( pReflection, rName, pTypeDescr, pDeclTypeDescr )
75 // XInterface
76 virtual Any SAL_CALL queryInterface( const Type & rType ) override;
77 virtual void SAL_CALL acquire() throw() override;
78 virtual void SAL_CALL release() throw() override;
80 // XTypeProvider
81 virtual Sequence< Type > SAL_CALL getTypes() override;
82 virtual Sequence< sal_Int8 > SAL_CALL getImplementationId() override;
84 // XIdlMember
85 virtual Reference< XIdlClass > SAL_CALL getDeclaringClass() override;
86 virtual OUString SAL_CALL getName() override;
87 // XIdlField
88 virtual Reference< XIdlClass > SAL_CALL getType() override;
89 virtual FieldAccessMode SAL_CALL getAccessMode() override;
90 virtual Any SAL_CALL get( const Any & rObj ) override;
91 virtual void SAL_CALL set( const Any & rObj, const Any & rValue ) override;
92 // XIdlField2: getType, getAccessMode and get are equal to XIdlField
93 virtual void SAL_CALL set( Any & rObj, const Any & rValue ) override;
95 private:
96 void checkException(
97 uno_Any * exception, Reference< XInterface > const & context) const;
100 // XInterface
102 Any IdlAttributeFieldImpl::queryInterface( const Type & rType )
104 Any aRet( ::cppu::queryInterface( rType,
105 static_cast< XIdlField * >( this ),
106 static_cast< XIdlField2 * >( this ) ) );
107 return (aRet.hasValue() ? aRet : IdlMemberImpl::queryInterface( rType ));
110 void IdlAttributeFieldImpl::acquire() throw()
112 IdlMemberImpl::acquire();
115 void IdlAttributeFieldImpl::release() throw()
117 IdlMemberImpl::release();
120 // XTypeProvider
122 Sequence< Type > IdlAttributeFieldImpl::getTypes()
124 static ::cppu::OTypeCollection * s_pTypes = nullptr;
125 if (! s_pTypes)
127 ::osl::MutexGuard aGuard( getMutexAccess() );
128 if (! s_pTypes)
130 static ::cppu::OTypeCollection s_aTypes(
131 cppu::UnoType<XIdlField2>::get(),
132 cppu::UnoType<XIdlField>::get(),
133 IdlMemberImpl::getTypes() );
134 s_pTypes = &s_aTypes;
137 return s_pTypes->getTypes();
140 Sequence< sal_Int8 > IdlAttributeFieldImpl::getImplementationId()
142 return css::uno::Sequence<sal_Int8>();
145 // XIdlMember
147 Reference< XIdlClass > IdlAttributeFieldImpl::getDeclaringClass()
149 if (! _xDeclClass.is())
151 ::osl::MutexGuard aGuard( getMutexAccess() );
152 if (! _xDeclClass.is())
154 OUString aName(getAttributeTypeDescr()->aBase.aBase.pTypeName);
155 sal_Int32 i = aName.indexOf(':');
156 OSL_ASSERT(i >= 0);
157 _xDeclClass = getReflection()->forName(aName.copy(0, i));
160 return _xDeclClass;
163 OUString IdlAttributeFieldImpl::getName()
165 return IdlMemberImpl::getName();
168 // XIdlField
170 Reference< XIdlClass > IdlAttributeFieldImpl::getType()
172 return getReflection()->forType(
173 getAttributeTypeDescr()->pAttributeTypeRef );
176 FieldAccessMode IdlAttributeFieldImpl::getAccessMode()
178 return (getAttributeTypeDescr()->bReadOnly
179 ? FieldAccessMode_READONLY : FieldAccessMode_READWRITE);
182 Any IdlAttributeFieldImpl::get( const Any & rObj )
184 uno_Interface * pUnoI = getReflection()->mapToUno(
185 rObj, reinterpret_cast<typelib_InterfaceTypeDescription *>(getDeclTypeDescr()) );
186 OSL_ENSURE( pUnoI, "### illegal destination object given!" );
187 if (pUnoI)
189 TypeDescription aTD( getAttributeTypeDescr()->pAttributeTypeRef );
190 typelib_TypeDescription * pTD = aTD.get();
192 uno_Any aExc;
193 uno_Any * pExc = &aExc;
194 void * pReturn = alloca( pTD->nSize );
196 (*pUnoI->pDispatcher)( pUnoI, getTypeDescr(), pReturn, nullptr, &pExc );
197 (*pUnoI->release)( pUnoI );
199 checkException(pExc, *o3tl::doAccess<Reference<XInterface>>(rObj));
200 Any aRet;
201 uno_any_destruct(
202 &aRet, reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
203 uno_any_constructAndConvert( &aRet, pReturn, pTD, getReflection()->getUno2Cpp().get() );
204 uno_destructData( pReturn, pTD, nullptr );
205 return aRet;
207 throw IllegalArgumentException(
208 "illegal object given!",
209 static_cast<XWeak *>(static_cast<OWeakObject *>(this)), 0 );
212 void IdlAttributeFieldImpl::set( Any & rObj, const Any & rValue )
214 if (getAttributeTypeDescr()->bReadOnly)
216 throw IllegalAccessException(
217 "cannot set readonly attribute!",
218 static_cast<XWeak *>(static_cast<OWeakObject *>(this)) );
221 uno_Interface * pUnoI = getReflection()->mapToUno(
222 rObj, reinterpret_cast<typelib_InterfaceTypeDescription *>(getDeclTypeDescr()) );
223 OSL_ENSURE( pUnoI, "### illegal destination object given!" );
224 if (pUnoI)
226 TypeDescription aTD( getAttributeTypeDescr()->pAttributeTypeRef );
227 typelib_TypeDescription * pTD = aTD.get();
229 // construct uno value to be set
230 void * pArgs[1];
231 void * pArg = pArgs[0] = alloca( pTD->nSize );
233 bool bAssign;
234 if (pTD->eTypeClass == typelib_TypeClass_ANY)
236 uno_copyAndConvertData( pArg, const_cast< Any * >(&rValue),
237 pTD, getReflection()->getCpp2Uno().get() );
238 bAssign = true;
240 else if (typelib_typedescriptionreference_equals( rValue.getValueTypeRef(), pTD->pWeakRef ))
242 uno_copyAndConvertData( pArg, const_cast< void * >(rValue.getValue()),
243 pTD, getReflection()->getCpp2Uno().get() );
244 bAssign = true;
246 else if (pTD->eTypeClass == typelib_TypeClass_INTERFACE)
248 Reference< XInterface > xObj;
249 bAssign = extract(
250 rValue, reinterpret_cast<typelib_InterfaceTypeDescription *>(pTD), xObj,
251 getReflection() );
252 if (bAssign)
254 *static_cast<void **>(pArg) = getReflection()->getCpp2Uno().mapInterface(
255 xObj.get(), reinterpret_cast<typelib_InterfaceTypeDescription *>(pTD) );
258 else
260 typelib_TypeDescription * pValueTD = nullptr;
261 TYPELIB_DANGER_GET( &pValueTD, rValue.getValueTypeRef() );
262 // construct temp uno val to do proper assignment: todo opt
263 void * pTemp = alloca( pValueTD->nSize );
264 uno_copyAndConvertData(
265 pTemp, const_cast<void *>(rValue.getValue()), pValueTD, getReflection()->getCpp2Uno().get() );
266 uno_constructData(
267 pArg, pTD );
268 // assignment does simple conversion
269 bAssign = uno_assignData(
270 pArg, pTD, pTemp, pValueTD, nullptr, nullptr, nullptr );
271 uno_destructData(
272 pTemp, pValueTD, nullptr );
273 TYPELIB_DANGER_RELEASE( pValueTD );
276 if (bAssign)
278 uno_Any aExc;
279 uno_Any * pExc = &aExc;
280 (*pUnoI->pDispatcher)( pUnoI, getTypeDescr(), nullptr, pArgs, &pExc );
281 (*pUnoI->release)( pUnoI );
283 uno_destructData( pArg, pTD, nullptr );
284 checkException(pExc, *o3tl::doAccess<Reference<XInterface>>(rObj));
285 return;
287 (*pUnoI->release)( pUnoI );
289 throw IllegalArgumentException(
290 "illegal value given!",
291 *o3tl::doAccess<Reference<XInterface>>(rObj), 1 );
293 throw IllegalArgumentException(
294 "illegal destination object given!",
295 static_cast<XWeak *>(static_cast<OWeakObject *>(this)), 0 );
298 void IdlAttributeFieldImpl::set( const Any & rObj, const Any & rValue )
300 IdlAttributeFieldImpl::set( const_cast< Any & >( rObj ), rValue );
303 void IdlAttributeFieldImpl::checkException(
304 uno_Any * exception, Reference< XInterface > const & context) const
306 if (exception != nullptr) {
307 Any e;
308 uno_any_destruct(&e, reinterpret_cast< uno_ReleaseFunc >(cpp_release));
309 uno_type_any_constructAndConvert(
310 &e, exception->pData, exception->pType,
311 getReflection()->getUno2Cpp().get());
312 uno_any_destruct(exception, nullptr);
313 if (!e.isExtractableTo(
314 cppu::UnoType<RuntimeException>::get()))
316 throw WrappedTargetRuntimeException(
317 "non-RuntimeException occurred when accessing an"
318 " interface type attribute",
319 context, e);
321 cppu::throwException(e);
326 class IdlInterfaceMethodImpl
327 : public IdlMemberImpl
328 , public XIdlMethod
330 std::unique_ptr<Sequence< Reference< XIdlClass > >> _pExceptionTypes;
331 std::unique_ptr<Sequence< Reference< XIdlClass > >> _pParamTypes;
332 std::unique_ptr<Sequence< ParamInfo >> _pParamInfos;
334 public:
335 typelib_InterfaceMethodTypeDescription * getMethodTypeDescr() const
336 { return reinterpret_cast<typelib_InterfaceMethodTypeDescription *>(getTypeDescr()); }
338 IdlInterfaceMethodImpl( IdlReflectionServiceImpl * pReflection, const OUString & rName,
339 typelib_TypeDescription * pTypeDescr, typelib_TypeDescription * pDeclTypeDescr )
340 : IdlMemberImpl( pReflection, rName, pTypeDescr, pDeclTypeDescr )
343 // XInterface
344 virtual Any SAL_CALL queryInterface( const Type & rType ) override;
345 virtual void SAL_CALL acquire() throw() override;
346 virtual void SAL_CALL release() throw() override;
348 // XTypeProvider
349 virtual Sequence< Type > SAL_CALL getTypes() override;
350 virtual Sequence< sal_Int8 > SAL_CALL getImplementationId() override;
352 // XIdlMember
353 virtual Reference< XIdlClass > SAL_CALL getDeclaringClass() override;
354 virtual OUString SAL_CALL getName() override;
355 // XIdlMethod
356 virtual Reference< XIdlClass > SAL_CALL getReturnType() override;
357 virtual Sequence< Reference< XIdlClass > > SAL_CALL getParameterTypes() override;
358 virtual Sequence< ParamInfo > SAL_CALL getParameterInfos() override;
359 virtual Sequence< Reference< XIdlClass > > SAL_CALL getExceptionTypes() override;
360 virtual MethodMode SAL_CALL getMode() override;
361 virtual Any SAL_CALL invoke( const Any & rObj, Sequence< Any > & rArgs ) override;
364 // XInterface
366 Any IdlInterfaceMethodImpl::queryInterface( const Type & rType )
368 Any aRet( ::cppu::queryInterface( rType, static_cast< XIdlMethod * >( this ) ) );
369 return (aRet.hasValue() ? aRet : IdlMemberImpl::queryInterface( rType ));
372 void IdlInterfaceMethodImpl::acquire() throw()
374 IdlMemberImpl::acquire();
377 void IdlInterfaceMethodImpl::release() throw()
379 IdlMemberImpl::release();
382 // XTypeProvider
384 Sequence< Type > IdlInterfaceMethodImpl::getTypes()
386 static ::cppu::OTypeCollection * s_pTypes = nullptr;
387 if (! s_pTypes)
389 ::osl::MutexGuard aGuard( getMutexAccess() );
390 if (! s_pTypes)
392 static ::cppu::OTypeCollection s_aTypes(
393 cppu::UnoType<XIdlMethod>::get(),
394 IdlMemberImpl::getTypes() );
395 s_pTypes = &s_aTypes;
398 return s_pTypes->getTypes();
401 Sequence< sal_Int8 > IdlInterfaceMethodImpl::getImplementationId()
403 return css::uno::Sequence<sal_Int8>();
406 // XIdlMember
408 Reference< XIdlClass > IdlInterfaceMethodImpl::getDeclaringClass()
410 if (! _xDeclClass.is())
412 ::osl::MutexGuard aGuard( getMutexAccess() );
413 if (! _xDeclClass.is())
415 OUString aName(getMethodTypeDescr()->aBase.aBase.pTypeName);
416 sal_Int32 i = aName.indexOf(':');
417 OSL_ASSERT(i >= 0);
418 _xDeclClass = getReflection()->forName(aName.copy(0, i));
421 return _xDeclClass;
424 OUString IdlInterfaceMethodImpl::getName()
426 return IdlMemberImpl::getName();
429 // XIdlMethod
431 Reference< XIdlClass > SAL_CALL IdlInterfaceMethodImpl::getReturnType()
433 return getReflection()->forType( getMethodTypeDescr()->pReturnTypeRef );
436 Sequence< Reference< XIdlClass > > IdlInterfaceMethodImpl::getExceptionTypes()
438 if (! _pExceptionTypes)
440 ::osl::MutexGuard aGuard( getMutexAccess() );
441 if (! _pExceptionTypes)
443 sal_Int32 nExc = getMethodTypeDescr()->nExceptions;
444 std::unique_ptr<Sequence< Reference< XIdlClass > >> pTempExceptionTypes(
445 new Sequence< Reference< XIdlClass > >( nExc ));
446 Reference< XIdlClass > * pExceptionTypes = pTempExceptionTypes->getArray();
448 typelib_TypeDescriptionReference ** ppExc =
449 getMethodTypeDescr()->ppExceptions;
450 IdlReflectionServiceImpl * pRefl = getReflection();
452 while (nExc--)
453 pExceptionTypes[nExc] = pRefl->forType( ppExc[nExc] );
455 _pExceptionTypes = std::move(pTempExceptionTypes);
458 return *_pExceptionTypes;
461 Sequence< Reference< XIdlClass > > IdlInterfaceMethodImpl::getParameterTypes()
463 if (! _pParamTypes)
465 ::osl::MutexGuard aGuard( getMutexAccess() );
466 if (! _pParamTypes)
468 sal_Int32 nParams = getMethodTypeDescr()->nParams;
469 std::unique_ptr<Sequence< Reference< XIdlClass > > > pTempParamTypes(
470 new Sequence< Reference< XIdlClass > >( nParams ));
471 Reference< XIdlClass > * pParamTypes = pTempParamTypes->getArray();
473 typelib_MethodParameter * pTypelibParams =
474 getMethodTypeDescr()->pParams;
475 IdlReflectionServiceImpl * pRefl = getReflection();
477 while (nParams--)
478 pParamTypes[nParams] = pRefl->forType( pTypelibParams[nParams].pTypeRef );
480 _pParamTypes = std::move(pTempParamTypes);
483 return *_pParamTypes;
486 Sequence< ParamInfo > IdlInterfaceMethodImpl::getParameterInfos()
488 if (! _pParamInfos)
490 ::osl::MutexGuard aGuard( getMutexAccess() );
491 if (! _pParamInfos)
493 sal_Int32 nParams = getMethodTypeDescr()->nParams;
494 std::unique_ptr<Sequence< ParamInfo > > pTempParamInfos( new Sequence< ParamInfo >( nParams ) );
495 ParamInfo * pParamInfos = pTempParamInfos->getArray();
497 typelib_MethodParameter * pTypelibParams =
498 getMethodTypeDescr()->pParams;
500 if (_pParamTypes) // use param types
502 const Reference< XIdlClass > * pParamTypes = _pParamTypes->getConstArray();
504 while (nParams--)
506 const typelib_MethodParameter & rParam = pTypelibParams[nParams];
507 ParamInfo & rInfo = pParamInfos[nParams];
508 rInfo.aName = rParam.pName;
509 if (rParam.bIn)
510 rInfo.aMode = (rParam.bOut ? ParamMode_INOUT : ParamMode_IN);
511 else
512 rInfo.aMode = ParamMode_OUT;
513 rInfo.aType = pParamTypes[nParams];
516 else // make also param types sequence if not already initialized
518 std::unique_ptr<Sequence< Reference< XIdlClass > > > pTempParamTypes(
519 new Sequence< Reference< XIdlClass > >( nParams ));
520 Reference< XIdlClass > * pParamTypes = pTempParamTypes->getArray();
522 IdlReflectionServiceImpl * pRefl = getReflection();
524 while (nParams--)
526 const typelib_MethodParameter & rParam = pTypelibParams[nParams];
527 ParamInfo & rInfo = pParamInfos[nParams];
528 rInfo.aName = rParam.pName;
529 if (rParam.bIn)
530 rInfo.aMode = (rParam.bOut ? ParamMode_INOUT : ParamMode_IN);
531 else
532 rInfo.aMode = ParamMode_OUT;
533 rInfo.aType = pParamTypes[nParams] = pRefl->forType( rParam.pTypeRef );
536 _pParamTypes = std::move(pTempParamTypes);
539 _pParamInfos = std::move(pTempParamInfos);
542 return *_pParamInfos;
545 MethodMode SAL_CALL IdlInterfaceMethodImpl::getMode()
547 return
548 getMethodTypeDescr()->bOneWay ? MethodMode_ONEWAY : MethodMode_TWOWAY;
551 Any SAL_CALL IdlInterfaceMethodImpl::invoke( const Any & rObj, Sequence< Any > & rArgs )
553 if (auto ifc = o3tl::tryAccess<css::uno::Reference<css::uno::XInterface>>(
554 rObj))
556 // acquire()/ release()
557 if (rtl_ustr_ascii_compare( getTypeDescr()->pTypeName->buffer,
558 "com.sun.star.uno.XInterface::acquire" ) == 0)
560 (*ifc)->acquire();
561 return Any();
563 else if (rtl_ustr_ascii_compare( getTypeDescr()->pTypeName->buffer,
564 "com.sun.star.uno.XInterface::release" ) == 0)
566 (*ifc)->release();
567 return Any();
571 uno_Interface * pUnoI = getReflection()->mapToUno(
572 rObj, reinterpret_cast<typelib_InterfaceTypeDescription *>(getDeclTypeDescr()) );
573 OSL_ENSURE( pUnoI, "### illegal destination object given!" );
574 if (pUnoI)
576 sal_Int32 nParams = getMethodTypeDescr()->nParams;
577 if (rArgs.getLength() != nParams)
579 (*pUnoI->release)( pUnoI );
580 throw IllegalArgumentException(
581 "expected " + OUString::number(nParams) +
582 " arguments, got " + OUString::number(rArgs.getLength()),
583 *o3tl::doAccess<Reference<XInterface>>(rObj), 1 );
586 Any * pCppArgs = rArgs.getArray();
587 typelib_MethodParameter * pParams = getMethodTypeDescr()->pParams;
588 typelib_TypeDescription * pReturnType = nullptr;
589 TYPELIB_DANGER_GET(
590 &pReturnType, getMethodTypeDescr()->pReturnTypeRef );
592 // C/C++ ABIs typically assume that structs are padded at the end, and
593 // that those padding bytes may be written to (e.g., to write into the
594 // end of a "short" struct by writing the full contents of a "long"
595 // register); so create enough space here (assuming that no ABI requires
596 // padding larger than 16 byte boundaries):
597 void * pUnoReturn = alloca( multipleOf16(pReturnType->nSize) );
598 void ** ppUnoArgs = static_cast<void **>(alloca( sizeof(void *) * nParams *2 ));
599 typelib_TypeDescription ** ppParamTypes = reinterpret_cast<typelib_TypeDescription **>(ppUnoArgs + nParams);
601 // convert arguments
602 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
604 ppParamTypes[nPos] = nullptr;
605 TYPELIB_DANGER_GET( ppParamTypes + nPos, pParams[nPos].pTypeRef );
606 typelib_TypeDescription * pTD = ppParamTypes[nPos];
608 ppUnoArgs[nPos] = alloca( pTD->nSize );
609 if (pParams[nPos].bIn)
611 bool bAssign;
612 if (typelib_typedescriptionreference_equals(
613 pCppArgs[nPos].getValueTypeRef(), pTD->pWeakRef ))
615 uno_type_copyAndConvertData(
616 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_ANY)
622 uno_type_any_constructAndConvert(
623 static_cast<uno_Any *>(ppUnoArgs[nPos]), const_cast<void *>(pCppArgs[nPos].getValue()),
624 pCppArgs[nPos].getValueTypeRef(), getReflection()->getCpp2Uno().get() );
625 bAssign = true;
627 else if (pTD->eTypeClass == typelib_TypeClass_INTERFACE)
629 Reference< XInterface > xDest;
630 bAssign = extract(
631 pCppArgs[nPos], reinterpret_cast<typelib_InterfaceTypeDescription *>(pTD),
632 xDest, getReflection() );
633 if (bAssign)
635 *static_cast<void **>(ppUnoArgs[nPos]) = getReflection()->getCpp2Uno().mapInterface(
636 xDest.get(), reinterpret_cast<typelib_InterfaceTypeDescription *>(pTD) );
639 else
641 typelib_TypeDescription * pValueTD = nullptr;
642 TYPELIB_DANGER_GET( &pValueTD, pCppArgs[nPos].getValueTypeRef() );
643 // construct temp uno val to do proper assignment: todo opt
644 void * pTemp = alloca( pValueTD->nSize );
645 uno_copyAndConvertData(
646 pTemp, const_cast<void *>(pCppArgs[nPos].getValue()), pValueTD,
647 getReflection()->getCpp2Uno().get() );
648 uno_constructData(
649 ppUnoArgs[nPos], pTD );
650 // assignment does simple conversion
651 bAssign = uno_assignData(
652 ppUnoArgs[nPos], pTD, pTemp, pValueTD, nullptr, nullptr, nullptr );
653 uno_destructData(
654 pTemp, pValueTD, nullptr );
655 TYPELIB_DANGER_RELEASE( pValueTD );
658 if (! bAssign)
660 IllegalArgumentException aExc(
661 "cannot coerce argument type during corereflection call:"
662 "\narg no.: " + OUString::number(nPos)
663 + " expected: \"" + OUString(pTD->pTypeName)
664 + "\" actual: \"" + OUString(pCppArgs[nPos].getValueTypeRef()->pTypeName)
665 + "\"",
666 *o3tl::doAccess<Reference<XInterface>>(rObj), static_cast<sal_Int16>(nPos) );
668 // cleanup
669 while (nPos--)
671 if (pParams[nPos].bIn)
672 uno_destructData( ppUnoArgs[nPos], ppParamTypes[nPos], nullptr );
673 TYPELIB_DANGER_RELEASE( ppParamTypes[nPos] );
675 TYPELIB_DANGER_RELEASE( pReturnType );
676 (*pUnoI->release)( pUnoI );
678 throw aExc;
683 uno_Any aUnoExc;
684 uno_Any * pUnoExc = &aUnoExc;
686 (*pUnoI->pDispatcher)(
687 pUnoI, getTypeDescr(), pUnoReturn, ppUnoArgs, &pUnoExc );
688 (*pUnoI->release)( pUnoI );
690 Any aRet;
691 if (pUnoExc)
693 // cleanup
694 while (nParams--)
696 if (pParams[nParams].bIn)
697 uno_destructData( ppUnoArgs[nParams], ppParamTypes[nParams], nullptr );
698 TYPELIB_DANGER_RELEASE( ppParamTypes[nParams] );
700 TYPELIB_DANGER_RELEASE( pReturnType );
702 InvocationTargetException aExc;
703 aExc.Context = *o3tl::doAccess<Reference<XInterface>>(rObj);
704 aExc.Message = "exception occurred during invocation!";
705 uno_any_destruct(
706 &aExc.TargetException,
707 reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
708 uno_type_copyAndConvertData(
709 &aExc.TargetException, pUnoExc, cppu::UnoType<Any>::get().getTypeLibType(),
710 getReflection()->getUno2Cpp().get() );
711 uno_any_destruct( pUnoExc, nullptr );
712 throw aExc;
714 else
716 // reconvert arguments and cleanup
717 while (nParams--)
719 if (pParams[nParams].bOut) // write back
721 uno_any_destruct(
722 &pCppArgs[nParams],
723 reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
724 uno_any_constructAndConvert(
725 &pCppArgs[nParams], ppUnoArgs[nParams], ppParamTypes[nParams],
726 getReflection()->getUno2Cpp().get() );
728 uno_destructData( ppUnoArgs[nParams], ppParamTypes[nParams], nullptr );
729 TYPELIB_DANGER_RELEASE( ppParamTypes[nParams] );
731 uno_any_destruct(
732 &aRet, reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
733 uno_any_constructAndConvert(
734 &aRet, pUnoReturn, pReturnType,
735 getReflection()->getUno2Cpp().get() );
736 uno_destructData( pUnoReturn, pReturnType, nullptr );
737 TYPELIB_DANGER_RELEASE( pReturnType );
739 return aRet;
741 throw IllegalArgumentException(
742 "illegal destination object given!",
743 static_cast<XWeak *>(static_cast<OWeakObject *>(this)), 0 );
747 InterfaceIdlClassImpl::~InterfaceIdlClassImpl()
749 for ( sal_Int32 nPos = _nMethods + _nAttributes; nPos--; )
750 typelib_typedescription_release( _pSortedMemberInit[nPos].second );
754 Sequence< Reference< XIdlClass > > InterfaceIdlClassImpl::getSuperclasses()
756 ::osl::MutexGuard aGuard(getMutexAccess());
757 if (_xSuperClasses.getLength() == 0) {
758 typelib_InterfaceTypeDescription * pType = getTypeDescr();
759 _xSuperClasses.realloc(pType->nBaseTypes);
760 for (sal_Int32 i = 0; i < pType->nBaseTypes; ++i) {
761 _xSuperClasses[i] = getReflection()->forType(
762 &pType->ppBaseTypes[i]->aBase);
763 OSL_ASSERT(_xSuperClasses[i].is());
766 return _xSuperClasses;
769 void InterfaceIdlClassImpl::initMembers()
771 sal_Int32 nAll = getTypeDescr()->nAllMembers;
772 std::unique_ptr<MemberInit[]> pSortedMemberInit(new MemberInit[nAll]);
773 typelib_TypeDescriptionReference ** ppAllMembers = getTypeDescr()->ppAllMembers;
775 for ( sal_Int32 nPos = 0; nPos < nAll; ++nPos )
777 sal_Int32 nIndex;
778 if (ppAllMembers[nPos]->eTypeClass == typelib_TypeClass_INTERFACE_METHOD)
780 // methods to front
781 nIndex = _nMethods;
782 ++_nMethods;
784 else
786 ++_nAttributes;
787 nIndex = (nAll - _nAttributes);
788 // attributes at the back
791 typelib_TypeDescription * pTD = nullptr;
792 typelib_typedescriptionreference_getDescription( &pTD, ppAllMembers[nPos] );
793 assert(pTD && "### cannot get type description!");
794 pSortedMemberInit[nIndex].first = reinterpret_cast<typelib_InterfaceMemberTypeDescription *>(pTD)->pMemberName;
795 pSortedMemberInit[nIndex].second = pTD;
798 _pSortedMemberInit = std::move(pSortedMemberInit);
801 sal_Bool InterfaceIdlClassImpl::isAssignableFrom( const Reference< XIdlClass > & xType )
803 if (xType.is() && xType->getTypeClass() == TypeClass_INTERFACE)
805 if (equals( xType ))
806 return true;
807 else
809 const Sequence< Reference< XIdlClass > > & rSeq = xType->getSuperclasses();
810 for (sal_Int32 i = 0; i < rSeq.getLength(); ++i) {
811 if (isAssignableFrom(rSeq[i])) {
812 return true;
817 return false;
820 Uik InterfaceIdlClassImpl::getUik()
822 return Uik(0, 0, 0, 0, 0);
823 // Uiks are deprecated and this function must not be called
826 Sequence< Reference< XIdlMethod > > InterfaceIdlClassImpl::getMethods()
828 ::osl::MutexGuard aGuard( getMutexAccess() );
829 if (! _pSortedMemberInit)
830 initMembers();
832 // create methods sequence
833 Sequence< Reference< XIdlMethod > > aRet( _nMethods );
834 Reference< XIdlMethod > * pRet = aRet.getArray();
835 for ( sal_Int32 nPos = _nMethods; nPos--; )
838 /*_aName2Method[_pSortedMemberInit[nPos].first] = */pRet[nPos] = new IdlInterfaceMethodImpl(
839 getReflection(), _pSortedMemberInit[nPos].first,
840 _pSortedMemberInit[nPos].second, IdlClassImpl::getTypeDescr() );
842 return aRet;
845 Sequence< Reference< XIdlField > > InterfaceIdlClassImpl::getFields()
847 ::osl::MutexGuard aGuard( getMutexAccess() );
848 if (! _pSortedMemberInit)
849 initMembers();
851 // create fields sequence
852 Sequence< Reference< XIdlField > > aRet( _nAttributes );
853 Reference< XIdlField > * pRet = aRet.getArray();
854 for ( sal_Int32 nPos = _nAttributes; nPos--; )
856 /*_aName2Field[_pSortedMemberInit[_nMethods+nPos].first] = */pRet[_nAttributes-nPos-1] =
857 new IdlAttributeFieldImpl(
858 getReflection(), _pSortedMemberInit[_nMethods+nPos].first,
859 _pSortedMemberInit[_nMethods+nPos].second, IdlClassImpl::getTypeDescr() );
861 return aRet;
864 Reference< XIdlMethod > InterfaceIdlClassImpl::getMethod( const OUString & rName )
866 ::osl::MutexGuard aGuard( getMutexAccess() );
867 if (! _pSortedMemberInit)
868 initMembers();
870 Reference< XIdlMethod > xRet;
872 // try weak map
873 const OUString2Method::const_iterator iFind( _aName2Method.find( rName ) );
874 if (iFind != _aName2Method.end())
875 xRet = (*iFind).second; // harden ref
877 if (! xRet.is())
879 for ( sal_Int32 nPos = _nMethods; nPos--; )
881 if (_pSortedMemberInit[nPos].first == rName)
883 _aName2Method[rName] = xRet = new IdlInterfaceMethodImpl(
884 getReflection(), rName,
885 _pSortedMemberInit[nPos].second, IdlClassImpl::getTypeDescr() );
886 break;
890 return xRet;
893 Reference< XIdlField > InterfaceIdlClassImpl::getField( const OUString & rName )
895 ::osl::MutexGuard aGuard( getMutexAccess() );
896 if (! _pSortedMemberInit)
897 initMembers();
899 Reference< XIdlField > xRet;
901 // try weak map
902 const OUString2Field::const_iterator iFind( _aName2Field.find( rName ) );
903 if (iFind != _aName2Field.end())
904 xRet = (*iFind).second; // harden ref
906 if (! xRet.is())
908 for ( sal_Int32 nPos = _nAttributes; nPos--; )
910 if (_pSortedMemberInit[_nMethods+nPos].first == rName)
912 _aName2Field[rName] = xRet = new IdlAttributeFieldImpl(
913 getReflection(), rName,
914 _pSortedMemberInit[_nMethods+nPos].second, IdlClassImpl::getTypeDescr() );
915 break;
919 return xRet;
922 void InterfaceIdlClassImpl::createObject( Any & rObj )
924 // interfaces cannot be constructed
925 rObj.clear();
931 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */