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 <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
;
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();
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
;
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
;
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
) )->
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() );
95 static_cast< uno_Any
* >( pReturn
), &pUnoI
, pTD
, 0 );
96 TYPELIB_DANGER_RELEASE( pTD
);
101 static_cast< uno_Any
* >( pReturn
), 0, 0, 0 );
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 );
120 RuntimeException
exc( "not implemented!" );
121 uno_type_any_construct(
122 *ppException
, &exc
, cppu::UnoType
<decltype(exc
)>::get().getTypeLibType(), 0 );
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
);
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
)
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
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());
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
));
213 throw RuntimeException(
214 "cannot get C++ to binary UNO mapping!" );
216 Mapping
uno2cpp(Environment(UNO_LB_UNO
), Environment::getCurrent());
219 throw RuntimeException(
220 "cannot get binary UNO to C++ mapping!" );
223 typelib_TypeDescription
* pTD
= 0;
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;
236 reinterpret_cast< typelib_InterfaceTypeDescription
* >( pTD
)->
237 ppMembers
[ 1 ] /* rethrowException() */ );
240 uno_Any
* exc
= &exc_mem
;
241 unoI
.dispatch( pMemberTD
, 0, 0, &exc
);
243 TYPELIB_DANGER_RELEASE( pMemberTD
);
244 TYPELIB_DANGER_RELEASE( pTD
);
248 throw RuntimeException( "rethrowing C++ exception failed!" );
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 );
261 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */