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

Generated by: LCOV version 1.10