update dev300-m58
[ooovba.git] / cppuhelper / source / exc_thrower.cxx
blob300b88992d361cd39ca810546aae224f409a757e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: exc_thrower.cxx,v $
10 * $Revision: 1.12 $
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;
53 namespace
56 using cppuhelper::detail::XExceptionThrower;
58 //==============================================================================
59 struct ExceptionThrower : public uno_Interface, XExceptionThrower
61 inline ExceptionThrower();
63 public:
64 static ExceptionThrower * get();
65 static inline Type const & getCppuType()
67 return ::getCppuType(
68 reinterpret_cast< Reference< XExceptionThrower > const * >(0) );
71 // XInterface
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 ();
77 // XExceptionThrower
78 virtual void SAL_CALL throwException( Any const & exc ) throw (Exception);
79 virtual void SAL_CALL rethrowException() throw (Exception);
82 extern "C"
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 ) )->
99 nPosition)
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() );
112 uno_any_construct(
113 reinterpret_cast< uno_Any * >( pReturn ), &pUnoI, pTD, 0 );
114 TYPELIB_DANGER_RELEASE( pTD );
116 else
118 uno_any_construct(
119 reinterpret_cast< uno_Any * >( pReturn ), 0, 0, 0 );
121 *ppException = 0;
122 break;
124 case 1: // acquire()
125 case 2: // release()
126 *ppException = 0;
127 break;
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 );
133 break;
135 default:
137 OSL_ASSERT( 0 );
138 RuntimeException exc(
139 OUSTR("not implemented!"), Reference< XInterface >() );
140 uno_type_any_construct(
141 *ppException, &exc, ::getCppuType( &exc ).getTypeLibType(), 0 );
142 break;
147 } // extern "C"
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 );
160 return Any();
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)
182 throw;
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;
197 if (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;
204 else
206 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
208 return s_pThrower;
211 } // anonymous namespace
214 namespace cppu
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());
229 if (! uno2cpp.is())
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)));
249 if (! cpp2uno.is())
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());
256 if (! uno2cpp.is())
258 throw RuntimeException(
259 OUSTR("cannot get binary UNO to C++ mapping!"),
260 Reference< XInterface >() );
263 typelib_TypeDescription * pTD = 0;
264 TYPELIB_DANGER_GET(
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;
274 TYPELIB_DANGER_GET(
275 &pMemberTD,
276 reinterpret_cast< typelib_InterfaceTypeDescription * >( pTD )->
277 ppMembers[ 1 ] /* rethrowException() */ );
279 uno_Any exc_mem;
280 uno_Any * exc = &exc_mem;
281 unoI.dispatch( pMemberTD, 0, 0, &exc );
283 TYPELIB_DANGER_RELEASE( pMemberTD );
284 TYPELIB_DANGER_RELEASE( pTD );
286 if (exc == 0)
288 throw RuntimeException(
289 OUSTR("rethrowing C++ exception failed!"),
290 Reference< XInterface >() );
293 Any ret;
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 );
298 return ret;