merge the formfield patch from ooo-build
[ooovba.git] / cli_ure / source / uno_bridge / cli_uno.cxx
blob694f4a8b16d554ab98fe09a5776db1e4b4b33983
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: cli_uno.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_cli_ure.hxx"
34 #include <sal/alloca.h>
35 #include "rtl/ustrbuf.hxx"
36 #include "cli_base.h"
37 #include "cli_bridge.h"
39 namespace sr=System::Reflection;
40 namespace css=com::sun::star;
41 using namespace rtl;
43 namespace cli_uno
46 union largest
48 sal_Int64 n;
49 double d;
50 void * p;
51 uno_Any a;
54 System::Object* Bridge::call_uno(uno_Interface * pUnoI,
55 typelib_TypeDescription* member_td,
56 typelib_TypeDescriptionReference * return_type,
57 sal_Int32 nParams, typelib_MethodParameter const * pParams,
58 System::Object * args[], System::Type* argTypes[],
59 System::Object** ppExc) const
61 // return mem
62 sal_Int32 return_size = sizeof (largest);
63 if ((0 != return_type) &&
64 (typelib_TypeClass_STRUCT == return_type->eTypeClass ||
65 typelib_TypeClass_EXCEPTION == return_type->eTypeClass))
67 TypeDescr return_td( return_type );
68 if (return_td.get()->nSize > sizeof (largest))
69 return_size = return_td.get()->nSize;
71 //Prepare memory that contains all converted arguments and return valuse
72 //The memory block contains first pointers to the arguments which are in the same block
73 // For example, 2 arguments, 1 ret.
75 // | Pointer
76 // | Pointer
77 // | Return value
78 // | Arg 1
79 // | Arg 2
81 // If an argument is larger then union largest, such as some structures, then the pointer
82 // points to an extra block of memory. The same goes for a big return value.
84 char * mem = (char *)alloca(
85 (nParams * sizeof (void *)) + return_size + (nParams * sizeof (largest)) );
86 //array of pointers to args
87 void ** uno_args = (void **)mem;
88 //If an attribute is set, then uno_ret must be null, e.g void setAttribute(int )
89 void * uno_ret= NULL;
90 if ( !(member_td->eTypeClass == typelib_TypeClass_INTERFACE_ATTRIBUTE && nParams == 1))
91 uno_ret = (mem + (nParams * sizeof (void *)));
92 largest * uno_args_mem = (largest *)(mem + (nParams * sizeof (void *)) + return_size);
94 OSL_ASSERT( (0 == nParams) || (nParams == args->get_Length()) );
95 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
97 typelib_MethodParameter const & param = pParams[ nPos ];
98 typelib_TypeDescriptionReference * type = param.pTypeRef;
100 uno_args[ nPos ] = &uno_args_mem[ nPos ];
101 if (typelib_TypeClass_STRUCT == type->eTypeClass ||
102 typelib_TypeClass_EXCEPTION == type->eTypeClass)
104 TypeDescr td( type );
105 if (td.get()->nSize > sizeof (largest))
106 uno_args[ nPos ] = alloca( td.get()->nSize );
109 if (param.bIn)
113 // in, in/out params
114 map_to_uno(
115 uno_args[ nPos ],args[nPos] , type, false /* no assign */);
117 catch (...)
119 // cleanup uno in args
120 for (sal_Int32 n = 0; n < nPos; ++n)
122 typelib_MethodParameter const & param = pParams[n];
123 if (param.bIn)
125 uno_type_destructData(uno_args[n], param.pTypeRef, 0);
128 throw;
132 uno_Any uno_exc_holder;
133 uno_Any * uno_exc = &uno_exc_holder;
134 // call binary uno
136 (*pUnoI->pDispatcher)( pUnoI, member_td, uno_ret, uno_args, &uno_exc );
138 if (0 == uno_exc)
140 // convert out args; destruct uno args
141 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
143 typelib_MethodParameter const & param = pParams[ nPos ];
144 typelib_TypeDescriptionReference * type = param.pTypeRef;
145 if (param.bOut)
149 map_to_cli(
150 &args[nPos], uno_args[nPos], param.pTypeRef,
151 argTypes != NULL ? argTypes[nPos] : NULL, false );
153 catch (...)
155 // cleanup further uno args
156 for ( sal_Int32 n = nPos; n < nParams; ++n )
158 uno_type_destructData( uno_args[n], pParams[n].pTypeRef, 0 );
160 // cleanup uno return value
161 uno_type_destructData( uno_ret, return_type, 0 );
162 throw;
165 //cleanup args
166 if (typelib_TypeClass_DOUBLE < type->eTypeClass &&
167 typelib_TypeClass_ENUM != type->eTypeClass) // opt
169 uno_type_destructData(uno_args[nPos], type, 0);
173 if ((0 != return_type) &&
174 (typelib_TypeClass_VOID != return_type->eTypeClass))
176 // convert uno return value
179 System::Object* cli_ret;
180 map_to_cli(
181 &cli_ret, uno_ret, return_type, 0, false);
182 uno_type_destructData(uno_ret, return_type, 0);
183 return cli_ret;
185 catch (...)
187 uno_type_destructData(uno_ret, return_type, 0);
188 throw;
191 return 0; // void return
193 else // exception occured
195 // destruct uno in args
196 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
198 typelib_MethodParameter const & param = pParams[ nPos ];
199 if (param.bIn)
201 uno_type_destructData( uno_args[ nPos ], param.pTypeRef, 0 );
204 map_to_cli(ppExc, uno_exc_holder.pData,
205 uno_exc_holder.pType, NULL, false);
206 return 0;
210 void Bridge::call_cli(
211 System::Object* cliI,
212 sr::MethodInfo* method,
213 typelib_TypeDescriptionReference * return_type,
214 typelib_MethodParameter * params, int nParams,
215 void * uno_ret, void * uno_args [], uno_Any ** uno_exc ) const
217 System::Object *args[]= new System::Object*[nParams];
218 for (int nPos= 0; nPos < nParams; nPos++)
220 typelib_MethodParameter const & param= params[nPos];
221 if (param.bIn)
223 map_to_cli( &args[nPos],
224 uno_args[nPos], param.pTypeRef, 0, false);
227 System::Object* retInvoke= NULL;
230 retInvoke= method->Invoke(cliI, args);
232 catch (sr::TargetInvocationException* e)
234 System::Exception* exc= e->get_InnerException();
235 css::uno::TypeDescription td(mapCliType(exc->GetType()));
236 // memory for exception
237 std::auto_ptr< rtl_mem > memExc(rtl_mem::allocate(td.get()->nSize));
238 map_to_uno(memExc.get(), exc, td.get()->pWeakRef, false);
239 (*uno_exc)->pType= td.get()->pWeakRef;
240 (*uno_exc)->pData= memExc.release();
241 return;
243 catch (System::Exception* e)
245 OUStringBuffer buf( 128 );
246 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(
247 "Unexspected exception during invocation of cli object. "
248 "Original message is: \n") );
249 buf.append(mapCliString(e->get_Message()));
250 throw BridgeRuntimeError( buf.makeStringAndClear() );
253 //convert out, in/out params
254 for (int nPos = 0; nPos < nParams; ++nPos )
256 typelib_MethodParameter const & param = params[ nPos ];
258 if (param.bOut)
262 map_to_uno(
263 uno_args[ nPos ], args[ nPos ], param.pTypeRef,
264 sal_True == param.bIn /* assign if inout */);
265 // out array
267 catch (...)
269 // cleanup uno pure out
270 for ( sal_Int32 n = 0; n < nPos; ++n )
272 typelib_MethodParameter const & param = params[ n ];
273 if (! param.bIn)
274 uno_type_destructData( uno_args[ n ], param.pTypeRef, 0 );
276 throw;
280 // return value
281 if (0 != return_type)
283 map_to_uno(
284 uno_ret, retInvoke, return_type, false /* no assign */);
286 // no exception occured
287 *uno_exc = 0;