LCOV - code coverage report
Current view: top level - cppu/source/uno - cascade_mapping.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 109 117 93.2 %
Date: 2014-11-03 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 "osl/interlck.h"
      21             : #include "rtl/ustring.hxx"
      22             : #include "uno/environment.hxx"
      23             : #include <uno/lbnames.h>
      24             : #include "uno/mapping.hxx"
      25             : #include "uno/dispatcher.h"
      26             : 
      27             : #include "cppu/EnvDcp.hxx"
      28             : 
      29             : #include "cascade_mapping.hxx"
      30             : 
      31             : #ifdef LOG_LIFECYLE_MediatorMapping
      32             : #  include <iostream>
      33             : #  define LOG_LIFECYLE_MediatorMapping_emit(x) x
      34             : 
      35             : #else
      36             : #  define LOG_LIFECYLE_MediatorMapping_emit(x)
      37             : 
      38             : #endif
      39             : 
      40             : 
      41             : using namespace com::sun::star;
      42             : 
      43             : class MediatorMapping : public uno_Mapping
      44             : {
      45             :     oslInterlockedCount m_refCount;
      46             : 
      47             :     uno::Mapping        m_from2uno;
      48             :     uno::Mapping        m_uno2to;
      49             : 
      50             :     uno::Environment    m_from;
      51             :     uno::Environment    m_interm;
      52             :     uno::Environment    m_to;
      53             : 
      54             : public:
      55             :     void acquire(void);
      56             :     void release(void);
      57             : 
      58             :     void mapInterface(void                            ** ppOut,
      59             :                       void                             * pInterface,
      60             :                       typelib_InterfaceTypeDescription * pInterfaceTypeDescr);
      61             :     MediatorMapping(uno_Environment * pFrom,
      62             :                     uno_Environment * pInterm,
      63             :                     uno_Environment * pTo);
      64             :     ~MediatorMapping();
      65             : };
      66             : 
      67             : extern "C" {
      68          16 : static void SAL_CALL s_acquire(uno_Mapping * mapping)
      69             : {
      70          16 :     MediatorMapping * pMediatorMapping = static_cast<MediatorMapping *>(mapping);
      71          16 :     pMediatorMapping->acquire();
      72          16 : }
      73             : 
      74          16 : static void SAL_CALL s_release(uno_Mapping * mapping)
      75             : {
      76          16 :     MediatorMapping * pMediatorMapping = static_cast<MediatorMapping *>(mapping);
      77          16 :     pMediatorMapping->release();
      78          16 : }
      79             : 
      80          16 : static void SAL_CALL s_mapInterface(
      81             :     uno_Mapping                      * mapping,
      82             :     void                            ** ppOut,
      83             :     void                             * pInterface,
      84             :     typelib_InterfaceTypeDescription * pInterfaceTypeDescr)
      85             : {
      86          16 :     MediatorMapping   * pMediatorMapping  = static_cast<MediatorMapping *>(mapping);
      87          16 :     pMediatorMapping->mapInterface(ppOut, pInterface, pInterfaceTypeDescr);
      88          16 : }
      89             : }
      90             : 
      91          16 : MediatorMapping::MediatorMapping(uno_Environment * pFrom,
      92             :                                  uno_Environment * pInterm,
      93             :                                  uno_Environment * pTo)
      94             :     : m_refCount(0),
      95             :       m_from2uno(pFrom, pInterm),
      96             :       m_uno2to  (pInterm, pTo),
      97             :       m_from    (pFrom),
      98             :       m_interm  (pInterm),
      99          16 :       m_to      (pTo)
     100             : {
     101             :     LOG_LIFECYLE_MediatorMapping_emit(std::cerr << __FUNCTION__ << std::endl);
     102             : 
     103          16 :     if (!m_from2uno.get() || !m_uno2to.get())
     104           0 :         abort();
     105             : 
     106          16 :     uno_Mapping::acquire      = s_acquire;
     107          16 :     uno_Mapping::release      = s_release;
     108          16 :     uno_Mapping::mapInterface = s_mapInterface;
     109          16 : }
     110             : 
     111          16 : MediatorMapping::~MediatorMapping()
     112             : {
     113             :     LOG_LIFECYLE_MediatorMapping_emit(std::cerr << __FUNCTION__ << std::endl);
     114          16 : }
     115             : 
     116          16 : void MediatorMapping::acquire(void)
     117             : {
     118             :     LOG_LIFECYLE_MediatorMapping_emit(std::cerr << __FUNCTION__ << std::endl);
     119             : 
     120          16 :     osl_atomic_increment(&m_refCount);
     121          16 : }
     122             : 
     123          16 : void MediatorMapping::release(void)
     124             : {
     125             :     LOG_LIFECYLE_MediatorMapping_emit(std::cerr << __FUNCTION__ << std::endl);
     126             : 
     127          16 :     if (osl_atomic_decrement(&m_refCount) == 0)
     128             :     {
     129          16 :         ::uno_revokeMapping(this);
     130             :     }
     131          16 : }
     132             : 
     133          16 : extern "C" { static void s_mapInterface_v(va_list * pParam)
     134             : {
     135          16 :     void                            ** ppOut               = va_arg(*pParam, void **);
     136          16 :     void                             * pInterface          = va_arg(*pParam, void *);
     137          16 :     typelib_InterfaceTypeDescription * pInterfaceTypeDescr = va_arg(*pParam, typelib_InterfaceTypeDescription *);
     138          16 :     uno_Mapping                      * pMapping            = va_arg(*pParam, uno_Mapping *);
     139             : 
     140          16 :     pMapping->mapInterface(pMapping, ppOut, pInterface, pInterfaceTypeDescr);
     141          16 : }}
     142             : 
     143          16 : void MediatorMapping::mapInterface(
     144             :     void                            ** ppOut,
     145             :     void                             * pInterface,
     146             :     typelib_InterfaceTypeDescription * pInterfaceTypeDescr)
     147             : {
     148          16 :     if (*ppOut != 0)
     149             :     {
     150           0 :         uno_ExtEnvironment * env = m_to.get()->pExtEnv;
     151             :         OSL_ASSERT( env != 0 );
     152           0 :         env->releaseInterface( env, *ppOut );
     153           0 :         *ppOut = NULL;
     154             :     }
     155             : 
     156          16 :     void * ret = 0;
     157          16 :     uno_Interface * pUnoI = 0;
     158             : 
     159          16 :     m_from.invoke(s_mapInterface_v, &pUnoI, pInterface, pInterfaceTypeDescr, m_from2uno.get());
     160             : 
     161          16 :     m_uno2to.mapInterface(&ret, pUnoI, pInterfaceTypeDescr);
     162             : 
     163          16 :     if (pUnoI)
     164          16 :         m_interm.get()->pExtEnv->releaseInterface(m_interm.get()->pExtEnv, pUnoI);
     165             : 
     166          16 :     *ppOut = ret;
     167          16 : }
     168             : 
     169          16 : extern "C" { static void SAL_CALL s_MediatorMapping_free(uno_Mapping * pMapping)
     170             :     SAL_THROW_EXTERN_C()
     171             : {
     172          16 :     delete static_cast<MediatorMapping *>(pMapping);
     173          16 : }}
     174             : 
     175             : 
     176             : 
     177          16 : static rtl::OUString getPrefix(rtl::OUString const & str1, rtl::OUString const & str2)
     178             : {
     179          16 :     sal_Int32 nIndex1 = 0;
     180          16 :     sal_Int32 nIndex2 = 0;
     181          16 :     sal_Int32 sim = 0;
     182             : 
     183          16 :     rtl::OUString token1;
     184          32 :     rtl::OUString token2;
     185             : 
     186          16 :     do
     187             :     {
     188          16 :         token1 = str1.getToken(0, ':', nIndex1);
     189          16 :         token2 = str2.getToken(0, ':', nIndex2);
     190             : 
     191          16 :         if (token1.equals(token2))
     192          16 :             sim += token1.getLength() + 1;
     193             :     }
     194          16 :     while(nIndex1 == nIndex2 && nIndex1 >= 0 && token1.equals(token2));
     195             : 
     196          16 :     rtl::OUString result;
     197             : 
     198          16 :     if (sim)
     199          16 :         result = str1.copy(0, sim - 1);
     200             : 
     201          32 :     return result;
     202             : }
     203             : 
     204             : //  rtl::OUString str1("abc:def:ghi");
     205             : //  rtl::OUString str2("abc:def");
     206             : //  rtl::OUString str3("abc");
     207             : //  rtl::OUString str4("");
     208             : 
     209             : //  rtl::OUString pref;
     210             : 
     211             : //  pref = getPrefix(str1, str1);
     212             : //  pref = getPrefix(str1, str2);
     213             : //  pref = getPrefix(str1, str3);
     214             : //  pref = getPrefix(str1, str4);
     215             : 
     216             : //  pref = getPrefix(str2, str1);
     217             : //  pref = getPrefix(str3, str1);
     218             : //  pref = getPrefix(str4, str1);
     219             : 
     220             : 
     221       16851 : void getCascadeMapping(uno_Mapping     ** ppMapping,
     222             :                        uno_Environment  * pFrom,
     223             :                        uno_Environment  * pTo,
     224             :                        rtl_uString      * pAddPurpose)
     225             : {
     226       16851 :     if (pAddPurpose && pAddPurpose->length)
     227       16843 :         return;
     228             : 
     229       16843 :     rtl::OUString uno_envType(UNO_LB_UNO);
     230             : 
     231       16859 :     rtl::OUString from_envType    = cppu::EnvDcp::getTypeName(pFrom->pTypeName);
     232       16859 :     rtl::OUString to_envType      = cppu::EnvDcp::getTypeName(pTo->pTypeName);
     233       16859 :     rtl::OUString from_envPurpose = cppu::EnvDcp::getPurpose(pFrom->pTypeName);
     234       16859 :     rtl::OUString to_envPurpose   = cppu::EnvDcp::getPurpose(pTo->pTypeName);
     235             : 
     236             : #ifdef LOG_CALLING_named_purpose_getMapping
     237             :     rtl::OString s_from_name = rtl::OUStringToOString(pFrom->pTypeName, RTL_TEXTENCODING_ASCII_US);
     238             :     rtl::OString s_to_name   = rtl::OUStringToOString(pTo->pTypeName,   RTL_TEXTENCODING_ASCII_US);
     239             : 
     240             :     std::cerr << __FUNCTION__ << " - creating mediation ";
     241             :     std::cerr << "pFrom: " << s_from_name.getStr();
     242             :     std::cerr <<" pTo: "   << s_to_name.getStr() << std::endl;
     243             : #endif
     244             : 
     245       16843 :     if (from_envPurpose == to_envPurpose) // gcc:bla => uno:bla
     246       16819 :         return;
     247             : 
     248             :     // reaching this point means, we need a mediated mapping!!!
     249             :     // we generall mediate via uno[:free]
     250          24 :     uno_Environment * pInterm = NULL;
     251             : 
     252             :     // chained uno -> uno
     253          24 :     if (from_envType == uno_envType && to_envType == uno_envType)
     254             :     {
     255           8 :         rtl::OUString purpose = getPrefix(from_envPurpose, to_envPurpose);
     256             : 
     257           8 :         rtl::OUString uno_envDcp = uno_envType;
     258           8 :         uno_envDcp += purpose;
     259             : 
     260             :         // direct mapping possible?
     261             :         // uno:bla-->uno:bla:blubb
     262           8 :         if (from_envPurpose.equals(purpose))
     263             :         {
     264           4 :             rtl::OUString rest = to_envPurpose.copy(purpose.getLength());
     265             : 
     266           4 :             sal_Int32 index = rest.indexOf(':', 1);
     267           4 :             if (index == -1)
     268             :             {
     269           4 :                 uno_getMapping(ppMapping, pFrom, pTo, rest.copy(1).pData);
     270           4 :                 return;
     271             :             }
     272             : 
     273           0 :             uno_envDcp += rest.copy(0, index);
     274             :         }
     275           4 :         else if (to_envPurpose.equals(purpose))
     276             :         {
     277           4 :             rtl::OUString rest = from_envPurpose.copy(purpose.getLength());
     278             : 
     279           4 :             sal_Int32 index = rest.indexOf(':', 1);
     280           4 :             if (index == -1)
     281             :             {
     282           4 :                 uno_getMapping(ppMapping, pFrom, pTo, rest.copy(1).pData);
     283           4 :                 return;
     284             :             }
     285             : 
     286           0 :             uno_envDcp += rest.copy(0, index);
     287             :         }
     288             : 
     289           0 :         uno_getEnvironment(&pInterm, uno_envDcp.pData, NULL);
     290             :     }
     291          16 :     else if (from_envType != uno_envType && to_envType == uno_envType) // <ANY> -> UNO ?
     292             :         // mediate via uno:purpose(fromEnv)
     293             :     {
     294           4 :         rtl::OUString     envDcp = uno_envType;
     295             : 
     296           4 :         envDcp += from_envPurpose;
     297           4 :          uno_getEnvironment(&pInterm, envDcp.pData, NULL);
     298             :     }
     299          12 :     else if (from_envType == uno_envType && to_envType != uno_envType) // UNO -> <ANY>?
     300             :         // mediate via uno(context)
     301             :     {
     302           4 :         rtl::OUString     envDcp = uno_envType;
     303             : 
     304           4 :          envDcp += to_envPurpose;
     305           4 :         uno_getEnvironment(&pInterm, envDcp.pData, NULL);
     306             :     }
     307             :     else // everything else
     308             :         // mediate via uno:purpose
     309             :     {
     310           8 :         rtl::OUString purpose = getPrefix(from_envPurpose, to_envPurpose);
     311             : 
     312          16 :         rtl::OUString uno_envDcp = uno_envType;
     313           8 :         uno_envDcp += purpose;
     314             : 
     315          16 :         uno_getEnvironment(&pInterm, uno_envDcp.pData, NULL);
     316             :     }
     317             : 
     318          16 :     uno_Mapping * pMapping = new MediatorMapping(pFrom, pInterm, pTo);
     319          16 :     pInterm->release(pInterm);
     320             : 
     321             : 
     322          16 :     pMapping->acquire(pMapping);
     323             : 
     324          16 :     ::uno_registerMapping(&pMapping, s_MediatorMapping_free, pFrom, pTo, pAddPurpose);
     325             : 
     326          16 :     if (*ppMapping)
     327           0 :         (*ppMapping)->release(*ppMapping);
     328             : 
     329          32 :     *ppMapping = pMapping;
     330             : }
     331             : 
     332             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10