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 .
19 #ifndef INCLUDED_CPPUHELPER_INTERFACECONTAINER_HXX
20 #define INCLUDED_CPPUHELPER_INTERFACECONTAINER_HXX
22 #include "sal/config.h"
26 #include "cppuhelper/interfacecontainer.h"
32 template< class key
, class hashImpl
, class equalImpl
>
33 inline OMultiTypeInterfaceContainerHelperVar
< key
, hashImpl
, equalImpl
>::OMultiTypeInterfaceContainerHelperVar( ::osl::Mutex
& rMutex_
)
36 m_pMap
= new InterfaceMap
;
40 template< class key
, class hashImpl
, class equalImpl
>
41 inline OMultiTypeInterfaceContainerHelperVar
< key
, hashImpl
, equalImpl
>::~OMultiTypeInterfaceContainerHelperVar()
43 typename
InterfaceMap::iterator iter
= m_pMap
->begin();
44 typename
InterfaceMap::iterator end
= m_pMap
->end();
48 delete static_cast<OInterfaceContainerHelper
*>((*iter
).second
);
56 template< class key
, class hashImpl
, class equalImpl
>
57 inline css::uno::Sequence
< key
> OMultiTypeInterfaceContainerHelperVar
< key
, hashImpl
, equalImpl
>::getContainedTypes() const
59 ::osl::MutexGuard
aGuard( rMutex
);
60 typename
InterfaceMap::size_type nSize
= m_pMap
->size();
63 css::uno::Sequence
< key
> aInterfaceTypes( nSize
);
64 key
* pArray
= aInterfaceTypes
.getArray();
66 typename
InterfaceMap::iterator iter
= m_pMap
->begin();
67 typename
InterfaceMap::iterator end
= m_pMap
->end();
72 // are interfaces added to this container?
73 if( static_cast<OInterfaceContainerHelper
*>((*iter
).second
)->getLength() )
74 // yes, put the type in the array
75 pArray
[i
++] = (*iter
).first
;
79 // may be empty container, reduce the sequence to the right size
80 aInterfaceTypes
= css::uno::Sequence
<key
>( pArray
, i
);
82 return aInterfaceTypes
;
84 return css::uno::Sequence
<key
>();
88 template< class key
, class hashImpl
, class equalImpl
>
89 OInterfaceContainerHelper
* OMultiTypeInterfaceContainerHelperVar
< key
, hashImpl
, equalImpl
>::getContainer(
90 const key
& rKey
) const
92 ::osl::MutexGuard
aGuard( rMutex
);
94 typename
InterfaceMap::iterator iter
= find( rKey
);
95 if( iter
!= m_pMap
->end() )
96 return static_cast<OInterfaceContainerHelper
*>( (*iter
).second
);
101 template< class key
, class hashImpl
, class equalImpl
>
102 sal_Int32 OMultiTypeInterfaceContainerHelperVar
< key
, hashImpl
, equalImpl
>::addInterface(
104 const css::uno::Reference
< css::uno::XInterface
> & rListener
)
106 ::osl::MutexGuard
aGuard( rMutex
);
107 typename
InterfaceMap::iterator iter
= find( rKey
);
108 if( iter
== m_pMap
->end() )
110 OInterfaceContainerHelper
* pLC
= new OInterfaceContainerHelper( rMutex
);
111 m_pMap
->push_back(std::pair
<key
, void*>(rKey
, pLC
));
112 return pLC
->addInterface( rListener
);
115 return static_cast<OInterfaceContainerHelper
*>((*iter
).second
)->addInterface( rListener
);
119 template< class key
, class hashImpl
, class equalImpl
>
120 inline sal_Int32 OMultiTypeInterfaceContainerHelperVar
< key
, hashImpl
, equalImpl
>::removeInterface(
122 const css::uno::Reference
< css::uno::XInterface
> & rListener
)
124 ::osl::MutexGuard
aGuard( rMutex
);
126 // search container with id nUik
127 typename
InterfaceMap::iterator iter
= find( rKey
);
129 if( iter
!= m_pMap
->end() )
130 return static_cast<OInterfaceContainerHelper
*>((*iter
).second
)->removeInterface( rListener
);
132 // no container with this id. Always return 0
137 template< class key
, class hashImpl
, class equalImpl
>
138 void OMultiTypeInterfaceContainerHelperVar
< key
, hashImpl
, equalImpl
>::disposeAndClear(
139 const css::lang::EventObject
& rEvt
)
141 typename
InterfaceMap::size_type nSize
= 0;
142 OInterfaceContainerHelper
** ppListenerContainers
= NULL
;
144 ::osl::MutexGuard
aGuard( rMutex
);
145 nSize
= m_pMap
->size();
148 typedef OInterfaceContainerHelper
* ppp
;
149 ppListenerContainers
= new ppp
[nSize
];
151 typename
InterfaceMap::iterator iter
= m_pMap
->begin();
152 typename
InterfaceMap::iterator end
= m_pMap
->end();
154 typename
InterfaceMap::size_type i
= 0;
157 ppListenerContainers
[i
++] = static_cast<OInterfaceContainerHelper
*>((*iter
).second
);
163 // create a copy, because do not fire event in a guarded section
164 for( typename
InterfaceMap::size_type i
= 0; i
< nSize
; i
++ )
166 if( ppListenerContainers
[i
] )
167 ppListenerContainers
[i
]->disposeAndClear( rEvt
);
170 delete [] ppListenerContainers
;
174 template< class key
, class hashImpl
, class equalImpl
>
175 void OMultiTypeInterfaceContainerHelperVar
< key
, hashImpl
, equalImpl
>::clear()
177 ::osl::MutexGuard
aGuard( rMutex
);
178 typename
InterfaceMap::iterator iter
= m_pMap
->begin();
179 typename
InterfaceMap::iterator end
= m_pMap
->end();
183 static_cast<OInterfaceContainerHelper
*>((*iter
).second
)->clear();
193 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */