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