LCOV - code coverage report
Current view: top level - cppuhelper/source - exc_thrower.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 65 83 78.3 %
Date: 2014-04-11 Functions: 11 13 84.6 %
Legend: Lines: hit not hit

          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 "rtl/instance.hxx"
      22             : #include "osl/diagnose.h"
      23             : #include "osl/doublecheckedlocking.h"
      24             : #include "osl/mutex.hxx"
      25             : #include "uno/dispatcher.hxx"
      26             : #include <uno/lbnames.h>
      27             : #include "uno/mapping.hxx"
      28             : #include "cppuhelper/detail/XExceptionThrower.hpp"
      29             : #include "com/sun/star/uno/RuntimeException.hpp"
      30             : 
      31             : #include "cppuhelper/exc_hlp.hxx"
      32             : 
      33             : using namespace ::osl;
      34             : using namespace ::cppu;
      35             : using namespace ::com::sun::star;
      36             : using namespace ::com::sun::star::uno;
      37             : 
      38             : namespace
      39             : {
      40             : 
      41             : using cppuhelper::detail::XExceptionThrower;
      42             : 
      43             : 
      44             : struct ExceptionThrower : public uno_Interface, XExceptionThrower
      45             : {
      46             :     inline ExceptionThrower();
      47             : 
      48         131 :     virtual ~ExceptionThrower() {}
      49             : 
      50       22208 :     static inline Type const & getCppuType()
      51             :     {
      52             :         return ::getCppuType(
      53       22208 :             reinterpret_cast< Reference< XExceptionThrower > const * >(0) );
      54             :     }
      55             : 
      56             :     // XInterface
      57             :     virtual Any SAL_CALL queryInterface( Type const & type )
      58             :         throw (RuntimeException, std::exception) SAL_OVERRIDE;
      59             :     virtual void SAL_CALL acquire() throw () SAL_OVERRIDE;
      60             :     virtual void SAL_CALL release() throw () SAL_OVERRIDE;
      61             : 
      62             :     // XExceptionThrower
      63             :     virtual void SAL_CALL throwException( Any const & exc ) throw (Exception, std::exception) SAL_OVERRIDE;
      64             :     virtual void SAL_CALL rethrowException() throw (Exception, std::exception) SAL_OVERRIDE;
      65             : };
      66             : 
      67             : extern "C"
      68             : {
      69             : 
      70             : 
      71      118776 : static void SAL_CALL ExceptionThrower_acquire_release_nop(
      72             :     SAL_UNUSED_PARAMETER uno_Interface * )
      73      118776 : {}
      74             : 
      75             : 
      76       39592 : static void SAL_CALL ExceptionThrower_dispatch(
      77             :     uno_Interface * pUnoI, typelib_TypeDescription const * pMemberType,
      78             :     void * pReturn, void * pArgs [], uno_Any ** ppException )
      79             : {
      80             :     OSL_ASSERT( pMemberType->eTypeClass == typelib_TypeClass_INTERFACE_METHOD );
      81             : 
      82       39592 :     switch (reinterpret_cast< typelib_InterfaceMemberTypeDescription * >(
      83             :                 const_cast< typelib_TypeDescription * >( pMemberType ) )->
      84             :             nPosition)
      85             :     {
      86             :     case 0: // queryInterace()
      87             :     {
      88             :         Type const & rType_demanded =
      89       19796 :             *reinterpret_cast< Type const * >( pArgs[ 0 ] );
      90       19796 :         if (rType_demanded.equals(
      91             :                 ::getCppuType( reinterpret_cast<
      92       19796 :                                Reference< XInterface > const * >(0) ) ) ||
      93           0 :             rType_demanded.equals( ExceptionThrower::getCppuType() ))
      94             :         {
      95       19796 :             typelib_TypeDescription * pTD = 0;
      96       19796 :             TYPELIB_DANGER_GET( &pTD, rType_demanded.getTypeLibType() );
      97             :             uno_any_construct(
      98       19796 :                 reinterpret_cast< uno_Any * >( pReturn ), &pUnoI, pTD, 0 );
      99       19796 :             TYPELIB_DANGER_RELEASE( pTD );
     100             :         }
     101             :         else
     102             :         {
     103             :             uno_any_construct(
     104           0 :                 reinterpret_cast< uno_Any * >( pReturn ), 0, 0, 0 );
     105             :         }
     106       19796 :         *ppException = 0;
     107       19796 :         break;
     108             :     }
     109             :     case 1: // acquire()
     110             :     case 2: // release()
     111           0 :         *ppException = 0;
     112           0 :         break;
     113             :     case 3: // throwException()
     114             :     {
     115       19796 :         uno_Any * pAny = reinterpret_cast< uno_Any * >( pArgs[ 0 ] );
     116             :         OSL_ASSERT( pAny->pType->eTypeClass == typelib_TypeClass_EXCEPTION );
     117       19796 :         uno_type_any_construct( *ppException, pAny->pData, pAny->pType, 0 );
     118       19796 :         break;
     119             :     }
     120             :     default:
     121             :     {
     122             :         OSL_ASSERT( false );
     123             :         RuntimeException exc(
     124           0 :             "not implemented!", Reference< XInterface >() );
     125             :         uno_type_any_construct(
     126           0 :             *ppException, &exc, ::getCppuType( &exc ).getTypeLibType(), 0 );
     127           0 :         break;
     128             :     }
     129             :     }
     130       39592 : }
     131             : 
     132             : } // extern "C"
     133             : 
     134             : 
     135        2412 : Any ExceptionThrower::queryInterface( Type const & type )
     136             :     throw (RuntimeException, std::exception)
     137             : {
     138        2412 :     if (type.equals( ::getCppuType( reinterpret_cast<
     139        2412 :                                     Reference< XInterface > const * >(0) ) ) ||
     140           0 :         type.equals( ExceptionThrower::getCppuType() ))
     141             :     {
     142        2412 :         XExceptionThrower * that = static_cast< XExceptionThrower * >( this );
     143        2412 :         return Any( &that, type );
     144             :     }
     145           0 :     return Any();
     146             : }
     147             : 
     148             : 
     149        7236 : void ExceptionThrower::acquire() throw ()
     150             : {
     151        7236 : }
     152             : 
     153        7236 : void ExceptionThrower::release() throw ()
     154             : {
     155        7236 : }
     156             : 
     157             : 
     158           0 : void ExceptionThrower::throwException( Any const & exc ) throw (Exception, std::exception)
     159             : {
     160             :     OSL_FAIL( "unexpected!" );
     161           0 :     throwException( exc );
     162           0 : }
     163             : 
     164             : 
     165        2412 : void ExceptionThrower::rethrowException() throw (Exception, std::exception)
     166             : {
     167        2412 :     throw;
     168             : }
     169             : 
     170             : 
     171         131 : inline ExceptionThrower::ExceptionThrower()
     172             : {
     173         131 :     uno_Interface::acquire = ExceptionThrower_acquire_release_nop;
     174         131 :     uno_Interface::release = ExceptionThrower_acquire_release_nop;
     175         131 :     uno_Interface::pDispatcher = ExceptionThrower_dispatch;
     176         131 : }
     177             : 
     178             : class theExceptionThrower : public rtl::Static<ExceptionThrower, theExceptionThrower> {};
     179             : 
     180             : } // anonymous namespace
     181             : 
     182             : 
     183             : namespace cppu
     184             : {
     185             : 
     186             : 
     187       19796 : void SAL_CALL throwException( Any const & exc ) SAL_THROW( (Exception) )
     188             : {
     189       19796 :     if (exc.getValueTypeClass() != TypeClass_EXCEPTION)
     190             :     {
     191             :         throw RuntimeException(
     192             :             "no UNO exception given "
     193             :             "(must be derived from com::sun::star::uno::Exception)!",
     194           0 :             Reference< XInterface >() );
     195             :     }
     196             : 
     197       19796 :     Mapping uno2cpp(Environment(UNO_LB_UNO), Environment::getCurrent());
     198       19796 :     if (! uno2cpp.is())
     199             :     {
     200             :         throw RuntimeException(
     201             :             "cannot get binary UNO to C++ mapping!",
     202           0 :             Reference< XInterface >() );
     203             :     }
     204             : 
     205       39592 :     Reference< XExceptionThrower > xThrower;
     206             :     uno2cpp.mapInterface(
     207             :         reinterpret_cast< void ** >( &xThrower ),
     208       19796 :         static_cast< uno_Interface * >( &theExceptionThrower::get() ),
     209       39592 :         ExceptionThrower::getCppuType() );
     210             :     OSL_ASSERT( xThrower.is() );
     211       39592 :     xThrower->throwException( exc );
     212           0 : }
     213             : 
     214             : 
     215        2412 : Any SAL_CALL getCaughtException()
     216             : {
     217        2412 :     Mapping cpp2uno(Environment::getCurrent(), Environment(UNO_LB_UNO));
     218        2412 :     if (! cpp2uno.is())
     219             :     {
     220             :         throw RuntimeException(
     221             :             "cannot get C++ to binary UNO mapping!",
     222           0 :             Reference< XInterface >() );
     223             :     }
     224        4824 :     Mapping uno2cpp(Environment(UNO_LB_UNO), Environment::getCurrent());
     225        2412 :     if (! uno2cpp.is())
     226             :     {
     227             :         throw RuntimeException(
     228             :             "cannot get binary UNO to C++ mapping!",
     229           0 :             Reference< XInterface >() );
     230             :     }
     231             : 
     232        2412 :     typelib_TypeDescription * pTD = 0;
     233             :     TYPELIB_DANGER_GET(
     234        2412 :         &pTD, ExceptionThrower::getCppuType().getTypeLibType() );
     235             : 
     236        4824 :     UnoInterfaceReference unoI;
     237             :     cpp2uno.mapInterface(
     238             :         reinterpret_cast< void ** >( &unoI.m_pUnoI ),
     239        2412 :         static_cast< XExceptionThrower * >( &theExceptionThrower::get() ), pTD );
     240             :     OSL_ASSERT( unoI.is() );
     241             : 
     242        2412 :     typelib_TypeDescription * pMemberTD = 0;
     243             :     TYPELIB_DANGER_GET(
     244             :         &pMemberTD,
     245             :         reinterpret_cast< typelib_InterfaceTypeDescription * >( pTD )->
     246        2412 :         ppMembers[ 1 ] /* rethrowException() */ );
     247             : 
     248             :     uno_Any exc_mem;
     249        2412 :     uno_Any * exc = &exc_mem;
     250        2412 :     unoI.dispatch( pMemberTD, 0, 0, &exc );
     251             : 
     252        2412 :     TYPELIB_DANGER_RELEASE( pMemberTD );
     253        2412 :     TYPELIB_DANGER_RELEASE( pTD );
     254             : 
     255        2412 :     if (exc == 0)
     256             :     {
     257             :         throw RuntimeException(
     258             :             "rethrowing C++ exception failed!",
     259           0 :             Reference< XInterface >() );
     260             :     }
     261             : 
     262        2412 :     Any ret;
     263        2412 :     uno_any_destruct( &ret, reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
     264             :     uno_type_any_constructAndConvert(
     265        2412 :         &ret, exc->pData, exc->pType, uno2cpp.get() );
     266        2412 :     uno_any_destruct( exc, 0 );
     267        4824 :     return ret;
     268             : }
     269             : 
     270             : }
     271             : 
     272             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10