Bump for 3.6-28
[LibreOffice.git] / bridges / source / cpp_uno / mingw_intel / except.cxx
blob308ccb842e74dd2cc12a78f43ab59e4c88d53947
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>
33 #include <cxxabi.h>
34 #ifndef _GLIBCXX_CDTOR_CALLABI // new in GCC 4.7 cxxabi.h
35 #define _GLIBCXX_CDTOR_CALLABI
36 #endif
38 #include <boost/unordered_map.hpp>
40 #include <rtl/instance.hxx>
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 "com/sun/star/uno/RuntimeException.hpp"
48 #include <typelib/typedescription.hxx>
49 #include <uno/any2.h>
51 #include "share.hxx"
54 using namespace ::std;
55 using namespace ::osl;
56 using namespace ::rtl;
57 using namespace ::com::sun::star::uno;
58 using namespace ::__cxxabiv1;
61 namespace CPPU_CURRENT_NAMESPACE
64 void dummy_can_throw_anything( char const * )
68 //==================================================================================================
69 static OUString toUNOname( char const * p ) SAL_THROW(())
71 #if OSL_DEBUG_LEVEL > 1
72 char const * start = p;
73 #endif
75 // example: N3com3sun4star4lang24IllegalArgumentExceptionE
77 OUStringBuffer buf( 64 );
78 OSL_ASSERT( 'N' == *p );
79 ++p; // skip N
81 while ('E' != *p)
83 // read chars count
84 long n = (*p++ - '0');
85 while ('0' <= *p && '9' >= *p)
87 n *= 10;
88 n += (*p++ - '0');
90 buf.appendAscii( p, n );
91 p += n;
92 if ('E' != *p)
93 buf.append( (sal_Unicode)'.' );
96 #if OSL_DEBUG_LEVEL > 1
97 OUString ret( buf.makeStringAndClear() );
98 OString c_ret( OUStringToOString( ret, RTL_TEXTENCODING_ASCII_US ) );
99 fprintf( stderr, "> toUNOname(): %s => %s\n", start, c_ret.getStr() );
100 return ret;
101 #else
102 return buf.makeStringAndClear();
103 #endif
106 //==================================================================================================
107 class RTTI
109 typedef boost::unordered_map< OUString, type_info *, OUStringHash > t_rtti_map;
111 Mutex m_mutex;
112 t_rtti_map m_rttis;
113 t_rtti_map m_generatedRttis;
115 public:
116 RTTI() SAL_THROW(());
117 ~RTTI() SAL_THROW(());
119 type_info * getRTTI( typelib_CompoundTypeDescription * ) SAL_THROW(());
121 //__________________________________________________________________________________________________
122 RTTI::RTTI() SAL_THROW(())
125 //__________________________________________________________________________________________________
126 RTTI::~RTTI() SAL_THROW(())
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 iRttiFind( m_rttis.find( unoName ) );
139 if (iRttiFind == 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 // try to lookup the symbol in the generated rtti map
157 t_rtti_map::const_iterator iFind( m_generatedRttis.find( unoName ) );
158 if (iFind == m_generatedRttis.end())
160 // we must generate it !
161 // symbol and rtti-name is nearly identical,
162 // the symbol is prefixed with __ZTI
163 char const * rttiName = symName.getStr() +5;
164 #if OSL_DEBUG_LEVEL > 1
165 fprintf( stderr,"generated rtti for %s\n", rttiName );
166 #endif
167 if (pTypeDescr->pBaseTypeDescription)
169 // ensure availability of base
170 type_info * base_rtti = getRTTI(
171 (typelib_CompoundTypeDescription *)pTypeDescr->pBaseTypeDescription );
172 rtti = new __si_class_type_info(
173 strdup( rttiName ), (__class_type_info *)base_rtti );
175 else
177 // this class has no base class
178 rtti = new __class_type_info( strdup( rttiName ) );
181 pair< t_rtti_map::iterator, bool > insertion(
182 m_generatedRttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
183 OSL_ENSURE( insertion.second, "### inserting new generated rtti failed?!" );
185 else // taking already generated rtti
187 rtti = iFind->second;
190 else
192 rtti = iRttiFind->second;
195 return rtti;
198 struct RTTISingleton: public rtl::Static< RTTI, RTTISingleton > {};
200 //--------------------------------------------------------------------------------------------------
201 extern "C" {
202 static void _GLIBCXX_CDTOR_CALLABI deleteException( void * pExc )
204 __cxa_exception const * header = ((__cxa_exception const *)pExc - 1);
205 typelib_TypeDescription * pTD = 0;
206 OUString unoName( toUNOname( header->exceptionType->name() ) );
207 ::typelib_typedescription_getByName( &pTD, unoName.pData );
208 OSL_ENSURE( pTD, "### unknown exception type! leaving out destruction => leaking!!!" );
209 if (pTD)
211 ::uno_destructData( pExc, pTD, cpp_release );
212 ::typelib_typedescription_release( pTD );
217 //==================================================================================================
218 void raiseException( uno_Any * pUnoExc, uno_Mapping * pUno2Cpp )
220 #if OSL_DEBUG_LEVEL > 1
221 OString cstr(
222 OUStringToOString(
223 *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
224 RTL_TEXTENCODING_ASCII_US ) );
225 fprintf( stderr, "> uno exception occurred: %s\n", cstr.getStr() );
226 #endif
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)
237 throw RuntimeException(
238 OUString( RTL_CONSTASCII_USTRINGPARAM("cannot get typedescription for type ") ) +
239 *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
240 Reference< XInterface >() );
243 pCppExc = __cxa_allocate_exception( pTypeDescr->nSize );
244 ::uno_copyAndConvertData( pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp );
246 // destruct uno exception
247 ::uno_any_destruct( pUnoExc, 0 );
248 rtti = (type_info *)RTTISingleton::get().getRTTI( (typelib_CompoundTypeDescription *) pTypeDescr );
249 TYPELIB_DANGER_RELEASE( pTypeDescr );
250 OSL_ENSURE( rtti, "### no rtti for throwing exception!" );
251 if (! rtti)
253 throw RuntimeException(
254 OUString( RTL_CONSTASCII_USTRINGPARAM("no rtti for type ") ) +
255 *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
256 Reference< XInterface >() );
260 __cxa_throw( pCppExc, rtti, deleteException );
263 //==================================================================================================
264 void fillUnoException( __cxa_exception * header, uno_Any * pUnoExc, uno_Mapping * pCpp2Uno )
266 if (! header)
268 RuntimeException aRE(
269 OUString( RTL_CONSTASCII_USTRINGPARAM("no exception header!") ),
270 Reference< XInterface >() );
271 Type const & rType = ::getCppuType( &aRE );
272 uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
273 #if OSL_DEBUG_LEVEL > 0
274 OString cstr( OUStringToOString( aRE.Message, RTL_TEXTENCODING_ASCII_US ) );
275 OSL_FAIL( cstr.getStr() );
276 #endif
277 return;
280 typelib_TypeDescription * pExcTypeDescr = 0;
281 OUString unoName( toUNOname( header->exceptionType->name() ) );
282 #if OSL_DEBUG_LEVEL > 1
283 OString cstr_unoName( OUStringToOString( unoName, RTL_TEXTENCODING_ASCII_US ) );
284 fprintf( stderr, "> c++ exception occurred: %s\n", cstr_unoName.getStr() );
285 #endif
286 typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData );
287 if (0 == pExcTypeDescr)
289 RuntimeException aRE(
290 OUString( RTL_CONSTASCII_USTRINGPARAM("exception type not found: ") ) + unoName,
291 Reference< XInterface >() );
292 Type const & rType = ::getCppuType( &aRE );
293 uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
294 #if OSL_DEBUG_LEVEL > 0
295 OString cstr( OUStringToOString( aRE.Message, RTL_TEXTENCODING_ASCII_US ) );
296 OSL_FAIL( cstr.getStr() );
297 #endif
299 else
301 // construct uno exception any
302 uno_any_constructAndConvert( pUnoExc, header->adjustedPtr, pExcTypeDescr, pCpp2Uno );
303 typelib_typedescription_release( pExcTypeDescr );
309 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */