1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <uno/dispatcher.hxx>
25 #include <uno/lbnames.h>
26 #include <uno/mapping.hxx>
27 #include <cppuhelper/detail/XExceptionThrower.hpp>
28 #include <com/sun/star/uno/RuntimeException.hpp>
30 #include <cppuhelper/exc_hlp.hxx>
32 using namespace ::osl
;
33 using namespace ::cppu
;
34 using namespace ::com::sun::star
;
35 using namespace ::com::sun::star::uno
;
40 using cppuhelper::detail::XExceptionThrower
;
43 struct ExceptionThrower
: public uno_Interface
, XExceptionThrower
45 inline ExceptionThrower();
47 virtual ~ExceptionThrower() {}
49 static Type
const & getCppuType()
51 return cppu::UnoType
<XExceptionThrower
>::get();
55 virtual Any SAL_CALL
queryInterface( Type
const & type
) override
;
56 virtual void SAL_CALL
acquire() throw () override
;
57 virtual void SAL_CALL
release() throw () override
;
60 virtual void SAL_CALL
throwException( Any
const & exc
) override
;
61 virtual void SAL_CALL
rethrowException() override
;
68 void ExceptionThrower_acquire_release_nop(
69 SAL_UNUSED_PARAMETER uno_Interface
* )
73 void ExceptionThrower_dispatch(
74 uno_Interface
* pUnoI
, typelib_TypeDescription
const * pMemberType
,
75 void * pReturn
, void * pArgs
[], uno_Any
** ppException
)
77 OSL_ASSERT( pMemberType
->eTypeClass
== typelib_TypeClass_INTERFACE_METHOD
);
79 switch (reinterpret_cast< typelib_InterfaceMemberTypeDescription
* >(
80 const_cast< typelib_TypeDescription
* >( pMemberType
) )->
83 case 0: // queryInterface()
85 Type
const & rType_demanded
=
86 *static_cast< Type
const * >( pArgs
[ 0 ] );
87 if (rType_demanded
.equals( cppu::UnoType
<XInterface
>::get() ) ||
88 rType_demanded
.equals( ExceptionThrower::getCppuType() ))
90 typelib_TypeDescription
* pTD
= nullptr;
91 TYPELIB_DANGER_GET( &pTD
, rType_demanded
.getTypeLibType() );
93 static_cast< uno_Any
* >( pReturn
), &pUnoI
, pTD
, nullptr );
94 TYPELIB_DANGER_RELEASE( pTD
);
99 static_cast< uno_Any
* >( pReturn
), nullptr, nullptr, nullptr );
101 *ppException
= nullptr;
106 *ppException
= nullptr;
108 case 3: // throwException()
110 uno_Any
* pAny
= static_cast< uno_Any
* >( pArgs
[ 0 ] );
111 OSL_ASSERT( pAny
->pType
->eTypeClass
== typelib_TypeClass_EXCEPTION
);
112 uno_type_any_construct( *ppException
, pAny
->pData
, pAny
->pType
, nullptr );
118 RuntimeException
exc( "not implemented!" );
119 uno_type_any_construct(
120 *ppException
, &exc
, cppu::UnoType
<decltype(exc
)>::get().getTypeLibType(), nullptr );
129 Any
ExceptionThrower::queryInterface( Type
const & type
)
131 if (type
.equals( cppu::UnoType
<XInterface
>::get() ) ||
132 type
.equals( ExceptionThrower::getCppuType() ))
134 XExceptionThrower
* that
= static_cast< XExceptionThrower
* >( this );
135 return Any( &that
, type
);
141 void ExceptionThrower::acquire() throw ()
145 void ExceptionThrower::release() throw ()
150 void ExceptionThrower::throwException( Any
const & exc
)
152 OSL_FAIL( "unexpected!" );
153 cppu::throwException( exc
);
157 void ExceptionThrower::rethrowException()
163 inline ExceptionThrower::ExceptionThrower()
165 uno_Interface::acquire
= ExceptionThrower_acquire_release_nop
;
166 uno_Interface::release
= ExceptionThrower_acquire_release_nop
;
167 uno_Interface::pDispatcher
= ExceptionThrower_dispatch
;
170 class theExceptionThrower
: public rtl::Static
<ExceptionThrower
, theExceptionThrower
> {};
172 } // anonymous namespace
179 void SAL_CALL
throwException( Any
const & exc
)
181 if (exc
.getValueTypeClass() != TypeClass_EXCEPTION
)
183 throw RuntimeException(
184 "no UNO exception given "
185 "(must be derived from com::sun::star::uno::Exception)!" );
188 Mapping
uno2cpp(Environment(UNO_LB_UNO
), Environment::getCurrent());
191 throw RuntimeException(
192 "cannot get binary UNO to C++ mapping!" );
195 Reference
< XExceptionThrower
> xThrower
;
196 uno2cpp
.mapInterface(
197 reinterpret_cast< void ** >( &xThrower
),
198 static_cast< uno_Interface
* >( &theExceptionThrower::get() ),
199 ExceptionThrower::getCppuType() );
200 OSL_ASSERT( xThrower
.is() );
201 xThrower
->throwException( exc
);
205 Any SAL_CALL
getCaughtException()
207 Mapping
cpp2uno(Environment::getCurrent(), Environment(UNO_LB_UNO
));
210 throw RuntimeException(
211 "cannot get C++ to binary UNO mapping!" );
213 Mapping
uno2cpp(Environment(UNO_LB_UNO
), Environment::getCurrent());
216 throw RuntimeException(
217 "cannot get binary UNO to C++ mapping!" );
220 typelib_TypeDescription
* pTD
= nullptr;
222 &pTD
, ExceptionThrower::getCppuType().getTypeLibType() );
224 UnoInterfaceReference unoI
;
225 cpp2uno
.mapInterface(
226 reinterpret_cast< void ** >( &unoI
.m_pUnoI
),
227 static_cast< XExceptionThrower
* >( &theExceptionThrower::get() ), pTD
);
228 OSL_ASSERT( unoI
.is() );
230 typelib_TypeDescription
* pMemberTD
= nullptr;
233 reinterpret_cast< typelib_InterfaceTypeDescription
* >( pTD
)->
234 ppMembers
[ 1 ] /* rethrowException() */ );
237 uno_Any
* exc
= &exc_mem
;
238 unoI
.dispatch( pMemberTD
, nullptr, nullptr, &exc
);
240 TYPELIB_DANGER_RELEASE( pMemberTD
);
241 TYPELIB_DANGER_RELEASE( pTD
);
245 throw RuntimeException( "rethrowing C++ exception failed!" );
249 uno_any_destruct( &ret
, reinterpret_cast< uno_ReleaseFunc
>(cpp_release
) );
250 uno_type_any_constructAndConvert(
251 &ret
, exc
->pData
, exc
->pType
, uno2cpp
.get() );
252 uno_any_destruct( exc
, nullptr );
258 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */