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 283 : virtual ~ExceptionThrower() {}
49 :
50 40348 : static inline Type const & getCppuType()
51 : {
52 : return ::getCppuType(
53 40348 : 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 205848 : static void SAL_CALL ExceptionThrower_acquire_release_nop(
72 : SAL_UNUSED_PARAMETER uno_Interface * )
73 205848 : {}
74 :
75 :
76 68616 : 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 68616 : 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 34308 : *reinterpret_cast< Type const * >( pArgs[ 0 ] );
90 34308 : if (rType_demanded.equals(
91 : ::getCppuType( reinterpret_cast<
92 34308 : Reference< XInterface > const * >(0) ) ) ||
93 0 : rType_demanded.equals( ExceptionThrower::getCppuType() ))
94 : {
95 34308 : typelib_TypeDescription * pTD = 0;
96 34308 : TYPELIB_DANGER_GET( &pTD, rType_demanded.getTypeLibType() );
97 : uno_any_construct(
98 34308 : reinterpret_cast< uno_Any * >( pReturn ), &pUnoI, pTD, 0 );
99 34308 : TYPELIB_DANGER_RELEASE( pTD );
100 : }
101 : else
102 : {
103 : uno_any_construct(
104 0 : reinterpret_cast< uno_Any * >( pReturn ), 0, 0, 0 );
105 : }
106 34308 : *ppException = 0;
107 34308 : break;
108 : }
109 : case 1: // acquire()
110 : case 2: // release()
111 0 : *ppException = 0;
112 0 : break;
113 : case 3: // throwException()
114 : {
115 34308 : uno_Any * pAny = reinterpret_cast< uno_Any * >( pArgs[ 0 ] );
116 : OSL_ASSERT( pAny->pType->eTypeClass == typelib_TypeClass_EXCEPTION );
117 34308 : uno_type_any_construct( *ppException, pAny->pData, pAny->pType, 0 );
118 34308 : break;
119 : }
120 : default:
121 : {
122 : OSL_ASSERT( false );
123 0 : RuntimeException exc( "not implemented!" );
124 : uno_type_any_construct(
125 0 : *ppException, &exc, ::getCppuType( &exc ).getTypeLibType(), 0 );
126 0 : break;
127 : }
128 : }
129 68616 : }
130 :
131 : } // extern "C"
132 :
133 :
134 6040 : Any ExceptionThrower::queryInterface( Type const & type )
135 : throw (RuntimeException, std::exception)
136 : {
137 6040 : if (type.equals( ::getCppuType( reinterpret_cast<
138 6040 : Reference< XInterface > const * >(0) ) ) ||
139 0 : type.equals( ExceptionThrower::getCppuType() ))
140 : {
141 6040 : XExceptionThrower * that = static_cast< XExceptionThrower * >( this );
142 6040 : return Any( &that, type );
143 : }
144 0 : return Any();
145 : }
146 :
147 :
148 18120 : void ExceptionThrower::acquire() throw ()
149 : {
150 18120 : }
151 :
152 18120 : void ExceptionThrower::release() throw ()
153 : {
154 18120 : }
155 :
156 :
157 0 : void ExceptionThrower::throwException( Any const & exc ) throw (Exception, std::exception)
158 : {
159 : OSL_FAIL( "unexpected!" );
160 0 : throwException( exc );
161 0 : }
162 :
163 :
164 6040 : void ExceptionThrower::rethrowException() throw (Exception, std::exception)
165 : {
166 6040 : throw;
167 : }
168 :
169 :
170 283 : inline ExceptionThrower::ExceptionThrower()
171 : {
172 283 : uno_Interface::acquire = ExceptionThrower_acquire_release_nop;
173 283 : uno_Interface::release = ExceptionThrower_acquire_release_nop;
174 283 : uno_Interface::pDispatcher = ExceptionThrower_dispatch;
175 283 : }
176 :
177 : class theExceptionThrower : public rtl::Static<ExceptionThrower, theExceptionThrower> {};
178 :
179 : } // anonymous namespace
180 :
181 :
182 : namespace cppu
183 : {
184 :
185 :
186 34308 : void SAL_CALL throwException( Any const & exc )
187 : {
188 34308 : if (exc.getValueTypeClass() != TypeClass_EXCEPTION)
189 : {
190 : throw RuntimeException(
191 : "no UNO exception given "
192 0 : "(must be derived from com::sun::star::uno::Exception)!" );
193 : }
194 :
195 34308 : Mapping uno2cpp(Environment(UNO_LB_UNO), Environment::getCurrent());
196 34308 : if (! uno2cpp.is())
197 : {
198 : throw RuntimeException(
199 0 : "cannot get binary UNO to C++ mapping!" );
200 : }
201 :
202 68616 : Reference< XExceptionThrower > xThrower;
203 : uno2cpp.mapInterface(
204 : reinterpret_cast< void ** >( &xThrower ),
205 34308 : static_cast< uno_Interface * >( &theExceptionThrower::get() ),
206 68616 : ExceptionThrower::getCppuType() );
207 : OSL_ASSERT( xThrower.is() );
208 68616 : xThrower->throwException( exc );
209 0 : }
210 :
211 :
212 6040 : Any SAL_CALL getCaughtException()
213 : {
214 6040 : Mapping cpp2uno(Environment::getCurrent(), Environment(UNO_LB_UNO));
215 6040 : if (! cpp2uno.is())
216 : {
217 : throw RuntimeException(
218 0 : "cannot get C++ to binary UNO mapping!" );
219 : }
220 12080 : Mapping uno2cpp(Environment(UNO_LB_UNO), Environment::getCurrent());
221 6040 : if (! uno2cpp.is())
222 : {
223 : throw RuntimeException(
224 0 : "cannot get binary UNO to C++ mapping!" );
225 : }
226 :
227 6040 : typelib_TypeDescription * pTD = 0;
228 : TYPELIB_DANGER_GET(
229 6040 : &pTD, ExceptionThrower::getCppuType().getTypeLibType() );
230 :
231 12080 : UnoInterfaceReference unoI;
232 : cpp2uno.mapInterface(
233 : reinterpret_cast< void ** >( &unoI.m_pUnoI ),
234 6040 : static_cast< XExceptionThrower * >( &theExceptionThrower::get() ), pTD );
235 : OSL_ASSERT( unoI.is() );
236 :
237 6040 : typelib_TypeDescription * pMemberTD = 0;
238 : TYPELIB_DANGER_GET(
239 : &pMemberTD,
240 : reinterpret_cast< typelib_InterfaceTypeDescription * >( pTD )->
241 6040 : ppMembers[ 1 ] /* rethrowException() */ );
242 :
243 : uno_Any exc_mem;
244 6040 : uno_Any * exc = &exc_mem;
245 6040 : unoI.dispatch( pMemberTD, 0, 0, &exc );
246 :
247 6040 : TYPELIB_DANGER_RELEASE( pMemberTD );
248 6040 : TYPELIB_DANGER_RELEASE( pTD );
249 :
250 6040 : if (exc == 0)
251 : {
252 0 : throw RuntimeException( "rethrowing C++ exception failed!" );
253 : }
254 :
255 6040 : Any ret;
256 6040 : uno_any_destruct( &ret, reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
257 : uno_type_any_constructAndConvert(
258 6040 : &ret, exc->pData, exc->pType, uno2cpp.get() );
259 6040 : uno_any_destruct( exc, 0 );
260 12080 : return ret;
261 : }
262 :
263 : }
264 :
265 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|