Bump version to 5.0-14
[LibreOffice.git] / cppuhelper / source / exc_thrower.cxx
blob177b1ba8872c5b054859a523e1aa9d8415e439c2
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 <osl/doublecheckedlocking.h>
24 #include <osl/mutex.hxx>
25 #include <uno/dispatcher.hxx>
26 #include <uno/lbnames.h>
27 #include <uno/mapping.hxx>
28 #include <cppuhelper/detail/XExceptionThrower.hpp>
29 #include <com/sun/star/uno/RuntimeException.hpp>
31 #include <cppuhelper/exc_hlp.hxx>
33 using namespace ::osl;
34 using namespace ::cppu;
35 using namespace ::com::sun::star;
36 using namespace ::com::sun::star::uno;
38 namespace
41 using cppuhelper::detail::XExceptionThrower;
44 struct ExceptionThrower : public uno_Interface, XExceptionThrower
46 inline ExceptionThrower();
48 virtual ~ExceptionThrower() {}
50 static inline Type const & getCppuType()
52 return cppu::UnoType<XExceptionThrower>::get();
55 // XInterface
56 virtual Any SAL_CALL queryInterface( Type const & type )
57 throw (RuntimeException, std::exception) SAL_OVERRIDE;
58 virtual void SAL_CALL acquire() throw () SAL_OVERRIDE;
59 virtual void SAL_CALL release() throw () SAL_OVERRIDE;
61 // XExceptionThrower
62 virtual void SAL_CALL throwException( Any const & exc ) throw (Exception, std::exception) SAL_OVERRIDE;
63 virtual void SAL_CALL rethrowException() throw (Exception, std::exception) SAL_OVERRIDE;
66 extern "C"
70 static void SAL_CALL ExceptionThrower_acquire_release_nop(
71 SAL_UNUSED_PARAMETER uno_Interface * )
75 static void SAL_CALL ExceptionThrower_dispatch(
76 uno_Interface * pUnoI, typelib_TypeDescription const * pMemberType,
77 void * pReturn, void * pArgs [], uno_Any ** ppException )
79 OSL_ASSERT( pMemberType->eTypeClass == typelib_TypeClass_INTERFACE_METHOD );
81 switch (reinterpret_cast< typelib_InterfaceMemberTypeDescription * >(
82 const_cast< typelib_TypeDescription * >( pMemberType ) )->
83 nPosition)
85 case 0: // queryInterace()
87 Type const & rType_demanded =
88 *static_cast< Type const * >( pArgs[ 0 ] );
89 if (rType_demanded.equals( cppu::UnoType<XInterface>::get() ) ||
90 rType_demanded.equals( ExceptionThrower::getCppuType() ))
92 typelib_TypeDescription * pTD = 0;
93 TYPELIB_DANGER_GET( &pTD, rType_demanded.getTypeLibType() );
94 uno_any_construct(
95 static_cast< uno_Any * >( pReturn ), &pUnoI, pTD, 0 );
96 TYPELIB_DANGER_RELEASE( pTD );
98 else
100 uno_any_construct(
101 static_cast< uno_Any * >( pReturn ), 0, 0, 0 );
103 *ppException = 0;
104 break;
106 case 1: // acquire()
107 case 2: // release()
108 *ppException = 0;
109 break;
110 case 3: // throwException()
112 uno_Any * pAny = static_cast< uno_Any * >( pArgs[ 0 ] );
113 OSL_ASSERT( pAny->pType->eTypeClass == typelib_TypeClass_EXCEPTION );
114 uno_type_any_construct( *ppException, pAny->pData, pAny->pType, 0 );
115 break;
117 default:
119 OSL_ASSERT( false );
120 RuntimeException exc( "not implemented!" );
121 uno_type_any_construct(
122 *ppException, &exc, cppu::UnoType<decltype(exc)>::get().getTypeLibType(), 0 );
123 break;
128 } // extern "C"
131 Any ExceptionThrower::queryInterface( Type const & type )
132 throw (RuntimeException, std::exception)
134 if (type.equals( cppu::UnoType<XInterface>::get() ) ||
135 type.equals( ExceptionThrower::getCppuType() ))
137 XExceptionThrower * that = static_cast< XExceptionThrower * >( 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 ) throw (Exception, std::exception)
155 OSL_FAIL( "unexpected!" );
156 cppu::throwException( exc );
160 void ExceptionThrower::rethrowException() throw (Exception, std::exception)
162 throw;
166 inline 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 Mapping uno2cpp(Environment(UNO_LB_UNO), Environment::getCurrent());
192 if (! uno2cpp.is())
194 throw RuntimeException(
195 "cannot get binary UNO to C++ mapping!" );
198 Reference< XExceptionThrower > xThrower;
199 uno2cpp.mapInterface(
200 reinterpret_cast< void ** >( &xThrower ),
201 static_cast< uno_Interface * >( &theExceptionThrower::get() ),
202 ExceptionThrower::getCppuType() );
203 OSL_ASSERT( xThrower.is() );
204 xThrower->throwException( exc );
208 Any SAL_CALL getCaughtException()
210 Mapping cpp2uno(Environment::getCurrent(), Environment(UNO_LB_UNO));
211 if (! cpp2uno.is())
213 throw RuntimeException(
214 "cannot get C++ to binary UNO mapping!" );
216 Mapping uno2cpp(Environment(UNO_LB_UNO), Environment::getCurrent());
217 if (! uno2cpp.is())
219 throw RuntimeException(
220 "cannot get binary UNO to C++ mapping!" );
223 typelib_TypeDescription * pTD = 0;
224 TYPELIB_DANGER_GET(
225 &pTD, ExceptionThrower::getCppuType().getTypeLibType() );
227 UnoInterfaceReference unoI;
228 cpp2uno.mapInterface(
229 reinterpret_cast< void ** >( &unoI.m_pUnoI ),
230 static_cast< XExceptionThrower * >( &theExceptionThrower::get() ), pTD );
231 OSL_ASSERT( unoI.is() );
233 typelib_TypeDescription * pMemberTD = 0;
234 TYPELIB_DANGER_GET(
235 &pMemberTD,
236 reinterpret_cast< typelib_InterfaceTypeDescription * >( pTD )->
237 ppMembers[ 1 ] /* rethrowException() */ );
239 uno_Any exc_mem;
240 uno_Any * exc = &exc_mem;
241 unoI.dispatch( pMemberTD, 0, 0, &exc );
243 TYPELIB_DANGER_RELEASE( pMemberTD );
244 TYPELIB_DANGER_RELEASE( pTD );
246 if (exc == 0)
248 throw RuntimeException( "rethrowing C++ exception failed!" );
251 Any ret;
252 uno_any_destruct( &ret, reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
253 uno_type_any_constructAndConvert(
254 &ret, exc->pData, exc->pType, uno2cpp.get() );
255 uno_any_destruct( exc, 0 );
256 return ret;
261 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */