nss: upgrade to release 3.73
[LibreOffice.git] / xmloff / source / core / unointerfacetouniqueidentifiermapper.cxx
blob4884d0ba5823acc8f7d1d01d73b9541ae9ca8451
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 <sal/config.h>
22 #include <o3tl/safeint.hxx>
23 #include <xmloff/unointerfacetouniqueidentifiermapper.hxx>
24 #include <algorithm>
26 using namespace ::com::sun::star;
27 using css::uno::Reference;
28 using css::uno::XInterface;
30 namespace comphelper
33 UnoInterfaceToUniqueIdentifierMapper::UnoInterfaceToUniqueIdentifierMapper()
34 : mnNextId( 1 )
38 const OUString& UnoInterfaceToUniqueIdentifierMapper::registerReference( const Reference< XInterface >& rInterface )
40 // Be certain that the references we store in our table are to the
41 // leading / primary XInterface - cf. findReference
42 uno::Reference< uno::XInterface > xRef( rInterface, uno::UNO_QUERY );
44 IdMap_t::const_iterator aIter;
45 if( findReference( xRef, aIter ) )
47 return (*aIter).first;
49 else
51 OUString aId = "id" + OUString::number( mnNextId++ );
52 return (*maEntries.emplace( aId, xRef ).first).first;
56 bool UnoInterfaceToUniqueIdentifierMapper::registerReference( const OUString& rIdentifier, const Reference< XInterface >& rInterface )
58 IdMap_t::const_iterator aIter;
60 // Be certain that the references we store in our table are to the
61 // leading / primary XInterface - cf. findReference
62 uno::Reference< uno::XInterface > xRef( rInterface, uno::UNO_QUERY );
64 if( findReference( xRef, aIter ) )
66 return rIdentifier != (*aIter).first;
68 else if( findIdentifier( rIdentifier, aIter ) || findReserved( rIdentifier ) )
70 return false;
72 else
74 maEntries.insert( IdMap_t::value_type( rIdentifier, xRef ) );
76 // see if this is a reference like something we would generate in the future
77 const sal_Unicode *p = rIdentifier.getStr();
78 sal_Int32 nLength = rIdentifier.getLength();
80 // see if the identifier is 'id' followed by a pure integer value
81 if( nLength < 2 || p[0] != 'i' || p[1] != 'd' )
82 return true;
84 nLength -= 2;
85 p += 2;
87 while(nLength--)
89 if( (*p < '0') || (*p > '9') )
90 return true; // a custom id, that will never conflict with generated id's
91 p++;
94 // the identifier is a pure integer value
95 // so we make sure we will never generate
96 // an integer value like this one
97 sal_Int32 nId = rIdentifier.copy(2).toInt32();
98 if (nId > 0 && mnNextId <= o3tl::make_unsigned(nId))
99 mnNextId = nId + 1;
101 return true;
105 const OUString& UnoInterfaceToUniqueIdentifierMapper::getIdentifier( const Reference< XInterface >& rInterface ) const
107 IdMap_t::const_iterator aIter;
108 if( findReference( rInterface, aIter ) )
110 return (*aIter).first;
112 else
114 static const OUString aEmpty;
115 return aEmpty;
119 const Reference< XInterface >& UnoInterfaceToUniqueIdentifierMapper::getReference( const OUString& rIdentifier ) const
121 IdMap_t::const_iterator aIter;
122 if( findIdentifier( rIdentifier, aIter ) )
124 return (*aIter).second;
126 else
128 static const Reference< XInterface > aEmpty;
129 return aEmpty;
133 bool UnoInterfaceToUniqueIdentifierMapper::findReference( const Reference< XInterface >& rInterface, IdMap_t::const_iterator& rIter ) const
135 uno::Reference< uno::XInterface > xRef( rInterface, uno::UNO_QUERY );
137 const IdMap_t::const_iterator aEnd( maEntries.end() );
138 rIter = std::find_if(maEntries.begin(), aEnd, [&xRef](const IdMap_t::value_type& rItem) {
139 // The Reference == operator, does a repeated queryInterface on
140 // this to ensure we got the right XInterface base-class. However,
141 // we can be sure that this has been done already by the time we
142 // get to here.
143 return rItem.second.get() == xRef.get();
146 return rIter != aEnd;
149 bool UnoInterfaceToUniqueIdentifierMapper::findIdentifier( const OUString& rIdentifier, IdMap_t::const_iterator& rIter ) const
151 rIter = maEntries.find( rIdentifier );
152 return rIter != maEntries.end();
155 bool UnoInterfaceToUniqueIdentifierMapper::reserveIdentifier( const OUString& rIdentifier )
157 if ( findReserved( rIdentifier ) )
158 return false;
160 maReserved.push_back( rIdentifier );
161 return true;
164 bool UnoInterfaceToUniqueIdentifierMapper::registerReservedReference(
165 const OUString& rIdentifier,
166 const css::uno::Reference< css::uno::XInterface >& rInterface )
168 Reserved_t::const_iterator aIt;
169 if ( !findReserved( rIdentifier, aIt ) )
170 return false;
172 Reserved_t::iterator aRemoveIt( maReserved.begin() + ( aIt - maReserved.begin() ) );
173 maReserved.erase( aRemoveIt );
174 registerReference( rIdentifier, rInterface );
176 return true;
179 bool UnoInterfaceToUniqueIdentifierMapper::findReserved( const OUString& rIdentifier ) const
181 Reserved_t::const_iterator aDummy;
182 return findReserved( rIdentifier, aDummy );
185 bool UnoInterfaceToUniqueIdentifierMapper::findReserved(
186 const OUString& rIdentifier,
187 Reserved_t::const_iterator& rIter ) const
189 rIter = std::find( maReserved.begin(), maReserved.end(), rIdentifier );
190 return rIter != maReserved.end();
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */