Bump for 3.6-28
[LibreOffice.git] / bridges / source / cpp_uno / gcc3_macosx_powerpc / except.cxx
blob19b2dc609bd1782be8549a6d5a2e68a53a50670b
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 <dlfcn.h>
32 #include <cxxabi.h>
33 #include <boost/unordered_map.hpp>
35 #include <rtl/instance.hxx>
36 #include <rtl/strbuf.hxx>
37 #include <rtl/ustrbuf.hxx>
38 #include <osl/diagnose.h>
39 #include <osl/mutex.hxx>
41 #include <com/sun/star/uno/genfunc.hxx>
42 #include <typelib/typedescription.hxx>
43 #include <uno/any2.h>
45 #include "share.hxx"
48 using namespace ::std;
49 using namespace ::osl;
50 using namespace ::rtl;
51 using namespace ::com::sun::star::uno;
52 using namespace ::__cxxabiv1;
55 namespace CPPU_CURRENT_NAMESPACE
58 void dummy_can_throw_anything( char const * )
62 //==================================================================================================
63 static OUString toUNOname( char const * p ) SAL_THROW(())
65 #ifdef DEBUG
66 char const * start = p;
67 #endif
69 // example: N3com3sun4star4lang24IllegalArgumentExceptionE
71 OUStringBuffer buf( 64 );
72 OSL_ASSERT( 'N' == *p );
73 ++p; // skip N
75 while ('E' != *p)
77 // read chars count
78 long n = (*p++ - '0');
79 while ('0' <= *p && '9' >= *p)
81 n *= 10;
82 n += (*p++ - '0');
84 buf.appendAscii( p, n );
85 p += n;
86 if ('E' != *p)
87 buf.append( (sal_Unicode)'.' );
90 #ifdef DEBUG
91 OUString ret( buf.makeStringAndClear() );
92 OString c_ret( OUStringToOString( ret, RTL_TEXTENCODING_ASCII_US ) );
93 fprintf( stderr, "> toUNOname(): %s => %s\n", start, c_ret.getStr() );
94 return ret;
95 #else
96 return buf.makeStringAndClear();
97 #endif
100 //==================================================================================================
101 class RTTI
103 typedef boost::unordered_map< OUString, type_info *, OUStringHash > t_rtti_map;
105 Mutex m_mutex;
106 t_rtti_map m_rttis;
107 t_rtti_map m_generatedRttis;
109 void * m_hApp;
111 public:
112 RTTI() SAL_THROW(());
113 ~RTTI() SAL_THROW(());
115 type_info * getRTTI( typelib_CompoundTypeDescription * ) SAL_THROW(());
117 //__________________________________________________________________________________________________
118 RTTI::RTTI() SAL_THROW(())
119 : m_hApp( dlopen( 0, RTLD_LAZY ) )
122 //__________________________________________________________________________________________________
123 RTTI::~RTTI() SAL_THROW(())
125 dlclose( m_hApp );
128 //__________________________________________________________________________________________________
129 type_info * RTTI::getRTTI( typelib_CompoundTypeDescription *pTypeDescr ) SAL_THROW(())
131 type_info * rtti;
133 OUString const & unoName = *(OUString const *)&pTypeDescr->aBase.pTypeName;
135 MutexGuard guard( m_mutex );
136 t_rtti_map::const_iterator iFind( m_rttis.find( unoName ) );
137 if (iFind == m_rttis.end())
139 // RTTI symbol
140 OStringBuffer buf( 64 );
141 buf.append( RTL_CONSTASCII_STRINGPARAM("_ZTIN") );
142 sal_Int32 index = 0;
145 OUString token( unoName.getToken( 0, '.', index ) );
146 buf.append( token.getLength() );
147 OString c_token( OUStringToOString( token, RTL_TEXTENCODING_ASCII_US ) );
148 buf.append( c_token );
150 while (index >= 0);
151 buf.append( 'E' );
153 OString symName( buf.makeStringAndClear() );
154 rtti = (type_info *)dlsym( m_hApp, symName.getStr() );
156 if (rtti)
158 pair< t_rtti_map::iterator, bool > insertion(
159 m_rttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
160 OSL_ENSURE( insertion.second, "### inserting new rtti failed?!" );
162 else
164 // try to lookup the symbol in the generated rtti map
165 t_rtti_map::const_iterator iiFind( m_generatedRttis.find( unoName ) );
166 if (iiFind == m_generatedRttis.end())
168 // we must generate it !
169 // symbol and rtti-name is nearly identical,
170 // the symbol is prefixed with _ZTI
171 char const * rttiName = symName.getStr() +4;
172 #ifdef DEBUG
173 fprintf( stderr,"generated rtti for %s\n", rttiName );
174 #endif
175 if (pTypeDescr->pBaseTypeDescription)
177 // ensure availability of base
178 type_info * base_rtti = getRTTI(
179 (typelib_CompoundTypeDescription *)pTypeDescr->pBaseTypeDescription );
180 rtti = new __si_class_type_info(
181 strdup( rttiName ), (__class_type_info *)base_rtti );
183 else
185 // this class has no base class
186 rtti = new __class_type_info( strdup( rttiName ) );
189 pair< t_rtti_map::iterator, bool > insertion(
190 m_generatedRttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
191 OSL_ENSURE( insertion.second, "### inserting new generated rtti failed?!" );
193 else // taking already generated rtti
195 rtti = iiFind->second;
199 else
201 rtti = iFind->second;
204 return rtti;
207 struct RTTISingleton: public rtl::Static< RTTI, RTTISingleton > {};
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 void * pCppExc;
228 type_info * rtti;
231 // construct cpp exception object
232 typelib_TypeDescription * pTypeDescr = 0;
233 TYPELIB_DANGER_GET( &pTypeDescr, pUnoExc->pType );
234 OSL_ASSERT( pTypeDescr );
235 if (! pTypeDescr)
236 terminate();
238 pCppExc = __cxa_allocate_exception( pTypeDescr->nSize );
239 ::uno_copyAndConvertData( pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp );
241 // destruct uno exception
242 ::uno_any_destruct( pUnoExc, 0 );
243 rtti = (type_info *)RTTISingleton::get().getRTTI( (typelib_CompoundTypeDescription *) pTypeDescr );
244 TYPELIB_DANGER_RELEASE( pTypeDescr );
245 OSL_ENSURE( rtti, "### no rtti for throwing exception!" );
246 if (! rtti)
247 terminate();
250 __cxa_throw( pCppExc, rtti, deleteException );
253 //==================================================================================================
254 void fillUnoException( __cxa_exception * header, uno_Any * pExc, uno_Mapping * pCpp2Uno )
256 OSL_ENSURE( header, "### no exception header!!!" );
257 if (! header)
258 terminate();
260 typelib_TypeDescription * pExcTypeDescr = 0;
261 OUString unoName( toUNOname( header->exceptionType->name() ) );
262 ::typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData );
263 OSL_ENSURE( pExcTypeDescr, "### can not get type description for exception!!!" );
264 if (! pExcTypeDescr)
265 terminate();
267 // construct uno exception any
268 ::uno_any_constructAndConvert( pExc, header->adjustedPtr, pExcTypeDescr, pCpp2Uno );
269 ::typelib_typedescription_release( pExcTypeDescr );
274 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */