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 ************************************************************************/
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 "com/sun/star/uno/RuntimeException.hpp"
43 #include <typelib/typedescription.hxx>
49 using namespace ::std
;
50 using namespace ::osl
;
51 using namespace ::rtl
;
52 using namespace ::com::sun::star::uno
;
53 using namespace ::__cxxabiv1
;
55 extern sal_Int32
* pHack
;
56 extern sal_Int32 nHack
;
58 namespace CPPU_CURRENT_NAMESPACE
60 void dummy_can_throw_anything( char const * )
64 //===================================================================
65 static OUString
toUNOname( char const * p
) SAL_THROW(())
67 #if OSL_DEBUG_LEVEL > 1
68 char const * start
= p
;
71 // example: N3com3sun4star4lang24IllegalArgumentExceptionE
73 OUStringBuffer
buf( 64 );
74 OSL_ASSERT( 'N' == *p
);
80 long n
= (*p
++ - '0');
81 while ('0' <= *p
&& '9' >= *p
)
86 buf
.appendAscii( p
, n
);
89 buf
.append( (sal_Unicode
)'.' );
92 #if OSL_DEBUG_LEVEL > 1
93 OUString
ret( buf
.makeStringAndClear() );
94 OString
c_ret( OUStringToOString( ret
, RTL_TEXTENCODING_ASCII_US
) );
95 fprintf( stderr
, "> toUNOname(): %s => %s\n", start
, c_ret
.getStr() );
98 return buf
.makeStringAndClear();
102 //=====================================================================
105 typedef boost::unordered_map
< OUString
, type_info
*, OUStringHash
> t_rtti_map
;
109 t_rtti_map m_generatedRttis
;
114 RTTI() SAL_THROW(());
115 ~RTTI() SAL_THROW(());
117 type_info
* getRTTI(typelib_CompoundTypeDescription
*) SAL_THROW(());
119 //____________________________________________________________________
120 RTTI::RTTI() SAL_THROW(())
121 : m_hApp( dlopen( 0, RTLD_LAZY
) )
124 //____________________________________________________________________
125 RTTI::~RTTI() SAL_THROW(())
130 //____________________________________________________________________
131 type_info
* RTTI::getRTTI( typelib_CompoundTypeDescription
*pTypeDescr
) SAL_THROW(())
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())
142 OStringBuffer
buf( 64 );
143 buf
.append( RTL_CONSTASCII_STRINGPARAM("_ZTIN") );
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
);
155 OString
symName( buf
.makeStringAndClear() );
156 rtti
= (type_info
*)dlsym( m_hApp
, symName
.getStr() );
160 pair
< t_rtti_map::iterator
, bool > insertion(
161 m_rttis
.insert( t_rtti_map::value_type( unoName
, rtti
) ) );
162 OSL_ENSURE( insertion
.second
, "### inserting new rtti failed?!" );
166 // try to lookup the symbol in the generated rtti map
167 t_rtti_map::const_iterator
iFind( m_generatedRttis
.find( unoName
) );
168 if (iFind
== m_generatedRttis
.end())
170 // we must generate it !
171 // symbol and rtti-name is nearly identical,
172 // the symbol is prefixed with _ZTI
173 char const * rttiName
= symName
.getStr() +4;
174 #if OSL_DEBUG_LEVEL > 1
175 fprintf( stderr
,"generated rtti for %s\n", rttiName
);
177 if (pTypeDescr
->pBaseTypeDescription
)
179 // ensure availability of base
180 type_info
* base_rtti
= getRTTI(
181 (typelib_CompoundTypeDescription
*)pTypeDescr
->pBaseTypeDescription
);
182 rtti
= new __si_class_type_info(
183 strdup( rttiName
), (__class_type_info
*)base_rtti
);
187 // this class has no base class
188 rtti
= new __class_type_info( strdup( rttiName
) );
191 pair
< t_rtti_map::iterator
, bool > insertion(
192 m_generatedRttis
.insert( t_rtti_map::value_type( unoName
, rtti
) ) );
193 OSL_ENSURE( insertion
.second
, "### inserting new generated rtti failed?!" );
195 else // taking already generated rtti
197 rtti
= iFind
->second
;
203 rtti
= iRttiFind
->second
;
209 struct RTTISingleton
: public rtl::Static
< RTTI
, RTTISingleton
> {};
211 //------------------------------------------------------------------
212 static void deleteException( void * pExc
)
214 __cxa_exception
const * header
= ((__cxa_exception
const *)pExc
- 1);
215 typelib_TypeDescription
* pTD
= 0;
216 OUString
unoName( toUNOname( header
->exceptionType
->name() ) );
217 ::typelib_typedescription_getByName( &pTD
, unoName
.pData
);
218 OSL_ENSURE( pTD
, "### unknown exception type! leaving out destruction => leaking!!!" );
221 ::uno_destructData( pExc
, pTD
, cpp_release
);
222 ::typelib_typedescription_release( pTD
);
226 //==================================================================
227 void raiseException( uno_Any
* pUnoExc
, uno_Mapping
* pUno2Cpp
)
229 #if OSL_DEBUG_LEVEL > 1
232 *reinterpret_cast< OUString
const * >( &pUnoExc
->pType
->pTypeName
),
233 RTL_TEXTENCODING_ASCII_US
) );
234 fprintf( stderr
, "> uno exception occurred: %s\n", cstr
.getStr() );
240 // construct cpp exception object
241 typelib_TypeDescription
* pTypeDescr
= 0;
242 TYPELIB_DANGER_GET( &pTypeDescr
, pUnoExc
->pType
);
243 OSL_ASSERT( pTypeDescr
);
246 throw RuntimeException(
247 OUString( RTL_CONSTASCII_USTRINGPARAM("cannot get typedescription for type ") ) +
248 *reinterpret_cast< OUString
const * >( &pUnoExc
->pType
->pTypeName
),
249 Reference
< XInterface
>() );
252 pCppExc
= __cxa_allocate_exception( pTypeDescr
->nSize
);
253 ::uno_copyAndConvertData( pCppExc
, pUnoExc
->pData
, pTypeDescr
, pUno2Cpp
);
255 // destruct uno exception
256 ::uno_any_destruct( pUnoExc
, 0 );
257 rtti
= (type_info
*)RTTISingleton::get().getRTTI( (typelib_CompoundTypeDescription
*) pTypeDescr
);
258 TYPELIB_DANGER_RELEASE( pTypeDescr
);
259 OSL_ENSURE( rtti
, "### no rtti for throwing exception!" );
262 throw RuntimeException(
263 OUString( RTL_CONSTASCII_USTRINGPARAM("no rtti for type ") ) +
264 *reinterpret_cast< OUString
const * >( &pUnoExc
->pType
->pTypeName
),
265 Reference
< XInterface
>() );
270 __cxa_throw( pCppExc
, rtti
, deleteException
);
273 static void* getAdjustedPtr(__cxa_exception
* header
)
275 return header
->adjustedPtr
;
278 //===================================================================
279 void fillUnoException( __cxa_exception
* header
, uno_Any
* pUnoExc
, uno_Mapping
* pCpp2Uno
)
283 RuntimeException
aRE(
284 OUString( RTL_CONSTASCII_USTRINGPARAM("no exception header!") ),
285 Reference
< XInterface
>() );
286 Type
const & rType
= ::getCppuType( &aRE
);
287 uno_type_any_constructAndConvert( pUnoExc
, &aRE
, rType
.getTypeLibType(), pCpp2Uno
);
288 #if OSL_DEBUG_LEVEL > 0
289 OString
cstr( OUStringToOString( aRE
.Message
, RTL_TEXTENCODING_ASCII_US
) );
290 OSL_FAIL( cstr
.getStr() );
295 typelib_TypeDescription
* pExcTypeDescr
= 0;
296 OUString
unoName( toUNOname( header
->exceptionType
->name() ) );
297 #if OSL_DEBUG_LEVEL > 1
298 OString
cstr_unoName( OUStringToOString( unoName
, RTL_TEXTENCODING_ASCII_US
) );
299 fprintf( stderr
, "> c++ exception occurred: %s\n", cstr_unoName
.getStr() );
301 typelib_typedescription_getByName( &pExcTypeDescr
, unoName
.pData
);
302 if (0 == pExcTypeDescr
)
304 RuntimeException
aRE(
305 OUString( RTL_CONSTASCII_USTRINGPARAM("exception type not found: ") ) + unoName
,
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_FAIL( cstr
.getStr() );
316 // construct uno exception any
317 uno_any_constructAndConvert( pUnoExc
, getAdjustedPtr(header
), pExcTypeDescr
, pCpp2Uno
);
318 typelib_typedescription_release( pExcTypeDescr
);
323 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */