Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include <stdio.h>
22 : #include <string.h>
23 :
24 : #include <rtl/ustrbuf.hxx>
25 : #include <osl/diagnose.h>
26 : #include <sal/log.hxx>
27 :
28 : #include <com/sun/star/uno/genfunc.hxx>
29 : #include "com/sun/star/uno/RuntimeException.hpp"
30 : #include <typelib/typedescription.hxx>
31 : #include <uno/any2.h>
32 :
33 : #include "rtti.hxx"
34 : #include "share.hxx"
35 :
36 :
37 : using namespace ::std;
38 : using namespace ::osl;
39 : using namespace ::com::sun::star::uno;
40 : using namespace ::__cxxabiv1;
41 :
42 :
43 : namespace CPPU_CURRENT_NAMESPACE
44 : {
45 :
46 42388 : static OUString toUNOname( char const * p )
47 : {
48 : #if OSL_DEBUG_LEVEL > 1
49 : char const * start = p;
50 : #endif
51 :
52 : // example: N3com3sun4star4lang24IllegalArgumentExceptionE
53 :
54 42388 : OUStringBuffer buf( 64 );
55 : OSL_ASSERT( 'N' == *p );
56 42388 : ++p; // skip N
57 :
58 299800 : while ('E' != *p)
59 : {
60 : // read chars count
61 215024 : long n = (*p++ - '0');
62 472442 : while ('0' <= *p && '9' >= *p)
63 : {
64 42394 : n *= 10;
65 42394 : n += (*p++ - '0');
66 : }
67 215024 : buf.appendAscii( p, n );
68 215024 : p += n;
69 215024 : if ('E' != *p)
70 172636 : buf.append( '.' );
71 : }
72 :
73 : #if OSL_DEBUG_LEVEL > 1
74 : OUString ret( buf.makeStringAndClear() );
75 : OString c_ret( OUStringToOString( ret, RTL_TEXTENCODING_ASCII_US ) );
76 : fprintf( stderr, "> toUNOname(): %s => %s\n", start, c_ret.getStr() );
77 : return ret;
78 : #else
79 42388 : return buf.makeStringAndClear();
80 : #endif
81 : }
82 :
83 : extern "C" {
84 34358 : static void _GLIBCXX_CDTOR_CALLABI deleteException( void * pExc )
85 : {
86 34358 : __cxa_exception const * header = ((__cxa_exception const *)pExc - 1);
87 34358 : typelib_TypeDescription * pTD = 0;
88 34358 : OUString unoName( toUNOname( header->exceptionType->name() ) );
89 34358 : ::typelib_typedescription_getByName( &pTD, unoName.pData );
90 : OSL_ENSURE( pTD, "### unknown exception type! leaving out destruction => leaking!!!" );
91 34358 : if (pTD)
92 : {
93 34358 : ::uno_destructData( pExc, pTD, cpp_release );
94 34358 : ::typelib_typedescription_release( pTD );
95 34358 : }
96 34358 : }
97 : }
98 :
99 34358 : void raiseException( uno_Any * pUnoExc, uno_Mapping * pUno2Cpp )
100 : {
101 : #if OSL_DEBUG_LEVEL > 1
102 : OString cstr(
103 : OUStringToOString(
104 : *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ),
105 : RTL_TEXTENCODING_ASCII_US ) );
106 : fprintf( stderr, "> uno exception occurred: %s\n", cstr.getStr() );
107 : #endif
108 : void * pCppExc;
109 : type_info * rtti;
110 :
111 : {
112 : // construct cpp exception object
113 34358 : typelib_TypeDescription * pTypeDescr = 0;
114 34358 : TYPELIB_DANGER_GET( &pTypeDescr, pUnoExc->pType );
115 : OSL_ASSERT( pTypeDescr );
116 34358 : if (! pTypeDescr)
117 : {
118 : throw RuntimeException(
119 0 : OUString("cannot get typedescription for type ") +
120 0 : *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ) );
121 : }
122 :
123 34358 : pCppExc = __cxxabiv1::__cxa_allocate_exception( pTypeDescr->nSize );
124 34358 : ::uno_copyAndConvertData( pCppExc, pUnoExc->pData, pTypeDescr, pUno2Cpp );
125 :
126 : // destruct uno exception
127 34358 : ::uno_any_destruct( pUnoExc, 0 );
128 : // avoiding locked counts
129 34358 : rtti = x86_64::getRtti(*pTypeDescr);
130 34358 : TYPELIB_DANGER_RELEASE( pTypeDescr );
131 : OSL_ENSURE( rtti, "### no rtti for throwing exception!" );
132 34358 : if (! rtti)
133 : {
134 : throw RuntimeException(
135 0 : OUString("no rtti for type ") +
136 0 : *reinterpret_cast< OUString const * >( &pUnoExc->pType->pTypeName ) );
137 : }
138 : }
139 :
140 34358 : __cxxabiv1::__cxa_throw( pCppExc, rtti, deleteException );
141 : }
142 :
143 8030 : void fillUnoException( __cxa_exception * header, uno_Any * pUnoExc, uno_Mapping * pCpp2Uno )
144 : {
145 8030 : if (! header)
146 : {
147 0 : RuntimeException aRE( "no exception header!" );
148 0 : Type const & rType = ::getCppuType( &aRE );
149 0 : uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
150 : #if OSL_DEBUG_LEVEL > 0
151 : OString cstr( OUStringToOString( aRE.Message, RTL_TEXTENCODING_ASCII_US ) );
152 : OSL_FAIL( cstr.getStr() );
153 : #endif
154 8030 : return;
155 : }
156 :
157 8030 : typelib_TypeDescription * pExcTypeDescr = 0;
158 8030 : OUString unoName( toUNOname( header->exceptionType->name() ) );
159 : #if OSL_DEBUG_LEVEL > 1
160 : OString cstr_unoName( OUStringToOString( unoName, RTL_TEXTENCODING_ASCII_US ) );
161 : fprintf( stderr, "> c++ exception occurred: %s\n", cstr_unoName.getStr() );
162 : #endif
163 8030 : typelib_typedescription_getByName( &pExcTypeDescr, unoName.pData );
164 8030 : if (0 == pExcTypeDescr)
165 : {
166 0 : RuntimeException aRE( OUString("exception type not found: ") + unoName );
167 0 : Type const & rType = ::getCppuType( &aRE );
168 0 : uno_type_any_constructAndConvert( pUnoExc, &aRE, rType.getTypeLibType(), pCpp2Uno );
169 : #if OSL_DEBUG_LEVEL > 0
170 : OString cstr( OUStringToOString( aRE.Message, RTL_TEXTENCODING_ASCII_US ) );
171 : OSL_FAIL( cstr.getStr() );
172 : #endif
173 : }
174 : else
175 : {
176 : // construct uno exception any
177 8030 : uno_any_constructAndConvert( pUnoExc, header->adjustedPtr, pExcTypeDescr, pCpp2Uno );
178 8030 : typelib_typedescription_release( pExcTypeDescr );
179 8030 : }
180 : }
181 :
182 : }
183 :
184 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|