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 <algorithm>
25 
26 using namespace ::com::sun::star;
27 using css::uno::Reference;
28 using css::uno::XInterface;
29 
30 namespace comphelper
31 {
32 
34 : mnNextId( 1 )
35 {
36 }
37 
39 {
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 );
43 
44  IdMap_t::const_iterator aIter;
45  if( findReference( xRef, aIter ) )
46  {
47  return (*aIter).first;
48  }
49  else
50  {
51  OUString aId = "id" + OUString::number( mnNextId++ );
52  return (*maEntries.emplace( aId, xRef ).first).first;
53  }
54 }
55 
56 bool UnoInterfaceToUniqueIdentifierMapper::registerReference( const OUString& rIdentifier, const Reference< XInterface >& rInterface )
57 {
58  IdMap_t::const_iterator aIter;
59 
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 );
63 
64  if( findReference( xRef, aIter ) )
65  {
66  return rIdentifier != (*aIter).first;
67  }
68  else if( findIdentifier( rIdentifier, aIter ) || findReserved( rIdentifier ) )
69  {
70  return false;
71  }
72  else
73  {
74  maEntries.insert( IdMap_t::value_type( rIdentifier, xRef ) );
75 
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();
79 
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;
83 
84  nLength -= 2;
85  p += 2;
86 
87  while(nLength--)
88  {
89  if( (*p < '0') || (*p > '9') )
90  return true; // a custom id, that will never conflict with generated id's
91  p++;
92  }
93 
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;
100 
101  return true;
102  }
103 }
104 
106 {
107  IdMap_t::const_iterator aIter;
108  if( findReference( rInterface, aIter ) )
109  {
110  return (*aIter).first;
111  }
112  else
113  {
114  static const OUString aEmpty;
115  return aEmpty;
116  }
117 }
118 
120 {
121  IdMap_t::const_iterator aIter;
122  if( findIdentifier( rIdentifier, aIter ) )
123  {
124  return (*aIter).second;
125  }
126  else
127  {
128  static const Reference< XInterface > aEmpty;
129  return aEmpty;
130  }
131 }
132 
133 bool UnoInterfaceToUniqueIdentifierMapper::findReference( const Reference< XInterface >& rInterface, IdMap_t::const_iterator& rIter ) const
134 {
135  uno::Reference< uno::XInterface > xRef( rInterface, uno::UNO_QUERY );
136 
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();
144  });
145 
146  return rIter != aEnd;
147 }
148 
149 bool UnoInterfaceToUniqueIdentifierMapper::findIdentifier( const OUString& rIdentifier, IdMap_t::const_iterator& rIter ) const
150 {
151  rIter = maEntries.find( rIdentifier );
152  return rIter != maEntries.end();
153 }
154 
156 {
157  if ( findReserved( rIdentifier ) )
158  return false;
159 
160  maReserved.push_back( rIdentifier );
161  return true;
162 }
163 
165  const OUString& rIdentifier,
166  const css::uno::Reference< css::uno::XInterface >& rInterface )
167 {
168  Reserved_t::const_iterator aIt;
169  if ( !findReserved( rIdentifier, aIt ) )
170  return false;
171 
172  Reserved_t::iterator aRemoveIt( maReserved.begin() + ( aIt - maReserved.begin() ) );
173  maReserved.erase( aRemoveIt );
174  registerReference( rIdentifier, rInterface );
175 
176  return true;
177 }
178 
179 bool UnoInterfaceToUniqueIdentifierMapper::findReserved( const OUString& rIdentifier ) const
180 {
181  Reserved_t::const_iterator aDummy;
182  return findReserved( rIdentifier, aDummy );
183 }
184 
186  const OUString& rIdentifier,
187  Reserved_t::const_iterator& rIter ) const
188 {
189  rIter = std::find( maReserved.begin(), maReserved.end(), rIdentifier );
190  return rIter != maReserved.end();
191 }
192 
193 }
194 
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool reserveIdentifier(const OUString &rIdentifier)
reserves an identifier for later registration.
bool findIdentifier(const OUString &rIdentifier, IdMap_t::const_iterator &rIter) const
sal_Int16 nId
OUString aId
const css::uno::Reference< css::uno::XInterface > & getReference(const OUString &rIdentifier) const
sal_uInt16 sal_Unicode
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
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.
void * p
bool findReference(const css::uno::Reference< css::uno::XInterface > &rInterface, IdMap_t::const_iterator &rIter) const
sal_Int32 nLength
Definition: xmltoken.cxx:36
const OUString & registerReference(const css::uno::Reference< css::uno::XInterface > &rInterface)
returns a unique identifier for the given uno object.