Branch libreoffice-5-0-4
[LibreOffice.git] / include / cppuhelper / interfacecontainer.hxx
blobac89d619773b24908284975c136d3f3ab4f431d2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
19 #ifndef INCLUDED_CPPUHELPER_INTERFACECONTAINER_HXX
20 #define INCLUDED_CPPUHELPER_INTERFACECONTAINER_HXX
22 #include <cppuhelper/interfacecontainer.h>
25 namespace cppu
28 template< class key , class hashImpl , class equalImpl >
29 inline OMultiTypeInterfaceContainerHelperVar< key , hashImpl , equalImpl >::OMultiTypeInterfaceContainerHelperVar( ::osl::Mutex & rMutex_ )
30 : rMutex( rMutex_ )
32 m_pMap = new InterfaceMap;
36 template< class key , class hashImpl , class equalImpl >
37 inline OMultiTypeInterfaceContainerHelperVar< key , hashImpl , equalImpl >::~OMultiTypeInterfaceContainerHelperVar()
39 typename InterfaceMap::iterator iter = m_pMap->begin();
40 typename InterfaceMap::iterator end = m_pMap->end();
42 while( iter != end )
44 delete static_cast<OInterfaceContainerHelper*>((*iter).second);
45 (*iter).second = 0;
46 ++iter;
48 delete m_pMap;
52 template< class key , class hashImpl , class equalImpl >
53 inline ::com::sun::star::uno::Sequence< key > OMultiTypeInterfaceContainerHelperVar< key , hashImpl , equalImpl >::getContainedTypes() const
55 ::osl::MutexGuard aGuard( rMutex );
56 typename InterfaceMap::size_type nSize = m_pMap->size();
57 if( nSize != 0 )
59 ::com::sun::star::uno::Sequence< key > aInterfaceTypes( nSize );
60 key * pArray = aInterfaceTypes.getArray();
62 typename InterfaceMap::iterator iter = m_pMap->begin();
63 typename InterfaceMap::iterator end = m_pMap->end();
65 sal_uInt32 i = 0;
66 while( iter != end )
68 // are interfaces added to this container?
69 if( static_cast<OInterfaceContainerHelper*>((*iter).second)->getLength() )
70 // yes, put the type in the array
71 pArray[i++] = (*iter).first;
72 ++iter;
74 if( i != nSize ) {
75 // may be empty container, reduce the sequence to the right size
76 aInterfaceTypes = ::com::sun::star::uno::Sequence<key>( pArray, i );
78 return aInterfaceTypes;
80 return ::com::sun::star::uno::Sequence<key>();
84 template< class key , class hashImpl , class equalImpl >
85 OInterfaceContainerHelper * OMultiTypeInterfaceContainerHelperVar< key , hashImpl , equalImpl >::getContainer(
86 const key & rKey ) const
88 ::osl::MutexGuard aGuard( rMutex );
90 typename InterfaceMap::iterator iter = find( rKey );
91 if( iter != m_pMap->end() )
92 return static_cast<OInterfaceContainerHelper*>( (*iter).second );
93 return 0;
97 template< class key , class hashImpl , class equalImpl >
98 sal_Int32 OMultiTypeInterfaceContainerHelperVar< key , hashImpl , equalImpl >::addInterface(
99 const key & rKey,
100 const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & rListener )
102 ::osl::MutexGuard aGuard( rMutex );
103 typename InterfaceMap::iterator iter = find( rKey );
104 if( iter == m_pMap->end() )
106 OInterfaceContainerHelper * pLC = new OInterfaceContainerHelper( rMutex );
107 m_pMap->push_back(std::pair<key, void*>(rKey, pLC));
108 return pLC->addInterface( rListener );
110 else
111 return static_cast<OInterfaceContainerHelper*>((*iter).second)->addInterface( rListener );
115 template< class key , class hashImpl , class equalImpl >
116 inline sal_Int32 OMultiTypeInterfaceContainerHelperVar< key , hashImpl , equalImpl >::removeInterface(
117 const key & rKey,
118 const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & rListener )
120 ::osl::MutexGuard aGuard( rMutex );
122 // search container with id nUik
123 typename InterfaceMap::iterator iter = find( rKey );
124 // container found?
125 if( iter != m_pMap->end() )
126 return static_cast<OInterfaceContainerHelper*>((*iter).second)->removeInterface( rListener );
128 // no container with this id. Always return 0
129 return 0;
133 template< class key , class hashImpl , class equalImpl >
134 void OMultiTypeInterfaceContainerHelperVar< key , hashImpl , equalImpl >::disposeAndClear(
135 const ::com::sun::star::lang::EventObject & rEvt )
137 typename InterfaceMap::size_type nSize = 0;
138 OInterfaceContainerHelper ** ppListenerContainers = NULL;
140 ::osl::MutexGuard aGuard( rMutex );
141 nSize = m_pMap->size();
142 if( nSize )
144 typedef OInterfaceContainerHelper* ppp;
145 ppListenerContainers = new ppp[nSize];
147 typename InterfaceMap::iterator iter = m_pMap->begin();
148 typename InterfaceMap::iterator end = m_pMap->end();
150 typename InterfaceMap::size_type i = 0;
151 while( iter != end )
153 ppListenerContainers[i++] = static_cast<OInterfaceContainerHelper*>((*iter).second);
154 ++iter;
159 // create a copy, because do not fire event in a guarded section
160 for( typename InterfaceMap::size_type i = 0; i < nSize; i++ )
162 if( ppListenerContainers[i] )
163 ppListenerContainers[i]->disposeAndClear( rEvt );
166 delete [] ppListenerContainers;
170 template< class key , class hashImpl , class equalImpl >
171 void OMultiTypeInterfaceContainerHelperVar< key , hashImpl , equalImpl >::clear()
173 ::osl::MutexGuard aGuard( rMutex );
174 typename InterfaceMap::iterator iter = m_pMap->begin();
175 typename InterfaceMap::iterator end = m_pMap->end();
177 while( iter != end )
179 static_cast<OInterfaceContainerHelper*>((*iter).second)->clear();
180 ++iter;
187 #endif
189 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */