LCOV - code coverage report
Current view: top level - bridges/source/cpp_uno/shared - bridge.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 62 71 87.3 %
Date: 2014-04-11 Functions: 10 10 100.0 %
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 "bridges/cpp_uno/shared/bridge.hxx"
      22             : 
      23             : #include "bridges/cpp_uno/shared/cppinterfaceproxy.hxx"
      24             : #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx"
      25             : 
      26             : #include "com/sun/star/uno/XInterface.hpp"
      27             : #include "osl/diagnose.h"
      28             : #include "osl/interlck.h"
      29             : #include "rtl/ustring.h"
      30             : #include "sal/types.h"
      31             : #include "typelib/typedescription.h"
      32             : #include "uno/dispatcher.h"
      33             : #include "uno/environment.h"
      34             : #include "uno/mapping.h"
      35             : 
      36             : namespace bridges { namespace cpp_uno { namespace shared {
      37             : 
      38       12042 : void freeMapping(uno_Mapping * pMapping)
      39             : {
      40       12042 :     delete static_cast< Bridge::Mapping * >( pMapping )->pBridge;
      41       12042 : }
      42             : 
      43       62250 : void acquireMapping(uno_Mapping * pMapping)
      44             : {
      45       62250 :     static_cast< Bridge::Mapping * >( pMapping )->pBridge->acquire();
      46       62250 : }
      47             : 
      48       74276 : void releaseMapping(uno_Mapping * pMapping)
      49             : {
      50       74276 :     static_cast< Bridge::Mapping * >( pMapping )->pBridge->release();
      51       74276 : }
      52             : 
      53      107578 : void cpp2unoMapping(
      54             :     uno_Mapping * pMapping, void ** ppUnoI, void * pCppI,
      55             :     typelib_InterfaceTypeDescription * pTypeDescr)
      56             : {
      57             :     OSL_ENSURE( ppUnoI && pTypeDescr, "### null ptr!" );
      58      107578 :     if (*ppUnoI)
      59             :     {
      60             :         (*reinterpret_cast< uno_Interface * >( *ppUnoI )->release)(
      61           0 :             reinterpret_cast< uno_Interface * >( *ppUnoI ) );
      62           0 :         *ppUnoI = 0;
      63             :     }
      64      107578 :     if (pCppI)
      65             :     {
      66      107576 :         Bridge * pBridge = static_cast< Bridge::Mapping * >( pMapping )->pBridge;
      67             : 
      68             :         // get object id of interface to be wrapped
      69      107576 :         rtl_uString * pOId = 0;
      70             :         (*pBridge->pCppEnv->getObjectIdentifier)(
      71      107576 :             pBridge->pCppEnv, &pOId, pCppI );
      72             :         OSL_ASSERT( pOId );
      73             : 
      74             :         // try to get any known interface from target environment
      75             :         (*pBridge->pUnoEnv->getRegisteredInterface)(
      76      107576 :             pBridge->pUnoEnv, ppUnoI, pOId, pTypeDescr );
      77             : 
      78      107576 :         if (! *ppUnoI) // no existing interface, register new proxy interface
      79             :         {
      80             :             // try to publish a new proxy (refcount initially 1)
      81             :             uno_Interface * pSurrogate
      82             :                 = bridges::cpp_uno::shared::UnoInterfaceProxy::create(
      83             :                     pBridge,
      84             :                     static_cast< ::com::sun::star::uno::XInterface * >( pCppI ),
      85       82756 :                     pTypeDescr, pOId );
      86             : 
      87             :             // proxy may be exchanged during registration
      88             :             (*pBridge->pUnoEnv->registerProxyInterface)(
      89             :                 pBridge->pUnoEnv, reinterpret_cast< void ** >( &pSurrogate ),
      90             :                 freeUnoInterfaceProxy, pOId,
      91       82756 :                 pTypeDescr );
      92             : 
      93       82756 :             *ppUnoI = pSurrogate;
      94             :         }
      95      107576 :         ::rtl_uString_release( pOId );
      96             :     }
      97      107578 : }
      98             : 
      99       60118 : void uno2cppMapping(
     100             :     uno_Mapping * pMapping, void ** ppCppI, void * pUnoI,
     101             :     typelib_InterfaceTypeDescription * pTypeDescr)
     102             : {
     103             :     OSL_ASSERT( ppCppI && pTypeDescr );
     104       60118 :     if (*ppCppI)
     105             :     {
     106             :         static_cast< ::com::sun::star::uno::XInterface * >( *ppCppI )->
     107           0 :             release();
     108           0 :         *ppCppI = 0;
     109             :     }
     110       60118 :     if (pUnoI)
     111             :     {
     112       60118 :         Bridge * pBridge = static_cast< Bridge::Mapping * >( pMapping )->pBridge;
     113             : 
     114             :         // get object id of uno interface to be wrapped
     115       60118 :         rtl_uString * pOId = 0;
     116             :         (*pBridge->pUnoEnv->getObjectIdentifier)(
     117       60118 :             pBridge->pUnoEnv, &pOId, pUnoI );
     118             :         OSL_ASSERT( pOId );
     119             : 
     120             :         // try to get any known interface from target environment
     121             :         (*pBridge->pCppEnv->getRegisteredInterface)(
     122       60118 :             pBridge->pCppEnv, ppCppI, pOId, pTypeDescr );
     123             : 
     124       60118 :         if (! *ppCppI) // no existing interface, register new proxy interface
     125             :         {
     126             :             // try to publish a new proxy (ref count initially 1)
     127             :             com::sun::star::uno::XInterface * pProxy
     128             :                 = bridges::cpp_uno::shared::CppInterfaceProxy::create(
     129             :                     pBridge, static_cast< uno_Interface * >( pUnoI ),
     130       25327 :                     pTypeDescr, pOId );
     131             : 
     132             :             // proxy may be exchanged during registration
     133             :             (*pBridge->pCppEnv->registerProxyInterface)(
     134             :                 pBridge->pCppEnv, reinterpret_cast< void ** >( &pProxy ),
     135             :                 freeCppInterfaceProxy, pOId,
     136       25327 :                 pTypeDescr );
     137             : 
     138       25327 :             *ppCppI = pProxy;
     139             :         }
     140       60118 :         ::rtl_uString_release( pOId );
     141             :     }
     142       60118 : }
     143             : 
     144       12102 : uno_Mapping * Bridge::createMapping(
     145             :     uno_ExtEnvironment * pCppEnv, uno_ExtEnvironment * pUnoEnv,
     146             :     bool bExportCpp2Uno) SAL_THROW(())
     147             : {
     148       12102 :     Bridge * bridge = new Bridge(pCppEnv, pUnoEnv, bExportCpp2Uno);
     149       12102 :     return bExportCpp2Uno ? &bridge->aCpp2Uno : &bridge->aUno2Cpp;
     150             : }
     151             : 
     152      170333 : void Bridge::acquire() SAL_THROW(())
     153             : {
     154      170333 :     if (1 == osl_atomic_increment( &nRef ))
     155             :     {
     156           0 :         if (bExportCpp2Uno)
     157             :         {
     158           0 :             uno_Mapping * pMapping = &aCpp2Uno;
     159             :             ::uno_registerMapping(
     160             :                 &pMapping, freeMapping, (uno_Environment *)pCppEnv,
     161           0 :                 (uno_Environment *)pUnoEnv, 0 );
     162             :         }
     163             :         else
     164             :         {
     165           0 :             uno_Mapping * pMapping = &aUno2Cpp;
     166             :             ::uno_registerMapping(
     167             :                 &pMapping, freeMapping, (uno_Environment *)pUnoEnv,
     168           0 :                 (uno_Environment *)pCppEnv, 0 );
     169             :         }
     170             :     }
     171      170333 : }
     172             : 
     173      181007 : void Bridge::release() SAL_THROW(())
     174             : {
     175      181007 :     if (! osl_atomic_decrement( &nRef ))
     176             :     {
     177       12042 :         ::uno_revokeMapping( bExportCpp2Uno ? &aCpp2Uno : &aUno2Cpp );
     178             :     }
     179      181007 : }
     180             : 
     181       12102 : Bridge::Bridge(
     182             :     uno_ExtEnvironment * pCppEnv_, uno_ExtEnvironment * pUnoEnv_,
     183             :     bool bExportCpp2Uno_) SAL_THROW(())
     184             :     : nRef( 1 )
     185             :     , pCppEnv( pCppEnv_ )
     186             :     , pUnoEnv( pUnoEnv_ )
     187       12102 :     , bExportCpp2Uno( bExportCpp2Uno_ )
     188             : {
     189       12102 :     aCpp2Uno.pBridge = this;
     190       12102 :     aCpp2Uno.acquire = acquireMapping;
     191       12102 :     aCpp2Uno.release = releaseMapping;
     192       12102 :     aCpp2Uno.mapInterface = cpp2unoMapping;
     193             : 
     194       12102 :     aUno2Cpp.pBridge = this;
     195       12102 :     aUno2Cpp.acquire = acquireMapping;
     196       12102 :     aUno2Cpp.release = releaseMapping;
     197       12102 :     aUno2Cpp.mapInterface = uno2cppMapping;
     198             : 
     199       12102 :     (*((uno_Environment *)pCppEnv)->acquire)( (uno_Environment *)pCppEnv );
     200       12102 :     (*((uno_Environment *)pUnoEnv)->acquire)( (uno_Environment *)pUnoEnv );
     201       12102 : }
     202             : 
     203       12042 : Bridge::~Bridge() SAL_THROW(())
     204             : {
     205       12042 :     (*((uno_Environment *)pUnoEnv)->release)( (uno_Environment *)pUnoEnv );
     206       12042 :     (*((uno_Environment *)pCppEnv)->release)( (uno_Environment *)pCppEnv );
     207       12042 : }
     208             : 
     209             : } } }
     210             : 
     211             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10