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 
31 #include <cppuhelper/exc_hlp.hxx>
32 
33 using namespace ::osl;
34 using namespace ::cppu;
35 using namespace ::com::sun::star;
36 using namespace ::com::sun::star::uno;
37 
38 namespace
39 {
40 
41 using cppuhelper::detail::XExceptionThrower;
42 
43 
44 struct 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() throw () override;
58  virtual void SAL_CALL release() throw () override;
59 
60  // XExceptionThrower
61  virtual void SAL_CALL throwException( Any const & exc ) override;
62  virtual void SAL_CALL rethrowException() override;
63 };
64 
65 extern "C"
66 {
67 
68 
69 void ExceptionThrower_acquire_release_nop(
70  SAL_UNUSED_PARAMETER uno_Interface * )
71 {}
72 
73 
74 void 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 
142 void ExceptionThrower::acquire() throw ()
143 {
144 }
145 
146 void ExceptionThrower::release() throw ()
147 {
148 }
149 
150 
151 void ExceptionThrower::throwException( Any const & exc )
152 {
153  OSL_FAIL( "unexpected!" );
154  cppu::throwException( exc );
155 }
156 
157 
158 void ExceptionThrower::rethrowException()
159 {
160  throw;
161 }
162 
163 
164 ExceptionThrower::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 class theExceptionThrower : public rtl::Static<ExceptionThrower, theExceptionThrower> {};
172 
173 #if defined(IOS) || (defined(__aarch64__) && defined(ANDROID))
174 // In the native iOS / Android app, where we don't have any Java, Python,
175 // BASIC, or other scripting, the only thing that would use the C++/UNO bridge
176 // functionality that invokes codeSnippet() was cppu::throwException().
177 //
178 // codeSnippet() is part of what corresponds to the code that uses
179 // run-time-generated machine code on other platforms. We can't generate code
180 // at run-time on iOS, that has been known forever.
181 //
182 // Instead of digging in and trying to understand what is wrong, another
183 // solution was chosen. It turns out that the number of types of exception
184 // objects thrown by cppu::throwException() is fairly small. During startup of
185 // the LibreOffice code, and loading of an .odt document, only one kind of
186 // exception is thrown this way... (The lovely
187 // css::ucb:InteractiveAugmentedIOException.)
188 //
189 // So we can simply have code that checks what the type of object being thrown
190 // is, and explicitly throws such an object then with a normal C++ throw
191 // statement. Seems to work.
192 template <class E> void tryThrow(css::uno::Any const& aException)
193 {
194  E aSpecificException;
195  if (aException >>= aSpecificException)
196  throw aSpecificException;
197 }
198 
199 void lo_mobile_throwException(css::uno::Any const& aException)
200 {
201  assert(aException.getValueTypeClass() == css::uno::TypeClass_EXCEPTION);
202 
203  tryThrow<css::ucb::InteractiveAugmentedIOException>(aException);
204  tryThrow<css::ucb::NameClashException>(aException);
205  tryThrow<css::uno::RuntimeException>(aException);
206 
207  SAL_WARN("cppuhelper", "lo_mobile_throwException: Unhandled exception type: " << aException.getValueTypeName());
208 
209  assert(false);
210 }
211 #endif // defined(IOS) || (defined(__aarch64__) && defined(ANDROID))
212 
213 } // anonymous namespace
214 
215 
216 namespace cppu
217 {
218 
219 
220 void SAL_CALL throwException( Any const & exc )
221 {
222  if (exc.getValueTypeClass() != TypeClass_EXCEPTION)
223  {
224  throw RuntimeException(
225  "no UNO exception given "
226  "(must be derived from com::sun::star::uno::Exception)!" );
227  }
228 
229 #if defined(IOS) || (defined(__aarch64__) && defined(ANDROID))
230  lo_mobile_throwException(exc);
231 #else
232  Mapping uno2cpp(Environment(UNO_LB_UNO), Environment::getCurrent());
233  if (! uno2cpp.is())
234  {
235  throw RuntimeException(
236  "cannot get binary UNO to C++ mapping!" );
237  }
238 
239  Reference< XExceptionThrower > xThrower;
240  uno2cpp.mapInterface(
241  reinterpret_cast< void ** >( &xThrower ),
242  static_cast< uno_Interface * >( &theExceptionThrower::get() ),
243  ExceptionThrower::getCppuType() );
244  OSL_ASSERT( xThrower.is() );
245  xThrower->throwException( exc );
246 #endif
247 }
248 
249 
251 {
252 #if defined(__aarch64__) && defined(ANDROID)
253  // FIXME This stuff works on 32bit ARM, let's use the shortcut only for
254  // the 64bit ARM.
255  return Any();
256 #else
257  Mapping cpp2uno(Environment::getCurrent(), Environment(UNO_LB_UNO));
258  if (! cpp2uno.is())
259  {
260  throw RuntimeException(
261  "cannot get C++ to binary UNO mapping!" );
262  }
263  Mapping uno2cpp(Environment(UNO_LB_UNO), Environment::getCurrent());
264  if (! uno2cpp.is())
265  {
266  throw RuntimeException(
267  "cannot get binary UNO to C++ mapping!" );
268  }
269 
270  typelib_TypeDescription * pTD = nullptr;
271  TYPELIB_DANGER_GET(
272  &pTD, ExceptionThrower::getCppuType().getTypeLibType() );
273 
274  UnoInterfaceReference unoI;
275  cpp2uno.mapInterface(
276  reinterpret_cast< void ** >( &unoI.m_pUnoI ),
277  static_cast< XExceptionThrower * >( &theExceptionThrower::get() ), pTD );
278  OSL_ASSERT( unoI.is() );
279 
280  typelib_TypeDescription * pMemberTD = nullptr;
281  TYPELIB_DANGER_GET(
282  &pMemberTD,
283  reinterpret_cast< typelib_InterfaceTypeDescription * >( pTD )->
284  ppMembers[ 1 ] /* rethrowException() */ );
285 
286  uno_Any exc_mem;
287  uno_Any * exc = &exc_mem;
288  unoI.dispatch( pMemberTD, nullptr, nullptr, &exc );
289 
290  TYPELIB_DANGER_RELEASE( pMemberTD );
291  TYPELIB_DANGER_RELEASE( pTD );
292 
293  if (exc == nullptr)
294  {
295  throw RuntimeException( "rethrowing C++ exception failed!" );
296  }
297 
298  Any ret;
299  uno_any_destruct( &ret, reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
301  &ret, exc->pData, exc->pType, uno2cpp.get() );
302  uno_any_destruct( exc, nullptr );
303  return ret;
304 #endif
305 }
306 
307 }
308 
309 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Type
void SAL_CALL throwException(Any const &exc)
void SAL_CALL uno_type_any_construct(uno_Any *pDest, void *pSource, typelib_TypeDescriptionReference *pType, uno_AcquireFunc acquire) 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_type_any_constructAndConvert(uno_Any *pDest, void *pSource, typelib_TypeDescriptionReference *pType, uno_Mapping *mapping) SAL_THROW_EXTERN_C()
Any SAL_CALL getCaughtException()
Use this function to get the dynamic type of a caught C++-UNO exception; completes the above function...
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
struct _uno_Any uno_Any
void SAL_CALL uno_any_destruct(uno_Any *pValue, uno_ReleaseFunc release) SAL_THROW_EXTERN_C()
css::uno::Type const & get()
struct _typelib_TypeDescription typelib_TypeDescription
#define SAL_WARN(area, stream)
css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType, Interface1 *p1)
Compares demanded type to given template argument types.