1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 #ifndef __COMPHELPER_UNOINTERFACETOUNIQUEIDENTIFIERMAPPER__
23 #define __COMPHELPER_UNOINTERFACETOUNIQUEIDENTIFIERMAPPER__
24 
25 #include <map>
26 #include <rtl/ustring.hxx>
27 #include <com/sun/star/uno/XInterface.hpp>
28 
29 namespace comphelper
30 {
31 
32 typedef ::std::map< rtl::OUString, const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > > IdMap_t;
33 
34 class UnoInterfaceToUniqueIdentifierMapper
35 {
36 public:
37 	UnoInterfaceToUniqueIdentifierMapper();
38 
39 	/** returns a unique identifier for the given uno object. IF a uno object is
40 		registered more than once, the returned identifier is always the same.
41 	*/
42 	const rtl::OUString& registerReference( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& rInterface );
43 
44 	/** registers the given uno object with the given identifier.
45 
46 		@returns
47 			false, if the given identifier already exists and is not associated with the given interface
48 	*/
49 	bool registerReference( const rtl::OUString& rIdentifier, const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& rInterface );
50 
51 	/** @returns
52 			the identifier for the given uno object. If this uno object is not already
53 			registered, an empty string is returned
54 	*/
55 	const rtl::OUString& getIdentifier( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& rInterface ) const;
56 
57 	/** @returns
58 		the uno object that is registered with the given identifier. If no uno object
59 		is registered with the given identifier, an empty reference is returned.
60 	*/
61 	const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& getReference( const rtl::OUString& rIdentifier ) const;
62 
63 private:
64 	bool findReference( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& rInterface, IdMap_t::const_iterator& rIter ) const;
65 	bool findIdentifier( const rtl::OUString& rIdentifier, IdMap_t::const_iterator& rIter ) const;
66 
67 	IdMap_t	maEntries;
68 	sal_Int32 mnNextId;
69 };
70 
71 }
72 
73 #endif
74