Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / cli_ure / source / uno_bridge / cli_uno.cxx
bloba40065cb53c9a6014086b9f066d15848fcbf88ba
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 ************************************************************************/
29 #include <sal/alloca.h>
30 #include "rtl/ustrbuf.hxx"
31 #include "cli_base.h"
32 #include "cli_bridge.h"
34 namespace sr=System::Reflection;
35 namespace css=com::sun::star;
37 using ::rtl::OUStringBuffer;
39 namespace cli_uno
42 union largest
44 sal_Int64 n;
45 double d;
46 void * p;
47 uno_Any a;
50 System::Object* Bridge::call_uno(uno_Interface * pUnoI,
51 typelib_TypeDescription* member_td,
52 typelib_TypeDescriptionReference * return_type,
53 sal_Int32 nParams, typelib_MethodParameter const * pParams,
54 System::Object * args[], System::Type* argTypes[],
55 System::Object** ppExc) const
57 // return mem
58 sal_Int32 return_size = sizeof (largest);
59 if ((0 != return_type) &&
60 (typelib_TypeClass_STRUCT == return_type->eTypeClass ||
61 typelib_TypeClass_EXCEPTION == return_type->eTypeClass))
63 TypeDescr return_td( return_type );
64 if (return_td.get()->nSize > sizeof (largest))
65 return_size = return_td.get()->nSize;
67 //Prepare memory that contains all converted arguments and return valuse
68 //The memory block contains first pointers to the arguments which are in the same block
69 // For example, 2 arguments, 1 ret.
71 // | Pointer
72 // | Pointer
73 // | Return value
74 // | Arg 1
75 // | Arg 2
77 // If an argument is larger then union largest, such as some structures, then the pointer
78 // points to an extra block of memory. The same goes for a big return value.
80 char * mem = (char *)alloca(
81 (nParams * sizeof (void *)) + return_size + (nParams * sizeof (largest)) );
82 //array of pointers to args
83 void ** uno_args = (void **)mem;
84 //If an attribute is set, then uno_ret must be null, e.g void setAttribute(int )
85 void * uno_ret= NULL;
86 if ( !(member_td->eTypeClass == typelib_TypeClass_INTERFACE_ATTRIBUTE && nParams == 1))
87 uno_ret = (mem + (nParams * sizeof (void *)));
88 largest * uno_args_mem = (largest *)(mem + (nParams * sizeof (void *)) + return_size);
90 OSL_ASSERT( (0 == nParams) || (nParams == args->get_Length()) );
91 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
93 typelib_MethodParameter const & param = pParams[ nPos ];
94 typelib_TypeDescriptionReference * type = param.pTypeRef;
96 uno_args[ nPos ] = &uno_args_mem[ nPos ];
97 if (typelib_TypeClass_STRUCT == type->eTypeClass ||
98 typelib_TypeClass_EXCEPTION == type->eTypeClass)
100 TypeDescr td( type );
101 if (td.get()->nSize > sizeof (largest))
102 uno_args[ nPos ] = alloca( td.get()->nSize );
105 if (param.bIn)
109 // in, in/out params
110 map_to_uno(
111 uno_args[ nPos ],args[nPos] , type, false /* no assign */);
113 catch (...)
115 // cleanup uno in args
116 for (sal_Int32 n = 0; n < nPos; ++n)
118 typelib_MethodParameter const & param = pParams[n];
119 if (param.bIn)
121 uno_type_destructData(uno_args[n], param.pTypeRef, 0);
124 throw;
128 uno_Any uno_exc_holder;
129 uno_Any * uno_exc = &uno_exc_holder;
130 // call binary uno
132 (*pUnoI->pDispatcher)( pUnoI, member_td, uno_ret, uno_args, &uno_exc );
134 if (0 == uno_exc)
136 // convert out args; destruct uno args
137 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
139 typelib_MethodParameter const & param = pParams[ nPos ];
140 typelib_TypeDescriptionReference * type = param.pTypeRef;
141 if (param.bOut)
145 map_to_cli(
146 &args[nPos], uno_args[nPos], param.pTypeRef,
147 argTypes != NULL ? argTypes[nPos] : NULL, false );
149 catch (...)
151 // cleanup further uno args
152 for ( sal_Int32 n = nPos; n < nParams; ++n )
154 uno_type_destructData( uno_args[n], pParams[n].pTypeRef, 0 );
156 // cleanup uno return value
157 uno_type_destructData( uno_ret, return_type, 0 );
158 throw;
161 //cleanup args
162 if (typelib_TypeClass_DOUBLE < type->eTypeClass &&
163 typelib_TypeClass_ENUM != type->eTypeClass) // opt
165 uno_type_destructData(uno_args[nPos], type, 0);
169 if ((0 != return_type) &&
170 (typelib_TypeClass_VOID != return_type->eTypeClass))
172 // convert uno return value
175 System::Object* cli_ret;
176 map_to_cli(
177 &cli_ret, uno_ret, return_type, 0, false);
178 uno_type_destructData(uno_ret, return_type, 0);
179 return cli_ret;
181 catch (...)
183 uno_type_destructData(uno_ret, return_type, 0);
184 throw;
187 return 0; // void return
189 else // exception occurred
191 // destruct uno in args
192 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
194 typelib_MethodParameter const & param = pParams[ nPos ];
195 if (param.bIn)
197 uno_type_destructData( uno_args[ nPos ], param.pTypeRef, 0 );
200 map_to_cli(ppExc, uno_exc_holder.pData,
201 uno_exc_holder.pType, NULL, false);
202 return 0;
206 void Bridge::call_cli(
207 System::Object* cliI,
208 sr::MethodInfo* method,
209 typelib_TypeDescriptionReference * return_type,
210 typelib_MethodParameter * params, int nParams,
211 void * uno_ret, void * uno_args [], uno_Any ** uno_exc ) const
213 System::Object *args[]= new System::Object*[nParams];
214 for (int nPos= 0; nPos < nParams; nPos++)
216 typelib_MethodParameter const & param= params[nPos];
217 if (param.bIn)
219 map_to_cli( &args[nPos],
220 uno_args[nPos], param.pTypeRef, 0, false);
223 System::Object* retInvoke= NULL;
226 retInvoke= method->Invoke(cliI, args);
228 catch (sr::TargetInvocationException* e)
230 System::Exception* exc= e->get_InnerException();
231 css::uno::TypeDescription td(mapCliType(exc->GetType()));
232 // memory for exception
233 std::auto_ptr< rtl_mem > memExc(rtl_mem::allocate(td.get()->nSize));
234 map_to_uno(memExc.get(), exc, td.get()->pWeakRef, false);
235 (*uno_exc)->pType= td.get()->pWeakRef;
236 (*uno_exc)->pData= memExc.release();
237 return;
239 catch (System::Exception* e)
241 OUStringBuffer buf( 128 );
242 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(
243 "Unexspected exception during invocation of cli object. "
244 "Original message is: \n") );
245 buf.append(mapCliString(e->get_Message()));
246 throw BridgeRuntimeError( buf.makeStringAndClear() );
249 //convert out, in/out params
250 for (int nPos = 0; nPos < nParams; ++nPos )
252 typelib_MethodParameter const & param = params[ nPos ];
254 if (param.bOut)
258 map_to_uno(
259 uno_args[ nPos ], args[ nPos ], param.pTypeRef,
260 sal_True == param.bIn /* assign if inout */);
261 // out array
263 catch (...)
265 // cleanup uno pure out
266 for ( sal_Int32 n = 0; n < nPos; ++n )
268 typelib_MethodParameter const & param = params[ n ];
269 if (! param.bIn)
270 uno_type_destructData( uno_args[ n ], param.pTypeRef, 0 );
272 throw;
276 // return value
277 if (0 != return_type)
279 map_to_uno(
280 uno_ret, retInvoke, return_type, false /* no assign */);
282 // no exception occurred
283 *uno_exc = 0;
291 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */