Update ooo320-m1
[ooovba.git] / bridges / source / cpp_uno / gcc3_freebsd_x86-64 / except.cxx
blobcb155bc9912a322ec2587ad47597d449ba6899c4
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: except.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_bridges.hxx"
34 #include <stdio.h>
35 #include <dlfcn.h>
36 #include <cxxabi.h>
37 #include <hash_map>
38 #include <sys/param.h>
40 #include <rtl/strbuf.hxx>
41 #include <rtl/ustrbuf.hxx>
42 #include <osl/diagnose.h>
43 #include <osl/mutex.hxx>
45 #include <com/sun/star/uno/genfunc.hxx>
46 #include "com/sun/star/uno/RuntimeException.hpp"
47 #include <typelib/typedescription.hxx>
48 #include <uno/any2.h>
50 #include "share.hxx"
53 using namespace ::std;
54 using namespace ::osl;
55 using namespace ::rtl;
56 using namespace ::com::sun::star::uno;
57 using namespace ::__cxxabiv1;
60 namespace CPPU_CURRENT_NAMESPACE
63 void dummy_can_throw_anything( char const * )
67 //==================================================================================================
68 static OUString toUNOname( char const * p ) SAL_THROW( () )
70 #if OSL_DEBUG_LEVEL > 1
71 char const * start = p;
72 #endif
74 // example: N3com3sun4star4lang24IllegalArgumentExceptionE
76 OUStringBuffer buf( 64 );
77 OSL_ASSERT( 'N' == *p );
78 ++p; // skip N
80 while ('E' != *p)
82 // read chars count
83 long n = (*p++ - '0');
84 while ('0' <= *p && '9' >= *p)
86 n *= 10;
87 n += (*p++ - '0');
89 buf.appendAscii( p, n );
90 p += n;
91 if ('E' != *p)
92 buf.append( (sal_Unicode)'.' );
95 #if OSL_DEBUG_LEVEL > 1
96 OUString ret( buf.makeStringAndClear() );
97 OString c_ret( OUStringToOString( ret, RTL_TEXTENCODING_ASCII_US ) );
98 fprintf( stderr, "> toUNOname(): %s => %s\n", start, c_ret.getStr() );
99 return ret;
100 #else
101 return buf.makeStringAndClear();
102 #endif
105 //==================================================================================================
106 class RTTI
108 typedef hash_map< OUString, type_info *, OUStringHash > t_rtti_map;
110 Mutex m_mutex;
111 t_rtti_map m_rttis;
112 t_rtti_map m_generatedRttis;
114 void * m_hApp;
116 public:
117 RTTI() SAL_THROW( () );
118 ~RTTI() SAL_THROW( () );
120 type_info * getRTTI( typelib_CompoundTypeDescription * ) SAL_THROW( () );
122 //__________________________________________________________________________________________________
123 RTTI::RTTI() SAL_THROW( () )
124 #if __FreeBSD_version < 602103
125 : m_hApp( dlopen( 0, RTLD_NOW | RTLD_GLOBAL ) )
126 #else
127 : m_hApp( dlopen( 0, RTLD_LAZY ) )
128 #endif
131 //__________________________________________________________________________________________________
132 RTTI::~RTTI() SAL_THROW( () )
134 dlclose( m_hApp );
137 //__________________________________________________________________________________________________
138 type_info * RTTI::getRTTI( typelib_CompoundTypeDescription *pTypeDescr ) SAL_THROW( () )
140 type_info * rtti;
142 OUString const & unoName = *(OUString const *)&pTypeDescr->aBase.pTypeName;
144 MutexGuard guard( m_mutex );
145 t_rtti_map::const_iterator iFind( m_rttis.find( unoName ) );
146 if (iFind == m_rttis.end())
148 // RTTI symbol
149 OStringBuffer buf( 64 );
150 buf.append( RTL_CONSTASCII_STRINGPARAM("_ZTIN") );
151 sal_Int32 index = 0;
154 OUString token( unoName.getToken( 0, '.', index ) );
155 buf.append( token.getLength() );
156 OString c_token( OUStringToOString( token, RTL_TEXTENCODING_ASCII_US ) );
157 buf.append( c_token );
159 while (index >= 0);
160 buf.append( 'E' );
162 OString symName( buf.makeStringAndClear() );
163 #if __FreeBSD_version < 602103 /* #i22253# */
164 rtti = (type_info *)dlsym( RTLD_DEFAULT, symName.getStr() );
165 #else
166 rtti = (type_info *)dlsym( m_hApp, symName.getStr() );
167 #endif
169 if (rtti)
171 pair< t_rtti_map::iterator, bool > insertion(
172 m_rttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
173 OSL_ENSURE( insertion.second, "### inserting new rtti failed?!" );
175 else
177 // try to lookup the symbol in the generated rtti map
178 t_rtti_map::const_iterator iFind( m_generatedRttis.find( unoName ) );
179 if (iFind == m_generatedRttis.end())
181 // we must generate it !
182 // symbol and rtti-name is nearly identical,
183 // the symbol is prefixed with _ZTI
184 char const * rttiName = symName.getStr() +4;
185 #if OSL_DEBUG_LEVEL > 1
186 fprintf( stderr,"generated rtti for %s\n", rttiName );
187 #endif
188 if (pTypeDescr->pBaseTypeDescription)
190 // ensure availability of base
191 type_info * base_rtti = getRTTI(
192 (typelib_CompoundTypeDescription *)pTypeDescr->pBaseTypeDescription );
193 rtti = new __si_class_type_info(
194 strdup( rttiName ), (__class_type_info *)base_rtti );
196 else
198 // this class has no base class
199 rtti = new __class_type_info( strdup( rttiName ) );
202 pair< t_rtti_map::iterator, bool > insertion(
203 m_generatedRttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
204 OSL_ENSURE( insertion.second, "### inserting new generated rtti failed?!" );
206 else // taking already generated rtti
208 rtti = iFind->second;
212 else
214 rtti = iFind->second;
217 return rtti;
220 //--------------------------------------------------------------------------------------------------
221 static void deleteException( void * pExc )
223 __cxa_exception const * header = ((__cxa_exception const *)pExc - 1);
224 typelib_TypeDescription * pTD = 0;
225 OUString unoName( toUNOname( header->exceptionType->name() ) );
226 ::typelib_typedescription_getByName( &pTD, unoName.pData );
227 OSL_ENSURE( pTD, "### unknown exception type! leaving out destruction => leaking!!!" );
228 if (pTD)
230 ::uno_destructData( pExc, pTD, cpp_release );
231 ::typelib_typedescription_release( pTD );
235 //==================================================================================================
236 void raiseException( uno_Any * pUnoExc, uno_Mapping * pUno2Cpp )
238 #if OSL_DEBUG_LEVEL > 1
239 OString cstr(
240 OUStringToOString(
241 *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
242 RTL_TEXTENCODING_ASCII_US ) );
243 fprintf( stderr, "> uno exception occured: %s\n", cstr.getStr() );
244 #endif
245 void * pCppExc;
246 type_info * rtti;
249 // construct cpp exception object
250 typelib_TypeDescription * pTypeDescr = 0;
251 TYPELIB_DANGER_GET( &pTypeDescr, pUnoExc->pType );
252 OSL_ASSERT( pTypeDescr );
253 if (! pTypeDescr)
255 throw RuntimeException(
256 OUString( RTL_CONSTASCII_USTRINGPARAM("cannot get typedescription for type ") ) +
257 *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
258 Reference< XInterface >() );
261 pCppExc = __cxa_allocate_exception( pTypeDescr->nSize );
262 ::uno_copyAndConvertData( pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp );
264 // destruct uno exception
265 ::uno_any_destruct( pUnoExc, 0 );
266 // avoiding locked counts
267 static RTTI * s_rtti = 0;
268 if (! s_rtti)
270 MutexGuard guard( Mutex::getGlobalMutex() );
271 if (! s_rtti)
273 #ifdef LEAK_STATIC_DATA
274 s_rtti = new RTTI();
275 #else
276 static RTTI rtti_data;
277 s_rtti = &rtti_data;
278 #endif
281 rtti = (type_info *)s_rtti->getRTTI( (typelib_CompoundTypeDescription *) pTypeDescr );
282 TYPELIB_DANGER_RELEASE( pTypeDescr );
283 OSL_ENSURE( rtti, "### no rtti for throwing exception!" );
284 if (! rtti)
286 throw RuntimeException(
287 OUString( RTL_CONSTASCII_USTRINGPARAM("no rtti for type ") ) +
288 *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
289 Reference< XInterface >() );
293 __cxa_throw( pCppExc, rtti, deleteException );
296 //==================================================================================================
297 void fillUnoException( __cxa_exception * header, uno_Any * pUnoExc, uno_Mapping * pCpp2Uno )
299 if (! header)
301 RuntimeException aRE(
302 OUString( RTL_CONSTASCII_USTRINGPARAM("no exception header!") ),
303 Reference< XInterface >() );
304 Type const & rType = ::getCppuType( &aRE );
305 uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
306 #if OSL_DEBUG_LEVEL > 0
307 OString cstr( OUStringToOString( aRE.Message, RTL_TEXTENCODING_ASCII_US ) );
308 OSL_ENSURE( 0, cstr.getStr() );
309 #endif
310 return;
313 typelib_TypeDescription * pExcTypeDescr = 0;
314 OUString unoName( toUNOname( header->exceptionType->name() ) );
315 #if OSL_DEBUG_LEVEL > 1
316 OString cstr_unoName( OUStringToOString( unoName, RTL_TEXTENCODING_ASCII_US ) );
317 fprintf( stderr, "> c++ exception occured: %s\n", cstr_unoName.getStr() );
318 #endif
319 typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData );
320 if (0 == pExcTypeDescr)
322 RuntimeException aRE(
323 OUString( RTL_CONSTASCII_USTRINGPARAM("exception type not found: ") ) + unoName,
324 Reference< XInterface >() );
325 Type const & rType = ::getCppuType( &aRE );
326 uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
327 #if OSL_DEBUG_LEVEL > 0
328 OString cstr( OUStringToOString( aRE.Message, RTL_TEXTENCODING_ASCII_US ) );
329 OSL_ENSURE( 0, cstr.getStr() );
330 #endif
332 else
334 // construct uno exception any
335 uno_any_constructAndConvert( pUnoExc, header->adjustedPtr, pExcTypeDescr, pCpp2Uno );
336 typelib_typedescription_release( pExcTypeDescr );