cid#1636693 COPY_INSTEAD_OF_MOVE
[LibreOffice.git] / bridges / source / cpp_uno / gcc3_linux_hppa / except.cxx
blobb5339eb2b1b65ef5b3fc6353239aa3f42a1b2a75
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <stdio.h>
21 #include <string.h>
22 #include <dlfcn.h>
23 #include <cxxabi.h>
24 #include <rtl/strbuf.hxx>
25 #include <rtl/ustrbuf.hxx>
26 #include <osl/mutex.hxx>
27 #include <sal/log.hxx>
29 #include <com/sun/star/uno/genfunc.hxx>
30 #include "com/sun/star/uno/RuntimeException.hpp"
31 #include <typelib/typedescription.hxx>
32 #include <uno/any2.h>
33 #include <unordered_map>
34 #include "share.hxx"
37 using namespace ::std;
38 using namespace ::osl;
39 using namespace ::com::sun::star::uno;
40 using namespace ::__cxxabiv1;
42 extern sal_Int32 * pHack;
43 extern sal_Int32 nHack;
45 namespace CPPU_CURRENT_NAMESPACE
47 void dummy_can_throw_anything( char const * )
51 static OUString toUNOname( char const * p )
53 #if OSL_DEBUG_LEVEL > 1
54 char const * start = p;
55 #endif
57 // example: N3com3sun4star4lang24IllegalArgumentExceptionE
59 OUStringBuffer buf( 64 );
60 assert( 'N' == *p );
61 ++p; // skip N
63 while ('E' != *p)
65 // read chars count
66 long n = (*p++ - '0');
67 while ('0' <= *p && '9' >= *p)
69 n *= 10;
70 n += (*p++ - '0');
72 buf.appendAscii( p, n );
73 p += n;
74 if ('E' != *p)
75 buf.append( '.' );
78 #if OSL_DEBUG_LEVEL > 1
79 OUString ret( buf.makeStringAndClear() );
80 OString c_ret( OUStringToOString( ret, RTL_TEXTENCODING_ASCII_US ) );
81 fprintf( stderr, "> toUNOname(): %s => %s\n", start, c_ret.getStr() );
82 return ret;
83 #else
84 return buf.makeStringAndClear();
85 #endif
88 class RTTI
90 typedef std::unordered_map< OUString, type_info * > t_rtti_map;
92 Mutex m_mutex;
93 t_rtti_map m_rttis;
94 t_rtti_map m_generatedRttis;
96 void * m_hApp;
98 public:
99 RTTI();
100 ~RTTI();
102 type_info * getRTTI(typelib_CompoundTypeDescription *);
105 RTTI::RTTI()
106 : m_hApp( dlopen( 0, RTLD_LAZY ) )
110 RTTI::~RTTI()
112 dlclose( m_hApp );
116 type_info * RTTI::getRTTI( typelib_CompoundTypeDescription *pTypeDescr )
118 type_info * rtti;
120 OUString const & unoName = *(OUString const *)&pTypeDescr->aBase.pTypeName;
122 MutexGuard guard( m_mutex );
123 t_rtti_map::const_iterator iRttiFind( m_rttis.find( unoName ) );
124 if (iRttiFind == m_rttis.end())
126 // RTTI symbol
127 OStringBuffer buf( 64 );
128 buf.append( "_ZTIN" );
129 sal_Int32 index = 0;
132 OUString token( unoName.getToken( 0, '.', index ) );
133 buf.append( token.getLength() );
134 OString c_token( OUStringToOString( token, RTL_TEXTENCODING_ASCII_US ) );
135 buf.append( c_token );
137 while (index >= 0);
138 buf.append( 'E' );
140 OString symName( buf.makeStringAndClear() );
141 rtti = (type_info *)dlsym( m_hApp, symName.getStr() );
143 if (rtti)
145 pair< t_rtti_map::iterator, bool > insertion(
146 m_rttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
147 assert(insertion.second && "### inserting new rtti failed?!");
149 else
151 // try to lookup the symbol in the generated rtti map
152 t_rtti_map::const_iterator iFind( m_generatedRttis.find( unoName ) );
153 if (iFind == m_generatedRttis.end())
155 // we must generate it !
156 // symbol and rtti-name is nearly identical,
157 // the symbol is prefixed with _ZTI
158 char const * rttiName = symName.getStr() +4;
159 #if OSL_DEBUG_LEVEL > 1
160 fprintf( stderr,"generated rtti for %s\n", rttiName );
161 #endif
162 if (pTypeDescr->pBaseTypeDescription)
164 // ensure availability of base
165 type_info * base_rtti = getRTTI(
166 (typelib_CompoundTypeDescription *)pTypeDescr->pBaseTypeDescription );
167 rtti = new __si_class_type_info(
168 strdup( rttiName ), (__class_type_info *)base_rtti );
170 else
172 // this class has no base class
173 rtti = new __class_type_info( strdup( rttiName ) );
176 pair< t_rtti_map::iterator, bool > insertion(
177 m_generatedRttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
178 assert(insertion.second && "### inserting new generated rtti failed?!");
180 else // taking already generated rtti
182 rtti = iFind->second;
186 else
188 rtti = iRttiFind->second;
191 return rtti;
195 static void deleteException( void * pExc )
197 __cxa_exception const * header = ((__cxa_exception const *)pExc - 1);
198 typelib_TypeDescription * pTD = 0;
199 OUString unoName( toUNOname( header->exceptionType->name() ) );
200 ::typelib_typedescription_getByName( &pTD, unoName.pData );
201 assert(pTD && "### unknown exception type! leaving out destruction => leaking!!!");
202 if (pTD)
204 ::uno_destructData( pExc, pTD, cpp_release );
205 ::typelib_typedescription_release( pTD );
209 void raiseException( uno_Any * pUnoExc, uno_Mapping * pUno2Cpp )
211 #if OSL_DEBUG_LEVEL > 1
212 OString cstr(
213 OUStringToOString(
214 OUString::unacquired( &pUnoExc->pType->pTypeName ),
215 RTL_TEXTENCODING_ASCII_US ) );
216 fprintf( stderr, "> uno exception occurred: %s\n", cstr.getStr() );
217 #endif
218 void * pCppExc;
219 type_info * rtti;
222 // construct cpp exception object
223 typelib_TypeDescription * pTypeDescr = 0;
224 TYPELIB_DANGER_GET( &pTypeDescr, pUnoExc->pType );
225 assert(pTypeDescr);
226 if (! pTypeDescr)
228 throw RuntimeException(
229 OUString("cannot get typedescription for type ") +
230 OUString::unacquired( &pUnoExc->pType->pTypeName ) );
233 pCppExc = __cxa_allocate_exception( pTypeDescr->nSize );
234 ::uno_copyAndConvertData( pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp );
236 // destruct uno exception
237 ::uno_any_destruct( pUnoExc, 0 );
238 // avoiding locked counts
239 static RTTI rtti_data;
240 rtti = (type_info*)rtti_data.getRTTI((typelib_CompoundTypeDescription*)pTypeDescr);
241 TYPELIB_DANGER_RELEASE( pTypeDescr );
242 assert(rtti && "### no rtti for throwing exception!");
243 if (! rtti)
245 throw RuntimeException(
246 OUString("no rtti for type ") +
247 OUString::unacquired( &pUnoExc->pType->pTypeName ) );
252 __cxa_throw( pCppExc, rtti, deleteException );
255 static void* getAdjustedPtr(__cxa_exception* header)
257 return header->adjustedPtr;
260 void fillUnoException(uno_Any * pUnoExc, uno_Mapping * pCpp2Uno)
262 __cxa_exception * header = __cxa_get_globals()->caughtExceptions;
263 if (! header)
265 RuntimeException aRE( "no exception header!" );
266 Type const & rType = cppu::UnoType<decltype(aRE)>::get();
267 uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
268 SAL_WARN("bridges", aRE.Message);
269 return;
272 std::type_info *exceptionType = __cxa_current_exception_type();
274 typelib_TypeDescription * pExcTypeDescr = 0;
275 OUString unoName( toUNOname( exceptionType->name() ) );
276 #if OSL_DEBUG_LEVEL > 1
277 OString cstr_unoName( OUStringToOString( unoName, RTL_TEXTENCODING_ASCII_US ) );
278 fprintf( stderr, "> c++ exception occurred: %s\n", cstr_unoName.getStr() );
279 #endif
280 typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData );
281 if (0 == pExcTypeDescr)
283 RuntimeException aRE(
284 OUString("exception type not found: ") + unoName );
285 Type const & rType = cppu::UnoType<decltype(aRE)>::get();
286 uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
287 SAL_WARN("bridges", aRE.Message);
289 else
291 // construct uno exception any
292 uno_any_constructAndConvert( pUnoExc, getAdjustedPtr(header), pExcTypeDescr, pCpp2Uno );
293 typelib_typedescription_release( pExcTypeDescr );
298 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */