LCOV - code coverage report
Current view: top level - stoc/source/corereflection - crefl.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 114 141 80.9 %
Date: 2014-11-03 Functions: 20 26 76.9 %
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 <cppuhelper/queryinterface.hxx>
      21             : #include <cppuhelper/implementationentry.hxx>
      22             : #include <cppuhelper/supportsservice.hxx>
      23             : 
      24             : #include <com/sun/star/lang/XComponent.hpp>
      25             : #include <com/sun/star/reflection/XConstantTypeDescription.hpp>
      26             : #include <com/sun/star/reflection/XTypeDescription.hpp>
      27             : #include <com/sun/star/uno/RuntimeException.hpp>
      28             : #include <uno/lbnames.h>
      29             : 
      30             : using namespace css;
      31             : using namespace css::uno;
      32             : using namespace css::lang;
      33             : using namespace css::reflection;
      34             : using namespace css::registry;
      35             : using namespace cppu;
      36             : using namespace osl;
      37             : 
      38             : 
      39             : #include "base.hxx"
      40             : 
      41             : 
      42             : namespace stoc_corefl
      43             : {
      44             : 
      45             : static const sal_Int32 CACHE_SIZE = 256;
      46             : 
      47             : #define SERVICENAME "com.sun.star.reflection.CoreReflection"
      48             : #define IMPLNAME    "com.sun.star.comp.stoc.CoreReflection"
      49             : 
      50         194 : static Sequence< OUString > core_getSupportedServiceNames()
      51             : {
      52         194 :     Sequence< OUString > seqNames(1);
      53         194 :     seqNames.getArray()[0] = OUString( SERVICENAME );
      54         194 :     return seqNames;
      55             : }
      56             : 
      57         194 : static OUString core_getImplementationName()
      58             : {
      59         194 :     return OUString(IMPLNAME);
      60             : }
      61             : 
      62         194 : IdlReflectionServiceImpl::IdlReflectionServiceImpl(
      63             :     const Reference< XComponentContext > & xContext )
      64             :     : OComponentHelper( _aComponentMutex )
      65         194 :     , _xMgr( xContext->getServiceManager(), UNO_QUERY )
      66         388 :     , _aElements( CACHE_SIZE )
      67             : {
      68         194 :     xContext->getValueByName( OUString(
      69         388 :         "/singletons/com.sun.star.reflection.theTypeDescriptionManager") ) >>= _xTDMgr;
      70             :     OSL_ENSURE( _xTDMgr.is(), "### cannot get singleton \"TypeDescriptionManager\" from context!" );
      71         194 : }
      72             : 
      73         270 : IdlReflectionServiceImpl::~IdlReflectionServiceImpl() {}
      74             : 
      75             : // XInterface
      76             : 
      77       40666 : Any IdlReflectionServiceImpl::queryInterface( const Type & rType )
      78             :     throw(css::uno::RuntimeException, std::exception)
      79             : {
      80             :     Any aRet( ::cppu::queryInterface(
      81             :         rType,
      82             :         static_cast< XIdlReflection * >( this ),
      83             :         static_cast< XHierarchicalNameAccess * >( this ),
      84       40666 :         static_cast< XServiceInfo * >( this ) ) );
      85             : 
      86       40666 :     return (aRet.hasValue() ? aRet : OComponentHelper::queryInterface( rType ));
      87             : }
      88             : 
      89      351914 : void IdlReflectionServiceImpl::acquire() throw()
      90             : {
      91      351914 :     OComponentHelper::acquire();
      92      351914 : }
      93             : 
      94      342363 : void IdlReflectionServiceImpl::release() throw()
      95             : {
      96      342363 :     OComponentHelper::release();
      97      342363 : }
      98             : 
      99             : // XTypeProvider
     100             : 
     101           0 : Sequence< Type > IdlReflectionServiceImpl::getTypes()
     102             :     throw (css::uno::RuntimeException, std::exception)
     103             : {
     104             :     static OTypeCollection * s_pTypes = 0;
     105           0 :     if (! s_pTypes)
     106             :     {
     107           0 :         MutexGuard aGuard( _aComponentMutex );
     108           0 :         if (! s_pTypes)
     109             :         {
     110             :             static OTypeCollection s_aTypes(
     111           0 :                 cppu::UnoType<XIdlReflection>::get(),
     112           0 :                 cppu::UnoType<XHierarchicalNameAccess>::get(),
     113           0 :                 cppu::UnoType<XServiceInfo>::get(),
     114           0 :                 OComponentHelper::getTypes() );
     115           0 :             s_pTypes = &s_aTypes;
     116           0 :         }
     117             :     }
     118           0 :     return s_pTypes->getTypes();
     119             : }
     120             : 
     121           0 : Sequence< sal_Int8 > IdlReflectionServiceImpl::getImplementationId()
     122             :     throw (css::uno::RuntimeException, std::exception)
     123             : {
     124           0 :     return css::uno::Sequence<sal_Int8>();
     125             : }
     126             : 
     127             : // XComponent
     128             : 
     129         190 : void IdlReflectionServiceImpl::dispose()
     130             :     throw(css::uno::RuntimeException, std::exception)
     131             : {
     132         190 :     OComponentHelper::dispose();
     133             : 
     134         190 :     MutexGuard aGuard( _aComponentMutex );
     135         190 :     _aElements.clear();
     136             : #ifdef TEST_LIST_CLASSES
     137             :     OSL_ENSURE( g_aClassNames.empty(), "### idl classes still alive!" );
     138             :     ClassNameList::const_iterator iPos( g_aClassNames.begin() );
     139             :     while (iPos != g_aClassNames.end())
     140             :     {
     141             :         OUString aName( *iPos );
     142             :         ++iPos;
     143             :     }
     144             : #endif
     145         190 : }
     146             : 
     147             : // XServiceInfo
     148             : 
     149           0 : OUString IdlReflectionServiceImpl::getImplementationName()
     150             :     throw(css::uno::RuntimeException, std::exception)
     151             : {
     152           0 :     return core_getImplementationName();
     153             : }
     154             : 
     155           0 : sal_Bool IdlReflectionServiceImpl::supportsService( const OUString & rServiceName )
     156             :     throw(css::uno::RuntimeException, std::exception)
     157             : {
     158           0 :     return cppu::supportsService(this, rServiceName);
     159             : }
     160             : 
     161           0 : Sequence< OUString > IdlReflectionServiceImpl::getSupportedServiceNames()
     162             :     throw(css::uno::RuntimeException, std::exception)
     163             : {
     164           0 :     return core_getSupportedServiceNames();
     165             : }
     166             : 
     167             : // XIdlReflection
     168             : 
     169           0 : Reference< XIdlClass > IdlReflectionServiceImpl::getType( const Any & rObj )
     170             :     throw(css::uno::RuntimeException, std::exception)
     171             : {
     172           0 :     return (rObj.hasValue() ? forType( rObj.getValueTypeRef() ) : Reference< XIdlClass >());
     173             : }
     174             : 
     175             : 
     176       20928 : inline Reference< XIdlClass > IdlReflectionServiceImpl::constructClass(
     177             :     typelib_TypeDescription * pTypeDescr )
     178             : {
     179             :     OSL_ENSURE( pTypeDescr->eTypeClass != typelib_TypeClass_TYPEDEF, "### unexpected typedef!" );
     180             : 
     181       20928 :     switch (pTypeDescr->eTypeClass)
     182             :     {
     183             :     case typelib_TypeClass_VOID:
     184             :     case typelib_TypeClass_CHAR:
     185             :     case typelib_TypeClass_BOOLEAN:
     186             :     case typelib_TypeClass_BYTE:
     187             :     case typelib_TypeClass_SHORT:
     188             :     case typelib_TypeClass_UNSIGNED_SHORT:
     189             :     case typelib_TypeClass_LONG:
     190             :     case typelib_TypeClass_UNSIGNED_LONG:
     191             :     case typelib_TypeClass_HYPER:
     192             :     case typelib_TypeClass_UNSIGNED_HYPER:
     193             :     case typelib_TypeClass_FLOAT:
     194             :     case typelib_TypeClass_DOUBLE:
     195             :     case typelib_TypeClass_STRING:
     196             :     case typelib_TypeClass_ANY:
     197        1148 :         return new IdlClassImpl( this, pTypeDescr->pTypeName, pTypeDescr->eTypeClass, pTypeDescr );
     198             : 
     199             :     case TypeClass_ENUM:
     200          22 :         return new EnumIdlClassImpl( this, pTypeDescr->pTypeName, pTypeDescr->eTypeClass, pTypeDescr );
     201             : 
     202             :     case typelib_TypeClass_STRUCT:
     203             :     case typelib_TypeClass_EXCEPTION:
     204         558 :         return new CompoundIdlClassImpl( this, pTypeDescr->pTypeName, pTypeDescr->eTypeClass, pTypeDescr );
     205             : 
     206             :     case typelib_TypeClass_SEQUENCE:
     207        1314 :         return new ArrayIdlClassImpl( this, pTypeDescr->pTypeName, pTypeDescr->eTypeClass, pTypeDescr );
     208             : 
     209             :     case typelib_TypeClass_INTERFACE:
     210       17820 :         return new InterfaceIdlClassImpl( this, pTypeDescr->pTypeName, pTypeDescr->eTypeClass, pTypeDescr );
     211             : 
     212             :     case typelib_TypeClass_TYPE:
     213          66 :         return new IdlClassImpl( this, pTypeDescr->pTypeName, pTypeDescr->eTypeClass, pTypeDescr );
     214             : 
     215             :     default:
     216             : #if OSL_DEBUG_LEVEL > 1
     217             :         OSL_TRACE( "### corereflection type unsupported: " );
     218             :         OString aName( OUStringToOString( pTypeDescr->pTypeName, RTL_TEXTENCODING_ASCII_US ) );
     219             :         OSL_TRACE( "%s", aName.getStr() );
     220             :         OSL_TRACE( "\n" );
     221             : #endif
     222           0 :         return Reference< XIdlClass >();
     223             :     }
     224             : }
     225             : 
     226     1013768 : Reference< XIdlClass > IdlReflectionServiceImpl::forName( const OUString & rTypeName )
     227             :     throw(css::uno::RuntimeException, std::exception)
     228             : {
     229     1013768 :     Reference< XIdlClass > xRet;
     230     2027536 :     Any aAny( _aElements.getValue( rTypeName ) );
     231             : 
     232     1013768 :     if (aAny.hasValue())
     233             :     {
     234     1001114 :         if (aAny.getValueTypeClass() == TypeClass_INTERFACE)
     235     1001114 :             xRet = *(const Reference< XIdlClass > *)aAny.getValue();
     236             :     }
     237             :     else
     238             :     {
     239             :         // try to get _type_ by name
     240       12654 :         typelib_TypeDescription * pTD = 0;
     241       12654 :         typelib_typedescription_getByName( &pTD, rTypeName.pData );
     242       12654 :         if (pTD)
     243             :         {
     244       12654 :             if ((xRet = constructClass( pTD )).is())
     245       12654 :                 _aElements.setValue( rTypeName, makeAny( xRet ) ); // update
     246       12654 :             typelib_typedescription_release( pTD );
     247             :         }
     248             :     }
     249             : 
     250     2027536 :     return xRet;
     251             : }
     252             : 
     253             : // XHierarchicalNameAccess
     254             : 
     255         132 : Any IdlReflectionServiceImpl::getByHierarchicalName( const OUString & rName )
     256             :     throw(css::container::NoSuchElementException, css::uno::RuntimeException, std::exception)
     257             : {
     258         132 :     Any aRet( _aElements.getValue( rName ) );
     259         132 :     if (! aRet.hasValue())
     260             :     {
     261          32 :         aRet = _xTDMgr->getByHierarchicalName( rName );
     262          32 :         if (aRet.getValueTypeClass() == TypeClass_INTERFACE)
     263             :         {
     264             :             // type retrieved from tdmgr
     265             :             OSL_ASSERT( (*(Reference< XInterface > *)aRet.getValue())->queryInterface(
     266             :                 cppu::UnoType<XTypeDescription>::get()).hasValue() );
     267             : 
     268             :             css::uno::Reference< css::reflection::XConstantTypeDescription >
     269          32 :                 ctd;
     270          32 :             if (aRet >>= ctd)
     271             :             {
     272           2 :                 aRet = ctd->getConstantValue();
     273             :             }
     274             :             else
     275             :             {
     276             :                 // if you are interested in a type then CALL forName()!!!
     277             :                 // this way is NOT recommended for types, because this method looks for constants first
     278             : 
     279             :                 // if td manager found some type, it will be in the cache (hopefully.. we just got it)
     280             :                 // so the second retrieving via c typelib callback chain should succeed...
     281             : 
     282             :                 // try to get _type_ by name
     283          30 :                 typelib_TypeDescription * pTD = 0;
     284          30 :                 typelib_typedescription_getByName( &pTD, rName.pData );
     285             : 
     286          30 :                 aRet.clear(); // kick XTypeDescription interface
     287             : 
     288          30 :                 if (pTD)
     289             :                 {
     290          16 :                     Reference< XIdlClass > xIdlClass( constructClass( pTD ) );
     291          16 :                     aRet.setValue( &xIdlClass, cppu::UnoType<XIdlClass>::get());
     292          16 :                     typelib_typedescription_release( pTD );
     293             :                 }
     294          32 :             }
     295             :         }
     296             :         // else is enum member(?)
     297             : 
     298             :         // update
     299          32 :         if (aRet.hasValue())
     300          18 :             _aElements.setValue( rName, aRet );
     301             :         else
     302             :         {
     303          14 :             throw container::NoSuchElementException( rName );
     304             :         }
     305             :     }
     306         118 :     return aRet;
     307             : }
     308             : 
     309         114 : sal_Bool IdlReflectionServiceImpl::hasByHierarchicalName( const OUString & rName )
     310             :     throw(css::uno::RuntimeException, std::exception)
     311             : {
     312             :     try
     313             :     {
     314         114 :         return getByHierarchicalName( rName ).hasValue();
     315             :     }
     316           0 :     catch (container::NoSuchElementException &)
     317             :     {
     318             :     }
     319           0 :     return sal_False;
     320             : }
     321             : 
     322             : 
     323       80588 : Reference< XIdlClass > IdlReflectionServiceImpl::forType( typelib_TypeDescription * pTypeDescr )
     324             :     throw(css::uno::RuntimeException)
     325             : {
     326       80588 :     Reference< XIdlClass > xRet;
     327      161176 :     OUString aName( pTypeDescr->pTypeName );
     328      161176 :     Any aAny( _aElements.getValue( aName ) );
     329             : 
     330       80588 :     if (aAny.hasValue())
     331             :     {
     332       72330 :         if (aAny.getValueTypeClass() == TypeClass_INTERFACE)
     333       72330 :             xRet = *(const Reference< XIdlClass > *)aAny.getValue();
     334             :     }
     335             :     else
     336             :     {
     337        8258 :         if ((xRet = constructClass( pTypeDescr )).is())
     338        8258 :             _aElements.setValue( aName, makeAny( xRet ) ); // update
     339             :     }
     340             : 
     341      161176 :     return xRet;
     342             : }
     343             : 
     344       65542 : Reference< XIdlClass > IdlReflectionServiceImpl::forType( typelib_TypeDescriptionReference * pRef )
     345             :     throw(css::uno::RuntimeException)
     346             : {
     347       65542 :     typelib_TypeDescription * pTD = 0;
     348       65542 :     TYPELIB_DANGER_GET( &pTD, pRef );
     349       65542 :     if (pTD)
     350             :     {
     351       65542 :         Reference< XIdlClass > xRet = forType( pTD );
     352       65542 :         TYPELIB_DANGER_RELEASE( pTD );
     353      131084 :         return xRet;
     354             :     }
     355             :     throw RuntimeException(
     356             :         "IdlReflectionServiceImpl::forType() failed!",
     357           0 :         (XWeak *)(OWeakObject *)this );
     358             : }
     359             : 
     360             : 
     361       25090 : const Mapping & IdlReflectionServiceImpl::getCpp2Uno()
     362             :     throw(css::uno::RuntimeException)
     363             : {
     364       25090 :     if (! _aCpp2Uno.is())
     365             :     {
     366          58 :         MutexGuard aGuard( getMutexAccess() );
     367          58 :         if (! _aCpp2Uno.is())
     368             :         {
     369         116 :             _aCpp2Uno = Mapping(
     370             :                 OUString( CPPU_CURRENT_LANGUAGE_BINDING_NAME ),
     371          58 :                 OUString( UNO_LB_UNO ) );
     372             :             OSL_ENSURE( _aCpp2Uno.is(), "### cannot get c++ to uno mapping!" );
     373          58 :             if (! _aCpp2Uno.is())
     374             :             {
     375             :                 throw RuntimeException(
     376             :                     "cannot get c++ to uno mapping!",
     377           0 :                     (XWeak *)(OWeakObject *)this );
     378             :             }
     379          58 :         }
     380             :     }
     381       25090 :     return _aCpp2Uno;
     382             : }
     383             : 
     384       10002 : const Mapping & IdlReflectionServiceImpl::getUno2Cpp()
     385             :     throw(css::uno::RuntimeException)
     386             : {
     387       10002 :     if (! _aUno2Cpp.is())
     388             :     {
     389          58 :         MutexGuard aGuard( getMutexAccess() );
     390          58 :         if (! _aUno2Cpp.is())
     391             :         {
     392         116 :             _aUno2Cpp = Mapping(
     393             :                 OUString( UNO_LB_UNO ),
     394          58 :                 OUString( CPPU_CURRENT_LANGUAGE_BINDING_NAME ) );
     395             :             OSL_ENSURE( _aUno2Cpp.is(), "### cannot get uno to c++ mapping!" );
     396          58 :             if (! _aUno2Cpp.is())
     397             :             {
     398             :                 throw RuntimeException(
     399             :                     "cannot get uno to c++ mapping!",
     400           0 :                     (XWeak *)(OWeakObject *)this );
     401             :             }
     402          58 :         }
     403             :     }
     404       10002 :     return _aUno2Cpp;
     405             : }
     406             : 
     407       12442 : uno_Interface * IdlReflectionServiceImpl::mapToUno(
     408             :     const Any & rObj, typelib_InterfaceTypeDescription * pTo )
     409             :     throw(css::uno::RuntimeException)
     410             : {
     411       12442 :     Reference< XInterface > xObj;
     412       12442 :     if (extract( rObj, pTo, xObj, this ))
     413       24884 :         return (uno_Interface *)getCpp2Uno().mapInterface( xObj.get(), pTo );
     414             : 
     415             :     throw RuntimeException(
     416             :         "illegal object given!",
     417       12442 :         (XWeak *)(OWeakObject *)this );
     418             : }
     419             : 
     420             : 
     421         194 : Reference< XInterface > SAL_CALL IdlReflectionServiceImpl_create(
     422             :     const Reference< XComponentContext > & xContext )
     423             :     throw(css::uno::Exception)
     424             : {
     425         194 :     return Reference< XInterface >( (XWeak *)(OWeakObject *)new IdlReflectionServiceImpl( xContext ) );
     426             : }
     427             : 
     428             : }
     429             : 
     430             : 
     431             : using namespace stoc_corefl;
     432             : 
     433             : static const struct ImplementationEntry g_entries[] =
     434             : {
     435             :     {
     436             :         IdlReflectionServiceImpl_create, core_getImplementationName,
     437             :         core_getSupportedServiceNames, createSingleComponentFactory,
     438             :         0, 0
     439             :     },
     440             :     { 0, 0, 0, 0, 0, 0 }
     441             : };
     442             : 
     443         194 : extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL reflection_component_getFactory(
     444             :     const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
     445             : {
     446         194 :     return component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey , g_entries );
     447             : }
     448             : 
     449             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10