Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
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"
30 :
31 : #include "cppuhelper/exc_hlp.hxx"
32 :
33 : using namespace ::osl;
34 : using namespace ::cppu;
35 : using namespace ::com::sun::star;
36 : using namespace ::com::sun::star::uno;
37 :
38 : namespace
39 : {
40 :
41 : using cppuhelper::detail::XExceptionThrower;
42 :
43 :
44 : struct ExceptionThrower : public uno_Interface, XExceptionThrower
45 : {
46 : inline ExceptionThrower();
47 :
48 0 : virtual ~ExceptionThrower() {}
49 :
50 0 : static inline Type const & getCppuType()
51 : {
52 : return ::getCppuType(
53 0 : reinterpret_cast< Reference< XExceptionThrower > const * >(0) );
54 : }
55 :
56 : // XInterface
57 : virtual Any SAL_CALL queryInterface( Type const & type )
58 : throw (RuntimeException, std::exception) SAL_OVERRIDE;
59 : virtual void SAL_CALL acquire() throw () SAL_OVERRIDE;
60 : virtual void SAL_CALL release() throw () SAL_OVERRIDE;
61 :
62 : // XExceptionThrower
63 : virtual void SAL_CALL throwException( Any const & exc ) throw (Exception, std::exception) SAL_OVERRIDE;
64 : virtual void SAL_CALL rethrowException() throw (Exception, std::exception) SAL_OVERRIDE;
65 : };
66 :
67 : extern "C"
68 : {
69 :
70 :
71 0 : static void SAL_CALL ExceptionThrower_acquire_release_nop(
72 : SAL_UNUSED_PARAMETER uno_Interface * )
73 0 : {}
74 :
75 :
76 0 : static void SAL_CALL ExceptionThrower_dispatch(
77 : uno_Interface * pUnoI, typelib_TypeDescription const * pMemberType,
78 : void * pReturn, void * pArgs [], uno_Any ** ppException )
79 : {
80 : OSL_ASSERT( pMemberType->eTypeClass == typelib_TypeClass_INTERFACE_METHOD );
81 :
82 0 : switch (reinterpret_cast< typelib_InterfaceMemberTypeDescription * >(
83 : const_cast< typelib_TypeDescription * >( pMemberType ) )->
84 : nPosition)
85 : {
86 : case 0: // queryInterace()
87 : {
88 : Type const & rType_demanded =
89 0 : *reinterpret_cast< Type const * >( pArgs[ 0 ] );
90 0 : if (rType_demanded.equals(
91 : ::getCppuType( reinterpret_cast<
92 0 : Reference< XInterface > const * >(0) ) ) ||
93 0 : rType_demanded.equals( ExceptionThrower::getCppuType() ))
94 : {
95 0 : typelib_TypeDescription * pTD = 0;
96 0 : TYPELIB_DANGER_GET( &pTD, rType_demanded.getTypeLibType() );
97 : uno_any_construct(
98 0 : reinterpret_cast< uno_Any * >( pReturn ), &pUnoI, pTD, 0 );
99 0 : TYPELIB_DANGER_RELEASE( pTD );
100 : }
101 : else
102 : {
103 : uno_any_construct(
104 0 : reinterpret_cast< uno_Any * >( pReturn ), 0, 0, 0 );
105 : }
106 0 : *ppException = 0;
107 0 : break;
108 : }
109 : case 1: // acquire()
110 : case 2: // release()
111 0 : *ppException = 0;
112 0 : break;
113 : case 3: // throwException()
114 : {
115 0 : uno_Any * pAny = reinterpret_cast< uno_Any * >( pArgs[ 0 ] );
116 : OSL_ASSERT( pAny->pType->eTypeClass == typelib_TypeClass_EXCEPTION );
117 0 : uno_type_any_construct( *ppException, pAny->pData, pAny->pType, 0 );
118 0 : break;
119 : }
120 : default:
121 : {
122 : OSL_ASSERT( false );
123 : RuntimeException exc(
124 0 : "not implemented!", Reference< XInterface >() );
125 : uno_type_any_construct(
126 0 : *ppException, &exc, ::getCppuType( &exc ).getTypeLibType(), 0 );
127 0 : break;
128 : }
129 : }
130 0 : }
131 :
132 : } // extern "C"
133 :
134 :
135 0 : Any ExceptionThrower::queryInterface( Type const & type )
136 : throw (RuntimeException, std::exception)
137 : {
138 0 : if (type.equals( ::getCppuType( reinterpret_cast<
139 0 : Reference< XInterface > const * >(0) ) ) ||
140 0 : type.equals( ExceptionThrower::getCppuType() ))
141 : {
142 0 : XExceptionThrower * that = static_cast< XExceptionThrower * >( this );
143 0 : return Any( &that, type );
144 : }
145 0 : return Any();
146 : }
147 :
148 :
149 0 : void ExceptionThrower::acquire() throw ()
150 : {
151 0 : }
152 :
153 0 : void ExceptionThrower::release() throw ()
154 : {
155 0 : }
156 :
157 :
158 0 : void ExceptionThrower::throwException( Any const & exc ) throw (Exception, std::exception)
159 : {
160 : OSL_FAIL( "unexpected!" );
161 0 : throwException( exc );
162 0 : }
163 :
164 :
165 0 : void ExceptionThrower::rethrowException() throw (Exception, std::exception)
166 : {
167 0 : throw;
168 : }
169 :
170 :
171 0 : inline ExceptionThrower::ExceptionThrower()
172 : {
173 0 : uno_Interface::acquire = ExceptionThrower_acquire_release_nop;
174 0 : uno_Interface::release = ExceptionThrower_acquire_release_nop;
175 0 : uno_Interface::pDispatcher = ExceptionThrower_dispatch;
176 0 : }
177 :
178 : class theExceptionThrower : public rtl::Static<ExceptionThrower, theExceptionThrower> {};
179 :
180 : } // anonymous namespace
181 :
182 :
183 : namespace cppu
184 : {
185 :
186 :
187 0 : void SAL_CALL throwException( Any const & exc ) SAL_THROW( (Exception) )
188 : {
189 0 : if (exc.getValueTypeClass() != TypeClass_EXCEPTION)
190 : {
191 : throw RuntimeException(
192 : "no UNO exception given "
193 : "(must be derived from com::sun::star::uno::Exception)!",
194 0 : Reference< XInterface >() );
195 : }
196 :
197 0 : Mapping uno2cpp(Environment(UNO_LB_UNO), Environment::getCurrent());
198 0 : if (! uno2cpp.is())
199 : {
200 : throw RuntimeException(
201 : "cannot get binary UNO to C++ mapping!",
202 0 : Reference< XInterface >() );
203 : }
204 :
205 0 : Reference< XExceptionThrower > xThrower;
206 : uno2cpp.mapInterface(
207 : reinterpret_cast< void ** >( &xThrower ),
208 0 : static_cast< uno_Interface * >( &theExceptionThrower::get() ),
209 0 : ExceptionThrower::getCppuType() );
210 : OSL_ASSERT( xThrower.is() );
211 0 : xThrower->throwException( exc );
212 0 : }
213 :
214 :
215 0 : Any SAL_CALL getCaughtException()
216 : {
217 0 : Mapping cpp2uno(Environment::getCurrent(), Environment(UNO_LB_UNO));
218 0 : if (! cpp2uno.is())
219 : {
220 : throw RuntimeException(
221 : "cannot get C++ to binary UNO mapping!",
222 0 : Reference< XInterface >() );
223 : }
224 0 : Mapping uno2cpp(Environment(UNO_LB_UNO), Environment::getCurrent());
225 0 : if (! uno2cpp.is())
226 : {
227 : throw RuntimeException(
228 : "cannot get binary UNO to C++ mapping!",
229 0 : Reference< XInterface >() );
230 : }
231 :
232 0 : typelib_TypeDescription * pTD = 0;
233 : TYPELIB_DANGER_GET(
234 0 : &pTD, ExceptionThrower::getCppuType().getTypeLibType() );
235 :
236 0 : UnoInterfaceReference unoI;
237 : cpp2uno.mapInterface(
238 : reinterpret_cast< void ** >( &unoI.m_pUnoI ),
239 0 : static_cast< XExceptionThrower * >( &theExceptionThrower::get() ), pTD );
240 : OSL_ASSERT( unoI.is() );
241 :
242 0 : typelib_TypeDescription * pMemberTD = 0;
243 : TYPELIB_DANGER_GET(
244 : &pMemberTD,
245 : reinterpret_cast< typelib_InterfaceTypeDescription * >( pTD )->
246 0 : ppMembers[ 1 ] /* rethrowException() */ );
247 :
248 : uno_Any exc_mem;
249 0 : uno_Any * exc = &exc_mem;
250 0 : unoI.dispatch( pMemberTD, 0, 0, &exc );
251 :
252 0 : TYPELIB_DANGER_RELEASE( pMemberTD );
253 0 : TYPELIB_DANGER_RELEASE( pTD );
254 :
255 0 : if (exc == 0)
256 : {
257 : throw RuntimeException(
258 : "rethrowing C++ exception failed!",
259 0 : Reference< XInterface >() );
260 : }
261 :
262 0 : Any ret;
263 0 : uno_any_destruct( &ret, reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
264 : uno_type_any_constructAndConvert(
265 0 : &ret, exc->pData, exc->pType, uno2cpp.get() );
266 0 : uno_any_destruct( exc, 0 );
267 0 : return ret;
268 : }
269 :
270 : }
271 :
272 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|