1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: cli_uno.cxx,v $
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"
37 #include "cli_bridge.h"
39 namespace sr
=System::Reflection
;
40 namespace css
=com::sun::star
;
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
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.
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 )
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
);
115 uno_args
[ nPos
],args
[nPos
] , type
, false /* no assign */);
119 // cleanup uno in args
120 for (sal_Int32 n
= 0; n
< nPos
; ++n
)
122 typelib_MethodParameter
const & param
= pParams
[n
];
125 uno_type_destructData(uno_args
[n
], param
.pTypeRef
, 0);
132 uno_Any uno_exc_holder
;
133 uno_Any
* uno_exc
= &uno_exc_holder
;
136 (*pUnoI
->pDispatcher
)( pUnoI
, member_td
, uno_ret
, uno_args
, &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
;
150 &args
[nPos
], uno_args
[nPos
], param
.pTypeRef
,
151 argTypes
!= NULL
? argTypes
[nPos
] : NULL
, false );
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 );
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
;
181 &cli_ret
, uno_ret
, return_type
, 0, false);
182 uno_type_destructData(uno_ret
, return_type
, 0);
187 uno_type_destructData(uno_ret
, return_type
, 0);
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
];
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);
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
];
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();
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
];
263 uno_args
[ nPos
], args
[ nPos
], param
.pTypeRef
,
264 sal_True
== param
.bIn
/* assign if inout */);
269 // cleanup uno pure out
270 for ( sal_Int32 n
= 0; n
< nPos
; ++n
)
272 typelib_MethodParameter
const & param
= params
[ n
];
274 uno_type_destructData( uno_args
[ n
], param
.pTypeRef
, 0 );
281 if (0 != return_type
)
284 uno_ret
, retInvoke
, return_type
, false /* no assign */);
286 // no exception occured