Bump for 3.6-28
[LibreOffice.git] / bridges / source / cpp_uno / mingw_intel / uno2cpp.cxx
blob87592da53f2553baa74de2184cda0a6daa5c4e27
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"
32 #include <uno/data.h>
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"
41 #include "share.hxx"
42 #include "smallstruct.hxx"
44 using namespace ::rtl;
45 using namespace ::com::sun::star::uno;
47 namespace
50 static void cpp_call(
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 ...
58 char * pCppStack =
59 #ifdef BROKEN_ALLOCA
60 (char *)malloc( sizeof(sal_Int32) + ((nParams+2) * sizeof(sal_Int64)) );
61 #else
62 (char *)alloca( sizeof(sal_Int32) + ((nParams+2) * sizeof(sal_Int64)) );
63 #endif
64 char * pCppStackStart = pCppStack;
66 // return
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
73 if (pReturnTypeDescr)
75 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr ))
77 pCppReturn = pUnoReturn; // direct way for simple types
79 else
81 // complex return via ptr
82 pCppReturn
83 = (bridges::cpp_uno::shared::relatesToInterfaceType(
84 pReturnTypeDescr )
85 #ifdef BROKEN_ALLOCA
86 ? malloc( pReturnTypeDescr->nSize )
87 #else
88 ? alloca( pReturnTypeDescr->nSize )
89 #endif
90 : pUnoReturn); // direct way
91 if (!bridges::cpp_uno::shared::isSmallStruct(pReturnTypeDescr)) {
92 *(void **)pCppStack = pCppReturn;
93 pCppStack += sizeof(void *);
97 // push this
98 void * pAdjustedThisPtr = reinterpret_cast< void ** >(pThis->getCppI())
99 + aVtableSlot.offset;
100 *(void**)pCppStack = pAdjustedThisPtr;
101 pCppStack += sizeof( void* );
103 // stack space
104 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32), "### unexpected size!" );
105 // args
106 #ifdef BROKEN_ALLOCA
107 void ** pCppArgs = (void **)malloc( 3 * sizeof(void *) * nParams );
108 #else
109 void ** pCppArgs = (void **)alloca( 3 * sizeof(void *) * nParams );
110 #endif
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 );
124 if (!rParam.bOut
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
136 break;
137 default:
138 break;
140 // no longer needed
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!
148 uno_constructData(
149 #ifdef BROKEN_ALLOCA
150 *(void **)pCppStack = pCppArgs[nPos] = malloc( pParamTypeDescr->nSize ),
151 #else
152 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
153 #endif
154 pParamTypeDescr );
155 pTempIndizes[nTempIndizes] = nPos; // default constructed for cpp call
156 // will be released at reconversion
157 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
159 // is in/inout
160 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
161 pParamTypeDescr ))
163 uno_copyAndConvertData(
164 #ifdef BROKEN_ALLOCA
165 *(void **)pCppStack = pCppArgs[nPos] = malloc( pParamTypeDescr->nSize ),
166 #else
167 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
168 #endif
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;
176 else // direct way
178 *(void **)pCppStack = pCppArgs[nPos] = pUnoArgs[nPos];
179 // no longer needed
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...
194 *ppUnoExc = 0;
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() );
211 else // pure out
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 );
218 #ifdef BROKEN_ALLOCA
219 free( pCppArgs[nIndex] );
220 #endif
222 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
224 // return value
225 if (pCppReturn && pUnoReturn != pCppReturn)
227 uno_copyAndConvertData( pUnoReturn, pCppReturn, pReturnTypeDescr,
228 pThis->getBridge()->getCpp2Uno() );
229 uno_destructData( pCppReturn, pReturnTypeDescr, cpp_release );
232 catch (...)
234 // fill uno exception
235 fillUnoException( __cxa_get_globals()->caughtExceptions, *ppUnoExc, pThis->getBridge()->getCpp2Uno() );
237 // temporary params
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] );
244 #ifdef BROKEN_ALLOCA
245 free( pCppArgs[nIndex] );
246 #endif
248 // return type
249 if (pReturnTypeDescr)
250 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
252 if (pCppReturn && pUnoReturn != pCppReturn)
254 #ifdef BROKEN_ALLOCA
255 free( pCppReturn );
256 #endif
258 #ifdef BROKEN_ALLOCA
259 free( pCppStackStart );
260 #endif
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 )
271 // is my surrogate
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(
280 getVtableSlot(
281 reinterpret_cast<
282 typelib_InterfaceAttributeTypeDescription const * >(
283 pMemberDescr)));
284 if (pReturn)
286 // dependent dispatch
287 cpp_call(
288 pThis, aVtableSlot,
289 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef,
290 0, 0, // no params
291 pReturn, pArgs, ppException );
293 else
295 // is SET
296 typelib_MethodParameter aParam;
297 aParam.pTypeRef =
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
309 cpp_call(
310 pThis, aVtableSlot,
311 pReturnTypeRef,
312 1, &aParam,
313 pReturn, pArgs, ppException );
315 typelib_typedescriptionreference_release( pReturnTypeRef );
318 break;
320 case typelib_TypeClass_INTERFACE_METHOD:
322 VtableSlot aVtableSlot(
323 getVtableSlot(
324 reinterpret_cast<
325 typelib_InterfaceMethodTypeDescription const * >(
326 pMemberDescr)));
327 switch (aVtableSlot.index)
329 // standard calls
330 case 1: // acquire uno interface
331 (*pUnoI->acquire)( pUnoI );
332 *ppException = 0;
333 break;
334 case 2: // release uno interface
335 (*pUnoI->release)( pUnoI );
336 *ppException = 0;
337 break;
338 case 0: // queryInterface() opt
340 typelib_TypeDescription * pTD = 0;
341 TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pArgs[0] )->getTypeLibType() );
342 if (pTD)
344 uno_Interface * pInterface = 0;
345 (*pThis->pBridge->getUnoEnv()->getRegisteredInterface)(
346 pThis->pBridge->getUnoEnv(),
347 (void **)&pInterface, pThis->oid.pData, (typelib_InterfaceTypeDescription *)pTD );
349 if (pInterface)
351 ::uno_any_construct(
352 reinterpret_cast< uno_Any * >( pReturn ),
353 &pInterface, pTD, 0 );
354 (*pInterface->release)( pInterface );
355 TYPELIB_DANGER_RELEASE( pTD );
356 *ppException = 0;
357 break;
359 TYPELIB_DANGER_RELEASE( pTD );
361 } // else perform queryInterface()
362 default:
363 // dependent dispatch
364 cpp_call(
365 pThis, aVtableSlot,
366 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pReturnTypeRef,
367 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->nParams,
368 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pParams,
369 pReturn, pArgs, ppException );
371 break;
373 default:
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 );
386 } } }
388 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */