Files
libreoffice/bridges/source/cpp_uno/gcc3_linux_intel/except.cxx

302 lines
8.8 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
re-base on ALv2 code. Includes: Patches contributed by: Armin Le Grand. #118558# Correcting OLE attributes of LO3.4 at load time by loading as OOo3.3, details see task. http://svn.apache.org/viewvc?view=revision&revision=1195906 #118485# - Styles for OLEs are not saved. http://svn.apache.org/viewvc?view=revision&revision=1182166 #118898# Adapted ImpGraphic::ImplGetBitmap to correctly convert metafiles http://svn.apache.org/viewvc?view=revision&revision=1293316 #119337# Solves the wrong get/setPropertyValue calls in SvxShapeText (and thus in SvxOle2Shape) http://svn.apache.org/viewvc?view=revision&revision=1344156 Patches contributed by Mathias Bauer (and others) gnumake4 work variously http://svn.apache.org/viewvc?view=revision&revision=1394707 http://svn.apache.org/viewvc?view=revision&revision=1394326 cws mba34issues01: #i117717#: remove wrong assertion http://svn.apache.org/viewvc?view=revision&revision=1172349 Patch contributed by Herbert Duerr goodbye Registration and License dialogs, don't let the door hit you http://svn.apache.org/viewvc?view=revision&revision=1172613 help gcc 4.6.0 on 32bit ubuntu 11.10" http://svn.apache.org/viewvc?view=revision&revision=1245357 Do not add targets for junit tests when junit is disabled. Patch contributed by Andre Fischer http://svn.apache.org/viewvc?view=revision&revision=1241508 Revert "sb140: #i117082# avoid unncessary static class data members commit 21d97438e2944861e26e4984195f959a0cce1e41. remove obsolete FreeBSD visibility special case. retain consolidated BSD bridge code, remove OS/2 pieces.
2012-11-12 17:21:24 +00:00
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <cstdio>
#include <cstring>
2001-10-19 12:32:39 +00:00
#include <dlfcn.h>
#include <rtl/strbuf.hxx>
#include <rtl/ustrbuf.hxx>
#include <osl/mutex.hxx>
#include <sal/log.hxx>
2001-10-19 12:32:39 +00:00
#include <com/sun/star/uno/genfunc.hxx>
#include "com/sun/star/uno/RuntimeException.hpp"
2001-10-19 12:32:39 +00:00
#include <typelib/typedescription.hxx>
#include <unordered_map>
2001-10-19 12:32:39 +00:00
#include "share.hxx"
using namespace ::std;
using namespace ::osl;
using namespace ::com::sun::star::uno;
using namespace ::__cxxabiv1;
namespace CPPU_CURRENT_NAMESPACE
{
void dummy_can_throw_anything( char const * )
{
}
static OUString toUNOname( char const * p )
2001-10-19 12:32:39 +00:00
{
#if OSL_DEBUG_LEVEL > 1
2001-10-22 10:33:49 +00:00
char const * start = p;
#endif
// example: N3com3sun4star4lang24IllegalArgumentExceptionE
2001-10-19 12:32:39 +00:00
OUStringBuffer buf( 64 );
assert( 'N' == *p );
2001-10-19 12:32:39 +00:00
++p; // skip N
while ('E' != *p)
{
// read chars count
long n = (*p++ - '0');
while ('0' <= *p && '9' >= *p)
{
n *= 10;
n += (*p++ - '0');
}
buf.appendAscii( p, n );
p += n;
if ('E' != *p)
buf.append( '.' );
2001-10-19 12:32:39 +00:00
}
#if OSL_DEBUG_LEVEL > 1
2001-10-19 12:32:39 +00:00
OUString ret( buf.makeStringAndClear() );
OString c_ret( OUStringToOString( ret, RTL_TEXTENCODING_ASCII_US ) );
fprintf( stderr, "> toUNOname(): %s => %s\n", start, c_ret.getStr() );
return ret;
#else
return buf.makeStringAndClear();
#endif
}
class RTTI
{
typedef std::unordered_map< OUString, type_info *, OUStringHash > t_rtti_map;
2001-10-19 12:32:39 +00:00
Mutex m_mutex;
t_rtti_map m_rttis;
t_rtti_map m_generatedRttis;
2001-10-19 12:32:39 +00:00
2001-10-22 10:33:49 +00:00
void * m_hApp;
2001-10-19 12:32:39 +00:00
public:
RTTI();
~RTTI();
2001-10-22 10:33:49 +00:00
type_info * getRTTI( typelib_CompoundTypeDescription * );
2001-10-19 12:32:39 +00:00
};
RTTI::RTTI()
2001-10-22 10:33:49 +00:00
: m_hApp( dlopen( 0, RTLD_LAZY ) )
{
}
RTTI::~RTTI()
2001-10-22 10:33:49 +00:00
{
dlclose( m_hApp );
}
type_info * RTTI::getRTTI( typelib_CompoundTypeDescription *pTypeDescr )
2001-10-19 12:32:39 +00:00
{
type_info * rtti;
OUString const & unoName = OUString::unacquired(&pTypeDescr->aBase.pTypeName);
2001-10-19 12:32:39 +00:00
MutexGuard guard( m_mutex );
t_rtti_map::const_iterator iRttiFind( m_rttis.find( unoName ) );
if (iRttiFind == m_rttis.end())
2001-10-19 12:32:39 +00:00
{
2001-10-22 10:33:49 +00:00
// RTTI symbol
OStringBuffer buf( 64 );
buf.append( "_ZTIN" );
2001-10-22 10:33:49 +00:00
sal_Int32 index = 0;
do
{
OUString token( unoName.getToken( 0, '.', index ) );
buf.append( token.getLength() );
OString c_token( OUStringToOString( token, RTL_TEXTENCODING_ASCII_US ) );
buf.append( c_token );
}
while (index >= 0);
buf.append( 'E' );
2001-11-08 11:35:28 +00:00
OString symName( buf.makeStringAndClear() );
rtti = static_cast<type_info *>(dlsym( m_hApp, symName.getStr() ));
2001-11-08 11:35:28 +00:00
if (rtti)
2001-10-19 12:32:39 +00:00
{
pair< t_rtti_map::iterator, bool > insertion(
m_rttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
SAL_WARN_IF( !insertion.second, "bridges", "### inserting new rtti failed?!" );
2001-10-19 12:32:39 +00:00
}
2001-10-22 10:33:49 +00:00
else
{
// try to lookup the symbol in the generated rtti map
t_rtti_map::const_iterator iFind( m_generatedRttis.find( unoName ) );
if (iFind == m_generatedRttis.end())
{
// we must generate it !
// symbol and rtti-name is nearly identical,
// the symbol is prefixed with _ZTI
2001-11-08 11:35:28 +00:00
char const * rttiName = symName.getStr() +4;
#if OSL_DEBUG_LEVEL > 1
2001-11-08 11:35:28 +00:00
fprintf( stderr,"generated rtti for %s\n", rttiName );
#endif
if (pTypeDescr->pBaseTypeDescription)
{
// ensure availability of base
type_info * base_rtti = getRTTI(
pTypeDescr->pBaseTypeDescription);
2001-11-08 11:35:28 +00:00
rtti = new __si_class_type_info(
strdup( rttiName ), static_cast<__class_type_info *>(base_rtti) );
2001-11-08 11:35:28 +00:00
}
else
{
// this class has no base class
rtti = new __class_type_info( strdup( rttiName ) );
}
pair< t_rtti_map::iterator, bool > insertion(
m_generatedRttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
SAL_WARN_IF( !insertion.second, "bridges", "### inserting new generated rtti failed?!" );
}
2001-11-08 11:35:28 +00:00
else // taking already generated rtti
{
rtti = iFind->second;
}
2001-10-22 10:33:49 +00:00
}
2001-10-19 12:32:39 +00:00
}
else
{
rtti = iRttiFind->second;
2001-10-19 12:32:39 +00:00
}
return rtti;
}
extern "C" {
static void _GLIBCXX_CDTOR_CALLABI deleteException( void * pExc )
2001-10-19 12:32:39 +00:00
{
__cxa_exception const * header = static_cast<__cxa_exception const *>(pExc) - 1;
2001-10-19 12:32:39 +00:00
typelib_TypeDescription * pTD = 0;
OUString unoName( toUNOname( header->exceptionType->name() ) );
::typelib_typedescription_getByName( &pTD, unoName.pData );
assert(pTD && "### unknown exception type! leaving out destruction => leaking!!!");
2001-10-19 12:32:39 +00:00
if (pTD)
{
::uno_destructData( pExc, pTD, cpp_release );
::typelib_typedescription_release( pTD );
}
}
}
2001-10-19 12:32:39 +00:00
void raiseException( uno_Any * pUnoExc, uno_Mapping * pUno2Cpp )
{
#if OSL_DEBUG_LEVEL > 1
OString cstr(
OUStringToOString(
OUString::unacquired( &pUnoExc->pType->pTypeName ),
RTL_TEXTENCODING_ASCII_US ) );
2010-12-05 19:11:58 +00:00
fprintf( stderr, "> uno exception occurred: %s\n", cstr.getStr() );
#endif
2001-10-19 12:32:39 +00:00
void * pCppExc;
type_info * rtti;
{
// construct cpp exception object
typelib_TypeDescription * pTypeDescr = 0;
TYPELIB_DANGER_GET( &pTypeDescr, pUnoExc->pType );
assert(pTypeDescr);
2001-10-19 12:32:39 +00:00
if (! pTypeDescr)
{
throw RuntimeException(
"cannot get typedescription for type " +
OUString::unacquired( &pUnoExc->pType->pTypeName ) );
}
2001-10-19 12:32:39 +00:00
pCppExc = __cxxabiv1::__cxa_allocate_exception( pTypeDescr->nSize );
2001-10-19 12:32:39 +00:00
::uno_copyAndConvertData( pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp );
// destruct uno exception
::uno_any_destruct( pUnoExc, 0 );
// avoiding locked counts
static RTTI * s_rtti = 0;
if (! s_rtti)
{
MutexGuard guard( Mutex::getGlobalMutex() );
if (! s_rtti)
{
#ifdef LEAK_STATIC_DATA
s_rtti = new RTTI();
#else
static RTTI rtti_data;
s_rtti = &rtti_data;
#endif
}
}
rtti = s_rtti->getRTTI(reinterpret_cast<typelib_CompoundTypeDescription *>(pTypeDescr));
2001-10-19 12:32:39 +00:00
TYPELIB_DANGER_RELEASE( pTypeDescr );
assert(rtti && "### no rtti for throwing exception!");
2001-10-19 12:32:39 +00:00
if (! rtti)
{
throw RuntimeException(
"no rtti for type " +
OUString::unacquired( &pUnoExc->pType->pTypeName ) );
}
2001-10-19 12:32:39 +00:00
}
__cxxabiv1::__cxa_throw( pCppExc, rtti, deleteException );
2001-10-19 12:32:39 +00:00
}
void fillUnoException( __cxa_exception * header, uno_Any * pUnoExc, uno_Mapping * pCpp2Uno )
2001-10-19 12:32:39 +00:00
{
if (! header)
{
RuntimeException aRE( "no exception header!" );
Type const & rType = cppu::UnoType<decltype(aRE)>::get();
uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
SAL_WARN("bridges", aRE.Message);
return;
}
2001-10-19 12:32:39 +00:00
typelib_TypeDescription * pExcTypeDescr = 0;
OUString unoName( toUNOname( header->exceptionType->name() ) );
#if OSL_DEBUG_LEVEL > 1
OString cstr_unoName( OUStringToOString( unoName, RTL_TEXTENCODING_ASCII_US ) );
2010-12-05 19:11:58 +00:00
fprintf( stderr, "> c++ exception occurred: %s\n", cstr_unoName.getStr() );
#endif
typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData );
if (0 == pExcTypeDescr)
{
RuntimeException aRE( "exception type not found: " + unoName );
Type const & rType = cppu::UnoType<decltype(aRE)>::get();
uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
SAL_WARN("bridges", aRE.Message);
}
else
{
// construct uno exception any
uno_any_constructAndConvert( pUnoExc, header->adjustedPtr, pExcTypeDescr, pCpp2Uno );
typelib_typedescription_release( pExcTypeDescr );
}
2001-10-19 12:32:39 +00:00
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */