android: Update app-specific/MIME type icons
[LibreOffice.git] / stoc / source / corereflection / criface.cxx
blob612b87106f3b166e6c9e38678edb2afd59032f6c
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 #include <o3tl/any.hxx>
30 #include <typelib/typedescription.hxx>
31 #include <uno/data.h>
33 #include "base.hxx"
35 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
36 #include <com/sun/star/reflection/XIdlField2.hpp>
37 #include <com/sun/star/uno/RuntimeException.hpp>
38 #include <cppuhelper/queryinterface.hxx>
39 #include <cppuhelper/exc_hlp.hxx>
40 #include <cppuhelper/typeprovider.hxx>
42 using namespace css::lang;
43 using namespace css::reflection;
44 using namespace css::uno;
46 namespace {
48 std::size_t multipleOf16(std::size_t n) {
49 assert(n <= std::numeric_limits<std::size_t>::max() - 15);
50 return (n + 15) & ~std::size_t(15);
55 namespace stoc_corefl
58 namespace {
60 typedef cppu::ImplInheritanceHelper<IdlMemberImpl, XIdlField, XIdlField2> IdlAttributeFieldImpl_Base;
61 class IdlAttributeFieldImpl : public IdlAttributeFieldImpl_Base
63 public:
64 typelib_InterfaceAttributeTypeDescription * getAttributeTypeDescr() const
65 { return reinterpret_cast<typelib_InterfaceAttributeTypeDescription *>(getTypeDescr()); }
67 IdlAttributeFieldImpl( IdlReflectionServiceImpl * pReflection, const OUString & rName,
68 typelib_TypeDescription * pTypeDescr, typelib_TypeDescription * pDeclTypeDescr )
69 : IdlAttributeFieldImpl_Base( pReflection, rName, pTypeDescr, pDeclTypeDescr )
72 // XIdlMember
73 virtual Reference< XIdlClass > SAL_CALL getDeclaringClass() override;
74 virtual OUString SAL_CALL getName() override;
75 // XIdlField
76 virtual Reference< XIdlClass > SAL_CALL getType() override;
77 virtual FieldAccessMode SAL_CALL getAccessMode() override;
78 virtual Any SAL_CALL get( const Any & rObj ) override;
79 virtual void SAL_CALL set( const Any & rObj, const Any & rValue ) override;
80 // XIdlField2: getType, getAccessMode and get are equal to XIdlField
81 virtual void SAL_CALL set( Any & rObj, const Any & rValue ) override;
83 private:
84 void checkException(
85 uno_Any * exception, Reference< XInterface > const & context) const;
90 // XIdlMember
92 Reference< XIdlClass > IdlAttributeFieldImpl::getDeclaringClass()
94 if (! _xDeclClass.is())
96 ::osl::MutexGuard aGuard( getMutexAccess() );
97 if (! _xDeclClass.is())
99 OUString aName(getAttributeTypeDescr()->aBase.aBase.pTypeName);
100 sal_Int32 i = aName.indexOf(':');
101 OSL_ASSERT(i >= 0);
102 _xDeclClass = getReflection()->forName(aName.copy(0, i));
105 return _xDeclClass;
108 OUString IdlAttributeFieldImpl::getName()
110 return IdlMemberImpl::getName();
113 // XIdlField
115 Reference< XIdlClass > IdlAttributeFieldImpl::getType()
117 return getReflection()->forType(
118 getAttributeTypeDescr()->pAttributeTypeRef );
121 FieldAccessMode IdlAttributeFieldImpl::getAccessMode()
123 return (getAttributeTypeDescr()->bReadOnly
124 ? FieldAccessMode_READONLY : FieldAccessMode_READWRITE);
127 Any IdlAttributeFieldImpl::get( const Any & rObj )
129 uno_Interface * pUnoI = getReflection()->mapToUno(
130 rObj, reinterpret_cast<typelib_InterfaceTypeDescription *>(getDeclTypeDescr()) );
131 OSL_ENSURE( pUnoI, "### illegal destination object given!" );
132 if (pUnoI)
134 TypeDescription aTD( getAttributeTypeDescr()->pAttributeTypeRef );
135 typelib_TypeDescription * pTD = aTD.get();
137 uno_Any aExc;
138 uno_Any * pExc = &aExc;
139 void * pReturn = alloca( pTD->nSize );
141 (*pUnoI->pDispatcher)( pUnoI, getTypeDescr(), pReturn, nullptr, &pExc );
142 (*pUnoI->release)( pUnoI );
144 checkException(pExc, *o3tl::doAccess<Reference<XInterface>>(rObj));
145 Any aRet;
146 uno_any_destruct(
147 &aRet, reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
148 uno_any_constructAndConvert( &aRet, pReturn, pTD, getReflection()->getUno2Cpp().get() );
149 uno_destructData( pReturn, pTD, nullptr );
150 return aRet;
152 throw IllegalArgumentException(
153 "illegal object given!",
154 static_cast<XWeak *>(static_cast<OWeakObject *>(this)), 0 );
157 void IdlAttributeFieldImpl::set( Any & rObj, const Any & rValue )
159 if (getAttributeTypeDescr()->bReadOnly)
161 throw IllegalAccessException(
162 "cannot set readonly attribute!",
163 static_cast<XWeak *>(static_cast<OWeakObject *>(this)) );
166 uno_Interface * pUnoI = getReflection()->mapToUno(
167 rObj, reinterpret_cast<typelib_InterfaceTypeDescription *>(getDeclTypeDescr()) );
168 OSL_ENSURE( pUnoI, "### illegal destination object given!" );
169 if (pUnoI)
171 TypeDescription aTD( getAttributeTypeDescr()->pAttributeTypeRef );
172 typelib_TypeDescription * pTD = aTD.get();
174 // construct uno value to be set
175 void * pArgs[1];
176 void * pArg = pArgs[0] = alloca( pTD->nSize );
178 bool bAssign;
179 if (pTD->eTypeClass == typelib_TypeClass_ANY)
181 uno_copyAndConvertData( pArg, const_cast< Any * >(&rValue),
182 pTD, getReflection()->getCpp2Uno().get() );
183 bAssign = true;
185 else if (typelib_typedescriptionreference_equals( rValue.getValueTypeRef(), pTD->pWeakRef ))
187 uno_copyAndConvertData( pArg, const_cast< void * >(rValue.getValue()),
188 pTD, getReflection()->getCpp2Uno().get() );
189 bAssign = true;
191 else if (pTD->eTypeClass == typelib_TypeClass_INTERFACE)
193 Reference< XInterface > xObj;
194 bAssign = extract(
195 rValue, reinterpret_cast<typelib_InterfaceTypeDescription *>(pTD), xObj,
196 getReflection() );
197 if (bAssign)
199 *static_cast<void **>(pArg) = getReflection()->getCpp2Uno().mapInterface(
200 xObj.get(), reinterpret_cast<typelib_InterfaceTypeDescription *>(pTD) );
203 else
205 typelib_TypeDescription * pValueTD = nullptr;
206 TYPELIB_DANGER_GET( &pValueTD, rValue.getValueTypeRef() );
207 // construct temp uno val to do proper assignment: todo opt
208 void * pTemp = alloca( pValueTD->nSize );
209 uno_copyAndConvertData(
210 pTemp, const_cast<void *>(rValue.getValue()), pValueTD, getReflection()->getCpp2Uno().get() );
211 uno_constructData(
212 pArg, pTD );
213 // assignment does simple conversion
214 bAssign = uno_assignData(
215 pArg, pTD, pTemp, pValueTD, nullptr, nullptr, nullptr );
216 uno_destructData(
217 pTemp, pValueTD, nullptr );
218 TYPELIB_DANGER_RELEASE( pValueTD );
221 if (bAssign)
223 uno_Any aExc;
224 uno_Any * pExc = &aExc;
225 (*pUnoI->pDispatcher)( pUnoI, getTypeDescr(), nullptr, pArgs, &pExc );
226 (*pUnoI->release)( pUnoI );
228 uno_destructData( pArg, pTD, nullptr );
229 checkException(pExc, *o3tl::doAccess<Reference<XInterface>>(rObj));
230 return;
232 (*pUnoI->release)( pUnoI );
234 throw IllegalArgumentException(
235 "illegal value given!",
236 *o3tl::doAccess<Reference<XInterface>>(rObj), 1 );
238 throw IllegalArgumentException(
239 "illegal destination object given!",
240 static_cast<XWeak *>(static_cast<OWeakObject *>(this)), 0 );
243 void IdlAttributeFieldImpl::set( const Any & rObj, const Any & rValue )
245 IdlAttributeFieldImpl::set( const_cast< Any & >( rObj ), rValue );
248 void IdlAttributeFieldImpl::checkException(
249 uno_Any * exception, Reference< XInterface > const & context) const
251 if (exception == nullptr)
252 return;
254 Any e;
255 uno_any_destruct(&e, reinterpret_cast< uno_ReleaseFunc >(cpp_release));
256 uno_type_any_constructAndConvert(
257 &e, exception->pData, exception->pType,
258 getReflection()->getUno2Cpp().get());
259 uno_any_destruct(exception, nullptr);
260 if (!e.isExtractableTo(
261 cppu::UnoType<RuntimeException>::get()))
263 throw WrappedTargetRuntimeException(
264 "non-RuntimeException occurred when accessing an"
265 " interface type attribute",
266 context, e);
268 cppu::throwException(e);
271 namespace {
273 typedef cppu::ImplInheritanceHelper<IdlMemberImpl, XIdlMethod> IdlInterfaceMethodImpl_Base;
274 class IdlInterfaceMethodImpl : public IdlInterfaceMethodImpl_Base
276 std::optional<Sequence< Reference< XIdlClass > >> m_xExceptionTypes;
277 std::optional<Sequence< Reference< XIdlClass > >> m_xParamTypes;
278 std::optional<Sequence< ParamInfo >> m_xParamInfos;
280 public:
281 typelib_InterfaceMethodTypeDescription * getMethodTypeDescr() const
282 { return reinterpret_cast<typelib_InterfaceMethodTypeDescription *>(getTypeDescr()); }
284 IdlInterfaceMethodImpl( IdlReflectionServiceImpl * pReflection, const OUString & rName,
285 typelib_TypeDescription * pTypeDescr, typelib_TypeDescription * pDeclTypeDescr )
286 : IdlInterfaceMethodImpl_Base( pReflection, rName, pTypeDescr, pDeclTypeDescr )
289 // XTypeProvider
290 virtual Sequence< sal_Int8 > SAL_CALL getImplementationId() override;
292 // XIdlMember
293 virtual Reference< XIdlClass > SAL_CALL getDeclaringClass() override;
294 virtual OUString SAL_CALL getName() override;
295 // XIdlMethod
296 virtual Reference< XIdlClass > SAL_CALL getReturnType() override;
297 virtual Sequence< Reference< XIdlClass > > SAL_CALL getParameterTypes() override;
298 virtual Sequence< ParamInfo > SAL_CALL getParameterInfos() override;
299 virtual Sequence< Reference< XIdlClass > > SAL_CALL getExceptionTypes() override;
300 virtual MethodMode SAL_CALL getMode() override;
301 virtual Any SAL_CALL invoke( const Any & rObj, Sequence< Any > & rArgs ) override;
306 // XTypeProvider
308 Sequence< sal_Int8 > IdlInterfaceMethodImpl::getImplementationId()
310 return css::uno::Sequence<sal_Int8>();
313 // XIdlMember
315 Reference< XIdlClass > IdlInterfaceMethodImpl::getDeclaringClass()
317 if (! _xDeclClass.is())
319 ::osl::MutexGuard aGuard( getMutexAccess() );
320 if (! _xDeclClass.is())
322 OUString aName(getMethodTypeDescr()->aBase.aBase.pTypeName);
323 sal_Int32 i = aName.indexOf(':');
324 OSL_ASSERT(i >= 0);
325 _xDeclClass = getReflection()->forName(aName.copy(0, i));
328 return _xDeclClass;
331 OUString IdlInterfaceMethodImpl::getName()
333 return IdlMemberImpl::getName();
336 // XIdlMethod
338 Reference< XIdlClass > SAL_CALL IdlInterfaceMethodImpl::getReturnType()
340 return getReflection()->forType( getMethodTypeDescr()->pReturnTypeRef );
343 Sequence< Reference< XIdlClass > > IdlInterfaceMethodImpl::getExceptionTypes()
345 if (! m_xExceptionTypes)
347 ::osl::MutexGuard aGuard( getMutexAccess() );
348 if (! m_xExceptionTypes)
350 sal_Int32 nExc = getMethodTypeDescr()->nExceptions;
351 Sequence< Reference< XIdlClass > > aTempExceptionTypes( nExc );
352 Reference< XIdlClass > * pExceptionTypes = aTempExceptionTypes.getArray();
354 typelib_TypeDescriptionReference ** ppExc =
355 getMethodTypeDescr()->ppExceptions;
356 IdlReflectionServiceImpl * pRefl = getReflection();
358 while (nExc--)
359 pExceptionTypes[nExc] = pRefl->forType( ppExc[nExc] );
361 m_xExceptionTypes = std::move(aTempExceptionTypes);
364 return *m_xExceptionTypes;
367 Sequence< Reference< XIdlClass > > IdlInterfaceMethodImpl::getParameterTypes()
369 if (! m_xParamTypes)
371 ::osl::MutexGuard aGuard( getMutexAccess() );
372 if (! m_xParamTypes)
374 sal_Int32 nParams = getMethodTypeDescr()->nParams;
375 Sequence< Reference< XIdlClass > > aTempParamTypes( nParams );
376 Reference< XIdlClass > * pParamTypes = aTempParamTypes.getArray();
378 typelib_MethodParameter * pTypelibParams =
379 getMethodTypeDescr()->pParams;
380 IdlReflectionServiceImpl * pRefl = getReflection();
382 while (nParams--)
383 pParamTypes[nParams] = pRefl->forType( pTypelibParams[nParams].pTypeRef );
385 m_xParamTypes = std::move(aTempParamTypes);
388 return *m_xParamTypes;
391 Sequence< ParamInfo > IdlInterfaceMethodImpl::getParameterInfos()
393 if (! m_xParamInfos)
395 ::osl::MutexGuard aGuard( getMutexAccess() );
396 if (! m_xParamInfos)
398 sal_Int32 nParams = getMethodTypeDescr()->nParams;
399 Sequence< ParamInfo > aTempParamInfos( nParams );
400 ParamInfo * pParamInfos = aTempParamInfos.getArray();
402 typelib_MethodParameter * pTypelibParams =
403 getMethodTypeDescr()->pParams;
405 if (m_xParamTypes) // use param types
407 const Reference< XIdlClass > * pParamTypes = m_xParamTypes->getConstArray();
409 while (nParams--)
411 const typelib_MethodParameter & rParam = pTypelibParams[nParams];
412 ParamInfo & rInfo = pParamInfos[nParams];
413 rInfo.aName = rParam.pName;
414 if (rParam.bIn)
415 rInfo.aMode = (rParam.bOut ? ParamMode_INOUT : ParamMode_IN);
416 else
417 rInfo.aMode = ParamMode_OUT;
418 rInfo.aType = pParamTypes[nParams];
421 else // make also param types sequence if not already initialized
423 Sequence< Reference< XIdlClass > > aTempParamTypes( nParams );
424 Reference< XIdlClass > * pParamTypes = aTempParamTypes.getArray();
426 IdlReflectionServiceImpl * pRefl = getReflection();
428 while (nParams--)
430 const typelib_MethodParameter & rParam = pTypelibParams[nParams];
431 ParamInfo & rInfo = pParamInfos[nParams];
432 rInfo.aName = rParam.pName;
433 if (rParam.bIn)
434 rInfo.aMode = (rParam.bOut ? ParamMode_INOUT : ParamMode_IN);
435 else
436 rInfo.aMode = ParamMode_OUT;
437 rInfo.aType = pParamTypes[nParams] = pRefl->forType( rParam.pTypeRef );
440 m_xParamTypes = std::move(aTempParamTypes);
443 m_xParamInfos = std::move(aTempParamInfos);
446 return *m_xParamInfos;
449 MethodMode SAL_CALL IdlInterfaceMethodImpl::getMode()
451 return
452 getMethodTypeDescr()->bOneWay ? MethodMode_ONEWAY : MethodMode_TWOWAY;
455 Any SAL_CALL IdlInterfaceMethodImpl::invoke( const Any & rObj, Sequence< Any > & rArgs )
457 if (auto ifc = o3tl::tryAccess<css::uno::Reference<css::uno::XInterface>>(
458 rObj))
460 // acquire()/ release()
461 if (rtl_ustr_ascii_compare( getTypeDescr()->pTypeName->buffer,
462 "com.sun.star.uno.XInterface::acquire" ) == 0)
464 (*ifc)->acquire();
465 return Any();
467 else if (rtl_ustr_ascii_compare( getTypeDescr()->pTypeName->buffer,
468 "com.sun.star.uno.XInterface::release" ) == 0)
470 (*ifc)->release();
471 return Any();
475 uno_Interface * pUnoI = getReflection()->mapToUno(
476 rObj, reinterpret_cast<typelib_InterfaceTypeDescription *>(getDeclTypeDescr()) );
477 OSL_ENSURE( pUnoI, "### illegal destination object given!" );
478 if (pUnoI)
480 sal_Int32 nParams = getMethodTypeDescr()->nParams;
481 if (rArgs.getLength() != nParams)
483 (*pUnoI->release)( pUnoI );
484 throw IllegalArgumentException(
485 "expected " + OUString::number(nParams) +
486 " arguments, got " + OUString::number(rArgs.getLength()),
487 *o3tl::doAccess<Reference<XInterface>>(rObj), 1 );
490 Any * pCppArgs = rArgs.getArray();
491 typelib_MethodParameter * pParams = getMethodTypeDescr()->pParams;
492 typelib_TypeDescription * pReturnType = nullptr;
493 TYPELIB_DANGER_GET(
494 &pReturnType, getMethodTypeDescr()->pReturnTypeRef );
496 // C/C++ ABIs typically assume that structs are padded at the end, and
497 // that those padding bytes may be written to (e.g., to write into the
498 // end of a "short" struct by writing the full contents of a "long"
499 // register); so create enough space here (assuming that no ABI requires
500 // padding larger than 16 byte boundaries):
501 void * pUnoReturn = (pReturnType->nSize == 0) ? nullptr : alloca( multipleOf16(pReturnType->nSize) );
502 void ** ppUnoArgs = static_cast<void **>(alloca( sizeof(void *) * nParams *2 ));
503 typelib_TypeDescription ** ppParamTypes = reinterpret_cast<typelib_TypeDescription **>(ppUnoArgs + nParams);
505 // convert arguments
506 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
508 ppParamTypes[nPos] = nullptr;
509 TYPELIB_DANGER_GET( ppParamTypes + nPos, pParams[nPos].pTypeRef );
510 typelib_TypeDescription * pTD = ppParamTypes[nPos];
512 ppUnoArgs[nPos] = alloca( pTD->nSize );
513 if (pParams[nPos].bIn)
515 bool bAssign;
516 if (typelib_typedescriptionreference_equals(
517 pCppArgs[nPos].getValueTypeRef(), pTD->pWeakRef ))
519 uno_type_copyAndConvertData(
520 ppUnoArgs[nPos], const_cast<void *>(pCppArgs[nPos].getValue()),
521 pCppArgs[nPos].getValueTypeRef(), getReflection()->getCpp2Uno().get() );
522 bAssign = true;
524 else if (pTD->eTypeClass == typelib_TypeClass_ANY)
526 uno_type_any_constructAndConvert(
527 static_cast<uno_Any *>(ppUnoArgs[nPos]), const_cast<void *>(pCppArgs[nPos].getValue()),
528 pCppArgs[nPos].getValueTypeRef(), getReflection()->getCpp2Uno().get() );
529 bAssign = true;
531 else if (pTD->eTypeClass == typelib_TypeClass_INTERFACE)
533 Reference< XInterface > xDest;
534 bAssign = extract(
535 pCppArgs[nPos], reinterpret_cast<typelib_InterfaceTypeDescription *>(pTD),
536 xDest, getReflection() );
537 if (bAssign)
539 *static_cast<void **>(ppUnoArgs[nPos]) = getReflection()->getCpp2Uno().mapInterface(
540 xDest.get(), reinterpret_cast<typelib_InterfaceTypeDescription *>(pTD) );
543 else
545 typelib_TypeDescription * pValueTD = nullptr;
546 TYPELIB_DANGER_GET( &pValueTD, pCppArgs[nPos].getValueTypeRef() );
547 // construct temp uno val to do proper assignment: todo opt
548 void * pTemp = alloca( pValueTD->nSize );
549 uno_copyAndConvertData(
550 pTemp, const_cast<void *>(pCppArgs[nPos].getValue()), pValueTD,
551 getReflection()->getCpp2Uno().get() );
552 uno_constructData(
553 ppUnoArgs[nPos], pTD );
554 // assignment does simple conversion
555 bAssign = uno_assignData(
556 ppUnoArgs[nPos], pTD, pTemp, pValueTD, nullptr, nullptr, nullptr );
557 uno_destructData(
558 pTemp, pValueTD, nullptr );
559 TYPELIB_DANGER_RELEASE( pValueTD );
562 if (! bAssign)
564 IllegalArgumentException aExc(
565 "cannot coerce argument type during corereflection call:"
566 "\narg no.: " + OUString::number(nPos)
567 + " expected: \"" + OUString::unacquired(&pTD->pTypeName)
568 + "\" actual: \"" + OUString::unacquired(&pCppArgs[nPos].getValueTypeRef()->pTypeName)
569 + "\"",
570 *o3tl::doAccess<Reference<XInterface>>(rObj), static_cast<sal_Int16>(nPos) );
572 // cleanup
573 while (nPos--)
575 if (pParams[nPos].bIn)
576 uno_destructData( ppUnoArgs[nPos], ppParamTypes[nPos], nullptr );
577 TYPELIB_DANGER_RELEASE( ppParamTypes[nPos] );
579 TYPELIB_DANGER_RELEASE( pReturnType );
580 (*pUnoI->release)( pUnoI );
582 throw aExc;
587 uno_Any aUnoExc;
588 uno_Any * pUnoExc = &aUnoExc;
590 (*pUnoI->pDispatcher)(
591 pUnoI, getTypeDescr(), pUnoReturn, ppUnoArgs, &pUnoExc );
592 (*pUnoI->release)( pUnoI );
594 Any aRet;
595 if (pUnoExc)
597 // cleanup
598 while (nParams--)
600 if (pParams[nParams].bIn)
601 uno_destructData( ppUnoArgs[nParams], ppParamTypes[nParams], nullptr );
602 TYPELIB_DANGER_RELEASE( ppParamTypes[nParams] );
604 TYPELIB_DANGER_RELEASE( pReturnType );
606 InvocationTargetException aExc;
607 aExc.Context = *o3tl::doAccess<Reference<XInterface>>(rObj);
608 aExc.Message = "exception occurred during invocation!";
609 uno_any_destruct(
610 &aExc.TargetException,
611 reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
612 uno_type_copyAndConvertData(
613 &aExc.TargetException, pUnoExc, cppu::UnoType<Any>::get().getTypeLibType(),
614 getReflection()->getUno2Cpp().get() );
615 uno_any_destruct( pUnoExc, nullptr );
616 throw aExc;
618 else
620 // reconvert arguments and cleanup
621 while (nParams--)
623 if (pParams[nParams].bOut) // write back
625 uno_any_destruct(
626 &pCppArgs[nParams],
627 reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
628 uno_any_constructAndConvert(
629 &pCppArgs[nParams], ppUnoArgs[nParams], ppParamTypes[nParams],
630 getReflection()->getUno2Cpp().get() );
632 uno_destructData( ppUnoArgs[nParams], ppParamTypes[nParams], nullptr );
633 TYPELIB_DANGER_RELEASE( ppParamTypes[nParams] );
635 uno_any_destruct(
636 &aRet, reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
637 uno_any_constructAndConvert(
638 &aRet, pUnoReturn, pReturnType,
639 getReflection()->getUno2Cpp().get() );
640 uno_destructData( pUnoReturn, pReturnType, nullptr );
641 TYPELIB_DANGER_RELEASE( pReturnType );
643 return aRet;
645 throw IllegalArgumentException(
646 "illegal destination object given!",
647 static_cast<XWeak *>(static_cast<OWeakObject *>(this)), 0 );
651 InterfaceIdlClassImpl::~InterfaceIdlClassImpl()
653 for ( sal_Int32 nPos = _nMethods + _nAttributes; nPos--; )
654 typelib_typedescription_release( _pSortedMemberInit[nPos].second );
658 Sequence< Reference< XIdlClass > > InterfaceIdlClassImpl::getSuperclasses()
660 ::osl::MutexGuard aGuard(getMutexAccess());
661 if (!_xSuperClasses.hasElements()) {
662 typelib_InterfaceTypeDescription * pType = getTypeDescr();
663 _xSuperClasses.realloc(pType->nBaseTypes);
664 auto pSuperClasses = _xSuperClasses.getArray();
665 for (sal_Int32 i = 0; i < pType->nBaseTypes; ++i) {
666 pSuperClasses[i] = getReflection()->forType(
667 &pType->ppBaseTypes[i]->aBase);
668 OSL_ASSERT(_xSuperClasses[i].is());
671 return _xSuperClasses;
674 void InterfaceIdlClassImpl::initMembers()
676 sal_Int32 nAll = getTypeDescr()->nAllMembers;
677 std::unique_ptr<MemberInit[]> pSortedMemberInit(new MemberInit[nAll]);
678 typelib_TypeDescriptionReference ** ppAllMembers = getTypeDescr()->ppAllMembers;
680 for ( sal_Int32 nPos = 0; nPos < nAll; ++nPos )
682 sal_Int32 nIndex;
683 if (ppAllMembers[nPos]->eTypeClass == typelib_TypeClass_INTERFACE_METHOD)
685 // methods to front
686 nIndex = _nMethods;
687 ++_nMethods;
689 else
691 ++_nAttributes;
692 nIndex = (nAll - _nAttributes);
693 // attributes at the back
696 typelib_TypeDescription * pTD = nullptr;
697 typelib_typedescriptionreference_getDescription( &pTD, ppAllMembers[nPos] );
698 assert(pTD && "### cannot get type description!");
699 pSortedMemberInit[nIndex].first = reinterpret_cast<typelib_InterfaceMemberTypeDescription *>(pTD)->pMemberName;
700 pSortedMemberInit[nIndex].second = pTD;
703 _pSortedMemberInit = std::move(pSortedMemberInit);
706 sal_Bool InterfaceIdlClassImpl::isAssignableFrom( const Reference< XIdlClass > & xType )
708 if (xType.is() && xType->getTypeClass() == TypeClass_INTERFACE)
710 if (equals( xType ))
711 return true;
712 else
714 const Sequence< Reference< XIdlClass > > & rSeq = xType->getSuperclasses();
715 if (std::any_of(rSeq.begin(), rSeq.end(),
716 [this](const Reference<XIdlClass>& rType){ return isAssignableFrom(rType); }))
717 return true;
720 return false;
723 Uik InterfaceIdlClassImpl::getUik()
725 return Uik(0, 0, 0, 0, 0);
726 // Uiks are deprecated and this function must not be called
729 Sequence< Reference< XIdlMethod > > InterfaceIdlClassImpl::getMethods()
731 ::osl::MutexGuard aGuard( getMutexAccess() );
732 if (! _pSortedMemberInit)
733 initMembers();
735 // create methods sequence
736 Sequence< Reference< XIdlMethod > > aRet( _nMethods );
737 Reference< XIdlMethod > * pRet = aRet.getArray();
738 for ( sal_Int32 nPos = _nMethods; nPos--; )
741 /*_aName2Method[_pSortedMemberInit[nPos].first] = */pRet[nPos] = new IdlInterfaceMethodImpl(
742 getReflection(), _pSortedMemberInit[nPos].first,
743 _pSortedMemberInit[nPos].second, IdlClassImpl::getTypeDescr() );
745 return aRet;
748 Sequence< Reference< XIdlField > > InterfaceIdlClassImpl::getFields()
750 ::osl::MutexGuard aGuard( getMutexAccess() );
751 if (! _pSortedMemberInit)
752 initMembers();
754 // create fields sequence
755 Sequence< Reference< XIdlField > > aRet( _nAttributes );
756 Reference< XIdlField > * pRet = aRet.getArray();
757 for ( sal_Int32 nPos = _nAttributes; nPos--; )
759 /*_aName2Field[_pSortedMemberInit[_nMethods+nPos].first] = */pRet[_nAttributes-nPos-1] =
760 new IdlAttributeFieldImpl(
761 getReflection(), _pSortedMemberInit[_nMethods+nPos].first,
762 _pSortedMemberInit[_nMethods+nPos].second, IdlClassImpl::getTypeDescr() );
764 return aRet;
767 Reference< XIdlMethod > InterfaceIdlClassImpl::getMethod( const OUString & rName )
769 ::osl::MutexGuard aGuard( getMutexAccess() );
770 if (! _pSortedMemberInit)
771 initMembers();
773 Reference< XIdlMethod > xRet;
775 // try weak map
776 const OUString2Method::const_iterator iFind( _aName2Method.find( rName ) );
777 if (iFind != _aName2Method.end())
778 xRet = (*iFind).second; // harden ref
780 if (! xRet.is())
782 for ( sal_Int32 nPos = _nMethods; nPos--; )
784 if (_pSortedMemberInit[nPos].first == rName)
786 _aName2Method[rName] = xRet = new IdlInterfaceMethodImpl(
787 getReflection(), rName,
788 _pSortedMemberInit[nPos].second, IdlClassImpl::getTypeDescr() );
789 break;
793 return xRet;
796 Reference< XIdlField > InterfaceIdlClassImpl::getField( const OUString & rName )
798 ::osl::MutexGuard aGuard( getMutexAccess() );
799 if (! _pSortedMemberInit)
800 initMembers();
802 Reference< XIdlField > xRet;
804 // try weak map
805 const OUString2Field::const_iterator iFind( _aName2Field.find( rName ) );
806 if (iFind != _aName2Field.end())
807 xRet = (*iFind).second; // harden ref
809 if (! xRet.is())
811 for ( sal_Int32 nPos = _nAttributes; nPos--; )
813 if (_pSortedMemberInit[_nMethods+nPos].first == rName)
815 _aName2Field[rName] = xRet = new IdlAttributeFieldImpl(
816 getReflection(), rName,
817 _pSortedMemberInit[_nMethods+nPos].second, IdlClassImpl::getTypeDescr() );
818 break;
822 return xRet;
825 void InterfaceIdlClassImpl::createObject( Any & rObj )
827 // interfaces cannot be constructed
828 rObj.clear();
834 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */