1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include <com/sun/star/uno/genfunc.hxx>
31 #include "com/sun/star/uno/RuntimeException.hpp"
33 #include <sal/alloca.h>
35 #include "bridges/cpp_uno/shared/bridge.hxx"
36 #include "bridges/cpp_uno/shared/types.hxx"
37 #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx"
38 #include "bridges/cpp_uno/shared/vtables.hxx"
40 #include "callvirtualmethod.hxx"
42 #include "smallstruct.hxx"
44 using namespace ::rtl
;
45 using namespace ::com::sun::star::uno
;
51 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
,
52 bridges::cpp_uno::shared::VtableSlot aVtableSlot
,
53 typelib_TypeDescriptionReference
* pReturnTypeRef
,
54 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
55 void * pUnoReturn
, void * pUnoArgs
[], uno_Any
** ppUnoExc
)
57 // max space for: [complex ret ptr], values|ptr ...
60 (char *)malloc( sizeof(sal_Int32
) + ((nParams
+2) * sizeof(sal_Int64
)) );
62 (char *)alloca( sizeof(sal_Int32
) + ((nParams
+2) * sizeof(sal_Int64
)) );
64 char * pCppStackStart
= pCppStack
;
67 typelib_TypeDescription
* pReturnTypeDescr
= 0;
68 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
69 OSL_ENSURE( pReturnTypeDescr
, "### expected return type description!" );
71 void * pCppReturn
= 0; // if != 0 && != pUnoReturn, needs reconversion
75 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr
))
77 pCppReturn
= pUnoReturn
; // direct way for simple types
81 // complex return via ptr
83 = (bridges::cpp_uno::shared::relatesToInterfaceType(
86 ? malloc( pReturnTypeDescr
->nSize
)
88 ? alloca( pReturnTypeDescr
->nSize
)
90 : pUnoReturn
); // direct way
91 if (!bridges::cpp_uno::shared::isSmallStruct(pReturnTypeDescr
)) {
92 *(void **)pCppStack
= pCppReturn
;
93 pCppStack
+= sizeof(void *);
98 void * pAdjustedThisPtr
= reinterpret_cast< void ** >(pThis
->getCppI())
100 *(void**)pCppStack
= pAdjustedThisPtr
;
101 pCppStack
+= sizeof( void* );
104 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32
), "### unexpected size!" );
107 void ** pCppArgs
= (void **)malloc( 3 * sizeof(void *) * nParams
);
109 void ** pCppArgs
= (void **)alloca( 3 * sizeof(void *) * nParams
);
111 // indizes of values this have to be converted (interface conversion cpp<=>uno)
112 sal_Int32
* pTempIndizes
= (sal_Int32
*)(pCppArgs
+ nParams
);
113 // type descriptions for reconversions
114 typelib_TypeDescription
** ppTempParamTypeDescr
= (typelib_TypeDescription
**)(pCppArgs
+ (2 * nParams
));
116 sal_Int32 nTempIndizes
= 0;
118 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
120 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
121 typelib_TypeDescription
* pParamTypeDescr
= 0;
122 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
125 && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
))
127 uno_copyAndConvertData( pCppArgs
[nPos
] = pCppStack
, pUnoArgs
[nPos
], pParamTypeDescr
,
128 pThis
->getBridge()->getUno2Cpp() );
130 switch (pParamTypeDescr
->eTypeClass
)
132 case typelib_TypeClass_HYPER
:
133 case typelib_TypeClass_UNSIGNED_HYPER
:
134 case typelib_TypeClass_DOUBLE
:
135 pCppStack
+= sizeof(sal_Int32
); // extra long
141 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
143 else // ptr to complex value | ref
145 if (! rParam
.bIn
) // is pure out
147 // cpp out is constructed mem, uno out is not!
150 *(void **)pCppStack
= pCppArgs
[nPos
] = malloc( pParamTypeDescr
->nSize
),
152 *(void **)pCppStack
= pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
155 pTempIndizes
[nTempIndizes
] = nPos
; // default constructed for cpp call
156 // will be released at reconversion
157 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
160 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
163 uno_copyAndConvertData(
165 *(void **)pCppStack
= pCppArgs
[nPos
] = malloc( pParamTypeDescr
->nSize
),
167 *(void **)pCppStack
= pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
169 pUnoArgs
[nPos
], pParamTypeDescr
,
170 pThis
->getBridge()->getUno2Cpp() );
172 pTempIndizes
[nTempIndizes
] = nPos
; // has to be reconverted
173 // will be released at reconversion
174 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
178 *(void **)pCppStack
= pCppArgs
[nPos
] = pUnoArgs
[nPos
];
180 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
183 pCppStack
+= sizeof(sal_Int32
); // standard parameter length
188 OSL_ENSURE( !( (pCppStack
- pCppStackStart
) & 3), "UNALIGNED STACK !!! (Please DO panic)" );
189 CPPU_CURRENT_NAMESPACE::callVirtualMethod(
190 pAdjustedThisPtr
, aVtableSlot
.index
,
191 pCppReturn
, pReturnTypeDescr
,
192 (sal_Int32
*)pCppStackStart
, (pCppStack
- pCppStackStart
) / sizeof(sal_Int32
) );
193 // NO exception occurred...
196 // reconvert temporary params
197 for ( ; nTempIndizes
--; )
199 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
200 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndizes
];
202 if (pParams
[nIndex
].bIn
)
204 if (pParams
[nIndex
].bOut
) // inout
206 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 ); // destroy uno value
207 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
208 pThis
->getBridge()->getCpp2Uno() );
213 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
214 pThis
->getBridge()->getCpp2Uno() );
216 // destroy temp cpp param => cpp: every param was constructed
217 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
219 free( pCppArgs
[nIndex
] );
222 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
225 if (pCppReturn
&& pUnoReturn
!= pCppReturn
)
227 uno_copyAndConvertData( pUnoReturn
, pCppReturn
, pReturnTypeDescr
,
228 pThis
->getBridge()->getCpp2Uno() );
229 uno_destructData( pCppReturn
, pReturnTypeDescr
, cpp_release
);
234 // fill uno exception
235 fillUnoException( __cxa_get_globals()->caughtExceptions
, *ppUnoExc
, pThis
->getBridge()->getCpp2Uno() );
238 for ( ; nTempIndizes
--; )
240 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
241 // destroy temp cpp param => cpp: every param was constructed
242 uno_destructData( pCppArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndizes
], cpp_release
);
243 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndizes
] );
245 free( pCppArgs
[nIndex
] );
249 if (pReturnTypeDescr
)
250 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
252 if (pCppReturn
&& pUnoReturn
!= pCppReturn
)
259 free( pCppStackStart
);
265 namespace bridges
{ namespace cpp_uno
{ namespace shared
{
267 void unoInterfaceProxyDispatch(
268 uno_Interface
* pUnoI
, const typelib_TypeDescription
* pMemberDescr
,
269 void * pReturn
, void * pArgs
[], uno_Any
** ppException
)
272 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
273 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy
* >(pUnoI
);
275 switch (pMemberDescr
->eTypeClass
)
277 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
279 VtableSlot
aVtableSlot(
282 typelib_InterfaceAttributeTypeDescription
const * >(
286 // dependent dispatch
289 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
,
291 pReturn
, pArgs
, ppException
);
296 typelib_MethodParameter aParam
;
298 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
;
299 aParam
.bIn
= sal_True
;
300 aParam
.bOut
= sal_False
;
302 typelib_TypeDescriptionReference
* pReturnTypeRef
= 0;
303 OUString
aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
304 typelib_typedescriptionreference_new(
305 &pReturnTypeRef
, typelib_TypeClass_VOID
, aVoidName
.pData
);
307 // dependent dispatch
308 aVtableSlot
.index
+= 1; // get, then set method
313 pReturn
, pArgs
, ppException
);
315 typelib_typedescriptionreference_release( pReturnTypeRef
);
320 case typelib_TypeClass_INTERFACE_METHOD
:
322 VtableSlot
aVtableSlot(
325 typelib_InterfaceMethodTypeDescription
const * >(
327 switch (aVtableSlot
.index
)
330 case 1: // acquire uno interface
331 (*pUnoI
->acquire
)( pUnoI
);
334 case 2: // release uno interface
335 (*pUnoI
->release
)( pUnoI
);
338 case 0: // queryInterface() opt
340 typelib_TypeDescription
* pTD
= 0;
341 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( pArgs
[0] )->getTypeLibType() );
344 uno_Interface
* pInterface
= 0;
345 (*pThis
->pBridge
->getUnoEnv()->getRegisteredInterface
)(
346 pThis
->pBridge
->getUnoEnv(),
347 (void **)&pInterface
, pThis
->oid
.pData
, (typelib_InterfaceTypeDescription
*)pTD
);
352 reinterpret_cast< uno_Any
* >( pReturn
),
353 &pInterface
, pTD
, 0 );
354 (*pInterface
->release
)( pInterface
);
355 TYPELIB_DANGER_RELEASE( pTD
);
359 TYPELIB_DANGER_RELEASE( pTD
);
361 } // else perform queryInterface()
363 // dependent dispatch
366 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pReturnTypeRef
,
367 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->nParams
,
368 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pParams
,
369 pReturn
, pArgs
, ppException
);
375 ::com::sun::star::uno::RuntimeException
aExc(
376 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
377 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>() );
379 Type
const & rExcType
= ::getCppuType( &aExc
);
380 // binary identical null reference
381 ::uno_type_any_construct( *ppException
, &aExc
, rExcType
.getTypeLibType(), 0 );
388 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */