LibreOffice Module xmloff (master) 1
unointerfacetouniqueidentifiermapper.cxx
Go to the documentation of this file.
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 <sal/config.h>
21
22#include <o3tl/safeint.hxx>
24#include <o3tl/string_view.hxx>
25#include <algorithm>
26
27using namespace ::com::sun::star;
28using css::uno::Reference;
29using css::uno::XInterface;
30
31namespace comphelper
32{
33
35: mnNextId( 1 )
36{
37}
38
39const 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 uno::Reference< uno::XInterface > xRef( rInterface, uno::UNO_QUERY );
44
45 IdMap_t::const_iterator aIter;
46 if( findReference( xRef, aIter ) )
47 {
48 return (*aIter).first;
49 }
50 else
51 {
52 OUString aId = "id" + OUString::number( mnNextId++ );
53 return (*maEntries.emplace( aId, xRef ).first).first;
54 }
55}
56
57bool UnoInterfaceToUniqueIdentifierMapper::registerReference( const OUString& rIdentifier, const Reference< XInterface >& rInterface )
58{
59 IdMap_t::const_iterator aIter;
60
61 // Be certain that the references we store in our table are to the
62 // leading / primary XInterface - cf. findReference
63 uno::Reference< uno::XInterface > xRef( rInterface, uno::UNO_QUERY );
64
65 if( findReference( xRef, aIter ) )
66 {
67 return rIdentifier != (*aIter).first;
68 }
69 else if( findIdentifier( rIdentifier, aIter ) || findReserved( rIdentifier ) )
70 {
71 return false;
72 }
73 else
74 {
75 maEntries.insert( IdMap_t::value_type( rIdentifier, xRef ) );
76
77 // see if this is a reference like something we would generate in the future
78 const sal_Unicode *p = rIdentifier.getStr();
79 sal_Int32 nLength = rIdentifier.getLength();
80
81 // see if the identifier is 'id' followed by a pure integer value
82 if( nLength < 2 || p[0] != 'i' || p[1] != 'd' )
83 return true;
84
85 nLength -= 2;
86 p += 2;
87
88 while(nLength--)
89 {
90 if( (*p < '0') || (*p > '9') )
91 return true; // a custom id, that will never conflict with generated id's
92 p++;
93 }
94
95 // the identifier is a pure integer value
96 // so we make sure we will never generate
97 // an integer value like this one
98 sal_Int32 nId = o3tl::toInt32(rIdentifier.subView(2));
99 if (nId > 0 && mnNextId <= o3tl::make_unsigned(nId))
100 mnNextId = nId + 1;
101
102 return true;
103 }
104}
105
106const OUString& UnoInterfaceToUniqueIdentifierMapper::getIdentifier( const Reference< XInterface >& rInterface ) const
107{
108 IdMap_t::const_iterator aIter;
109 if( findReference( rInterface, aIter ) )
110 {
111 return (*aIter).first;
112 }
113 else
114 {
115 static const OUString aEmpty;
116 return aEmpty;
117 }
118}
119
120const Reference< XInterface >& UnoInterfaceToUniqueIdentifierMapper::getReference( const OUString& rIdentifier ) const
121{
122 IdMap_t::const_iterator aIter;
123 if( findIdentifier( rIdentifier, aIter ) )
124 {
125 return (*aIter).second;
126 }
127 else
128 {
129 static const Reference< XInterface > aEmpty;
130 return aEmpty;
131 }
132}
133
134bool UnoInterfaceToUniqueIdentifierMapper::findReference( const Reference< XInterface >& rInterface, IdMap_t::const_iterator& rIter ) const
135{
136 uno::Reference< uno::XInterface > xRef( rInterface, uno::UNO_QUERY );
137
138 const IdMap_t::const_iterator aEnd( maEntries.end() );
139 rIter = std::find_if(maEntries.begin(), aEnd, [&xRef](const IdMap_t::value_type& rItem) {
140 // The Reference == operator, does a repeated queryInterface on
141 // this to ensure we got the right XInterface base-class. However,
142 // we can be sure that this has been done already by the time we
143 // get to here.
144 return rItem.second.get() == xRef.get();
145 });
146
147 return rIter != aEnd;
148}
149
150bool UnoInterfaceToUniqueIdentifierMapper::findIdentifier( const OUString& rIdentifier, IdMap_t::const_iterator& rIter ) const
151{
152 rIter = maEntries.find( rIdentifier );
153 return rIter != maEntries.end();
154}
155
157{
158 if ( findReserved( rIdentifier ) )
159 return false;
160
161 maReserved.push_back( rIdentifier );
162 return true;
163}
164
166 const OUString& rIdentifier,
167 const css::uno::Reference< css::uno::XInterface >& rInterface )
168{
169 Reserved_t::const_iterator aIt;
170 if ( !findReserved( rIdentifier, aIt ) )
171 return false;
172
173 Reserved_t::iterator aRemoveIt( maReserved.begin() + ( aIt - maReserved.begin() ) );
174 maReserved.erase( aRemoveIt );
175 registerReference( rIdentifier, rInterface );
176
177 return true;
178}
179
180bool UnoInterfaceToUniqueIdentifierMapper::findReserved( const OUString& rIdentifier ) const
181{
182 Reserved_t::const_iterator aDummy;
183 return findReserved( rIdentifier, aDummy );
184}
185
187 const OUString& rIdentifier,
188 Reserved_t::const_iterator& rIter ) const
189{
190 rIter = std::find( maReserved.begin(), maReserved.end(), rIdentifier );
191 return rIter != maReserved.end();
192}
193
194}
195
196/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const OUString & getIdentifier(const css::uno::Reference< css::uno::XInterface > &rInterface) const
bool registerReservedReference(const OUString &rIdentifier, const css::uno::Reference< css::uno::XInterface > &rInterface)
registers the given uno object with reserved identifier.
const OUString & registerReference(const css::uno::Reference< css::uno::XInterface > &rInterface)
returns a unique identifier for the given uno object.
const css::uno::Reference< css::uno::XInterface > & getReference(const OUString &rIdentifier) const
bool findIdentifier(const OUString &rIdentifier, IdMap_t::const_iterator &rIter) const
bool reserveIdentifier(const OUString &rIdentifier)
reserves an identifier for later registration.
bool findReference(const css::uno::Reference< css::uno::XInterface > &rInterface, IdMap_t::const_iterator &rIter) const
void * p
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
sal_Int32 toInt32(std::u16string_view str, sal_Int16 radix=10)
sal_Int16 nId
sal_uInt16 sal_Unicode
sal_Int32 nLength
Definition: xmltoken.cxx:38