update dev300-m58
[ooovba.git] / bridges / source / cpp_uno / gcc3_macosx_powerpc / except.cxx
blobc5a8a15aa1804fa0d86f45bb943902bff289ae11
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.7 $
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 <typelib/typedescription.hxx>
46 #include <uno/any2.h>
48 #include "share.hxx"
51 using namespace ::std;
52 using namespace ::osl;
53 using namespace ::rtl;
54 using namespace ::com::sun::star::uno;
55 using namespace ::__cxxabiv1;
58 namespace CPPU_CURRENT_NAMESPACE
61 void dummy_can_throw_anything( char const * )
65 //==================================================================================================
66 static OUString toUNOname( char const * p ) SAL_THROW( () )
68 #ifdef DEBUG
69 char const * start = p;
70 #endif
72 // example: N3com3sun4star4lang24IllegalArgumentExceptionE
74 OUStringBuffer buf( 64 );
75 OSL_ASSERT( 'N' == *p );
76 ++p; // skip N
78 while ('E' != *p)
80 // read chars count
81 long n = (*p++ - '0');
82 while ('0' <= *p && '9' >= *p)
84 n *= 10;
85 n += (*p++ - '0');
87 buf.appendAscii( p, n );
88 p += n;
89 if ('E' != *p)
90 buf.append( (sal_Unicode)'.' );
93 #ifdef DEBUG
94 OUString ret( buf.makeStringAndClear() );
95 OString c_ret( OUStringToOString( ret, RTL_TEXTENCODING_ASCII_US ) );
96 fprintf( stderr, "> toUNOname(): %s => %s\n", start, c_ret.getStr() );
97 return ret;
98 #else
99 return buf.makeStringAndClear();
100 #endif
103 //==================================================================================================
104 class RTTI
106 typedef hash_map< OUString, type_info *, OUStringHash > t_rtti_map;
108 Mutex m_mutex;
109 t_rtti_map m_rttis;
110 t_rtti_map m_generatedRttis;
112 void * m_hApp;
114 public:
115 RTTI() SAL_THROW( () );
116 ~RTTI() SAL_THROW( () );
118 type_info * getRTTI( typelib_CompoundTypeDescription * ) SAL_THROW( () );
120 //__________________________________________________________________________________________________
121 RTTI::RTTI() SAL_THROW( () )
122 : m_hApp( dlopen( 0, RTLD_LAZY ) )
125 //__________________________________________________________________________________________________
126 RTTI::~RTTI() SAL_THROW( () )
128 dlclose( m_hApp );
131 //__________________________________________________________________________________________________
132 type_info * RTTI::getRTTI( typelib_CompoundTypeDescription *pTypeDescr ) SAL_THROW( () )
134 type_info * rtti;
136 OUString const & unoName = *(OUString const *)&pTypeDescr->aBase.pTypeName;
138 MutexGuard guard( m_mutex );
139 t_rtti_map::const_iterator iFind( m_rttis.find( unoName ) );
140 if (iFind == m_rttis.end())
142 // RTTI symbol
143 OStringBuffer buf( 64 );
144 buf.append( RTL_CONSTASCII_STRINGPARAM("_ZTIN") );
145 sal_Int32 index = 0;
148 OUString token( unoName.getToken( 0, '.', index ) );
149 buf.append( token.getLength() );
150 OString c_token( OUStringToOString( token, RTL_TEXTENCODING_ASCII_US ) );
151 buf.append( c_token );
153 while (index >= 0);
154 buf.append( 'E' );
156 OString symName( buf.makeStringAndClear() );
157 rtti = (type_info *)dlsym( m_hApp, symName.getStr() );
159 if (rtti)
161 pair< t_rtti_map::iterator, bool > insertion(
162 m_rttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
163 OSL_ENSURE( insertion.second, "### inserting new rtti failed?!" );
165 else
167 // try to lookup the symbol in the generated rtti map
168 t_rtti_map::const_iterator iiFind( m_generatedRttis.find( unoName ) );
169 if (iiFind == m_generatedRttis.end())
171 // we must generate it !
172 // symbol and rtti-name is nearly identical,
173 // the symbol is prefixed with _ZTI
174 char const * rttiName = symName.getStr() +4;
175 #ifdef DEBUG
176 fprintf( stderr,"generated rtti for %s\n", rttiName );
177 #endif
178 if (pTypeDescr->pBaseTypeDescription)
180 // ensure availability of base
181 type_info * base_rtti = getRTTI(
182 (typelib_CompoundTypeDescription *)pTypeDescr->pBaseTypeDescription );
183 rtti = new __si_class_type_info(
184 strdup( rttiName ), (__class_type_info *)base_rtti );
186 else
188 // this class has no base class
189 rtti = new __class_type_info( strdup( rttiName ) );
192 pair< t_rtti_map::iterator, bool > insertion(
193 m_generatedRttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
194 OSL_ENSURE( insertion.second, "### inserting new generated rtti failed?!" );
196 else // taking already generated rtti
198 rtti = iiFind->second;
202 else
204 rtti = iFind->second;
207 return rtti;
210 //--------------------------------------------------------------------------------------------------
211 static void deleteException( void * pExc )
213 __cxa_exception const * header = ((__cxa_exception const *)pExc - 1);
214 typelib_TypeDescription * pTD = 0;
215 OUString unoName( toUNOname( header->exceptionType->name() ) );
216 ::typelib_typedescription_getByName( &pTD, unoName.pData );
217 OSL_ENSURE( pTD, "### unknown exception type! leaving out destruction => leaking!!!" );
218 if (pTD)
220 ::uno_destructData( pExc, pTD, cpp_release );
221 ::typelib_typedescription_release( pTD );
225 //==================================================================================================
226 void raiseException( uno_Any * pUnoExc, uno_Mapping * pUno2Cpp )
228 void * pCppExc;
229 type_info * rtti;
232 // construct cpp exception object
233 typelib_TypeDescription * pTypeDescr = 0;
234 TYPELIB_DANGER_GET( &pTypeDescr, pUnoExc->pType );
235 OSL_ASSERT( pTypeDescr );
236 if (! pTypeDescr)
237 terminate();
239 pCppExc = __cxa_allocate_exception( pTypeDescr->nSize );
240 ::uno_copyAndConvertData( pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp );
242 // destruct uno exception
243 ::uno_any_destruct( pUnoExc, 0 );
244 // avoiding locked counts
245 static RTTI * s_rtti = 0;
246 if (! s_rtti)
248 MutexGuard guard( Mutex::getGlobalMutex() );
249 if (! s_rtti)
251 #ifdef LEAK_STATIC_DATA
252 s_rtti = new RTTI();
253 #else
254 static RTTI rtti_data;
255 s_rtti = &rtti_data;
256 #endif
259 rtti = (type_info *)s_rtti->getRTTI( (typelib_CompoundTypeDescription *) pTypeDescr );
260 TYPELIB_DANGER_RELEASE( pTypeDescr );
261 OSL_ENSURE( rtti, "### no rtti for throwing exception!" );
262 if (! rtti)
263 terminate();
266 __cxa_throw( pCppExc, rtti, deleteException );
269 //==================================================================================================
270 void fillUnoException( __cxa_exception * header, uno_Any * pExc, uno_Mapping * pCpp2Uno )
272 OSL_ENSURE( header, "### no exception header!!!" );
273 if (! header)
274 terminate();
276 typelib_TypeDescription * pExcTypeDescr = 0;
277 OUString unoName( toUNOname( header->exceptionType->name() ) );
278 ::typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData );
279 OSL_ENSURE( pExcTypeDescr, "### can not get type description for exception!!!" );
280 if (! pExcTypeDescr)
281 terminate();
283 // construct uno exception any
284 ::uno_any_constructAndConvert( pExc, header->adjustedPtr, pExcTypeDescr, pCpp2Uno );
285 ::typelib_typedescription_release( pExcTypeDescr );