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

Generated by: LCOV version 1.10