1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_cppuhelper.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "osl/diagnose.h"
32*cdf0e10cSrcweir #include "osl/doublecheckedlocking.h"
33*cdf0e10cSrcweir #include "osl/mutex.hxx"
34*cdf0e10cSrcweir #include "uno/dispatcher.hxx"
35*cdf0e10cSrcweir #include "uno/mapping.hxx"
36*cdf0e10cSrcweir #include "cppuhelper/detail/XExceptionThrower.hpp"
37*cdf0e10cSrcweir #include "com/sun/star/uno/RuntimeException.hpp"
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir #include "cppuhelper/exc_hlp.hxx"
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir using namespace ::rtl;
45*cdf0e10cSrcweir using namespace ::osl;
46*cdf0e10cSrcweir using namespace ::cppu;
47*cdf0e10cSrcweir using namespace ::com::sun::star;
48*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir namespace
51*cdf0e10cSrcweir {
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir using cppuhelper::detail::XExceptionThrower;
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir //==============================================================================
56*cdf0e10cSrcweir struct ExceptionThrower : public uno_Interface, XExceptionThrower
57*cdf0e10cSrcweir {
58*cdf0e10cSrcweir     inline ExceptionThrower();
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir public:
61*cdf0e10cSrcweir     static ExceptionThrower * get();
62*cdf0e10cSrcweir     static inline Type const & getCppuType()
63*cdf0e10cSrcweir     {
64*cdf0e10cSrcweir         return ::getCppuType(
65*cdf0e10cSrcweir             reinterpret_cast< Reference< XExceptionThrower > const * >(0) );
66*cdf0e10cSrcweir     }
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir     // XInterface
69*cdf0e10cSrcweir     virtual Any SAL_CALL queryInterface( Type const & type )
70*cdf0e10cSrcweir         throw (RuntimeException);
71*cdf0e10cSrcweir     virtual void SAL_CALL acquire() throw ();
72*cdf0e10cSrcweir     virtual void SAL_CALL release() throw ();
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir     // XExceptionThrower
75*cdf0e10cSrcweir     virtual void SAL_CALL throwException( Any const & exc ) throw (Exception);
76*cdf0e10cSrcweir     virtual void SAL_CALL rethrowException() throw (Exception);
77*cdf0e10cSrcweir };
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir extern "C"
80*cdf0e10cSrcweir {
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir //------------------------------------------------------------------------------
83*cdf0e10cSrcweir static void SAL_CALL ExceptionThrower_acquire_release_nop( uno_Interface * )
84*cdf0e10cSrcweir {
85*cdf0e10cSrcweir }
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir //------------------------------------------------------------------------------
88*cdf0e10cSrcweir static void SAL_CALL ExceptionThrower_dispatch(
89*cdf0e10cSrcweir     uno_Interface * pUnoI, typelib_TypeDescription const * pMemberType,
90*cdf0e10cSrcweir     void * pReturn, void * pArgs [], uno_Any ** ppException )
91*cdf0e10cSrcweir {
92*cdf0e10cSrcweir     OSL_ASSERT( pMemberType->eTypeClass == typelib_TypeClass_INTERFACE_METHOD );
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir     switch (reinterpret_cast< typelib_InterfaceMemberTypeDescription * >(
95*cdf0e10cSrcweir                 const_cast< typelib_TypeDescription * >( pMemberType ) )->
96*cdf0e10cSrcweir             nPosition)
97*cdf0e10cSrcweir     {
98*cdf0e10cSrcweir     case 0: // queryInterace()
99*cdf0e10cSrcweir     {
100*cdf0e10cSrcweir         Type const & rType_demanded =
101*cdf0e10cSrcweir             *reinterpret_cast< Type const * >( pArgs[ 0 ] );
102*cdf0e10cSrcweir         if (rType_demanded.equals(
103*cdf0e10cSrcweir                 ::getCppuType( reinterpret_cast<
104*cdf0e10cSrcweir                                Reference< XInterface > const * >(0) ) ) ||
105*cdf0e10cSrcweir             rType_demanded.equals( ExceptionThrower::getCppuType() ))
106*cdf0e10cSrcweir         {
107*cdf0e10cSrcweir             typelib_TypeDescription * pTD = 0;
108*cdf0e10cSrcweir             TYPELIB_DANGER_GET( &pTD, rType_demanded.getTypeLibType() );
109*cdf0e10cSrcweir             uno_any_construct(
110*cdf0e10cSrcweir                 reinterpret_cast< uno_Any * >( pReturn ), &pUnoI, pTD, 0 );
111*cdf0e10cSrcweir             TYPELIB_DANGER_RELEASE( pTD );
112*cdf0e10cSrcweir         }
113*cdf0e10cSrcweir         else
114*cdf0e10cSrcweir         {
115*cdf0e10cSrcweir             uno_any_construct(
116*cdf0e10cSrcweir                 reinterpret_cast< uno_Any * >( pReturn ), 0, 0, 0 );
117*cdf0e10cSrcweir         }
118*cdf0e10cSrcweir         *ppException = 0;
119*cdf0e10cSrcweir         break;
120*cdf0e10cSrcweir     }
121*cdf0e10cSrcweir     case 1: // acquire()
122*cdf0e10cSrcweir     case 2: // release()
123*cdf0e10cSrcweir         *ppException = 0;
124*cdf0e10cSrcweir         break;
125*cdf0e10cSrcweir     case 3: // throwException()
126*cdf0e10cSrcweir     {
127*cdf0e10cSrcweir         uno_Any * pAny = reinterpret_cast< uno_Any * >( pArgs[ 0 ] );
128*cdf0e10cSrcweir         OSL_ASSERT( pAny->pType->eTypeClass == typelib_TypeClass_EXCEPTION );
129*cdf0e10cSrcweir         uno_type_any_construct( *ppException, pAny->pData, pAny->pType, 0 );
130*cdf0e10cSrcweir         break;
131*cdf0e10cSrcweir     }
132*cdf0e10cSrcweir     default:
133*cdf0e10cSrcweir     {
134*cdf0e10cSrcweir         OSL_ASSERT( 0 );
135*cdf0e10cSrcweir         RuntimeException exc(
136*cdf0e10cSrcweir             OUSTR("not implemented!"), Reference< XInterface >() );
137*cdf0e10cSrcweir         uno_type_any_construct(
138*cdf0e10cSrcweir             *ppException, &exc, ::getCppuType( &exc ).getTypeLibType(), 0 );
139*cdf0e10cSrcweir         break;
140*cdf0e10cSrcweir     }
141*cdf0e10cSrcweir     }
142*cdf0e10cSrcweir }
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir } // extern "C"
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir //______________________________________________________________________________
147*cdf0e10cSrcweir Any ExceptionThrower::queryInterface( Type const & type )
148*cdf0e10cSrcweir     throw (RuntimeException)
149*cdf0e10cSrcweir {
150*cdf0e10cSrcweir     if (type.equals( ::getCppuType( reinterpret_cast<
151*cdf0e10cSrcweir                                     Reference< XInterface > const * >(0) ) ) ||
152*cdf0e10cSrcweir         type.equals( ExceptionThrower::getCppuType() ))
153*cdf0e10cSrcweir     {
154*cdf0e10cSrcweir         XExceptionThrower * that = static_cast< XExceptionThrower * >( this );
155*cdf0e10cSrcweir         return Any( &that, type );
156*cdf0e10cSrcweir     }
157*cdf0e10cSrcweir     return Any();
158*cdf0e10cSrcweir }
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir //______________________________________________________________________________
161*cdf0e10cSrcweir void ExceptionThrower::acquire() throw ()
162*cdf0e10cSrcweir {
163*cdf0e10cSrcweir }
164*cdf0e10cSrcweir //______________________________________________________________________________
165*cdf0e10cSrcweir void ExceptionThrower::release() throw ()
166*cdf0e10cSrcweir {
167*cdf0e10cSrcweir }
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir //______________________________________________________________________________
170*cdf0e10cSrcweir void ExceptionThrower::throwException( Any const & exc ) throw (Exception)
171*cdf0e10cSrcweir {
172*cdf0e10cSrcweir     OSL_ENSURE( 0, "unexpected!" );
173*cdf0e10cSrcweir     throwException( exc );
174*cdf0e10cSrcweir }
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir //______________________________________________________________________________
177*cdf0e10cSrcweir void ExceptionThrower::rethrowException() throw (Exception)
178*cdf0e10cSrcweir {
179*cdf0e10cSrcweir     throw;
180*cdf0e10cSrcweir }
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir //______________________________________________________________________________
183*cdf0e10cSrcweir inline ExceptionThrower::ExceptionThrower()
184*cdf0e10cSrcweir {
185*cdf0e10cSrcweir     uno_Interface::acquire = ExceptionThrower_acquire_release_nop;
186*cdf0e10cSrcweir     uno_Interface::release = ExceptionThrower_acquire_release_nop;
187*cdf0e10cSrcweir     uno_Interface::pDispatcher = ExceptionThrower_dispatch;
188*cdf0e10cSrcweir }
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir //______________________________________________________________________________
191*cdf0e10cSrcweir ExceptionThrower * ExceptionThrower::get()
192*cdf0e10cSrcweir {
193*cdf0e10cSrcweir     ExceptionThrower * s_pThrower = 0;
194*cdf0e10cSrcweir     if (s_pThrower == 0)
195*cdf0e10cSrcweir     {
196*cdf0e10cSrcweir         MutexGuard guard( Mutex::getGlobalMutex() );
197*cdf0e10cSrcweir         static ExceptionThrower s_thrower;
198*cdf0e10cSrcweir         OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
199*cdf0e10cSrcweir         s_pThrower = &s_thrower;
200*cdf0e10cSrcweir     }
201*cdf0e10cSrcweir     else
202*cdf0e10cSrcweir     {
203*cdf0e10cSrcweir         OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
204*cdf0e10cSrcweir     }
205*cdf0e10cSrcweir     return s_pThrower;
206*cdf0e10cSrcweir }
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir } // anonymous namespace
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir namespace cppu
212*cdf0e10cSrcweir {
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir //==============================================================================
215*cdf0e10cSrcweir void SAL_CALL throwException( Any const & exc ) SAL_THROW( (Exception) )
216*cdf0e10cSrcweir {
217*cdf0e10cSrcweir     if (exc.getValueTypeClass() != TypeClass_EXCEPTION)
218*cdf0e10cSrcweir     {
219*cdf0e10cSrcweir         throw RuntimeException(
220*cdf0e10cSrcweir             OUSTR("no UNO exception given "
221*cdf0e10cSrcweir                   "(must be derived from com::sun::star::uno::Exception)!"),
222*cdf0e10cSrcweir             Reference< XInterface >() );
223*cdf0e10cSrcweir     }
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir     Mapping uno2cpp(Environment(OUSTR(UNO_LB_UNO)), Environment::getCurrent());
226*cdf0e10cSrcweir     if (! uno2cpp.is())
227*cdf0e10cSrcweir     {
228*cdf0e10cSrcweir         throw RuntimeException(
229*cdf0e10cSrcweir             OUSTR("cannot get binary UNO to C++ mapping!"),
230*cdf0e10cSrcweir             Reference< XInterface >() );
231*cdf0e10cSrcweir     }
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir     Reference< XExceptionThrower > xThrower;
234*cdf0e10cSrcweir     uno2cpp.mapInterface(
235*cdf0e10cSrcweir         reinterpret_cast< void ** >( &xThrower ),
236*cdf0e10cSrcweir         static_cast< uno_Interface * >( ExceptionThrower::get() ),
237*cdf0e10cSrcweir         ExceptionThrower::getCppuType() );
238*cdf0e10cSrcweir     OSL_ASSERT( xThrower.is() );
239*cdf0e10cSrcweir     xThrower->throwException( exc );
240*cdf0e10cSrcweir }
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir //==============================================================================
243*cdf0e10cSrcweir Any SAL_CALL getCaughtException()
244*cdf0e10cSrcweir {
245*cdf0e10cSrcweir     Mapping cpp2uno(Environment::getCurrent(), Environment(OUSTR(UNO_LB_UNO)));
246*cdf0e10cSrcweir     if (! cpp2uno.is())
247*cdf0e10cSrcweir     {
248*cdf0e10cSrcweir         throw RuntimeException(
249*cdf0e10cSrcweir             OUSTR("cannot get C++ to binary UNO mapping!"),
250*cdf0e10cSrcweir             Reference< XInterface >() );
251*cdf0e10cSrcweir     }
252*cdf0e10cSrcweir     Mapping uno2cpp(Environment(OUSTR(UNO_LB_UNO)), Environment::getCurrent());
253*cdf0e10cSrcweir     if (! uno2cpp.is())
254*cdf0e10cSrcweir     {
255*cdf0e10cSrcweir         throw RuntimeException(
256*cdf0e10cSrcweir             OUSTR("cannot get binary UNO to C++ mapping!"),
257*cdf0e10cSrcweir             Reference< XInterface >() );
258*cdf0e10cSrcweir     }
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir     typelib_TypeDescription * pTD = 0;
261*cdf0e10cSrcweir     TYPELIB_DANGER_GET(
262*cdf0e10cSrcweir         &pTD, ExceptionThrower::getCppuType().getTypeLibType() );
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir     UnoInterfaceReference unoI;
265*cdf0e10cSrcweir     cpp2uno.mapInterface(
266*cdf0e10cSrcweir         reinterpret_cast< void ** >( &unoI.m_pUnoI ),
267*cdf0e10cSrcweir         static_cast< XExceptionThrower * >( ExceptionThrower::get() ), pTD );
268*cdf0e10cSrcweir     OSL_ASSERT( unoI.is() );
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir     typelib_TypeDescription * pMemberTD = 0;
271*cdf0e10cSrcweir     TYPELIB_DANGER_GET(
272*cdf0e10cSrcweir         &pMemberTD,
273*cdf0e10cSrcweir         reinterpret_cast< typelib_InterfaceTypeDescription * >( pTD )->
274*cdf0e10cSrcweir         ppMembers[ 1 ] /* rethrowException() */ );
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir     uno_Any exc_mem;
277*cdf0e10cSrcweir     uno_Any * exc = &exc_mem;
278*cdf0e10cSrcweir     unoI.dispatch( pMemberTD, 0, 0, &exc );
279*cdf0e10cSrcweir 
280*cdf0e10cSrcweir     TYPELIB_DANGER_RELEASE( pMemberTD );
281*cdf0e10cSrcweir     TYPELIB_DANGER_RELEASE( pTD );
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir     if (exc == 0)
284*cdf0e10cSrcweir     {
285*cdf0e10cSrcweir         throw RuntimeException(
286*cdf0e10cSrcweir             OUSTR("rethrowing C++ exception failed!"),
287*cdf0e10cSrcweir             Reference< XInterface >() );
288*cdf0e10cSrcweir     }
289*cdf0e10cSrcweir 
290*cdf0e10cSrcweir     Any ret;
291*cdf0e10cSrcweir     uno_any_destruct( &ret, reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
292*cdf0e10cSrcweir     uno_type_any_constructAndConvert(
293*cdf0e10cSrcweir         &ret, exc->pData, exc->pType, uno2cpp.get() );
294*cdf0e10cSrcweir     uno_any_destruct( exc, 0 );
295*cdf0e10cSrcweir     return ret;
296*cdf0e10cSrcweir }
297*cdf0e10cSrcweir 
298*cdf0e10cSrcweir }
299