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 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1070
37 #include <boost/unordered_map.hpp>
39 #include <rtl/instance.hxx>
40 #include <rtl/strbuf.hxx>
41 #include <rtl/ustrbuf.hxx>
42 #include <osl/diagnose.h>
43 #include <osl/mutex.hxx>
45 #include <com/sun/star/uno/genfunc.hxx>
46 #include "com/sun/star/uno/RuntimeException.hpp"
47 #include <typelib/typedescription.hxx>
53 using namespace ::std
;
54 using namespace ::osl
;
55 using namespace ::rtl
;
56 using namespace ::com::sun::star::uno
;
57 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1070
58 using namespace ::__cxxabiv1
;
61 namespace CPPU_CURRENT_NAMESPACE
64 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
66 // MacOSX10.4u.sdk/usr/include/c++/4.0.0/cxxabi.h defined
67 // __cxxabiv1::__class_type_info and __cxxabiv1::__si_class_type_info but
68 // MacOSX10.7.sdk/usr/include/cxxabi.h no longer does, so instances of those
69 // classes need to be created manually:
71 // std::type_info defined in <typeinfo> offers a protected ctor:
72 struct FAKE_type_info
: public std::type_info
{
73 FAKE_type_info(char const * name
): type_info(name
) {}
76 // Modeled after __cxxabiv1::__si_class_type_info defined in
77 // MacOSX10.4u.sdk/usr/include/c++/4.0.0/cxxabi.h (i.e.,
78 // abi::__si_class_type_info documented at
79 // <http://www.codesourcery.com/public/cxx-abi/abi.html#rtti>):
80 struct FAKE_si_class_type_info
: public FAKE_type_info
{
81 FAKE_si_class_type_info(char const * name
, std::type_info
const * theBase
):
82 FAKE_type_info(name
), base(theBase
) {}
84 std::type_info
const * base
;
85 // actually a __cxxabiv1::__class_type_info pointer
89 struct Derived
: Base
{};
91 std::type_info
* create_FAKE_class_type_info(char const * name
) {
92 std::type_info
* p
= new FAKE_type_info(name
);
93 // cxxabiv1::__class_type_info has no data members in addition to
95 *reinterpret_cast< void ** >(p
) = *reinterpret_cast< void * const * >(
97 // copy correct __cxxabiv1::__class_type_info vtable into place
101 std::type_info
* create_FAKE_si_class_type_info(
102 char const * name
, std::type_info
const * base
)
104 std::type_info
* p
= new FAKE_si_class_type_info(name
, base
);
105 *reinterpret_cast< void ** >(p
) = *reinterpret_cast< void * const * >(
107 // copy correct __cxxabiv1::__si_class_type_info vtable into place
113 void dummy_can_throw_anything( char const * )
117 //==================================================================================================
118 static OUString
toUNOname( char const * p
) SAL_THROW(())
120 #if OSL_DEBUG_LEVEL > 1
121 char const * start
= p
;
124 // example: N3com3sun4star4lang24IllegalArgumentExceptionE
126 OUStringBuffer
buf( 64 );
127 OSL_ASSERT( 'N' == *p
);
133 long n
= (*p
++ - '0');
134 while ('0' <= *p
&& '9' >= *p
)
139 buf
.appendAscii( p
, n
);
142 buf
.append( (sal_Unicode
)'.' );
145 #if OSL_DEBUG_LEVEL > 1
146 OUString
ret( buf
.makeStringAndClear() );
147 OString
c_ret( OUStringToOString( ret
, RTL_TEXTENCODING_ASCII_US
) );
148 fprintf( stderr
, "> toUNOname(): %s => %s\n", start
, c_ret
.getStr() );
151 return buf
.makeStringAndClear();
155 //==================================================================================================
158 typedef boost::unordered_map
< OUString
, type_info
*, OUStringHash
> t_rtti_map
;
162 t_rtti_map m_generatedRttis
;
167 RTTI() SAL_THROW(());
168 ~RTTI() SAL_THROW(());
170 type_info
* getRTTI( typelib_CompoundTypeDescription
* ) SAL_THROW(());
172 //__________________________________________________________________________________________________
173 RTTI::RTTI() SAL_THROW(())
174 : m_hApp( dlopen( 0, RTLD_LAZY
) )
177 //__________________________________________________________________________________________________
178 RTTI::~RTTI() SAL_THROW(())
183 //__________________________________________________________________________________________________
184 type_info
* RTTI::getRTTI( typelib_CompoundTypeDescription
*pTypeDescr
) SAL_THROW(())
188 OUString
const & unoName
= *(OUString
const *)&pTypeDescr
->aBase
.pTypeName
;
190 MutexGuard
guard( m_mutex
);
191 t_rtti_map::const_iterator
iFind( m_rttis
.find( unoName
) );
192 if (iFind
== m_rttis
.end())
195 OStringBuffer
buf( 64 );
196 buf
.append( RTL_CONSTASCII_STRINGPARAM("_ZTIN") );
200 OUString
token( unoName
.getToken( 0, '.', index
) );
201 buf
.append( token
.getLength() );
202 OString
c_token( OUStringToOString( token
, RTL_TEXTENCODING_ASCII_US
) );
203 buf
.append( c_token
);
208 OString
symName( buf
.makeStringAndClear() );
209 rtti
= (type_info
*)dlsym( m_hApp
, symName
.getStr() );
213 pair
< t_rtti_map::iterator
, bool > insertion(
214 m_rttis
.insert( t_rtti_map::value_type( unoName
, rtti
) ) );
215 OSL_ENSURE( insertion
.second
, "### inserting new rtti failed?!" );
219 // try to lookup the symbol in the generated rtti map
220 t_rtti_map::const_iterator
iFind2( m_generatedRttis
.find( unoName
) );
221 if (iFind2
== m_generatedRttis
.end())
223 // we must generate it !
224 // symbol and rtti-name is nearly identical,
225 // the symbol is prefixed with _ZTI
226 char const * rttiName
= symName
.getStr() +4;
227 #if OSL_DEBUG_LEVEL > 1
228 fprintf( stderr
,"generated rtti for %s\n", rttiName
);
230 if (pTypeDescr
->pBaseTypeDescription
)
232 // ensure availability of base
233 type_info
* base_rtti
= getRTTI(
234 (typelib_CompoundTypeDescription
*)pTypeDescr
->pBaseTypeDescription
);
235 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1070
236 rtti
= new __si_class_type_info(
237 strdup( rttiName
), (__class_type_info
*)base_rtti
);
239 rtti
= create_FAKE_si_class_type_info(
240 strdup( rttiName
), base_rtti
);
245 // this class has no base class
246 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1070
247 rtti
= new __class_type_info( strdup( rttiName
) );
249 rtti
= create_FAKE_class_type_info( strdup( rttiName
) );
253 pair
< t_rtti_map::iterator
, bool > insertion(
254 m_generatedRttis
.insert( t_rtti_map::value_type( unoName
, rtti
) ) );
255 OSL_ENSURE( insertion
.second
, "### inserting new generated rtti failed?!" );
257 else // taking already generated rtti
259 rtti
= iFind2
->second
;
265 rtti
= iFind
->second
;
271 struct RTTISingleton
: public rtl::Static
< RTTI
, RTTISingleton
> {};
273 //--------------------------------------------------------------------------------------------------
274 static void deleteException( void * pExc
)
276 __cxa_exception
const * header
= ((__cxa_exception
const *)pExc
- 1);
277 typelib_TypeDescription
* pTD
= 0;
278 OUString
unoName( toUNOname( header
->exceptionType
->name() ) );
279 ::typelib_typedescription_getByName( &pTD
, unoName
.pData
);
280 OSL_ENSURE( pTD
, "### unknown exception type! leaving out destruction => leaking!!!" );
283 ::uno_destructData( pExc
, pTD
, cpp_release
);
284 ::typelib_typedescription_release( pTD
);
288 //==================================================================================================
289 void raiseException( uno_Any
* pUnoExc
, uno_Mapping
* pUno2Cpp
)
291 #if OSL_DEBUG_LEVEL > 1
294 *reinterpret_cast< OUString
const * >( &pUnoExc
->pType
->pTypeName
),
295 RTL_TEXTENCODING_ASCII_US
) );
296 fprintf( stderr
, "> uno exception occurred: %s\n", cstr
.getStr() );
302 // construct cpp exception object
303 typelib_TypeDescription
* pTypeDescr
= 0;
304 TYPELIB_DANGER_GET( &pTypeDescr
, pUnoExc
->pType
);
305 OSL_ASSERT( pTypeDescr
);
308 throw RuntimeException(
309 OUString( RTL_CONSTASCII_USTRINGPARAM("cannot get typedescription for type ") ) +
310 *reinterpret_cast< OUString
const * >( &pUnoExc
->pType
->pTypeName
),
311 Reference
< XInterface
>() );
314 pCppExc
= __cxa_allocate_exception( pTypeDescr
->nSize
);
315 ::uno_copyAndConvertData( pCppExc
, pUnoExc
->pData
, pTypeDescr
, pUno2Cpp
);
317 // destruct uno exception
318 ::uno_any_destruct( pUnoExc
, 0 );
319 rtti
= (type_info
*)RTTISingleton::get().getRTTI( (typelib_CompoundTypeDescription
*) pTypeDescr
);
320 TYPELIB_DANGER_RELEASE( pTypeDescr
);
321 OSL_ENSURE( rtti
, "### no rtti for throwing exception!" );
324 throw RuntimeException(
325 OUString( RTL_CONSTASCII_USTRINGPARAM("no rtti for type ") ) +
326 *reinterpret_cast< OUString
const * >( &pUnoExc
->pType
->pTypeName
),
327 Reference
< XInterface
>() );
331 __cxa_throw( pCppExc
, rtti
, deleteException
);
334 //==================================================================================================
335 void fillUnoException( __cxa_exception
* header
, uno_Any
* pUnoExc
, uno_Mapping
* pCpp2Uno
)
339 RuntimeException
aRE(
340 OUString( RTL_CONSTASCII_USTRINGPARAM("no exception header!") ),
341 Reference
< XInterface
>() );
342 Type
const & rType
= ::getCppuType( &aRE
);
343 uno_type_any_constructAndConvert( pUnoExc
, &aRE
, rType
.getTypeLibType(), pCpp2Uno
);
344 #if OSL_DEBUG_LEVEL > 0
345 OString
cstr( OUStringToOString( aRE
.Message
, RTL_TEXTENCODING_ASCII_US
) );
346 OSL_FAIL( cstr
.getStr() );
351 typelib_TypeDescription
* pExcTypeDescr
= 0;
352 OUString
unoName( toUNOname( header
->exceptionType
->name() ) );
353 #if OSL_DEBUG_LEVEL > 1
354 OString
cstr_unoName( OUStringToOString( unoName
, RTL_TEXTENCODING_ASCII_US
) );
355 fprintf( stderr
, "> c++ exception occurred: %s\n", cstr_unoName
.getStr() );
357 typelib_typedescription_getByName( &pExcTypeDescr
, unoName
.pData
);
358 if (0 == pExcTypeDescr
)
360 RuntimeException
aRE(
361 OUString( RTL_CONSTASCII_USTRINGPARAM("exception type not found: ") ) + unoName
,
362 Reference
< XInterface
>() );
363 Type
const & rType
= ::getCppuType( &aRE
);
364 uno_type_any_constructAndConvert( pUnoExc
, &aRE
, rType
.getTypeLibType(), pCpp2Uno
);
365 #if OSL_DEBUG_LEVEL > 0
366 OString
cstr( OUStringToOString( aRE
.Message
, RTL_TEXTENCODING_ASCII_US
) );
367 OSL_FAIL( cstr
.getStr() );
372 // construct uno exception any
373 uno_any_constructAndConvert( pUnoExc
, header
->adjustedPtr
, pExcTypeDescr
, pCpp2Uno
);
374 typelib_typedescription_release( pExcTypeDescr
);
380 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */