Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / cppuhelper / source / exc_thrower.cxx
blob5e029feae98252fb9035d8e3bbc1ea029f033c11
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <rtl/instance.hxx>
22 #include <osl/diagnose.h>
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/uno/RuntimeException.hpp>
29 #include <cppuhelper/exc_hlp.hxx>
31 #ifdef IOS
32 #include <ios/ios.hxx>
33 #endif
35 using namespace ::osl;
36 using namespace ::cppu;
37 using namespace ::com::sun::star;
38 using namespace ::com::sun::star::uno;
40 namespace
43 using cppuhelper::detail::XExceptionThrower;
46 struct ExceptionThrower : public uno_Interface, XExceptionThrower
48 ExceptionThrower();
50 virtual ~ExceptionThrower() {}
52 static Type const & getCppuType()
54 return cppu::UnoType<XExceptionThrower>::get();
57 // XInterface
58 virtual Any SAL_CALL queryInterface( Type const & type ) override;
59 virtual void SAL_CALL acquire() throw () override;
60 virtual void SAL_CALL release() throw () override;
62 // XExceptionThrower
63 virtual void SAL_CALL throwException( Any const & exc ) override;
64 virtual void SAL_CALL rethrowException() override;
67 extern "C"
71 void ExceptionThrower_acquire_release_nop(
72 SAL_UNUSED_PARAMETER uno_Interface * )
76 void ExceptionThrower_dispatch(
77 uno_Interface * pUnoI, typelib_TypeDescription const * pMemberType,
78 void * pReturn, void * pArgs [], uno_Any ** ppException )
80 OSL_ASSERT( pMemberType->eTypeClass == typelib_TypeClass_INTERFACE_METHOD );
82 switch (reinterpret_cast< typelib_InterfaceMemberTypeDescription * >(
83 const_cast< typelib_TypeDescription * >( pMemberType ) )->
84 nPosition)
86 case 0: // queryInterface()
88 Type const & rType_demanded =
89 *static_cast< Type const * >( pArgs[ 0 ] );
90 if (rType_demanded.equals( cppu::UnoType<XInterface>::get() ) ||
91 rType_demanded.equals( ExceptionThrower::getCppuType() ))
93 typelib_TypeDescription * pTD = nullptr;
94 TYPELIB_DANGER_GET( &pTD, rType_demanded.getTypeLibType() );
95 uno_any_construct(
96 static_cast< uno_Any * >( pReturn ), &pUnoI, pTD, nullptr );
97 TYPELIB_DANGER_RELEASE( pTD );
99 else
101 uno_any_construct(
102 static_cast< uno_Any * >( pReturn ), nullptr, nullptr, nullptr );
104 *ppException = nullptr;
105 break;
107 case 1: // acquire()
108 case 2: // release()
109 *ppException = nullptr;
110 break;
111 case 3: // throwException()
113 uno_Any * pAny = static_cast< uno_Any * >( pArgs[ 0 ] );
114 OSL_ASSERT( pAny->pType->eTypeClass == typelib_TypeClass_EXCEPTION );
115 uno_type_any_construct( *ppException, pAny->pData, pAny->pType, nullptr );
116 break;
118 default:
120 OSL_ASSERT( false );
121 RuntimeException exc( "not implemented!" );
122 uno_type_any_construct(
123 *ppException, &exc, cppu::UnoType<decltype(exc)>::get().getTypeLibType(), nullptr );
124 break;
129 } // extern "C"
132 Any ExceptionThrower::queryInterface( Type const & type )
134 if (type.equals( cppu::UnoType<XInterface>::get() ) ||
135 type.equals( ExceptionThrower::getCppuType() ))
137 XExceptionThrower * that = this;
138 return Any( &that, type );
140 return Any();
144 void ExceptionThrower::acquire() throw ()
148 void ExceptionThrower::release() throw ()
153 void ExceptionThrower::throwException( Any const & exc )
155 OSL_FAIL( "unexpected!" );
156 cppu::throwException( exc );
160 void ExceptionThrower::rethrowException()
162 throw;
166 ExceptionThrower::ExceptionThrower()
168 uno_Interface::acquire = ExceptionThrower_acquire_release_nop;
169 uno_Interface::release = ExceptionThrower_acquire_release_nop;
170 uno_Interface::pDispatcher = ExceptionThrower_dispatch;
173 class theExceptionThrower : public rtl::Static<ExceptionThrower, theExceptionThrower> {};
175 } // anonymous namespace
178 namespace cppu
182 void SAL_CALL throwException( Any const & exc )
184 if (exc.getValueTypeClass() != TypeClass_EXCEPTION)
186 throw RuntimeException(
187 "no UNO exception given "
188 "(must be derived from com::sun::star::uno::Exception)!" );
191 #ifdef IOS
192 lo_ios_throwException(exc);
193 #else
194 Mapping uno2cpp(Environment(UNO_LB_UNO), Environment::getCurrent());
195 if (! uno2cpp.is())
197 throw RuntimeException(
198 "cannot get binary UNO to C++ mapping!" );
201 Reference< XExceptionThrower > xThrower;
202 uno2cpp.mapInterface(
203 reinterpret_cast< void ** >( &xThrower ),
204 static_cast< uno_Interface * >( &theExceptionThrower::get() ),
205 ExceptionThrower::getCppuType() );
206 OSL_ASSERT( xThrower.is() );
207 xThrower->throwException( exc );
208 #endif
212 Any SAL_CALL getCaughtException()
214 Mapping cpp2uno(Environment::getCurrent(), Environment(UNO_LB_UNO));
215 if (! cpp2uno.is())
217 throw RuntimeException(
218 "cannot get C++ to binary UNO mapping!" );
220 Mapping uno2cpp(Environment(UNO_LB_UNO), Environment::getCurrent());
221 if (! uno2cpp.is())
223 throw RuntimeException(
224 "cannot get binary UNO to C++ mapping!" );
227 typelib_TypeDescription * pTD = nullptr;
228 TYPELIB_DANGER_GET(
229 &pTD, ExceptionThrower::getCppuType().getTypeLibType() );
231 UnoInterfaceReference unoI;
232 cpp2uno.mapInterface(
233 reinterpret_cast< void ** >( &unoI.m_pUnoI ),
234 static_cast< XExceptionThrower * >( &theExceptionThrower::get() ), pTD );
235 OSL_ASSERT( unoI.is() );
237 typelib_TypeDescription * pMemberTD = nullptr;
238 TYPELIB_DANGER_GET(
239 &pMemberTD,
240 reinterpret_cast< typelib_InterfaceTypeDescription * >( pTD )->
241 ppMembers[ 1 ] /* rethrowException() */ );
243 uno_Any exc_mem;
244 uno_Any * exc = &exc_mem;
245 unoI.dispatch( pMemberTD, nullptr, nullptr, &exc );
247 TYPELIB_DANGER_RELEASE( pMemberTD );
248 TYPELIB_DANGER_RELEASE( pTD );
250 if (exc == nullptr)
252 throw RuntimeException( "rethrowing C++ exception failed!" );
255 Any ret;
256 uno_any_destruct( &ret, reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
257 uno_type_any_constructAndConvert(
258 &ret, exc->pData, exc->pType, uno2cpp.get() );
259 uno_any_destruct( exc, nullptr );
260 return ret;
265 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */