1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <o3tl/string_view.hxx>
27 using namespace ::com::sun::star
;
28 using css::uno::Reference
;
29 using css::uno::XInterface
;
34 UnoInterfaceToUniqueIdentifierMapper::UnoInterfaceToUniqueIdentifierMapper()
39 const OUString
& UnoInterfaceToUniqueIdentifierMapper::registerReference( const Reference
< XInterface
>& rInterface
)
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
);
45 IdMap_t::const_iterator aIter
;
46 if( findReference( xRef
, aIter
) )
48 return (*aIter
).first
;
52 OUString aId
= "id" + OUString::number( mnNextId
++ );
53 return (*maEntries
.emplace( aId
, xRef
).first
).first
;
57 bool UnoInterfaceToUniqueIdentifierMapper::registerReference( const OUString
& rIdentifier
, const Reference
< XInterface
>& rInterface
)
59 IdMap_t::const_iterator aIter
;
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
);
65 if( findReference( xRef
, aIter
) )
67 return rIdentifier
!= (*aIter
).first
;
69 else if( findIdentifier( rIdentifier
, aIter
) || findReserved( rIdentifier
) )
75 maEntries
.insert( IdMap_t::value_type( rIdentifier
, xRef
) );
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();
81 // see if the identifier is 'id' followed by a pure integer value
82 if( nLength
< 2 || p
[0] != 'i' || p
[1] != 'd' )
90 if( (*p
< '0') || (*p
> '9') )
91 return true; // a custom id, that will never conflict with generated id's
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
))
106 const OUString
& UnoInterfaceToUniqueIdentifierMapper::getIdentifier( const Reference
< XInterface
>& rInterface
) const
108 IdMap_t::const_iterator aIter
;
109 if( findReference( rInterface
, aIter
) )
111 return (*aIter
).first
;
115 static const OUString aEmpty
;
120 const Reference
< XInterface
>& UnoInterfaceToUniqueIdentifierMapper::getReference( const OUString
& rIdentifier
) const
122 IdMap_t::const_iterator aIter
;
123 if( findIdentifier( rIdentifier
, aIter
) )
125 return (*aIter
).second
;
129 static const Reference
< XInterface
> aEmpty
;
134 bool UnoInterfaceToUniqueIdentifierMapper::findReference( const Reference
< XInterface
>& rInterface
, IdMap_t::const_iterator
& rIter
) const
136 uno::Reference
< uno::XInterface
> xRef( rInterface
, uno::UNO_QUERY
);
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
144 return rItem
.second
.get() == xRef
.get();
147 return rIter
!= aEnd
;
150 bool UnoInterfaceToUniqueIdentifierMapper::findIdentifier( const OUString
& rIdentifier
, IdMap_t::const_iterator
& rIter
) const
152 rIter
= maEntries
.find( rIdentifier
);
153 return rIter
!= maEntries
.end();
156 bool UnoInterfaceToUniqueIdentifierMapper::reserveIdentifier( const OUString
& rIdentifier
)
158 if ( findReserved( rIdentifier
) )
161 maReserved
.push_back( rIdentifier
);
165 bool UnoInterfaceToUniqueIdentifierMapper::registerReservedReference(
166 const OUString
& rIdentifier
,
167 const css::uno::Reference
< css::uno::XInterface
>& rInterface
)
169 Reserved_t::const_iterator aIt
;
170 if ( !findReserved( rIdentifier
, aIt
) )
173 Reserved_t::iterator
aRemoveIt( maReserved
.begin() + ( aIt
- maReserved
.begin() ) );
174 maReserved
.erase( aRemoveIt
);
175 registerReference( rIdentifier
, rInterface
);
180 bool UnoInterfaceToUniqueIdentifierMapper::findReserved( const OUString
& rIdentifier
) const
182 Reserved_t::const_iterator aDummy
;
183 return findReserved( rIdentifier
, aDummy
);
186 bool UnoInterfaceToUniqueIdentifierMapper::findReserved(
187 const OUString
& rIdentifier
,
188 Reserved_t::const_iterator
& rIter
) const
190 rIter
= std::find( maReserved
.begin(), maReserved
.end(), rIdentifier
);
191 return rIter
!= maReserved
.end();
196 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */