Update ooo320-m1
[ooovba.git] / bridges / source / cpp_uno / gcc3_linux_s390x / except.cxx
blobac959ef7d6faf3e656edc0a68712663c00a125ea
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 $
11 * $Revision: 1.3 $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_bridges.hxx"
35 #include <stdio.h>
36 #include <string.h>
37 #include <dlfcn.h>
38 #include <cxxabi.h>
39 #include <hash_map>
41 #include <rtl/strbuf.hxx>
42 #include <rtl/ustrbuf.hxx>
43 #include <osl/diagnose.h>
44 #include <osl/mutex.hxx>
46 #include <com/sun/star/uno/genfunc.hxx>
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 : m_hApp( dlopen( 0, RTLD_LAZY ) )
127 //__________________________________________________________________________________________________
128 RTTI::~RTTI() SAL_THROW( () )
130 dlclose( m_hApp );
133 //__________________________________________________________________________________________________
134 type_info * RTTI::getRTTI( typelib_CompoundTypeDescription *pTypeDescr ) SAL_THROW( () )
136 type_info * rtti;
138 OUString const & unoName = *(OUString const *)&pTypeDescr->aBase.pTypeName;
140 MutexGuard guard( m_mutex );
141 t_rtti_map::const_iterator iFind( m_rttis.find( unoName ) );
142 if (iFind == m_rttis.end())
144 // RTTI symbol
145 OStringBuffer buf( 64 );
146 buf.append( RTL_CONSTASCII_STRINGPARAM("_ZTIN") );
147 sal_Int32 index = 0;
150 OUString token( unoName.getToken( 0, '.', index ) );
151 buf.append( token.getLength() );
152 OString c_token( OUStringToOString( token, RTL_TEXTENCODING_ASCII_US ) );
153 buf.append( c_token );
155 while (index >= 0);
156 buf.append( 'E' );
158 OString symName( buf.makeStringAndClear() );
159 rtti = (type_info *)dlsym( m_hApp, symName.getStr() );
161 if (rtti)
163 pair< t_rtti_map::iterator, bool > insertion(
164 m_rttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
165 OSL_ENSURE( insertion.second, "### inserting new rtti failed?!" );
167 else
169 // try to lookup the symbol in the generated rtti map
170 t_rtti_map::const_iterator iFind( m_generatedRttis.find( unoName ) );
171 if (iFind == m_generatedRttis.end())
173 // we must generate it !
174 // symbol and rtti-name is nearly identical,
175 // the symbol is prefixed with _ZTI
176 char const * rttiName = symName.getStr() +4;
177 #if OSL_DEBUG_LEVEL > 1
178 fprintf( stderr,"generated rtti for %s\n", rttiName );
179 #endif
180 if (pTypeDescr->pBaseTypeDescription)
182 // ensure availability of base
183 type_info * base_rtti = getRTTI(
184 (typelib_CompoundTypeDescription *)pTypeDescr->pBaseTypeDescription );
185 rtti = new __si_class_type_info(
186 strdup( rttiName ), (__class_type_info *)base_rtti );
188 else
190 // this class has no base class
191 rtti = new __class_type_info( strdup( rttiName ) );
194 pair< t_rtti_map::iterator, bool > insertion(
195 m_generatedRttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
196 OSL_ENSURE( insertion.second, "### inserting new generated rtti failed?!" );
198 else // taking already generated rtti
200 rtti = iFind->second;
204 else
206 rtti = iFind->second;
209 return rtti;
212 //--------------------------------------------------------------------------------------------------
213 static void deleteException( void * pExc )
215 __cxa_exception const * header = ((__cxa_exception const *)pExc - 1);
216 typelib_TypeDescription * pTD = 0;
217 OUString unoName( toUNOname( header->exceptionType->name() ) );
218 ::typelib_typedescription_getByName( &pTD, unoName.pData );
219 OSL_ENSURE( pTD, "### unknown exception type! leaving out destruction => leaking!!!" );
220 if (pTD)
222 ::uno_destructData( pExc, pTD, cpp_release );
223 ::typelib_typedescription_release( pTD );
227 //==================================================================================================
228 void raiseException( uno_Any * pUnoExc, uno_Mapping * pUno2Cpp )
230 void * pCppExc;
231 type_info * rtti;
234 // construct cpp exception object
235 typelib_TypeDescription * pTypeDescr = 0;
236 TYPELIB_DANGER_GET( &pTypeDescr, pUnoExc->pType );
237 OSL_ASSERT( pTypeDescr );
238 if (! pTypeDescr)
239 terminate();
241 pCppExc = __cxa_allocate_exception( pTypeDescr->nSize );
242 ::uno_copyAndConvertData( pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp );
244 // destruct uno exception
245 ::uno_any_destruct( pUnoExc, 0 );
246 // avoiding locked counts
247 static RTTI * s_rtti = 0;
248 if (! s_rtti)
250 MutexGuard guard( Mutex::getGlobalMutex() );
251 if (! s_rtti)
253 #ifdef LEAK_STATIC_DATA
254 s_rtti = new RTTI();
255 #else
256 static RTTI rtti_data;
257 s_rtti = &rtti_data;
258 #endif
261 rtti = (type_info *)s_rtti->getRTTI( (typelib_CompoundTypeDescription *) pTypeDescr );
262 TYPELIB_DANGER_RELEASE( pTypeDescr );
263 OSL_ENSURE( rtti, "### no rtti for throwing exception!" );
264 if (! rtti)
265 terminate();
268 __cxa_throw( pCppExc, rtti, deleteException );
271 //==================================================================================================
272 void fillUnoException( __cxa_exception * header, uno_Any * pExc, uno_Mapping * pCpp2Uno )
274 OSL_ENSURE( header, "### no exception header!!!" );
275 if (! header)
276 terminate();
278 typelib_TypeDescription * pExcTypeDescr = 0;
279 OUString unoName( toUNOname( header->exceptionType->name() ) );
280 ::typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData );
281 OSL_ENSURE( pExcTypeDescr, "### can not get type description for exception!!!" );
282 if (! pExcTypeDescr)
283 terminate();
285 // construct uno exception any
286 ::uno_any_constructAndConvert( pExc, header->adjustedPtr, pExcTypeDescr, pCpp2Uno );
287 ::typelib_typedescription_release( pExcTypeDescr );
291 /* vi:set tabstop=4 shiftwidth=4 expandtab: */