Bump for 3.6-28
[LibreOffice.git] / bridges / source / cpp_uno / gcc3_linux_s390x / except.cxx
blob09cdb4526d6c5105d2801854ad49cf0a2e7fc932
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include <stdio.h>
31 #include <string.h>
32 #include <dlfcn.h>
33 #include <cxxabi.h>
34 #include <boost/unordered_map.hpp>
36 #include <rtl/instance.hxx>
37 #include <rtl/strbuf.hxx>
38 #include <rtl/ustrbuf.hxx>
39 #include <osl/diagnose.h>
40 #include <osl/mutex.hxx>
42 #include <com/sun/star/uno/genfunc.hxx>
43 #include <typelib/typedescription.hxx>
44 #include <uno/any2.h>
46 #include "share.hxx"
49 using namespace ::std;
50 using namespace ::osl;
51 using namespace ::rtl;
52 using namespace ::com::sun::star::uno;
53 using namespace ::__cxxabiv1;
56 namespace CPPU_CURRENT_NAMESPACE
59 void dummy_can_throw_anything( char const * )
63 //==================================================================================================
64 static OUString toUNOname( char const * p ) SAL_THROW(())
66 #if OSL_DEBUG_LEVEL > 1
67 char const * start = p;
68 #endif
70 // example: N3com3sun4star4lang24IllegalArgumentExceptionE
72 OUStringBuffer buf( 64 );
73 OSL_ASSERT( 'N' == *p );
74 ++p; // skip N
76 while ('E' != *p)
78 // read chars count
79 long n = (*p++ - '0');
80 while ('0' <= *p && '9' >= *p)
82 n *= 10;
83 n += (*p++ - '0');
85 buf.appendAscii( p, n );
86 p += n;
87 if ('E' != *p)
88 buf.append( (sal_Unicode)'.' );
91 #if OSL_DEBUG_LEVEL > 1
92 OUString ret( buf.makeStringAndClear() );
93 OString c_ret( OUStringToOString( ret, RTL_TEXTENCODING_ASCII_US ) );
94 fprintf( stderr, "> toUNOname(): %s => %s\n", start, c_ret.getStr() );
95 return ret;
96 #else
97 return buf.makeStringAndClear();
98 #endif
101 //==================================================================================================
102 class RTTI
104 typedef boost::unordered_map< OUString, type_info *, OUStringHash > t_rtti_map;
106 Mutex m_mutex;
107 t_rtti_map m_rttis;
108 t_rtti_map m_generatedRttis;
110 void * m_hApp;
112 public:
113 RTTI() SAL_THROW(());
114 ~RTTI() SAL_THROW(());
116 type_info * getRTTI( typelib_CompoundTypeDescription * ) SAL_THROW(());
118 //__________________________________________________________________________________________________
119 RTTI::RTTI() SAL_THROW(())
120 : m_hApp( dlopen( 0, RTLD_LAZY ) )
123 //__________________________________________________________________________________________________
124 RTTI::~RTTI() SAL_THROW(())
126 dlclose( m_hApp );
129 //__________________________________________________________________________________________________
130 type_info * RTTI::getRTTI( typelib_CompoundTypeDescription *pTypeDescr ) SAL_THROW(())
132 type_info * rtti;
134 OUString const & unoName = *(OUString const *)&pTypeDescr->aBase.pTypeName;
136 MutexGuard guard( m_mutex );
137 t_rtti_map::const_iterator iRttiFind( m_rttis.find( unoName ) );
138 if (iRttiFind == m_rttis.end())
140 // RTTI symbol
141 OStringBuffer buf( 64 );
142 buf.append( RTL_CONSTASCII_STRINGPARAM("_ZTIN") );
143 sal_Int32 index = 0;
146 OUString token( unoName.getToken( 0, '.', index ) );
147 buf.append( token.getLength() );
148 OString c_token( OUStringToOString( token, RTL_TEXTENCODING_ASCII_US ) );
149 buf.append( c_token );
151 while (index >= 0);
152 buf.append( 'E' );
154 OString symName( buf.makeStringAndClear() );
155 rtti = (type_info *)dlsym( m_hApp, symName.getStr() );
157 if (rtti)
159 pair< t_rtti_map::iterator, bool > insertion(
160 m_rttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
161 OSL_ENSURE( insertion.second, "### inserting new rtti failed?!" );
163 else
165 // try to lookup the symbol in the generated rtti map
166 t_rtti_map::const_iterator iFind( m_generatedRttis.find( unoName ) );
167 if (iFind == m_generatedRttis.end())
169 // we must generate it !
170 // symbol and rtti-name is nearly identical,
171 // the symbol is prefixed with _ZTI
172 char const * rttiName = symName.getStr() +4;
173 #if OSL_DEBUG_LEVEL > 1
174 fprintf( stderr,"generated rtti for %s\n", rttiName );
175 #endif
176 if (pTypeDescr->pBaseTypeDescription)
178 // ensure availability of base
179 type_info * base_rtti = getRTTI(
180 (typelib_CompoundTypeDescription *)pTypeDescr->pBaseTypeDescription );
181 rtti = new __si_class_type_info(
182 strdup( rttiName ), (__class_type_info *)base_rtti );
184 else
186 // this class has no base class
187 rtti = new __class_type_info( strdup( rttiName ) );
190 pair< t_rtti_map::iterator, bool > insertion(
191 m_generatedRttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
192 OSL_ENSURE( insertion.second, "### inserting new generated rtti failed?!" );
194 else // taking already generated rtti
196 rtti = iFind->second;
200 else
202 rtti = iRttiFind->second;
205 return rtti;
208 struct RTTISingleton: public rtl::Static< RTTI, RTTISingleton > {};
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 rtti = (type_info *)RTTISingleton::get().getRTTI( (typelib_CompoundTypeDescription *) pTypeDescr );
245 TYPELIB_DANGER_RELEASE( pTypeDescr );
246 OSL_ENSURE( rtti, "### no rtti for throwing exception!" );
247 if (! rtti)
248 terminate();
251 __cxa_throw( pCppExc, rtti, deleteException );
254 //==================================================================================================
255 void fillUnoException( __cxa_exception * header, uno_Any * pExc, uno_Mapping * pCpp2Uno )
257 OSL_ENSURE( header, "### no exception header!!!" );
258 if (! header)
259 terminate();
261 typelib_TypeDescription * pExcTypeDescr = 0;
262 OUString unoName( toUNOname( header->exceptionType->name() ) );
263 ::typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData );
264 OSL_ENSURE( pExcTypeDescr, "### can not get type description for exception!!!" );
265 if (! pExcTypeDescr)
266 terminate();
268 // construct uno exception any
269 ::uno_any_constructAndConvert( pExc, header->adjustedPtr, pExcTypeDescr, pCpp2Uno );
270 ::typelib_typedescription_release( pExcTypeDescr );
275 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */