LCOV - code coverage report
Current view: top level - libreoffice/xmloff/source/core - unointerfacetouniqueidentifiermapper.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 14 59 23.7 %
Date: 2012-12-27 Functions: 3 7 42.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             : 
      21             : #include <xmloff/unointerfacetouniqueidentifiermapper.hxx>
      22             : 
      23             : using namespace ::com::sun::star;
      24             : using ::com::sun::star::uno::Reference;
      25             : using ::com::sun::star::uno::XInterface;
      26             : using ::rtl::OUString;
      27             : 
      28             : namespace comphelper
      29             : {
      30             : 
      31         952 : UnoInterfaceToUniqueIdentifierMapper::UnoInterfaceToUniqueIdentifierMapper()
      32         952 : : mnNextId( 1 )
      33             : {
      34         952 : }
      35             : 
      36             : /** returns a unique identifier for the given uno object. IF a uno object is
      37             :     registered more than once, the returned identifier is always the same.
      38             : */
      39           0 : const OUString& UnoInterfaceToUniqueIdentifierMapper::registerReference( const Reference< XInterface >& rInterface )
      40             : {
      41             :     // Be certain that the references we store in our table are to the
      42             :     // leading / primary XInterface - cf. findReference
      43           0 :     uno::Reference< uno::XInterface > xRef( rInterface, uno::UNO_QUERY );
      44             : 
      45           0 :     IdMap_t::const_iterator aIter;
      46           0 :     if( findReference( xRef, aIter ) )
      47             :     {
      48           0 :         return (*aIter).first;
      49             :     }
      50             :     else
      51             :     {
      52           0 :         OUString aId( "id" );
      53           0 :         aId += OUString::valueOf( mnNextId++ );
      54           0 :         return (*maEntries.insert( IdMap_t::value_type( aId, xRef ) ).first).first;
      55           0 :     }
      56             : }
      57             : 
      58             : /** registers the given uno object with the given identifier.
      59             : 
      60             :     @returns
      61             :         false, if the given identifier already exists and is not associated with the given interface
      62             : */
      63           0 : bool UnoInterfaceToUniqueIdentifierMapper::registerReference( const OUString& rIdentifier, const Reference< XInterface >& rInterface )
      64             : {
      65           0 :     IdMap_t::const_iterator aIter;
      66             : 
      67             :     // Be certain that the references we store in our table are to the
      68             :     // leading / primary XInterface - cf. findReference
      69           0 :     uno::Reference< uno::XInterface > xRef( rInterface, uno::UNO_QUERY );
      70             : 
      71           0 :     if( findReference( xRef, aIter ) )
      72             :     {
      73           0 :         return rIdentifier != (*aIter).first;
      74             :     }
      75           0 :     else if( findIdentifier( rIdentifier, aIter ) )
      76             :     {
      77           0 :         return false;
      78             :     }
      79             :     else
      80             :     {
      81           0 :         maEntries.insert( IdMap_t::value_type( rIdentifier, xRef ) );
      82             : 
      83             :         // see if this is a reference like something we would generate in the future
      84           0 :         const sal_Unicode *p = rIdentifier.getStr();
      85           0 :         sal_Int32 nLength = rIdentifier.getLength();
      86             : 
      87             :         // see if the identifier is 'id' followed by a pure integer value
      88           0 :         if( nLength < 2 || p[0] != 'i' || p[1] != 'd' )
      89           0 :             return true;
      90             : 
      91           0 :         nLength -= 2;
      92           0 :         p += 2;
      93             : 
      94           0 :         while(nLength--)
      95             :         {
      96           0 :             if( (*p < '0') || (*p > '9') )
      97           0 :                 return true; // a custom id, that will never conflict with genereated id's
      98             : 
      99           0 :             p++;
     100             :         }
     101             : 
     102             :         // the identifier is a pure integer value
     103             :         // so we make sure we will never generate
     104             :         // an integer value like this one
     105           0 :         sal_Int32 nId = rIdentifier.copy(2).toInt32();
     106           0 :         if( mnNextId <= nId )
     107           0 :             mnNextId = nId + 1;
     108             : 
     109           0 :         return true;
     110           0 :     }
     111             : }
     112             : 
     113             : /** @returns
     114             :         the identifier for the given uno object. If this uno object is not already
     115             :         registered, an empty string is returned
     116             : */
     117          20 : const OUString& UnoInterfaceToUniqueIdentifierMapper::getIdentifier( const Reference< XInterface >& rInterface ) const
     118             : {
     119          20 :     IdMap_t::const_iterator aIter;
     120          20 :     if( findReference( rInterface, aIter ) )
     121             :     {
     122           0 :         return (*aIter).first;
     123             :     }
     124             :     else
     125             :     {
     126          20 :         static const OUString aEmpty;
     127          20 :         return aEmpty;
     128             :     }
     129             : }
     130             : 
     131             : /** @returns
     132             :     the uno object that is registered with the given identifier. If no uno object
     133             :     is registered with the given identifier, an empty reference is returned.
     134             : */
     135           0 : const Reference< XInterface >& UnoInterfaceToUniqueIdentifierMapper::getReference( const OUString& rIdentifier ) const
     136             : {
     137           0 :     IdMap_t::const_iterator aIter;
     138           0 :     if( findIdentifier( rIdentifier, aIter ) )
     139             :     {
     140           0 :         return (*aIter).second;
     141             :     }
     142             :     else
     143             :     {
     144           0 :         static const Reference< XInterface > aEmpty;
     145           0 :         return aEmpty;
     146             :     }
     147             : }
     148             : 
     149          20 : bool UnoInterfaceToUniqueIdentifierMapper::findReference( const Reference< XInterface >& rInterface, IdMap_t::const_iterator& rIter ) const
     150             : {
     151          20 :     uno::Reference< uno::XInterface > xRef( rInterface, uno::UNO_QUERY );
     152             : 
     153          20 :     rIter = maEntries.begin();
     154             : 
     155          20 :     const IdMap_t::const_iterator aEnd( maEntries.end() );
     156          40 :     while( rIter != aEnd )
     157             :     {
     158             :         // The Reference == operator, does a repeated queryInterface on
     159             :         // this to ensure we got the right XInterface base-class. However,
     160             :         // we can be sure that this has been done already by the time we
     161             :         // get to here.
     162           0 :         if( (*rIter).second.get() == xRef.get() )
     163           0 :             return true;
     164             : 
     165           0 :         rIter++;
     166             :     }
     167             : 
     168          20 :     return false;
     169             : }
     170             : 
     171           0 : bool UnoInterfaceToUniqueIdentifierMapper::findIdentifier( const OUString& rIdentifier, IdMap_t::const_iterator& rIter ) const
     172             : {
     173           0 :     rIter = maEntries.find( rIdentifier );
     174           0 :     return rIter != maEntries.end();
     175             : }
     176             : 
     177             : }
     178             : 
     179             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10