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 ************************************************************************/
32 #include <boost/unordered_map.hpp>
34 #include <rtl/instance.hxx>
35 #include <rtl/strbuf.hxx>
36 #include <rtl/ustrbuf.hxx>
37 #include <osl/diagnose.h>
38 #include <osl/mutex.hxx>
40 #include <com/sun/star/uno/genfunc.hxx>
41 #include <typelib/typedescription.hxx>
47 using namespace ::std
;
48 using namespace ::osl
;
49 using namespace ::rtl
;
50 using namespace ::com::sun::star::uno
;
51 using namespace ::__cxxabiv1
;
54 namespace CPPU_CURRENT_NAMESPACE
57 void dummy_can_throw_anything( char const * )
61 //==================================================================================================
62 static OUString
toUNOname( char const * p
) SAL_THROW(())
64 #if defined BRIDGES_DEBUG
65 char const * start
= p
;
68 // example: N3com3sun4star4lang24IllegalArgumentExceptionE
70 OUStringBuffer
buf( 64 );
71 OSL_ASSERT( 'N' == *p
);
77 long n
= (*p
++ - '0');
78 while ('0' <= *p
&& '9' >= *p
)
83 buf
.appendAscii( p
, n
);
86 buf
.append( (sal_Unicode
)'.' );
89 #if defined BRIDGES_DEBUG
90 OUString
ret( buf
.makeStringAndClear() );
91 OString
c_ret( OUStringToOString( ret
, RTL_TEXTENCODING_ASCII_US
) );
92 fprintf( stderr
, "> toUNOname(): %s => %s\n", start
, c_ret
.getStr() );
95 return buf
.makeStringAndClear();
99 //==================================================================================================
102 typedef boost::unordered_map
< OUString
, type_info
*, OUStringHash
> t_rtti_map
;
106 t_rtti_map m_generatedRttis
;
111 RTTI() SAL_THROW(());
112 ~RTTI() SAL_THROW(());
114 type_info
* getRTTI( typelib_CompoundTypeDescription
* ) SAL_THROW(());
116 //__________________________________________________________________________________________________
117 RTTI::RTTI() SAL_THROW(())
118 : m_hApp( dlopen( 0, RTLD_LAZY
) )
121 //__________________________________________________________________________________________________
122 RTTI::~RTTI() SAL_THROW(())
127 //__________________________________________________________________________________________________
128 type_info
* RTTI::getRTTI( typelib_CompoundTypeDescription
*pTypeDescr
) SAL_THROW(())
132 OUString
const & unoName
= *(OUString
const *)&pTypeDescr
->aBase
.pTypeName
;
134 MutexGuard
guard( m_mutex
);
135 t_rtti_map::const_iterator
iRttiFind( m_rttis
.find( unoName
) );
136 if (iRttiFind
== m_rttis
.end())
139 OStringBuffer
buf( 64 );
140 buf
.append( RTL_CONSTASCII_STRINGPARAM("_ZTIN") );
144 OUString
token( unoName
.getToken( 0, '.', index
) );
145 buf
.append( token
.getLength() );
146 OString
c_token( OUStringToOString( token
, RTL_TEXTENCODING_ASCII_US
) );
147 buf
.append( c_token
);
152 OString
symName( buf
.makeStringAndClear() );
153 rtti
= (type_info
*)dlsym( m_hApp
, symName
.getStr() );
157 pair
< t_rtti_map::iterator
, bool > insertion(
158 m_rttis
.insert( t_rtti_map::value_type( unoName
, rtti
) ) );
159 OSL_ENSURE( insertion
.second
, "### inserting new rtti failed?!" );
163 // try to lookup the symbol in the generated rtti map
164 t_rtti_map::const_iterator
iFind( m_generatedRttis
.find( unoName
) );
165 if (iFind
== m_generatedRttis
.end())
167 // we must generate it !
168 // symbol and rtti-name is nearly identical,
169 // the symbol is prefixed with _ZTI
170 char const * rttiName
= symName
.getStr() +4;
171 #if defined BRIDGES_DEBUG
172 fprintf( stderr
,"generated rtti for %s\n", rttiName
);
174 if (pTypeDescr
->pBaseTypeDescription
)
176 // ensure availability of base
177 type_info
* base_rtti
= getRTTI(
178 (typelib_CompoundTypeDescription
*)pTypeDescr
->pBaseTypeDescription
);
179 rtti
= new __si_class_type_info(
180 strdup( rttiName
), (__class_type_info
*)base_rtti
);
184 // this class has no base class
185 rtti
= new __class_type_info( strdup( rttiName
) );
188 pair
< t_rtti_map::iterator
, bool > insertion(
189 m_generatedRttis
.insert( t_rtti_map::value_type( unoName
, rtti
) ) );
190 OSL_ENSURE( insertion
.second
, "### inserting new generated rtti failed?!" );
192 else // taking already generated rtti
194 rtti
= iFind
->second
;
200 rtti
= iRttiFind
->second
;
206 struct RTTISingleton
: public rtl::Static
< RTTI
, RTTISingleton
> {};
208 //--------------------------------------------------------------------------------------------------
209 static void deleteException( void * pExc
)
211 __cxa_exception
const * header
= ((__cxa_exception
const *)pExc
- 1);
212 typelib_TypeDescription
* pTD
= 0;
213 OUString
unoName( toUNOname( header
->exceptionType
->name() ) );
214 ::typelib_typedescription_getByName( &pTD
, unoName
.pData
);
215 OSL_ENSURE( pTD
, "### unknown exception type! leaving out destruction => leaking!!!" );
218 ::uno_destructData( pExc
, pTD
, cpp_release
);
219 ::typelib_typedescription_release( pTD
);
223 //==================================================================================================
224 void raiseException( uno_Any
* pUnoExc
, uno_Mapping
* pUno2Cpp
)
226 #if defined BRIDGES_DEBUG
229 *reinterpret_cast< OUString
const * >( &pUnoExc
->pType
->pTypeName
),
230 RTL_TEXTENCODING_ASCII_US
) );
231 fprintf( stderr
, "> uno exception occurred: %s\n", cstr
.getStr() );
237 // construct cpp exception object
238 typelib_TypeDescription
* pTypeDescr
= 0;
239 TYPELIB_DANGER_GET( &pTypeDescr
, pUnoExc
->pType
);
240 OSL_ASSERT( pTypeDescr
);
243 throw RuntimeException(
244 OUString( RTL_CONSTASCII_USTRINGPARAM("cannot get typedescription for type ") ) +
245 *reinterpret_cast< OUString
const * >( &pUnoExc
->pType
->pTypeName
),
246 Reference
< XInterface
>() );
249 pCppExc
= __cxa_allocate_exception( pTypeDescr
->nSize
);
250 ::uno_copyAndConvertData( pCppExc
, pUnoExc
->pData
, pTypeDescr
, pUno2Cpp
);
252 // destruct uno exception
253 ::uno_any_destruct( pUnoExc
, 0 );
254 rtti
= (type_info
*)RTTISingleton::get().getRTTI( (typelib_CompoundTypeDescription
*) pTypeDescr
);
255 TYPELIB_DANGER_RELEASE( pTypeDescr
);
256 OSL_ENSURE( rtti
, "### no rtti for throwing exception!" );
259 throw RuntimeException(
260 OUString( RTL_CONSTASCII_USTRINGPARAM("no rtti for type ") ) +
261 *reinterpret_cast< OUString
const * >( &pUnoExc
->pType
->pTypeName
),
262 Reference
< XInterface
>() );
266 __cxa_throw( pCppExc
, rtti
, deleteException
);
269 //==================================================================================================
270 void fillUnoException( __cxa_exception
* header
, uno_Any
* pUnoExc
, uno_Mapping
* pCpp2Uno
)
274 RuntimeException
aRE(
275 OUString( RTL_CONSTASCII_USTRINGPARAM("no exception header!") ),
276 Reference
< XInterface
>() );
277 Type
const & rType
= ::getCppuType( &aRE
);
278 uno_type_any_constructAndConvert( pUnoExc
, &aRE
, rType
.getTypeLibType(), pCpp2Uno
);
280 OString
cstr( OUStringToOString( aRE
.Message
, RTL_TEXTENCODING_ASCII_US
) );
281 OSL_FAIL( cstr
.getStr() );
286 typelib_TypeDescription
* pExcTypeDescr
= 0;
287 OUString
unoName( toUNOname( header
->exceptionType
->name() ) );
288 #if defined BRIDGES_DEBUG
289 OString
cstr_unoName( OUStringToOString( unoName
, RTL_TEXTENCODING_ASCII_US
) );
290 fprintf( stderr
, "> c++ exception occurred: %s\n", cstr_unoName
.getStr() );
292 typelib_typedescription_getByName( &pExcTypeDescr
, unoName
.pData
);
293 if (0 == pExcTypeDescr
)
295 RuntimeException
aRE(
296 OUString( RTL_CONSTASCII_USTRINGPARAM("exception type not found: ") ) + unoName
,
297 Reference
< XInterface
>() );
298 Type
const & rType
= ::getCppuType( &aRE
);
299 uno_type_any_constructAndConvert( pUnoExc
, &aRE
, rType
.getTypeLibType(), pCpp2Uno
);
301 OString
cstr( OUStringToOString( aRE
.Message
, RTL_TEXTENCODING_ASCII_US
) );
302 OSL_FAIL( cstr
.getStr() );
307 // construct uno exception any
308 uno_any_constructAndConvert( pUnoExc
, header
->adjustedPtr
, pExcTypeDescr
, pCpp2Uno
);
309 typelib_typedescription_release( pExcTypeDescr
);
315 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */