Bump for 3.6-28
[LibreOffice.git] / xmloff / source / core / unointerfacetouniqueidentifiermapper.cxx
blobddcafa82dfb6c7496e299039e10e2889f59dff21
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include <xmloff/unointerfacetouniqueidentifiermapper.hxx>
32 using ::com::sun::star::uno::Reference;
33 using ::com::sun::star::uno::XInterface;
34 using ::rtl::OUString;
36 namespace comphelper
39 UnoInterfaceToUniqueIdentifierMapper::UnoInterfaceToUniqueIdentifierMapper()
40 : mnNextId( 1 )
44 /** returns a unique identifier for the given uno object. IF a uno object is
45 registered more than once, the returned identifier is always the same.
47 const OUString& UnoInterfaceToUniqueIdentifierMapper::registerReference( const Reference< XInterface >& rInterface )
49 IdMap_t::const_iterator aIter;
50 if( findReference( rInterface, aIter ) )
52 return (*aIter).first;
54 else
56 OUString aId( RTL_CONSTASCII_USTRINGPARAM( "id" ) );
57 aId += OUString::valueOf( mnNextId++ );
58 return (*maEntries.insert( IdMap_t::value_type( aId, rInterface ) ).first).first;
62 /** registers the given uno object with the given identifier.
64 @returns
65 false, if the given identifier already exists and is not associated with the given interface
67 bool UnoInterfaceToUniqueIdentifierMapper::registerReference( const OUString& rIdentifier, const Reference< XInterface >& rInterface )
69 IdMap_t::const_iterator aIter;
70 if( findReference( rInterface, aIter ) )
72 return rIdentifier != (*aIter).first;
74 else if( findIdentifier( rIdentifier, aIter ) )
76 return false;
78 else
80 maEntries.insert( IdMap_t::value_type( rIdentifier, rInterface ) );
82 // see if this is a reference like something we would generate in the future
83 const sal_Unicode *p = rIdentifier.getStr();
84 sal_Int32 nLength = rIdentifier.getLength();
86 // see if the identifier is 'id' followed by a pure integer value
87 if( nLength < 2 || p[0] != 'i' || p[1] != 'd' )
88 return true;
90 nLength -= 2;
91 p += 2;
93 while(nLength--)
95 if( (*p < '0') || (*p > '9') )
96 return true; // a custom id, that will never conflict with genereated id's
98 p++;
101 // the identifier is a pure integer value
102 // so we make sure we will never generate
103 // an integer value like this one
104 sal_Int32 nId = rIdentifier.copy(2).toInt32();
105 if( mnNextId <= nId )
106 mnNextId = nId + 1;
108 return true;
112 /** @returns
113 the identifier for the given uno object. If this uno object is not already
114 registered, an empty string is returned
116 const OUString& UnoInterfaceToUniqueIdentifierMapper::getIdentifier( const Reference< XInterface >& rInterface ) const
118 IdMap_t::const_iterator aIter;
119 if( findReference( rInterface, aIter ) )
121 return (*aIter).first;
123 else
125 static const OUString aEmpty;
126 return aEmpty;
130 /** @returns
131 the uno object that is registered with the given identifier. If no uno object
132 is registered with the given identifier, an empty reference is returned.
134 const Reference< XInterface >& UnoInterfaceToUniqueIdentifierMapper::getReference( const OUString& rIdentifier ) const
136 IdMap_t::const_iterator aIter;
137 if( findIdentifier( rIdentifier, aIter ) )
139 return (*aIter).second;
141 else
143 static const Reference< XInterface > aEmpty;
144 return aEmpty;
148 bool UnoInterfaceToUniqueIdentifierMapper::findReference( const Reference< XInterface >& rInterface, IdMap_t::const_iterator& rIter ) const
150 rIter = maEntries.begin();
151 const IdMap_t::const_iterator aEnd( maEntries.end() );
152 while( rIter != aEnd )
154 if( (*rIter).second == rInterface )
155 return true;
157 rIter++;
160 return false;
163 bool UnoInterfaceToUniqueIdentifierMapper::findIdentifier( const OUString& rIdentifier, IdMap_t::const_iterator& rIter ) const
165 rIter = maEntries.find( rIdentifier );
166 return rIter != maEntries.end();
171 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */