LCOV - code coverage report
Current view: top level - cppu/source/uno - cascade_mapping.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 109 117 93.2 %
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 "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();
      56             :     void release();
      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          24 : static void SAL_CALL s_acquire(uno_Mapping * mapping)
      69             : {
      70          24 :     MediatorMapping * pMediatorMapping = static_cast<MediatorMapping *>(mapping);
      71          24 :     pMediatorMapping->acquire();
      72          24 : }
      73             : 
      74          24 : static void SAL_CALL s_release(uno_Mapping * mapping)
      75             : {
      76          24 :     MediatorMapping * pMediatorMapping = static_cast<MediatorMapping *>(mapping);
      77          24 :     pMediatorMapping->release();
      78          24 : }
      79             : 
      80          24 : static void SAL_CALL s_mapInterface(
      81             :     uno_Mapping                      * mapping,
      82             :     void                            ** ppOut,
      83             :     void                             * pInterface,
      84             :     typelib_InterfaceTypeDescription * pInterfaceTypeDescr)
      85             : {
      86          24 :     MediatorMapping   * pMediatorMapping  = static_cast<MediatorMapping *>(mapping);
      87          24 :     pMediatorMapping->mapInterface(ppOut, pInterface, pInterfaceTypeDescr);
      88          24 : }
      89             : }
      90             : 
      91          24 : 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          24 :       m_to      (pTo)
     100             : {
     101             :     LOG_LIFECYLE_MediatorMapping_emit(std::cerr << __FUNCTION__ << std::endl);
     102             : 
     103          24 :     if (!m_from2uno.get() || !m_uno2to.get())
     104           0 :         abort();
     105             : 
     106          24 :     uno_Mapping::acquire      = s_acquire;
     107          24 :     uno_Mapping::release      = s_release;
     108          24 :     uno_Mapping::mapInterface = s_mapInterface;
     109          24 : }
     110             : 
     111          24 : MediatorMapping::~MediatorMapping()
     112             : {
     113             :     LOG_LIFECYLE_MediatorMapping_emit(std::cerr << __FUNCTION__ << std::endl);
     114          24 : }
     115             : 
     116          24 : void MediatorMapping::acquire()
     117             : {
     118             :     LOG_LIFECYLE_MediatorMapping_emit(std::cerr << __FUNCTION__ << std::endl);
     119             : 
     120          24 :     osl_atomic_increment(&m_refCount);
     121          24 : }
     122             : 
     123          24 : void MediatorMapping::release()
     124             : {
     125             :     LOG_LIFECYLE_MediatorMapping_emit(std::cerr << __FUNCTION__ << std::endl);
     126             : 
     127          24 :     if (osl_atomic_decrement(&m_refCount) == 0)
     128             :     {
     129          24 :         ::uno_revokeMapping(this);
     130             :     }
     131          24 : }
     132             : 
     133          24 : extern "C" { static void s_mapInterface_v(va_list * pParam)
     134             : {
     135          24 :     void                            ** ppOut               = va_arg(*pParam, void **);
     136          24 :     void                             * pInterface          = va_arg(*pParam, void *);
     137          24 :     typelib_InterfaceTypeDescription * pInterfaceTypeDescr = va_arg(*pParam, typelib_InterfaceTypeDescription *);
     138          24 :     uno_Mapping                      * pMapping            = va_arg(*pParam, uno_Mapping *);
     139             : 
     140          24 :     pMapping->mapInterface(pMapping, ppOut, pInterface, pInterfaceTypeDescr);
     141          24 : }}
     142             : 
     143          24 : void MediatorMapping::mapInterface(
     144             :     void                            ** ppOut,
     145             :     void                             * pInterface,
     146             :     typelib_InterfaceTypeDescription * pInterfaceTypeDescr)
     147             : {
     148          24 :     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          24 :     void * ret = 0;
     157          24 :     uno_Interface * pUnoI = 0;
     158             : 
     159          24 :     m_from.invoke(s_mapInterface_v, &pUnoI, pInterface, pInterfaceTypeDescr, m_from2uno.get());
     160             : 
     161          24 :     m_uno2to.mapInterface(&ret, pUnoI, pInterfaceTypeDescr);
     162             : 
     163          24 :     if (pUnoI)
     164          24 :         m_interm.get()->pExtEnv->releaseInterface(m_interm.get()->pExtEnv, pUnoI);
     165             : 
     166          24 :     *ppOut = ret;
     167          24 : }
     168             : 
     169          24 : extern "C" { static void SAL_CALL s_MediatorMapping_free(uno_Mapping * pMapping)
     170             :     SAL_THROW_EXTERN_C()
     171             : {
     172          24 :     delete static_cast<MediatorMapping *>(pMapping);
     173          24 : }}
     174             : 
     175             : 
     176             : 
     177          22 : static rtl::OUString getPrefix(rtl::OUString const & str1, rtl::OUString const & str2)
     178             : {
     179          22 :     sal_Int32 nIndex1 = 0;
     180          22 :     sal_Int32 nIndex2 = 0;
     181          22 :     sal_Int32 sim = 0;
     182             : 
     183          22 :     rtl::OUString token1;
     184          44 :     rtl::OUString token2;
     185             : 
     186          22 :     do
     187             :     {
     188          22 :         token1 = str1.getToken(0, ':', nIndex1);
     189          22 :         token2 = str2.getToken(0, ':', nIndex2);
     190             : 
     191          22 :         if (token1.equals(token2))
     192          22 :             sim += token1.getLength() + 1;
     193             :     }
     194          22 :     while(nIndex1 == nIndex2 && nIndex1 >= 0 && token1.equals(token2));
     195             : 
     196          22 :     rtl::OUString result;
     197             : 
     198          22 :     if (sim)
     199          22 :         result = str1.copy(0, sim - 1);
     200             : 
     201          44 :     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       11735 : void getCascadeMapping(uno_Mapping     ** ppMapping,
     222             :                        uno_Environment  * pFrom,
     223             :                        uno_Environment  * pTo,
     224             :                        rtl_uString      * pAddPurpose)
     225             : {
     226       11735 :     if (pAddPurpose && pAddPurpose->length)
     227       11721 :         return;
     228             : 
     229       11725 :     rtl::OUString uno_envType(UNO_LB_UNO);
     230             : 
     231       11749 :     rtl::OUString from_envType    = cppu::EnvDcp::getTypeName(pFrom->pTypeName);
     232       11749 :     rtl::OUString to_envType      = cppu::EnvDcp::getTypeName(pTo->pTypeName);
     233       11749 :     rtl::OUString from_envPurpose = cppu::EnvDcp::getPurpose(pFrom->pTypeName);
     234       11749 :     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       11725 :     if (from_envPurpose == to_envPurpose) // gcc:bla => uno:bla
     246       11691 :         return;
     247             : 
     248             :     // reaching this point means, we need a mediated mapping!!!
     249             :     // we generall mediate via uno[:free]
     250          34 :     uno_Environment * pInterm = NULL;
     251             : 
     252             :     // chained uno -> uno
     253          34 :     if (from_envType == uno_envType && to_envType == uno_envType)
     254             :     {
     255          10 :         rtl::OUString purpose = getPrefix(from_envPurpose, to_envPurpose);
     256             : 
     257          10 :         rtl::OUString uno_envDcp = uno_envType;
     258          10 :         uno_envDcp += purpose;
     259             : 
     260             :         // direct mapping possible?
     261             :         // uno:bla-->uno:bla:blubb
     262          10 :         if (from_envPurpose.equals(purpose))
     263             :         {
     264           5 :             rtl::OUString rest = to_envPurpose.copy(purpose.getLength());
     265             : 
     266           5 :             sal_Int32 index = rest.indexOf(':', 1);
     267           5 :             if (index == -1)
     268             :             {
     269           5 :                 uno_getMapping(ppMapping, pFrom, pTo, rest.copy(1).pData);
     270           5 :                 return;
     271             :             }
     272             : 
     273           0 :             uno_envDcp += rest.copy(0, index);
     274             :         }
     275           5 :         else if (to_envPurpose.equals(purpose))
     276             :         {
     277           5 :             rtl::OUString rest = from_envPurpose.copy(purpose.getLength());
     278             : 
     279           5 :             sal_Int32 index = rest.indexOf(':', 1);
     280           5 :             if (index == -1)
     281             :             {
     282           5 :                 uno_getMapping(ppMapping, pFrom, pTo, rest.copy(1).pData);
     283           5 :                 return;
     284             :             }
     285             : 
     286           0 :             uno_envDcp += rest.copy(0, index);
     287             :         }
     288             : 
     289           0 :         uno_getEnvironment(&pInterm, uno_envDcp.pData, NULL);
     290             :     }
     291          24 :     else if (from_envType != uno_envType && to_envType == uno_envType) // <ANY> -> UNO ?
     292             :         // mediate via uno:purpose(fromEnv)
     293             :     {
     294           6 :         rtl::OUString     envDcp = uno_envType;
     295             : 
     296           6 :         envDcp += from_envPurpose;
     297           6 :          uno_getEnvironment(&pInterm, envDcp.pData, NULL);
     298             :     }
     299          18 :     else if (from_envType == uno_envType && to_envType != uno_envType) // UNO -> <ANY>?
     300             :         // mediate via uno(context)
     301             :     {
     302           6 :         rtl::OUString     envDcp = uno_envType;
     303             : 
     304           6 :          envDcp += to_envPurpose;
     305           6 :         uno_getEnvironment(&pInterm, envDcp.pData, NULL);
     306             :     }
     307             :     else // everything else
     308             :         // mediate via uno:purpose
     309             :     {
     310          12 :         rtl::OUString purpose = getPrefix(from_envPurpose, to_envPurpose);
     311             : 
     312          24 :         rtl::OUString uno_envDcp = uno_envType;
     313          12 :         uno_envDcp += purpose;
     314             : 
     315          24 :         uno_getEnvironment(&pInterm, uno_envDcp.pData, NULL);
     316             :     }
     317             : 
     318          24 :     uno_Mapping * pMapping = new MediatorMapping(pFrom, pInterm, pTo);
     319          24 :     pInterm->release(pInterm);
     320             : 
     321             : 
     322          24 :     pMapping->acquire(pMapping);
     323             : 
     324          24 :     ::uno_registerMapping(&pMapping, s_MediatorMapping_free, pFrom, pTo, pAddPurpose);
     325             : 
     326          24 :     if (*ppMapping)
     327           0 :         (*ppMapping)->release(*ppMapping);
     328             : 
     329          48 :     *ppMapping = pMapping;
     330             : }
     331             : 
     332             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11