Bump for 3.6-28
[LibreOffice.git] / bridges / source / cpp_uno / gcc3_linux_mips / except.cxx
blobc94d32d68fd5e7621c76dd8db8899bf17aa8fc44
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
28 #include <stdio.h>
29 #include <string.h>
30 #include <dlfcn.h>
31 #include <cxxabi.h>
32 #include <boost/unordered_map.hpp>
34 #include <rtl/instance.hxx>
35 #include <rtl/strbuf.hxx>
36 #include <rtl/ustrbuf.hxx>
37 #include <osl/diagnose.h>
38 #include <osl/mutex.hxx>
40 #include <com/sun/star/uno/genfunc.hxx>
41 #include <typelib/typedescription.hxx>
42 #include <uno/any2.h>
44 #include "share.hxx"
47 using namespace ::std;
48 using namespace ::osl;
49 using namespace ::rtl;
50 using namespace ::com::sun::star::uno;
51 using namespace ::__cxxabiv1;
54 namespace CPPU_CURRENT_NAMESPACE
57 void dummy_can_throw_anything( char const * )
61 //==================================================================================================
62 static OUString toUNOname( char const * p ) SAL_THROW(())
64 #if defined BRIDGES_DEBUG
65 char const * start = p;
66 #endif
68 // example: N3com3sun4star4lang24IllegalArgumentExceptionE
70 OUStringBuffer buf( 64 );
71 OSL_ASSERT( 'N' == *p );
72 ++p; // skip N
74 while ('E' != *p)
76 // read chars count
77 long n = (*p++ - '0');
78 while ('0' <= *p && '9' >= *p)
80 n *= 10;
81 n += (*p++ - '0');
83 buf.appendAscii( p, n );
84 p += n;
85 if ('E' != *p)
86 buf.append( (sal_Unicode)'.' );
89 #if defined BRIDGES_DEBUG
90 OUString ret( buf.makeStringAndClear() );
91 OString c_ret( OUStringToOString( ret, RTL_TEXTENCODING_ASCII_US ) );
92 fprintf( stderr, "> toUNOname(): %s => %s\n", start, c_ret.getStr() );
93 return ret;
94 #else
95 return buf.makeStringAndClear();
96 #endif
99 //==================================================================================================
100 class RTTI
102 typedef boost::unordered_map< OUString, type_info *, OUStringHash > t_rtti_map;
104 Mutex m_mutex;
105 t_rtti_map m_rttis;
106 t_rtti_map m_generatedRttis;
108 void * m_hApp;
110 public:
111 RTTI() SAL_THROW(());
112 ~RTTI() SAL_THROW(());
114 type_info * getRTTI( typelib_CompoundTypeDescription * ) SAL_THROW(());
116 //__________________________________________________________________________________________________
117 RTTI::RTTI() SAL_THROW(())
118 : m_hApp( dlopen( 0, RTLD_LAZY ) )
121 //__________________________________________________________________________________________________
122 RTTI::~RTTI() SAL_THROW(())
124 dlclose( m_hApp );
127 //__________________________________________________________________________________________________
128 type_info * RTTI::getRTTI( typelib_CompoundTypeDescription *pTypeDescr ) SAL_THROW(())
130 type_info * rtti;
132 OUString const & unoName = *(OUString const *)&pTypeDescr->aBase.pTypeName;
134 MutexGuard guard( m_mutex );
135 t_rtti_map::const_iterator iRttiFind( m_rttis.find( unoName ) );
136 if (iRttiFind == m_rttis.end())
138 // RTTI symbol
139 OStringBuffer buf( 64 );
140 buf.append( RTL_CONSTASCII_STRINGPARAM("_ZTIN") );
141 sal_Int32 index = 0;
144 OUString token( unoName.getToken( 0, '.', index ) );
145 buf.append( token.getLength() );
146 OString c_token( OUStringToOString( token, RTL_TEXTENCODING_ASCII_US ) );
147 buf.append( c_token );
149 while (index >= 0);
150 buf.append( 'E' );
152 OString symName( buf.makeStringAndClear() );
153 rtti = (type_info *)dlsym( m_hApp, symName.getStr() );
155 if (rtti)
157 pair< t_rtti_map::iterator, bool > insertion(
158 m_rttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
159 OSL_ENSURE( insertion.second, "### inserting new rtti failed?!" );
161 else
163 // try to lookup the symbol in the generated rtti map
164 t_rtti_map::const_iterator iFind( m_generatedRttis.find( unoName ) );
165 if (iFind == m_generatedRttis.end())
167 // we must generate it !
168 // symbol and rtti-name is nearly identical,
169 // the symbol is prefixed with _ZTI
170 char const * rttiName = symName.getStr() +4;
171 #if defined BRIDGES_DEBUG
172 fprintf( stderr,"generated rtti for %s\n", rttiName );
173 #endif
174 if (pTypeDescr->pBaseTypeDescription)
176 // ensure availability of base
177 type_info * base_rtti = getRTTI(
178 (typelib_CompoundTypeDescription *)pTypeDescr->pBaseTypeDescription );
179 rtti = new __si_class_type_info(
180 strdup( rttiName ), (__class_type_info *)base_rtti );
182 else
184 // this class has no base class
185 rtti = new __class_type_info( strdup( rttiName ) );
188 pair< t_rtti_map::iterator, bool > insertion(
189 m_generatedRttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
190 OSL_ENSURE( insertion.second, "### inserting new generated rtti failed?!" );
192 else // taking already generated rtti
194 rtti = iFind->second;
198 else
200 rtti = iRttiFind->second;
203 return rtti;
206 struct RTTISingleton: public rtl::Static< RTTI, RTTISingleton > {};
208 //--------------------------------------------------------------------------------------------------
209 static void deleteException( void * pExc )
211 __cxa_exception const * header = ((__cxa_exception const *)pExc - 1);
212 typelib_TypeDescription * pTD = 0;
213 OUString unoName( toUNOname( header->exceptionType->name() ) );
214 ::typelib_typedescription_getByName( &pTD, unoName.pData );
215 OSL_ENSURE( pTD, "### unknown exception type! leaving out destruction => leaking!!!" );
216 if (pTD)
218 ::uno_destructData( pExc, pTD, cpp_release );
219 ::typelib_typedescription_release( pTD );
223 //==================================================================================================
224 void raiseException( uno_Any * pUnoExc, uno_Mapping * pUno2Cpp )
226 #if defined BRIDGES_DEBUG
227 OString cstr(
228 OUStringToOString(
229 *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
230 RTL_TEXTENCODING_ASCII_US ) );
231 fprintf( stderr, "> uno exception occurred: %s\n", cstr.getStr() );
232 #endif
233 void * pCppExc;
234 type_info * rtti;
237 // construct cpp exception object
238 typelib_TypeDescription * pTypeDescr = 0;
239 TYPELIB_DANGER_GET( &pTypeDescr, pUnoExc->pType );
240 OSL_ASSERT( pTypeDescr );
241 if (! pTypeDescr)
243 throw RuntimeException(
244 OUString( RTL_CONSTASCII_USTRINGPARAM("cannot get typedescription for type ") ) +
245 *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
246 Reference< XInterface >() );
249 pCppExc = __cxa_allocate_exception( pTypeDescr->nSize );
250 ::uno_copyAndConvertData( pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp );
252 // destruct uno exception
253 ::uno_any_destruct( pUnoExc, 0 );
254 rtti = (type_info *)RTTISingleton::get().getRTTI( (typelib_CompoundTypeDescription *) pTypeDescr );
255 TYPELIB_DANGER_RELEASE( pTypeDescr );
256 OSL_ENSURE( rtti, "### no rtti for throwing exception!" );
257 if (! rtti)
259 throw RuntimeException(
260 OUString( RTL_CONSTASCII_USTRINGPARAM("no rtti for type ") ) +
261 *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
262 Reference< XInterface >() );
266 __cxa_throw( pCppExc, rtti, deleteException );
269 //==================================================================================================
270 void fillUnoException( __cxa_exception * header, uno_Any * pUnoExc, uno_Mapping * pCpp2Uno )
272 if (! header)
274 RuntimeException aRE(
275 OUString( RTL_CONSTASCII_USTRINGPARAM("no exception header!") ),
276 Reference< XInterface >() );
277 Type const & rType = ::getCppuType( &aRE );
278 uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
279 #if defined _DEBUG
280 OString cstr( OUStringToOString( aRE.Message, RTL_TEXTENCODING_ASCII_US ) );
281 OSL_FAIL( cstr.getStr() );
282 #endif
283 return;
286 typelib_TypeDescription * pExcTypeDescr = 0;
287 OUString unoName( toUNOname( header->exceptionType->name() ) );
288 #if defined BRIDGES_DEBUG
289 OString cstr_unoName( OUStringToOString( unoName, RTL_TEXTENCODING_ASCII_US ) );
290 fprintf( stderr, "> c++ exception occurred: %s\n", cstr_unoName.getStr() );
291 #endif
292 typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData );
293 if (0 == pExcTypeDescr)
295 RuntimeException aRE(
296 OUString( RTL_CONSTASCII_USTRINGPARAM("exception type not found: ") ) + unoName,
297 Reference< XInterface >() );
298 Type const & rType = ::getCppuType( &aRE );
299 uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
300 #if defined _DEBUG
301 OString cstr( OUStringToOString( aRE.Message, RTL_TEXTENCODING_ASCII_US ) );
302 OSL_FAIL( cstr.getStr() );
303 #endif
305 else
307 // construct uno exception any
308 uno_any_constructAndConvert( pUnoExc, header->adjustedPtr, pExcTypeDescr, pCpp2Uno );
309 typelib_typedescription_release( pExcTypeDescr );
315 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */