LCOV - code coverage report
Current view: top level - binaryurp/source - proxy.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 79 89 88.8 %
Date: 2015-06-13 12:38:46 Functions: 12 12 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             : #include "sal/config.h"
      21             : 
      22             : #include <cassert>
      23             : #include <exception>
      24             : #include <vector>
      25             : 
      26             : #include "cppuhelper/exc_hlp.hxx"
      27             : #include "rtl/ref.hxx"
      28             : #include "rtl/ustring.hxx"
      29             : #include "sal/types.h"
      30             : #include "typelib/typedescription.h"
      31             : #include "typelib/typedescription.hxx"
      32             : #include "uno/any2.h"
      33             : #include "uno/dispatcher.h"
      34             : #include "uno/dispatcher.hxx"
      35             : 
      36             : #include "binaryany.hxx"
      37             : #include "bridge.hxx"
      38             : #include "proxy.hxx"
      39             : 
      40             : namespace binaryurp {
      41             : 
      42             : namespace {
      43             : 
      44       25475 : extern "C" void SAL_CALL proxy_acquireInterface(uno_Interface * pInterface) {
      45             :     assert(pInterface != 0);
      46       25475 :     static_cast< Proxy * >(pInterface)->do_acquire();
      47       25475 : }
      48             : 
      49       27456 : extern "C" void SAL_CALL proxy_releaseInterface(uno_Interface * pInterface) {
      50             :     assert(pInterface != 0);
      51       27456 :     static_cast< Proxy * >(pInterface)->do_release();
      52       27456 : }
      53             : 
      54        7140 : extern "C" void SAL_CALL proxy_dispatchInterface(
      55             :     uno_Interface * pUnoI, typelib_TypeDescription const * pMemberType,
      56             :     void * pReturn, void ** pArgs, uno_Any ** ppException)
      57             : {
      58             :     assert(pUnoI != 0);
      59             :     static_cast< Proxy * >(pUnoI)->do_dispatch(
      60        7140 :         pMemberType, pReturn, pArgs, ppException);
      61        7140 : }
      62             : 
      63             : }
      64             : 
      65        1989 : Proxy::Proxy(
      66             :     rtl::Reference< Bridge > const & bridge, OUString const & oid,
      67             :     css::uno::TypeDescription const & type):
      68        1989 :     bridge_(bridge), oid_(oid), type_(type), references_(1)
      69             : {
      70             :     assert(bridge.is());
      71        1989 :     acquire = &proxy_acquireInterface;
      72        1989 :     release = &proxy_releaseInterface;
      73        1989 :     pDispatcher = &proxy_dispatchInterface;
      74        1989 : }
      75             : 
      76             : 
      77             : 
      78       25475 : void Proxy::do_acquire() {
      79       25475 :     if (osl_atomic_increment(&references_) == 1) {
      80           0 :         bridge_->resurrectProxy(*this);
      81             :     }
      82       25475 : }
      83             : 
      84       27456 : void Proxy::do_release() {
      85       27456 :     if (osl_atomic_decrement(&references_) == 0) {
      86        1981 :         bridge_->revokeProxy(*this);
      87             :     }
      88       27456 : }
      89             : 
      90        1981 : void Proxy::do_free() {
      91        1981 :     bridge_->freeProxy(*this);
      92        1981 :     delete this;
      93        1981 : }
      94             : 
      95        7140 : void Proxy::do_dispatch(
      96             :     typelib_TypeDescription const * member, void * returnValue,
      97             :     void ** arguments, uno_Any ** exception) const
      98             : {
      99             :     try {
     100             :         try {
     101        7140 :             do_dispatch_throw(member, returnValue, arguments, exception);
     102           0 :         } catch (const std::exception & e) {
     103             :             throw css::uno::RuntimeException(
     104           0 :                 "caught C++ exception: " +
     105             :                 OStringToOUString(
     106           0 :                     OString(e.what()), RTL_TEXTENCODING_ASCII_US));
     107             :                 // best-effort string conversion
     108             :         }
     109           2 :     } catch (const css::uno::RuntimeException &) {
     110           2 :         css::uno::Any exc(cppu::getCaughtException());
     111             :         uno_copyAndConvertData(
     112             :             *exception, &exc,
     113           2 :             (css::uno::TypeDescription(cppu::UnoType< css::uno::Any >::get()).
     114             :              get()),
     115           4 :             bridge_->getCppToBinaryMapping().get());
     116             :     }
     117        7140 : }
     118             : 
     119       77916 : bool Proxy::isProxy(
     120             :     rtl::Reference< Bridge > const & bridge,
     121             :     css::uno::UnoInterfaceReference const & object, OUString * oid)
     122             : {
     123             :     assert(object.is());
     124       78047 :     return object.m_pUnoI->acquire == &proxy_acquireInterface &&
     125       78047 :         static_cast< Proxy * >(object.m_pUnoI)->isProxy(bridge, oid);
     126             : }
     127             : 
     128        1981 : Proxy::~Proxy() {}
     129             : 
     130        7140 : void Proxy::do_dispatch_throw(
     131             :     typelib_TypeDescription const * member, void * returnValue,
     132             :     void ** arguments, uno_Any ** exception) const
     133             : {
     134             :     //TODO: Optimize queryInterface:
     135             :     assert(member != 0);
     136        7140 :     bool setter = false;
     137        7140 :     std::vector< BinaryAny > inArgs;
     138        7140 :     switch (member->eTypeClass) {
     139             :     case typelib_TypeClass_INTERFACE_ATTRIBUTE:
     140         964 :         setter = returnValue == 0;
     141         964 :         if (setter) {
     142             :             inArgs.push_back(
     143             :                 BinaryAny(
     144             :                     css::uno::TypeDescription(
     145             :                         reinterpret_cast<
     146             :                             typelib_InterfaceAttributeTypeDescription const * >(
     147             :                                 member)->
     148             :                         pAttributeTypeRef),
     149           0 :                     arguments[0]));
     150             :         }
     151         964 :         break;
     152             :     case typelib_TypeClass_INTERFACE_METHOD:
     153             :         {
     154             :             typelib_InterfaceMethodTypeDescription const * mtd =
     155             :                 reinterpret_cast<
     156        6176 :                     typelib_InterfaceMethodTypeDescription const * >(member);
     157       12040 :             for (sal_Int32 i = 0; i != mtd->nParams; ++i) {
     158        5864 :                 if (mtd->pParams[i].bIn) {
     159             :                     inArgs.push_back(
     160             :                         BinaryAny(
     161        5857 :                             css::uno::TypeDescription(mtd->pParams[i].pTypeRef),
     162       11714 :                             arguments[i]));
     163             :                 }
     164             :             }
     165        6176 :             break;
     166             :         }
     167             :     default:
     168             :         assert(false); // this cannot happen
     169           0 :         break;
     170             :     }
     171        7140 :     BinaryAny ret;
     172       14280 :     std::vector< BinaryAny > outArgs;
     173       14278 :     if (bridge_->makeCall(
     174             :             oid_,
     175             :             css::uno::TypeDescription(
     176             :                 const_cast< typelib_TypeDescription * >(member)),
     177       14280 :             setter, inArgs, &ret, &outArgs))
     178             :     {
     179             :         assert(ret.getType().get()->eTypeClass == typelib_TypeClass_EXCEPTION);
     180             :         uno_any_construct(
     181          16 :             *exception, ret.getValue(ret.getType()), ret.getType().get(), 0);
     182             :     } else {
     183        7122 :         switch (member->eTypeClass) {
     184             :         case typelib_TypeClass_INTERFACE_ATTRIBUTE:
     185         964 :             if (!setter) {
     186             :                 css::uno::TypeDescription t(
     187             :                     reinterpret_cast<
     188             :                         typelib_InterfaceAttributeTypeDescription const * >(
     189             :                             member)->
     190         964 :                     pAttributeTypeRef);
     191         964 :                 uno_copyData(returnValue, ret.getValue(t), t.get(), 0);
     192             :             }
     193         964 :             break;
     194             :         case typelib_TypeClass_INTERFACE_METHOD:
     195             :             {
     196             :                 typelib_InterfaceMethodTypeDescription const * mtd =
     197             :                     reinterpret_cast<
     198             :                         typelib_InterfaceMethodTypeDescription const * >(
     199        6158 :                             member);
     200        6158 :                 css::uno::TypeDescription t(mtd->pReturnTypeRef);
     201        6158 :                 if (t.get()->eTypeClass != typelib_TypeClass_VOID) {
     202        1072 :                     uno_copyData(returnValue, ret.getValue(t), t.get(), 0);
     203             :                 }
     204        6158 :                 std::vector< BinaryAny >::iterator i(outArgs.begin());
     205       12020 :                 for (sal_Int32 j = 0; j != mtd->nParams; ++j) {
     206        5862 :                     if (mtd->pParams[j].bOut) {
     207           7 :                         css::uno::TypeDescription pt(mtd->pParams[j].pTypeRef);
     208           7 :                         if (mtd->pParams[j].bIn) {
     209             :                             (void) uno_assignData(
     210           0 :                                 arguments[j], pt.get(), i++->getValue(pt),
     211           0 :                                 pt.get(), 0, 0, 0);
     212             :                         } else {
     213             :                             uno_copyData(
     214           7 :                                 arguments[j], i++->getValue(pt), pt.get(), 0);
     215           7 :                         }
     216             :                     }
     217             :                 }
     218             :                 assert(i == outArgs.end());
     219        6158 :                 break;
     220             :             }
     221             :         default:
     222             :             assert(false); // this cannot happen
     223           0 :             break;
     224             :         }
     225        7122 :         *exception = 0;
     226        7140 :     }
     227        7138 : }
     228             : 
     229         131 : bool Proxy::isProxy(
     230             :     rtl::Reference< Bridge > const & bridge, OUString * oid) const
     231             : {
     232             :     assert(oid != 0);
     233         131 :     if (bridge == bridge_) {
     234         131 :         *oid = oid_;
     235         131 :         return true;
     236             :     } else {
     237           0 :         return false;
     238             :     }
     239             : }
     240             : 
     241             : }
     242             : 
     243             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11