merge the formfield patch from ooo-build
[ooovba.git] / bridges / source / cpp_uno / gcc3_macosx_intel / except.cxx
blobe862494525a148cbb67ca55bc13026ece7757ee0
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>
39 #include <rtl/strbuf.hxx>
40 #include <rtl/ustrbuf.hxx>
41 #include <osl/diagnose.h>
42 #include <osl/mutex.hxx>
44 #include <com/sun/star/uno/genfunc.hxx>
45 #include "com/sun/star/uno/RuntimeException.hpp"
46 #include <typelib/typedescription.hxx>
47 #include <uno/any2.h>
49 #include "share.hxx"
52 using namespace ::std;
53 using namespace ::osl;
54 using namespace ::rtl;
55 using namespace ::com::sun::star::uno;
56 using namespace ::__cxxabiv1;
59 namespace CPPU_CURRENT_NAMESPACE
62 void dummy_can_throw_anything( char const * )
66 //==================================================================================================
67 static OUString toUNOname( char const * p ) SAL_THROW( () )
69 #if OSL_DEBUG_LEVEL > 1
70 char const * start = p;
71 #endif
73 // example: N3com3sun4star4lang24IllegalArgumentExceptionE
75 OUStringBuffer buf( 64 );
76 OSL_ASSERT( 'N' == *p );
77 ++p; // skip N
79 while ('E' != *p)
81 // read chars count
82 long n = (*p++ - '0');
83 while ('0' <= *p && '9' >= *p)
85 n *= 10;
86 n += (*p++ - '0');
88 buf.appendAscii( p, n );
89 p += n;
90 if ('E' != *p)
91 buf.append( (sal_Unicode)'.' );
94 #if OSL_DEBUG_LEVEL > 1
95 OUString ret( buf.makeStringAndClear() );
96 OString c_ret( OUStringToOString( ret, RTL_TEXTENCODING_ASCII_US ) );
97 fprintf( stderr, "> toUNOname(): %s => %s\n", start, c_ret.getStr() );
98 return ret;
99 #else
100 return buf.makeStringAndClear();
101 #endif
104 //==================================================================================================
105 class RTTI
107 typedef hash_map< OUString, type_info *, OUStringHash > t_rtti_map;
109 Mutex m_mutex;
110 t_rtti_map m_rttis;
111 t_rtti_map m_generatedRttis;
113 void * m_hApp;
115 public:
116 RTTI() SAL_THROW( () );
117 ~RTTI() SAL_THROW( () );
119 type_info * getRTTI( typelib_CompoundTypeDescription * ) SAL_THROW( () );
121 //__________________________________________________________________________________________________
122 RTTI::RTTI() SAL_THROW( () )
123 : m_hApp( dlopen( 0, RTLD_LAZY ) )
126 //__________________________________________________________________________________________________
127 RTTI::~RTTI() SAL_THROW( () )
129 dlclose( m_hApp );
132 //__________________________________________________________________________________________________
133 type_info * RTTI::getRTTI( typelib_CompoundTypeDescription *pTypeDescr ) SAL_THROW( () )
135 type_info * rtti;
137 OUString const & unoName = *(OUString const *)&pTypeDescr->aBase.pTypeName;
139 MutexGuard guard( m_mutex );
140 t_rtti_map::const_iterator iFind( m_rttis.find( unoName ) );
141 if (iFind == m_rttis.end())
143 // RTTI symbol
144 OStringBuffer buf( 64 );
145 buf.append( RTL_CONSTASCII_STRINGPARAM("_ZTIN") );
146 sal_Int32 index = 0;
149 OUString token( unoName.getToken( 0, '.', index ) );
150 buf.append( token.getLength() );
151 OString c_token( OUStringToOString( token, RTL_TEXTENCODING_ASCII_US ) );
152 buf.append( c_token );
154 while (index >= 0);
155 buf.append( 'E' );
157 OString symName( buf.makeStringAndClear() );
158 rtti = (type_info *)dlsym( m_hApp, symName.getStr() );
160 if (rtti)
162 pair< t_rtti_map::iterator, bool > insertion(
163 m_rttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
164 OSL_ENSURE( insertion.second, "### inserting new rtti failed?!" );
166 else
168 // try to lookup the symbol in the generated rtti map
169 t_rtti_map::const_iterator iFind2( m_generatedRttis.find( unoName ) );
170 if (iFind2 == m_generatedRttis.end())
172 // we must generate it !
173 // symbol and rtti-name is nearly identical,
174 // the symbol is prefixed with _ZTI
175 char const * rttiName = symName.getStr() +4;
176 #if OSL_DEBUG_LEVEL > 1
177 fprintf( stderr,"generated rtti for %s\n", rttiName );
178 #endif
179 if (pTypeDescr->pBaseTypeDescription)
181 // ensure availability of base
182 type_info * base_rtti = getRTTI(
183 (typelib_CompoundTypeDescription *)pTypeDescr->pBaseTypeDescription );
184 rtti = new __si_class_type_info(
185 strdup( rttiName ), (__class_type_info *)base_rtti );
187 else
189 // this class has no base class
190 rtti = new __class_type_info( strdup( rttiName ) );
193 pair< t_rtti_map::iterator, bool > insertion(
194 m_generatedRttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
195 OSL_ENSURE( insertion.second, "### inserting new generated rtti failed?!" );
197 else // taking already generated rtti
199 rtti = iFind2->second;
203 else
205 rtti = iFind->second;
208 return rtti;
211 //--------------------------------------------------------------------------------------------------
212 static void deleteException( void * pExc )
214 __cxa_exception const * header = ((__cxa_exception const *)pExc - 1);
215 typelib_TypeDescription * pTD = 0;
216 OUString unoName( toUNOname( header->exceptionType->name() ) );
217 ::typelib_typedescription_getByName( &pTD, unoName.pData );
218 OSL_ENSURE( pTD, "### unknown exception type! leaving out destruction => leaking!!!" );
219 if (pTD)
221 ::uno_destructData( pExc, pTD, cpp_release );
222 ::typelib_typedescription_release( pTD );
226 //==================================================================================================
227 void raiseException( uno_Any * pUnoExc, uno_Mapping * pUno2Cpp )
229 #if OSL_DEBUG_LEVEL > 1
230 OString cstr(
231 OUStringToOString(
232 *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
233 RTL_TEXTENCODING_ASCII_US ) );
234 fprintf( stderr, "> uno exception occured: %s\n", cstr.getStr() );
235 #endif
236 void * pCppExc;
237 type_info * rtti;
240 // construct cpp exception object
241 typelib_TypeDescription * pTypeDescr = 0;
242 TYPELIB_DANGER_GET( &pTypeDescr, pUnoExc->pType );
243 OSL_ASSERT( pTypeDescr );
244 if (! pTypeDescr)
246 throw RuntimeException(
247 OUString( RTL_CONSTASCII_USTRINGPARAM("cannot get typedescription for type ") ) +
248 *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
249 Reference< XInterface >() );
252 pCppExc = __cxa_allocate_exception( pTypeDescr->nSize );
253 ::uno_copyAndConvertData( pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp );
255 // destruct uno exception
256 ::uno_any_destruct( pUnoExc, 0 );
257 // avoiding locked counts
258 static RTTI * s_rtti = 0;
259 if (! s_rtti)
261 MutexGuard guard( Mutex::getGlobalMutex() );
262 if (! s_rtti)
264 #ifdef LEAK_STATIC_DATA
265 s_rtti = new RTTI();
266 #else
267 static RTTI rtti_data;
268 s_rtti = &rtti_data;
269 #endif
272 rtti = (type_info *)s_rtti->getRTTI( (typelib_CompoundTypeDescription *) pTypeDescr );
273 TYPELIB_DANGER_RELEASE( pTypeDescr );
274 OSL_ENSURE( rtti, "### no rtti for throwing exception!" );
275 if (! rtti)
277 throw RuntimeException(
278 OUString( RTL_CONSTASCII_USTRINGPARAM("no rtti for type ") ) +
279 *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
280 Reference< XInterface >() );
284 __cxa_throw( pCppExc, rtti, deleteException );
287 //==================================================================================================
288 void fillUnoException( __cxa_exception * header, uno_Any * pUnoExc, uno_Mapping * pCpp2Uno )
290 if (! header)
292 RuntimeException aRE(
293 OUString( RTL_CONSTASCII_USTRINGPARAM("no exception header!") ),
294 Reference< XInterface >() );
295 Type const & rType = ::getCppuType( &aRE );
296 uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
297 #if OSL_DEBUG_LEVEL > 0
298 OString cstr( OUStringToOString( aRE.Message, RTL_TEXTENCODING_ASCII_US ) );
299 OSL_ENSURE( 0, cstr.getStr() );
300 #endif
301 return;
304 typelib_TypeDescription * pExcTypeDescr = 0;
305 OUString unoName( toUNOname( header->exceptionType->name() ) );
306 #if OSL_DEBUG_LEVEL > 1
307 OString cstr_unoName( OUStringToOString( unoName, RTL_TEXTENCODING_ASCII_US ) );
308 fprintf( stderr, "> c++ exception occured: %s\n", cstr_unoName.getStr() );
309 #endif
310 typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData );
311 if (0 == pExcTypeDescr)
313 RuntimeException aRE(
314 OUString( RTL_CONSTASCII_USTRINGPARAM("exception type not found: ") ) + unoName,
315 Reference< XInterface >() );
316 Type const & rType = ::getCppuType( &aRE );
317 uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
318 #if OSL_DEBUG_LEVEL > 0
319 OString cstr( OUStringToOString( aRE.Message, RTL_TEXTENCODING_ASCII_US ) );
320 OSL_ENSURE( 0, cstr.getStr() );
321 #endif
323 else
325 // construct uno exception any
326 uno_any_constructAndConvert( pUnoExc, header->adjustedPtr, pExcTypeDescr, pCpp2Uno );
327 typelib_typedescription_release( pExcTypeDescr );