Update ooo320-m1
[ooovba.git] / bridges / source / cpp_uno / gcc3_netbsd_intel / except.cxx
blob229e3d5e95d884b2b2d34da093086c278f69f777
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 <bridges/cpp_uno/bridge.hxx>
45 #include <typelib/typedescription.hxx>
46 #include <uno/any2.h>
48 #include "share.hxx"
50 #ifndef RTLD_DEFAULT
51 #define RTLD_DEFAULT ((void *) -2)
52 #endif
54 using namespace ::std;
55 using namespace ::osl;
56 using namespace ::rtl;
57 using namespace ::com::sun::star::uno;
58 using namespace ::__cxxabiv1;
61 namespace CPPU_CURRENT_NAMESPACE
64 void dummy_can_throw_anything( char const * )
68 //==================================================================================================
69 static OUString toUNOname( char const * p ) SAL_THROW( () )
71 #if OSL_DEBUG_LEVEL > 1
72 char const * start = p;
73 #endif
75 // example: N3com3sun4star4lang24IllegalArgumentExceptionE
77 OUStringBuffer buf( 64 );
78 OSL_ASSERT( 'N' == *p );
79 ++p; // skip N
81 while ('E' != *p)
83 // read chars count
84 long n = (*p++ - '0');
85 while ('0' <= *p && '9' >= *p)
87 n *= 10;
88 n += (*p++ - '0');
90 buf.appendAscii( p, n );
91 p += n;
92 if ('E' != *p)
93 buf.append( (sal_Unicode)'.' );
96 #if OSL_DEBUG_LEVEL > 1
97 OUString ret( buf.makeStringAndClear() );
98 OString c_ret( OUStringToOString( ret, RTL_TEXTENCODING_ASCII_US ) );
99 fprintf( stderr, "> toUNOname(): %s => %s\n", start, c_ret.getStr() );
100 return ret;
101 #else
102 return buf.makeStringAndClear();
103 #endif
106 //==================================================================================================
107 class RTTI
109 typedef hash_map< OUString, type_info *, OUStringHash > t_rtti_map;
111 Mutex m_mutex;
112 t_rtti_map m_rttis;
113 t_rtti_map m_generatedRttis;
115 void * m_hApp;
117 public:
118 RTTI() SAL_THROW( () );
119 ~RTTI() SAL_THROW( () );
121 type_info * getRTTI( typelib_CompoundTypeDescription * ) SAL_THROW( () );
123 //__________________________________________________________________________________________________
124 RTTI::RTTI() SAL_THROW( () )
125 : m_hApp( dlopen( 0 , RTLD_LAZY ) )
128 //__________________________________________________________________________________________________
129 RTTI::~RTTI() SAL_THROW( () )
131 dlclose( m_hApp );
134 //__________________________________________________________________________________________________
135 type_info * RTTI::getRTTI( typelib_CompoundTypeDescription *pTypeDescr ) SAL_THROW( () )
137 type_info * rtti;
139 OUString const & unoName = *(OUString const *)&pTypeDescr->aBase.pTypeName;
141 MutexGuard guard( m_mutex );
142 t_rtti_map::const_iterator iFind( m_rttis.find( unoName ) );
143 if (iFind == m_rttis.end())
145 // RTTI symbol
146 OStringBuffer buf( 64 );
147 buf.append( RTL_CONSTASCII_STRINGPARAM("_ZTIN") );
148 sal_Int32 index = 0;
151 OUString token( unoName.getToken( 0, '.', index ) );
152 buf.append( token.getLength() );
153 OString c_token( OUStringToOString( token, RTL_TEXTENCODING_ASCII_US ) );
154 buf.append( c_token );
156 while (index >= 0);
157 buf.append( 'E' );
159 OString symName( buf.makeStringAndClear() );
160 rtti = (type_info *)dlsym( RTLD_DEFAULT, symName.getStr() );
162 if (rtti)
164 pair< t_rtti_map::iterator, bool > insertion(
165 m_rttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
166 OSL_ENSURE( insertion.second, "### inserting new rtti failed?!" );
168 else
170 // try to lookup the symbol in the generated rtti map
171 t_rtti_map::const_iterator iFind( m_generatedRttis.find( unoName ) );
172 if (iFind == m_generatedRttis.end())
174 // we must generate it !
175 // symbol and rtti-name is nearly identical,
176 // the symbol is prefixed with _ZTI
177 char const * rttiName = symName.getStr() +4;
178 #if OSL_DEBUG_LEVEL > 1
179 fprintf( stderr,"generated rtti for %s\n", rttiName );
180 #endif
181 if (pTypeDescr->pBaseTypeDescription)
183 // ensure availability of base
184 type_info * base_rtti = getRTTI(
185 (typelib_CompoundTypeDescription *)pTypeDescr->pBaseTypeDescription );
186 rtti = new __si_class_type_info(
187 strdup( rttiName ), (__class_type_info *)base_rtti );
189 else
191 // this class has no base class
192 rtti = new __class_type_info( strdup( rttiName ) );
195 pair< t_rtti_map::iterator, bool > insertion(
196 m_generatedRttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
197 OSL_ENSURE( insertion.second, "### inserting new generated rtti failed?!" );
199 else // taking already generated rtti
201 rtti = iFind->second;
205 else
207 rtti = iFind->second;
210 return rtti;
213 //--------------------------------------------------------------------------------------------------
214 static void deleteException( void * pExc )
216 __cxa_exception const * header = ((__cxa_exception const *)pExc - 1);
217 typelib_TypeDescription * pTD = 0;
218 OUString unoName( toUNOname( header->exceptionType->name() ) );
219 ::typelib_typedescription_getByName( &pTD, unoName.pData );
220 OSL_ENSURE( pTD, "### unknown exception type! leaving out destruction => leaking!!!" );
221 if (pTD)
223 ::uno_destructData( pExc, pTD, cpp_release );
224 ::typelib_typedescription_release( pTD );
228 //==================================================================================================
229 void raiseException( uno_Any * pUnoExc, uno_Mapping * pUno2Cpp )
231 #if defined DEBUG
232 OString cstr(
233 OUStringToOString(
234 *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
235 RTL_TEXTENCODING_ASCII_US ) );
236 fprintf( stderr, "> uno exception occured: %s\n", cstr.getStr() );
237 #endif
238 void * pCppExc;
239 type_info * rtti;
242 // construct cpp exception object
243 typelib_TypeDescription * pTypeDescr = 0;
244 TYPELIB_DANGER_GET( &pTypeDescr, pUnoExc->pType );
245 OSL_ASSERT( pTypeDescr );
246 if (! pTypeDescr)
248 throw RuntimeException(
249 OUString( RTL_CONSTASCII_USTRINGPARAM("cannot get typedescription for type ") ) +
250 *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
251 Reference< XInterface >() );
254 pCppExc = __cxa_allocate_exception( pTypeDescr->nSize );
255 ::uno_copyAndConvertData( pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp );
257 // destruct uno exception
258 ::uno_any_destruct( pUnoExc, 0 );
259 // avoiding locked counts
260 static RTTI * s_rtti = 0;
261 if (! s_rtti)
263 MutexGuard guard( Mutex::getGlobalMutex() );
264 if (! s_rtti)
266 #ifdef LEAK_STATIC_DATA
267 s_rtti = new RTTI();
268 #else
269 static RTTI rtti_data;
270 s_rtti = &rtti_data;
271 #endif
274 rtti = (type_info *)s_rtti->getRTTI( (typelib_CompoundTypeDescription *) pTypeDescr );
275 TYPELIB_DANGER_RELEASE( pTypeDescr );
276 OSL_ENSURE( rtti, "### no rtti for throwing exception!" );
277 if (! rtti)
279 throw RuntimeException(
280 OUString( RTL_CONSTASCII_USTRINGPARAM("no rtti for type ") ) +
281 *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
282 Reference< XInterface >() );
286 __cxa_throw( pCppExc, rtti, deleteException );
289 //==================================================================================================
290 void fillUnoException( __cxa_exception * header, uno_Any * pUnoExc, uno_Mapping * pCpp2Uno )
292 if (! header)
294 RuntimeException aRE(
295 OUString( RTL_CONSTASCII_USTRINGPARAM("no exception header!") ),
296 Reference< XInterface >() );
297 Type const & rType = ::getCppuType( &aRE );
298 uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
299 #if defined _DEBUG
300 OString cstr( OUStringToOString( aRE.Message, RTL_TEXTENCODING_ASCII_US ) );
301 OSL_ENSURE( 0, cstr.getStr() );
302 #endif
303 return;
306 typelib_TypeDescription * pExcTypeDescr = 0;
307 OUString unoName( toUNOname( header->exceptionType->name() ) );
308 #if defined DEBUG
309 OString cstr_unoName( OUStringToOString( unoName, RTL_TEXTENCODING_ASCII_US ) );
310 fprintf( stderr, "> c++ exception occured: %s\n", cstr_unoName.getStr() );
311 #endif
312 typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData );
313 if (0 == pExcTypeDescr)
315 RuntimeException aRE(
316 OUString( RTL_CONSTASCII_USTRINGPARAM("exception type not found: ") ) + unoName,
317 Reference< XInterface >() );
318 Type const & rType = ::getCppuType( &aRE );
319 uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
320 #if defined _DEBUG
321 OString cstr( OUStringToOString( aRE.Message, RTL_TEXTENCODING_ASCII_US ) );
322 OSL_ENSURE( 0, cstr.getStr() );
323 #endif
325 else
327 // construct uno exception any
328 uno_any_constructAndConvert( pUnoExc, header->adjustedPtr, pExcTypeDescr, pCpp2Uno );
329 typelib_typedescription_release( pExcTypeDescr );