1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: exc_thrower.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_cppuhelper.hxx"
34 #include "osl/diagnose.h"
35 #include "osl/doublecheckedlocking.h"
36 #include "osl/mutex.hxx"
37 #include "uno/dispatcher.hxx"
38 #include "uno/mapping.hxx"
39 #include "cppuhelper/detail/XExceptionThrower.hpp"
40 #include "com/sun/star/uno/RuntimeException.hpp"
42 #include "cppuhelper/exc_hlp.hxx"
44 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
47 using namespace ::rtl
;
48 using namespace ::osl
;
49 using namespace ::cppu
;
50 using namespace ::com::sun::star
;
51 using namespace ::com::sun::star::uno
;
56 using cppuhelper::detail::XExceptionThrower
;
58 //==============================================================================
59 struct ExceptionThrower
: public uno_Interface
, XExceptionThrower
61 inline ExceptionThrower();
64 static ExceptionThrower
* get();
65 static inline Type
const & getCppuType()
68 reinterpret_cast< Reference
< XExceptionThrower
> const * >(0) );
72 virtual Any SAL_CALL
queryInterface( Type
const & type
)
73 throw (RuntimeException
);
74 virtual void SAL_CALL
acquire() throw ();
75 virtual void SAL_CALL
release() throw ();
78 virtual void SAL_CALL
throwException( Any
const & exc
) throw (Exception
);
79 virtual void SAL_CALL
rethrowException() throw (Exception
);
85 //------------------------------------------------------------------------------
86 static void SAL_CALL
ExceptionThrower_acquire_release_nop( uno_Interface
* )
90 //------------------------------------------------------------------------------
91 static void SAL_CALL
ExceptionThrower_dispatch(
92 uno_Interface
* pUnoI
, typelib_TypeDescription
const * pMemberType
,
93 void * pReturn
, void * pArgs
[], uno_Any
** ppException
)
95 OSL_ASSERT( pMemberType
->eTypeClass
== typelib_TypeClass_INTERFACE_METHOD
);
97 switch (reinterpret_cast< typelib_InterfaceMemberTypeDescription
* >(
98 const_cast< typelib_TypeDescription
* >( pMemberType
) )->
101 case 0: // queryInterace()
103 Type
const & rType_demanded
=
104 *reinterpret_cast< Type
const * >( pArgs
[ 0 ] );
105 if (rType_demanded
.equals(
106 ::getCppuType( reinterpret_cast<
107 Reference
< XInterface
> const * >(0) ) ) ||
108 rType_demanded
.equals( ExceptionThrower::getCppuType() ))
110 typelib_TypeDescription
* pTD
= 0;
111 TYPELIB_DANGER_GET( &pTD
, rType_demanded
.getTypeLibType() );
113 reinterpret_cast< uno_Any
* >( pReturn
), &pUnoI
, pTD
, 0 );
114 TYPELIB_DANGER_RELEASE( pTD
);
119 reinterpret_cast< uno_Any
* >( pReturn
), 0, 0, 0 );
128 case 3: // throwException()
130 uno_Any
* pAny
= reinterpret_cast< uno_Any
* >( pArgs
[ 0 ] );
131 OSL_ASSERT( pAny
->pType
->eTypeClass
== typelib_TypeClass_EXCEPTION
);
132 uno_type_any_construct( *ppException
, pAny
->pData
, pAny
->pType
, 0 );
138 RuntimeException
exc(
139 OUSTR("not implemented!"), Reference
< XInterface
>() );
140 uno_type_any_construct(
141 *ppException
, &exc
, ::getCppuType( &exc
).getTypeLibType(), 0 );
149 //______________________________________________________________________________
150 Any
ExceptionThrower::queryInterface( Type
const & type
)
151 throw (RuntimeException
)
153 if (type
.equals( ::getCppuType( reinterpret_cast<
154 Reference
< XInterface
> const * >(0) ) ) ||
155 type
.equals( ExceptionThrower::getCppuType() ))
157 XExceptionThrower
* that
= static_cast< XExceptionThrower
* >( this );
158 return Any( &that
, type
);
163 //______________________________________________________________________________
164 void ExceptionThrower::acquire() throw ()
167 //______________________________________________________________________________
168 void ExceptionThrower::release() throw ()
172 //______________________________________________________________________________
173 void ExceptionThrower::throwException( Any
const & exc
) throw (Exception
)
175 OSL_ENSURE( 0, "unexpected!" );
176 throwException( exc
);
179 //______________________________________________________________________________
180 void ExceptionThrower::rethrowException() throw (Exception
)
185 //______________________________________________________________________________
186 inline ExceptionThrower::ExceptionThrower()
188 uno_Interface::acquire
= ExceptionThrower_acquire_release_nop
;
189 uno_Interface::release
= ExceptionThrower_acquire_release_nop
;
190 uno_Interface::pDispatcher
= ExceptionThrower_dispatch
;
193 //______________________________________________________________________________
194 ExceptionThrower
* ExceptionThrower::get()
196 ExceptionThrower
* s_pThrower
= 0;
199 MutexGuard
guard( Mutex::getGlobalMutex() );
200 static ExceptionThrower s_thrower
;
201 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
202 s_pThrower
= &s_thrower
;
206 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
211 } // anonymous namespace
217 //==============================================================================
218 void SAL_CALL
throwException( Any
const & exc
) SAL_THROW( (Exception
) )
220 if (exc
.getValueTypeClass() != TypeClass_EXCEPTION
)
222 throw RuntimeException(
223 OUSTR("no UNO exception given "
224 "(must be derived from com::sun::star::uno::Exception)!"),
225 Reference
< XInterface
>() );
228 Mapping
uno2cpp(Environment(OUSTR(UNO_LB_UNO
)), Environment::getCurrent());
231 throw RuntimeException(
232 OUSTR("cannot get binary UNO to C++ mapping!"),
233 Reference
< XInterface
>() );
236 Reference
< XExceptionThrower
> xThrower
;
237 uno2cpp
.mapInterface(
238 reinterpret_cast< void ** >( &xThrower
),
239 static_cast< uno_Interface
* >( ExceptionThrower::get() ),
240 ExceptionThrower::getCppuType() );
241 OSL_ASSERT( xThrower
.is() );
242 xThrower
->throwException( exc
);
245 //==============================================================================
246 Any SAL_CALL
getCaughtException()
248 Mapping
cpp2uno(Environment::getCurrent(), Environment(OUSTR(UNO_LB_UNO
)));
251 throw RuntimeException(
252 OUSTR("cannot get C++ to binary UNO mapping!"),
253 Reference
< XInterface
>() );
255 Mapping
uno2cpp(Environment(OUSTR(UNO_LB_UNO
)), Environment::getCurrent());
258 throw RuntimeException(
259 OUSTR("cannot get binary UNO to C++ mapping!"),
260 Reference
< XInterface
>() );
263 typelib_TypeDescription
* pTD
= 0;
265 &pTD
, ExceptionThrower::getCppuType().getTypeLibType() );
267 UnoInterfaceReference unoI
;
268 cpp2uno
.mapInterface(
269 reinterpret_cast< void ** >( &unoI
.m_pUnoI
),
270 static_cast< XExceptionThrower
* >( ExceptionThrower::get() ), pTD
);
271 OSL_ASSERT( unoI
.is() );
273 typelib_TypeDescription
* pMemberTD
= 0;
276 reinterpret_cast< typelib_InterfaceTypeDescription
* >( pTD
)->
277 ppMembers
[ 1 ] /* rethrowException() */ );
280 uno_Any
* exc
= &exc_mem
;
281 unoI
.dispatch( pMemberTD
, 0, 0, &exc
);
283 TYPELIB_DANGER_RELEASE( pMemberTD
);
284 TYPELIB_DANGER_RELEASE( pTD
);
288 throw RuntimeException(
289 OUSTR("rethrowing C++ exception failed!"),
290 Reference
< XInterface
>() );
294 uno_any_destruct( &ret
, reinterpret_cast< uno_ReleaseFunc
>(cpp_release
) );
295 uno_type_any_constructAndConvert(
296 &ret
, exc
->pData
, exc
->pType
, uno2cpp
.get() );
297 uno_any_destruct( exc
, 0 );