Update ooo320-m1
[ooovba.git] / bridges / source / cpp_uno / gcc3_solaris_sparc / except.cxx
blob8b1d3dbf4ae0e2ddd4410cbd6bbe02a559126f12
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"
33 #include <stdio.h>
34 #include <dlfcn.h>
35 #include <cxxabi.h>
36 #include <hash_map>
38 #include <rtl/strbuf.hxx>
39 #include <rtl/ustrbuf.hxx>
40 #include <osl/diagnose.h>
41 #include <osl/mutex.hxx>
43 #include <com/sun/star/uno/genfunc.hxx>
44 #include <typelib/typedescription.hxx>
45 #include <uno/any2.h>
47 #include "share.hxx"
50 using namespace ::std;
51 using namespace ::osl;
52 using namespace ::rtl;
53 using namespace ::com::sun::star::uno;
54 using namespace ::__cxxabiv1;
57 namespace CPPU_CURRENT_NAMESPACE
60 void dummy_can_throw_anything( char const * )
64 //==================================================================================================
65 static OUString toUNOname( char const * p ) SAL_THROW( () )
67 #if defined BRIDGES_DEBUG
68 char const * start = p;
69 #endif
71 // example: N3com3sun4star4lang24IllegalArgumentExceptionE
73 OUStringBuffer buf( 64 );
74 OSL_ASSERT( 'N' == *p );
75 ++p; // skip N
77 while ('E' != *p)
79 // read chars count
80 long n = (*p++ - '0');
81 while ('0' <= *p && '9' >= *p)
83 n *= 10;
84 n += (*p++ - '0');
86 buf.appendAscii( p, n );
87 p += n;
88 if ('E' != *p)
89 buf.append( (sal_Unicode)'.' );
92 #if defined BRIDGES_DEBUG
93 OUString ret( buf.makeStringAndClear() );
94 OString c_ret( OUStringToOString( ret, RTL_TEXTENCODING_ASCII_US ) );
95 fprintf( stderr, "> toUNOname(): %s => %s\n", start, c_ret.getStr() );
96 return ret;
97 #else
98 return buf.makeStringAndClear();
99 #endif
102 //==================================================================================================
103 class RTTI
105 typedef hash_map< OUString, type_info *, OUStringHash > t_rtti_map;
107 Mutex m_mutex;
108 t_rtti_map m_rttis;
109 t_rtti_map m_generatedRttis;
111 void * m_hApp;
113 public:
114 RTTI() SAL_THROW( () );
115 ~RTTI() SAL_THROW( () );
117 type_info * getRTTI( typelib_CompoundTypeDescription * ) SAL_THROW( () );
119 //__________________________________________________________________________________________________
120 RTTI::RTTI() SAL_THROW( () )
121 : m_hApp( dlopen( 0, RTLD_LAZY ) )
124 //__________________________________________________________________________________________________
125 RTTI::~RTTI() SAL_THROW( () )
127 dlclose( m_hApp );
130 //__________________________________________________________________________________________________
131 type_info * RTTI::getRTTI( typelib_CompoundTypeDescription *pTypeDescr ) SAL_THROW( () )
133 type_info * rtti;
135 OUString const & unoName = *(OUString const *)&pTypeDescr->aBase.pTypeName;
137 MutexGuard guard( m_mutex );
138 t_rtti_map::const_iterator iFind( m_rttis.find( unoName ) );
139 if (iFind == m_rttis.end())
141 // RTTI symbol
142 OStringBuffer buf( 64 );
143 buf.append( RTL_CONSTASCII_STRINGPARAM("_ZTIN") );
144 sal_Int32 index = 0;
147 OUString token( unoName.getToken( 0, '.', index ) );
148 buf.append( token.getLength() );
149 OString c_token( OUStringToOString( token, RTL_TEXTENCODING_ASCII_US ) );
150 buf.append( c_token );
152 while (index >= 0);
153 buf.append( 'E' );
155 OString symName( buf.makeStringAndClear() );
156 rtti = (type_info *)dlsym( m_hApp, symName.getStr() );
158 if (rtti)
160 pair< t_rtti_map::iterator, bool > insertion(
161 m_rttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
162 OSL_ENSURE( insertion.second, "### inserting new rtti failed?!" );
164 else
166 // try to lookup the symbol in the generated rtti map
167 t_rtti_map::const_iterator iFind( m_generatedRttis.find( unoName ) );
168 if (iFind == m_generatedRttis.end())
170 // we must generate it !
171 // symbol and rtti-name is nearly identical,
172 // the symbol is prefixed with _ZTI
173 char const * rttiName = symName.getStr() +4;
174 #if defined BRIDGES_DEBUG
175 fprintf( stderr,"generated rtti for %s\n", rttiName );
176 #endif
177 if (pTypeDescr->pBaseTypeDescription)
179 // ensure availability of base
180 type_info * base_rtti = getRTTI(
181 (typelib_CompoundTypeDescription *)pTypeDescr->pBaseTypeDescription );
182 rtti = new __si_class_type_info(
183 strdup( rttiName ), (__class_type_info *)base_rtti );
185 else
187 // this class has no base class
188 rtti = new __class_type_info( strdup( rttiName ) );
191 pair< t_rtti_map::iterator, bool > insertion(
192 m_generatedRttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
193 OSL_ENSURE( insertion.second, "### inserting new generated rtti failed?!" );
195 else // taking already generated rtti
197 rtti = iFind->second;
201 else
203 rtti = iFind->second;
206 return rtti;
209 //--------------------------------------------------------------------------------------------------
210 static void deleteException( void * pExc )
212 __cxa_exception const * header = ((__cxa_exception const *)pExc - 1);
213 typelib_TypeDescription * pTD = 0;
214 OUString unoName( toUNOname( header->exceptionType->name() ) );
215 ::typelib_typedescription_getByName( &pTD, unoName.pData );
216 OSL_ENSURE( pTD, "### unknown exception type! leaving out destruction => leaking!!!" );
217 if (pTD)
219 ::uno_destructData( pExc, pTD, cpp_release );
220 ::typelib_typedescription_release( pTD );
224 //==================================================================================================
225 void raiseException( uno_Any * pUnoExc, uno_Mapping * pUno2Cpp )
227 #if defined BRIDGES_DEBUG
228 OString cstr(
229 OUStringToOString(
230 *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
231 RTL_TEXTENCODING_ASCII_US ) );
232 fprintf( stderr, "> uno exception occured: %s\n", cstr.getStr() );
233 #endif
234 void * pCppExc;
235 type_info * rtti;
238 // construct cpp exception object
239 typelib_TypeDescription * pTypeDescr = 0;
240 TYPELIB_DANGER_GET( &pTypeDescr, pUnoExc->pType );
241 OSL_ASSERT( pTypeDescr );
242 if (! pTypeDescr)
244 throw RuntimeException(
245 OUString( RTL_CONSTASCII_USTRINGPARAM("cannot get typedescription for type ") ) +
246 *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
247 Reference< XInterface >() );
250 pCppExc = __cxa_allocate_exception( pTypeDescr->nSize );
251 ::uno_copyAndConvertData( pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp );
253 // destruct uno exception
254 ::uno_any_destruct( pUnoExc, 0 );
255 // avoiding locked counts
256 static RTTI * s_rtti = 0;
257 if (! s_rtti)
259 MutexGuard guard( Mutex::getGlobalMutex() );
260 if (! s_rtti)
262 #ifdef LEAK_STATIC_DATA
263 s_rtti = new RTTI();
264 #else
265 static RTTI rtti_data;
266 s_rtti = &rtti_data;
267 #endif
270 rtti = (type_info *)s_rtti->getRTTI( (typelib_CompoundTypeDescription *) pTypeDescr );
271 TYPELIB_DANGER_RELEASE( pTypeDescr );
272 OSL_ENSURE( rtti, "### no rtti for throwing exception!" );
273 if (! rtti)
275 throw RuntimeException(
276 OUString( RTL_CONSTASCII_USTRINGPARAM("no rtti for type ") ) +
277 *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
278 Reference< XInterface >() );
282 __cxa_throw( pCppExc, rtti, deleteException );
285 //==================================================================================================
286 void fillUnoException( __cxa_exception * header, uno_Any * pUnoExc, uno_Mapping * pCpp2Uno )
288 if (! header)
290 RuntimeException aRE(
291 OUString( RTL_CONSTASCII_USTRINGPARAM("no exception header!") ),
292 Reference< XInterface >() );
293 Type const & rType = ::getCppuType( &aRE );
294 uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
295 #if defined _DEBUG
296 OString cstr( OUStringToOString( aRE.Message, RTL_TEXTENCODING_ASCII_US ) );
297 OSL_ENSURE( 0, cstr.getStr() );
298 #endif
299 return;
302 typelib_TypeDescription * pExcTypeDescr = 0;
303 OUString unoName( toUNOname( header->exceptionType->name() ) );
304 #if defined BRIDGES_DEBUG
305 OString cstr_unoName( OUStringToOString( unoName, RTL_TEXTENCODING_ASCII_US ) );
306 fprintf( stderr, "> c++ exception occured: %s\n", cstr_unoName.getStr() );
307 #endif
308 typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData );
309 if (0 == pExcTypeDescr)
311 RuntimeException aRE(
312 OUString( RTL_CONSTASCII_USTRINGPARAM("exception type not found: ") ) + unoName,
313 Reference< XInterface >() );
314 Type const & rType = ::getCppuType( &aRE );
315 uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
316 #if defined _DEBUG
317 OString cstr( OUStringToOString( aRE.Message, RTL_TEXTENCODING_ASCII_US ) );
318 OSL_ENSURE( 0, cstr.getStr() );
319 #endif
321 else
323 // construct uno exception any
324 uno_any_constructAndConvert( pUnoExc, header->adjustedPtr, pExcTypeDescr, pCpp2Uno );
325 typelib_typedescription_release( pExcTypeDescr );