1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: except.cxx,v $
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 ************************************************************************/
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 "com/sun/star/uno/RuntimeException.hpp"
44 #include <typelib/typedescription.hxx>
50 using namespace ::std
;
51 using namespace ::osl
;
52 using namespace ::rtl
;
53 using namespace ::com::sun::star::uno
;
54 using namespace ::__cxxabiv1
;
56 extern sal_Int32
* pHack
;
57 extern sal_Int32 nHack
;
59 namespace CPPU_CURRENT_NAMESPACE
61 void dummy_can_throw_anything( char const * )
65 //===================================================================
66 static OUString
toUNOname( char const * p
) SAL_THROW( () )
68 #if OSL_DEBUG_LEVEL > 1
69 char const * start
= p
;
72 // example: N3com3sun4star4lang24IllegalArgumentExceptionE
74 OUStringBuffer
buf( 64 );
75 OSL_ASSERT( 'N' == *p
);
81 long n
= (*p
++ - '0');
82 while ('0' <= *p
&& '9' >= *p
)
87 buf
.appendAscii( p
, n
);
90 buf
.append( (sal_Unicode
)'.' );
93 #if OSL_DEBUG_LEVEL > 1
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() );
99 return buf
.makeStringAndClear();
103 //=====================================================================
106 typedef hash_map
< OUString
, type_info
*, OUStringHash
> t_rtti_map
;
110 t_rtti_map m_generatedRttis
;
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( () )
131 //____________________________________________________________________
132 type_info
* RTTI::getRTTI( typelib_CompoundTypeDescription
*pTypeDescr
) SAL_THROW( () )
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())
143 OStringBuffer
buf( 64 );
144 buf
.append( RTL_CONSTASCII_STRINGPARAM("_ZTIN") );
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
);
156 OString
symName( buf
.makeStringAndClear() );
157 rtti
= (type_info
*)dlsym( m_hApp
, symName
.getStr() );
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?!" );
167 // try to lookup the symbol in the generated rtti map
168 t_rtti_map::const_iterator
iFind( m_generatedRttis
.find( unoName
) );
169 if (iFind
== 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 #if OSL_DEBUG_LEVEL > 1
176 fprintf( stderr
,"generated rtti for %s\n", rttiName
);
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
);
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
= iFind
->second
;
204 rtti
= iFind
->second
;
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!!!" );
220 ::uno_destructData( pExc
, pTD
, cpp_release
);
221 ::typelib_typedescription_release( pTD
);
225 //==================================================================
226 void raiseException( uno_Any
* pUnoExc
, uno_Mapping
* pUno2Cpp
)
228 #if OSL_DEBUG_LEVEL > 1
231 *reinterpret_cast< OUString
const * >( &pUnoExc
->pType
->pTypeName
),
232 RTL_TEXTENCODING_ASCII_US
) );
233 fprintf( stderr
, "> uno exception occured: %s\n", cstr
.getStr() );
239 // construct cpp exception object
240 typelib_TypeDescription
* pTypeDescr
= 0;
241 TYPELIB_DANGER_GET( &pTypeDescr
, pUnoExc
->pType
);
242 OSL_ASSERT( pTypeDescr
);
245 throw RuntimeException(
246 OUString( RTL_CONSTASCII_USTRINGPARAM("cannot get typedescription for type ") ) +
247 *reinterpret_cast< OUString
const * >( &pUnoExc
->pType
->pTypeName
),
248 Reference
< XInterface
>() );
251 pCppExc
= __cxa_allocate_exception( pTypeDescr
->nSize
);
252 ::uno_copyAndConvertData( pCppExc
, pUnoExc
->pData
, pTypeDescr
, pUno2Cpp
);
254 // destruct uno exception
255 ::uno_any_destruct( pUnoExc
, 0 );
256 // avoiding locked counts
257 static RTTI
* s_rtti
= 0;
260 MutexGuard
guard( Mutex::getGlobalMutex() );
263 #ifdef LEAK_STATIC_DATA
266 static RTTI rtti_data
;
271 rtti
= (type_info
*)s_rtti
->getRTTI( (typelib_CompoundTypeDescription
*) pTypeDescr
);
272 TYPELIB_DANGER_RELEASE( pTypeDescr
);
273 OSL_ENSURE( rtti
, "### no rtti for throwing exception!" );
276 throw RuntimeException(
277 OUString( RTL_CONSTASCII_USTRINGPARAM("no rtti for type ") ) +
278 *reinterpret_cast< OUString
const * >( &pUnoExc
->pType
->pTypeName
),
279 Reference
< XInterface
>() );
284 __cxa_throw( pCppExc
, rtti
, deleteException
);
288 static void* getAdjustedPtr(__cxa_exception
* header
)
290 return (void*)header
->unwindHeader
.barrier_cache
.bitpattern
[0];
293 static void* getAdjustedPtr(__cxa_exception
* header
)
295 return header
->adjustedPtr
;
299 //===================================================================
300 void fillUnoException( __cxa_exception
* header
, uno_Any
* pUnoExc
, uno_Mapping
* pCpp2Uno
)
304 RuntimeException
aRE(
305 OUString( RTL_CONSTASCII_USTRINGPARAM("no exception header!") ),
306 Reference
< XInterface
>() );
307 Type
const & rType
= ::getCppuType( &aRE
);
308 uno_type_any_constructAndConvert( pUnoExc
, &aRE
, rType
.getTypeLibType(), pCpp2Uno
);
309 #if OSL_DEBUG_LEVEL > 0
310 OString
cstr( OUStringToOString( aRE
.Message
, RTL_TEXTENCODING_ASCII_US
) );
311 OSL_ENSURE( 0, cstr
.getStr() );
316 typelib_TypeDescription
* pExcTypeDescr
= 0;
317 OUString
unoName( toUNOname( header
->exceptionType
->name() ) );
318 #if OSL_DEBUG_LEVEL > 1
319 OString
cstr_unoName( OUStringToOString( unoName
, RTL_TEXTENCODING_ASCII_US
) );
320 fprintf( stderr
, "> c++ exception occured: %s\n", cstr_unoName
.getStr() );
322 typelib_typedescription_getByName( &pExcTypeDescr
, unoName
.pData
);
323 if (0 == pExcTypeDescr
)
325 RuntimeException
aRE(
326 OUString( RTL_CONSTASCII_USTRINGPARAM("exception type not found: ") ) + unoName
,
327 Reference
< XInterface
>() );
328 Type
const & rType
= ::getCppuType( &aRE
);
329 uno_type_any_constructAndConvert( pUnoExc
, &aRE
, rType
.getTypeLibType(), pCpp2Uno
);
330 #if OSL_DEBUG_LEVEL > 0
331 OString
cstr( OUStringToOString( aRE
.Message
, RTL_TEXTENCODING_ASCII_US
) );
332 OSL_ENSURE( 0, cstr
.getStr() );
337 // construct uno exception any
338 uno_any_constructAndConvert( pUnoExc
, getAdjustedPtr(header
), pExcTypeDescr
, pCpp2Uno
);
339 typelib_typedescription_release( pExcTypeDescr
);
344 /* vi:set tabstop=4 shiftwidth=4 expandtab: */