LibreOffice Module cppuhelper (master) 1
exc_thrower.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <rtl/instance.hxx>
21#include <osl/diagnose.h>
22#include <sal/log.hxx>
23#include <uno/dispatcher.hxx>
24#include <uno/lbnames.h>
25#include <uno/mapping.hxx>
26#include <cppuhelper/detail/XExceptionThrower.hpp>
27#include <com/sun/star/ucb/InteractiveAugmentedIOException.hpp>
28#include <com/sun/star/ucb/NameClashException.hpp>
29#include <com/sun/star/uno/RuntimeException.hpp>
30
32
33using namespace ::osl;
34using namespace ::cppu;
35using namespace ::com::sun::star;
36using namespace ::com::sun::star::uno;
37
38namespace
39{
40
41using cppuhelper::detail::XExceptionThrower;
42
43
44struct ExceptionThrower : public uno_Interface, XExceptionThrower
45{
46 ExceptionThrower();
47
48 virtual ~ExceptionThrower() {}
49
50 static Type const & getCppuType()
51 {
53 }
54
55 // XInterface
56 virtual Any SAL_CALL queryInterface( Type const & type ) override;
57 virtual void SAL_CALL acquire() noexcept override;
58 virtual void SAL_CALL release() noexcept override;
59
60 // XExceptionThrower
61 virtual void SAL_CALL throwException( Any const & exc ) override;
62 virtual void SAL_CALL rethrowException() override;
63};
64
65extern "C"
66{
67
68
69void ExceptionThrower_acquire_release_nop(
70 SAL_UNUSED_PARAMETER uno_Interface * )
71{}
72
73
74void ExceptionThrower_dispatch(
75 uno_Interface * pUnoI, typelib_TypeDescription const * pMemberType,
76 void * pReturn, void * pArgs [], uno_Any ** ppException )
77{
78 OSL_ASSERT( pMemberType->eTypeClass == typelib_TypeClass_INTERFACE_METHOD );
79
80 switch (reinterpret_cast< typelib_InterfaceMemberTypeDescription * >(
81 const_cast< typelib_TypeDescription * >( pMemberType ) )->
82 nPosition)
83 {
84 case 0: // queryInterface()
85 {
86 Type const & rType_demanded =
87 *static_cast< Type const * >( pArgs[ 0 ] );
88 if (rType_demanded.equals( cppu::UnoType<XInterface>::get() ) ||
89 rType_demanded.equals( ExceptionThrower::getCppuType() ))
90 {
91 typelib_TypeDescription * pTD = nullptr;
92 TYPELIB_DANGER_GET( &pTD, rType_demanded.getTypeLibType() );
94 static_cast< uno_Any * >( pReturn ), &pUnoI, pTD, nullptr );
95 TYPELIB_DANGER_RELEASE( pTD );
96 }
97 else
98 {
100 static_cast< uno_Any * >( pReturn ), nullptr, nullptr, nullptr );
101 }
102 *ppException = nullptr;
103 break;
104 }
105 case 1: // acquire()
106 case 2: // release()
107 *ppException = nullptr;
108 break;
109 case 3: // throwException()
110 {
111 uno_Any * pAny = static_cast< uno_Any * >( pArgs[ 0 ] );
112 OSL_ASSERT( pAny->pType->eTypeClass == typelib_TypeClass_EXCEPTION );
113 uno_type_any_construct( *ppException, pAny->pData, pAny->pType, nullptr );
114 break;
115 }
116 default:
117 {
118 OSL_ASSERT( false );
119 RuntimeException exc( "not implemented!" );
121 *ppException, &exc, cppu::UnoType<decltype(exc)>::get().getTypeLibType(), nullptr );
122 break;
123 }
124 }
125}
126
127} // extern "C"
128
129
131{
132 if (type.equals( cppu::UnoType<XInterface>::get() ) ||
133 type.equals( ExceptionThrower::getCppuType() ))
134 {
135 XExceptionThrower * that = this;
136 return Any( &that, type );
137 }
138 return Any();
139}
140
141
142void ExceptionThrower::acquire() noexcept
143{
144}
145
146void ExceptionThrower::release() noexcept
147{
148}
149
150
151void ExceptionThrower::throwException( Any const & exc )
152{
153 OSL_FAIL( "unexpected!" );
155}
156
157
158void ExceptionThrower::rethrowException()
159{
160 throw;
161}
162
163
164ExceptionThrower::ExceptionThrower()
165{
166 uno_Interface::acquire = ExceptionThrower_acquire_release_nop;
167 uno_Interface::release = ExceptionThrower_acquire_release_nop;
168 uno_Interface::pDispatcher = ExceptionThrower_dispatch;
169}
170
171#if defined(IOS) || defined(ANDROID) || defined(EMSCRIPTEN)
172#define RETHROW_FAKE_EXCEPTIONS 1
173#else
174#define RETHROW_FAKE_EXCEPTIONS 0
175#endif
176
177class theExceptionThrower : public rtl::Static<ExceptionThrower, theExceptionThrower> {};
178
179#if RETHROW_FAKE_EXCEPTIONS
180// In the native iOS / Android app, where we don't have any Java, Python,
181// BASIC, or other scripting, the only thing that would use the C++/UNO bridge
182// functionality that invokes codeSnippet() was cppu::throwException().
183//
184// codeSnippet() is part of what corresponds to the code that uses
185// run-time-generated machine code on other platforms. We can't generate code
186// at run-time on iOS, that has been known forever.
187//
188// Instead of digging in and trying to understand what is wrong, another
189// solution was chosen. It turns out that the number of types of exception
190// objects thrown by cppu::throwException() is fairly small. During startup of
191// the LibreOffice code, and loading of an .odt document, only one kind of
192// exception is thrown this way... (The lovely
193// css::ucb:InteractiveAugmentedIOException.)
194//
195// So we can simply have code that checks what the type of object being thrown
196// is, and explicitly throws such an object then with a normal C++ throw
197// statement. Seems to work.
198template <class E> void tryThrow(css::uno::Any const& aException)
199{
200 E aSpecificException;
201 if (aException >>= aSpecificException)
202 throw aSpecificException;
203}
204
205void lo_mobile_throwException(css::uno::Any const& aException)
206{
207 assert(aException.getValueTypeClass() == css::uno::TypeClass_EXCEPTION);
208
209 tryThrow<css::ucb::InteractiveAugmentedIOException>(aException);
210 tryThrow<css::ucb::NameClashException>(aException);
211 tryThrow<css::uno::RuntimeException>(aException);
212
213 SAL_WARN("cppuhelper", "lo_mobile_throwException: Unhandled exception type: " << aException.getValueTypeName());
214
215 assert(false);
216}
217#endif // RETHROW_FAKE_EXCEPTIONS
218
219} // anonymous namespace
220
221
222namespace cppu
223{
224
225
226void SAL_CALL throwException( Any const & exc )
227{
228 if (exc.getValueTypeClass() != TypeClass_EXCEPTION)
229 {
230 throw RuntimeException(
231 "no UNO exception given "
232 "(must be derived from com::sun::star::uno::Exception)!" );
233 }
234
235#if RETHROW_FAKE_EXCEPTIONS
236 lo_mobile_throwException(exc);
237#else
238 Mapping uno2cpp(Environment(UNO_LB_UNO), Environment::getCurrent());
239 if (! uno2cpp.is())
240 {
241 throw RuntimeException(
242 "cannot get binary UNO to C++ mapping!" );
243 }
244
245 Reference< XExceptionThrower > xThrower;
246 uno2cpp.mapInterface(
247 reinterpret_cast< void ** >( &xThrower ),
248 static_cast< uno_Interface * >( &theExceptionThrower::get() ),
249 ExceptionThrower::getCppuType() );
250 OSL_ASSERT( xThrower.is() );
251 xThrower->throwException( exc );
252#endif // !RETHROW_FAKE_EXCEPTIONS
253}
254
255
257{
258 // why does this differ from RETHROW_FAKE_EXCEPTIONS?
259#if defined(ANDROID) || defined(EMSCRIPTEN)
260 return Any();
261#else
262 Mapping cpp2uno(Environment::getCurrent(), Environment(UNO_LB_UNO));
263 if (! cpp2uno.is())
264 {
265 throw RuntimeException(
266 "cannot get C++ to binary UNO mapping!" );
267 }
268 Mapping uno2cpp(Environment(UNO_LB_UNO), Environment::getCurrent());
269 if (! uno2cpp.is())
270 {
271 throw RuntimeException(
272 "cannot get binary UNO to C++ mapping!" );
273 }
274
275 typelib_TypeDescription * pTD = nullptr;
276 TYPELIB_DANGER_GET(
277 &pTD, ExceptionThrower::getCppuType().getTypeLibType() );
278
279 UnoInterfaceReference unoI;
280 cpp2uno.mapInterface(
281 reinterpret_cast< void ** >( &unoI.m_pUnoI ),
282 static_cast< XExceptionThrower * >( &theExceptionThrower::get() ), pTD );
283 OSL_ASSERT( unoI.is() );
284
285 typelib_TypeDescription * pMemberTD = nullptr;
286 TYPELIB_DANGER_GET(
287 &pMemberTD,
288 reinterpret_cast< typelib_InterfaceTypeDescription * >( pTD )->
289 ppMembers[ 1 ] /* rethrowException() */ );
290
291 uno_Any exc_mem;
292 uno_Any * exc = &exc_mem;
293 unoI.dispatch( pMemberTD, nullptr, nullptr, &exc );
294
295 TYPELIB_DANGER_RELEASE( pMemberTD );
296 TYPELIB_DANGER_RELEASE( pTD );
297
298 if (exc == nullptr)
299 {
300 throw RuntimeException( "rethrowing C++ exception failed!" );
301 }
302
303 Any ret;
304 uno_any_destruct( &ret, reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
306 &ret, exc->pData, exc->pType, uno2cpp.get() );
307 uno_any_destruct( exc, nullptr );
308 return ret;
309#endif
310}
311
312}
313
314/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SAL_CALL uno_type_any_constructAndConvert(uno_Any *pDest, void *pSource, typelib_TypeDescriptionReference *pType, uno_Mapping *mapping) SAL_THROW_EXTERN_C()
void SAL_CALL uno_any_construct(uno_Any *pDest, void *pSource, typelib_TypeDescription *pTypeDescr, uno_AcquireFunc acquire) SAL_THROW_EXTERN_C()
void SAL_CALL uno_any_destruct(uno_Any *pValue, uno_ReleaseFunc release) SAL_THROW_EXTERN_C()
void SAL_CALL uno_type_any_construct(uno_Any *pDest, void *pSource, typelib_TypeDescriptionReference *pType, uno_AcquireFunc acquire) SAL_THROW_EXTERN_C()
css::uno::Type const & get()
#define SAL_WARN(area, stream)
struct _typelib_TypeDescription typelib_TypeDescription
struct _uno_Any uno_Any
Type
css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType, Interface1 *p1)
Compares demanded type to given template argument types.
void SAL_CALL throwException(Any const &exc)
Any SAL_CALL getCaughtException()
Use this function to get the dynamic type of a caught C++-UNO exception; completes the above function...
ResultType type