bump product version to 5.0.4.1
[LibreOffice.git] / xmloff / source / core / unointerfacetouniqueidentifiermapper.cxx
blob0e2a2cbc2a247a1c3450b8e02e6693ac266eb071
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <xmloff/unointerfacetouniqueidentifiermapper.hxx>
22 using namespace ::com::sun::star;
23 using ::com::sun::star::uno::Reference;
24 using ::com::sun::star::uno::XInterface;
26 namespace comphelper
29 UnoInterfaceToUniqueIdentifierMapper::UnoInterfaceToUniqueIdentifierMapper()
30 : mnNextId( 1 )
34 const OUString& UnoInterfaceToUniqueIdentifierMapper::registerReference( const Reference< XInterface >& rInterface )
36 // Be certain that the references we store in our table are to the
37 // leading / primary XInterface - cf. findReference
38 uno::Reference< uno::XInterface > xRef( rInterface, uno::UNO_QUERY );
40 IdMap_t::const_iterator aIter;
41 if( findReference( xRef, aIter ) )
43 return (*aIter).first;
45 else
47 OUString aId( "id" );
48 aId += OUString::number( mnNextId++ );
49 return (*maEntries.insert( IdMap_t::value_type( aId, xRef ) ).first).first;
53 bool UnoInterfaceToUniqueIdentifierMapper::registerReference( const OUString& rIdentifier, const Reference< XInterface >& rInterface )
55 IdMap_t::const_iterator aIter;
57 // Be certain that the references we store in our table are to the
58 // leading / primary XInterface - cf. findReference
59 uno::Reference< uno::XInterface > xRef( rInterface, uno::UNO_QUERY );
61 if( findReference( xRef, aIter ) )
63 return rIdentifier != (*aIter).first;
65 else if( findIdentifier( rIdentifier, aIter ) )
67 return false;
69 else
71 insertReference( rIdentifier, xRef );
74 return true;
77 void UnoInterfaceToUniqueIdentifierMapper::registerReferenceAlways( const OUString& rIdentifier, const Reference< XInterface >& rInterface )
79 // Be certain that the references we store in our table are to the
80 // leading / primary XInterface - cf. findReference
81 uno::Reference< uno::XInterface > xRef( rInterface, uno::UNO_QUERY );
83 insertReference( rIdentifier, xRef );
86 const OUString& UnoInterfaceToUniqueIdentifierMapper::getIdentifier( const Reference< XInterface >& rInterface ) const
88 IdMap_t::const_iterator aIter;
89 if( findReference( rInterface, aIter ) )
91 return (*aIter).first;
93 else
95 static const OUString aEmpty;
96 return aEmpty;
100 const Reference< XInterface >& UnoInterfaceToUniqueIdentifierMapper::getReference( const OUString& rIdentifier ) const
102 IdMap_t::const_iterator aIter;
103 if( findIdentifier( rIdentifier, aIter ) )
105 return (*aIter).second;
107 else
109 static const Reference< XInterface > aEmpty;
110 return aEmpty;
114 bool UnoInterfaceToUniqueIdentifierMapper::findReference( const Reference< XInterface >& rInterface, IdMap_t::const_iterator& rIter ) const
116 uno::Reference< uno::XInterface > xRef( rInterface, uno::UNO_QUERY );
118 rIter = maEntries.begin();
120 const IdMap_t::const_iterator aEnd( maEntries.end() );
121 while( rIter != aEnd )
123 // The Reference == operator, does a repeated queryInterface on
124 // this to ensure we got the right XInterface base-class. However,
125 // we can be sure that this has been done already by the time we
126 // get to here.
127 if( (*rIter).second.get() == xRef.get() )
128 return true;
130 ++rIter;
133 return false;
136 bool UnoInterfaceToUniqueIdentifierMapper::findIdentifier( const OUString& rIdentifier, IdMap_t::const_iterator& rIter ) const
138 rIter = maEntries.find( rIdentifier );
139 return rIter != maEntries.end();
142 void UnoInterfaceToUniqueIdentifierMapper::insertReference( const OUString& rIdentifier, const Reference< XInterface >& rInterface )
144 maEntries[rIdentifier] = rInterface;
146 // see if this is a reference like something we would generate in the future
147 const sal_Unicode *p = rIdentifier.getStr();
148 sal_Int32 nLength = rIdentifier.getLength();
150 // see if the identifier is 'id' followed by a pure integer value
151 if( nLength < 2 || p[0] != 'i' || p[1] != 'd' )
152 return;
154 nLength -= 2;
155 p += 2;
157 while(nLength--)
159 if( (*p < '0') || (*p > '9') )
160 return; // a custom id, that will never conflict with genereated id's
162 p++;
165 // the identifier is a pure integer value
166 // so we make sure we will never generate
167 // an integer value like this one
168 sal_Int32 nId = rIdentifier.copy(2).toInt32();
169 if( mnNextId <= nId )
170 mnNextId = nId + 1;
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */