LibreOffice
LibreOffice 4.2 SDK C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
interfacecontainer.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 #ifndef INCLUDED_CPPUHELPER_INTERFACECONTAINER_HXX
20 #define INCLUDED_CPPUHELPER_INTERFACECONTAINER_HXX
21 
23 
24 
25 namespace cppu
26 {
27 
28 template< class key , class hashImpl , class equalImpl >
30  SAL_THROW(())
31  : rMutex( rMutex_ )
32 {
33  m_pMap = new InterfaceMap;
34 }
35 
36 //===================================================================
37 template< class key , class hashImpl , class equalImpl >
39  SAL_THROW(())
40 {
41  typename InterfaceMap::iterator iter = m_pMap->begin();
42  typename InterfaceMap::iterator end = m_pMap->end();
43 
44  while( iter != end )
45  {
46  delete (OInterfaceContainerHelper*)(*iter).second;
47  (*iter).second = 0;
48  ++iter;
49  }
50  delete m_pMap;
51 }
52 
53 //===================================================================
54 template< class key , class hashImpl , class equalImpl >
56  SAL_THROW(())
57 {
58  ::osl::MutexGuard aGuard( rMutex );
59  typename InterfaceMap::size_type nSize = m_pMap->size();
60  if( nSize != 0 )
61  {
62  ::com::sun::star::uno::Sequence< key > aInterfaceTypes( nSize );
63  key * pArray = aInterfaceTypes.getArray();
64 
65  typename InterfaceMap::iterator iter = m_pMap->begin();
66  typename InterfaceMap::iterator end = m_pMap->end();
67 
68  sal_uInt32 i = 0;
69  while( iter != end )
70  {
71  // are interfaces added to this container?
72  if( ((OInterfaceContainerHelper*)(*iter).second)->getLength() )
73  // yes, put the type in the array
74  pArray[i++] = (*iter).first;
75  iter++;
76  }
77  if( i != nSize ) {
78  // may be empty container, reduce the sequence to the right size
79  aInterfaceTypes = ::com::sun::star::uno::Sequence<key>( pArray, i );
80  }
81  return aInterfaceTypes;
82  }
83  return ::com::sun::star::uno::Sequence<key>();
84 }
85 
86 //===================================================================
87 template< class key , class hashImpl , class equalImpl >
89  const key & rKey ) const SAL_THROW(())
90 {
91  ::osl::MutexGuard aGuard( rMutex );
92 
93  typename InterfaceMap::iterator iter = find( rKey );
94  if( iter != m_pMap->end() )
95  return (OInterfaceContainerHelper*) (*iter).second;
96  return 0;
97 }
98 
99 //===================================================================
100 template< class key , class hashImpl , class equalImpl >
102  const key & rKey,
103  const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & rListener )
104  SAL_THROW(())
105 {
106  ::osl::MutexGuard aGuard( rMutex );
107  typename InterfaceMap::iterator iter = find( rKey );
108  if( iter == m_pMap->end() )
109  {
111  m_pMap->push_back(std::pair<key, void*>(rKey, pLC));
112  return pLC->addInterface( rListener );
113  }
114  else
115  return ((OInterfaceContainerHelper*)(*iter).second)->addInterface( rListener );
116 }
117 
118 //===================================================================
119 template< class key , class hashImpl , class equalImpl >
121  const key & rKey,
122  const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & rListener )
123  SAL_THROW(())
124 {
125  ::osl::MutexGuard aGuard( rMutex );
126 
127  // search container with id nUik
128  typename InterfaceMap::iterator iter = find( rKey );
129  // container found?
130  if( iter != m_pMap->end() )
131  return ((OInterfaceContainerHelper*)(*iter).second)->removeInterface( rListener );
132 
133  // no container with this id. Always return 0
134  return 0;
135 }
136 
137 //===================================================================
138 template< class key , class hashImpl , class equalImpl >
140  const ::com::sun::star::lang::EventObject & rEvt )
141  SAL_THROW(())
142 {
143  typename InterfaceMap::size_type nSize = 0;
144  OInterfaceContainerHelper ** ppListenerContainers = NULL;
145  {
146  ::osl::MutexGuard aGuard( rMutex );
147  nSize = m_pMap->size();
148  if( nSize )
149  {
150  typedef OInterfaceContainerHelper* ppp;
151  ppListenerContainers = new ppp[nSize];
152 
153  typename InterfaceMap::iterator iter = m_pMap->begin();
154  typename InterfaceMap::iterator end = m_pMap->end();
155 
156  typename InterfaceMap::size_type i = 0;
157  while( iter != end )
158  {
159  ppListenerContainers[i++] = (OInterfaceContainerHelper*)(*iter).second;
160  ++iter;
161  }
162  }
163  }
164 
165  // create a copy, because do not fire event in a guarded section
166  for( typename InterfaceMap::size_type i = 0; i < nSize; i++ )
167  {
168  if( ppListenerContainers[i] )
169  ppListenerContainers[i]->disposeAndClear( rEvt );
170  }
171 
172  delete [] ppListenerContainers;
173 }
174 
175 //===================================================================
176 template< class key , class hashImpl , class equalImpl >
178 {
179  ::osl::MutexGuard aGuard( rMutex );
180  typename InterfaceMap::iterator iter = m_pMap->begin();
181  typename InterfaceMap::iterator end = m_pMap->end();
182 
183  while( iter != end )
184  {
185  ((OInterfaceContainerHelper*)(*iter).second)->clear();
186  ++iter;
187  }
188 }
189 
190 
191 }
192 
193 #endif
194 
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 addInterface(const key &rKey, const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > &r)
Inserts an element into the container with the specified key.
Definition: interfacecontainer.hxx:101
sal_Int32 removeInterface(const key &rKey, const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > &rxIFace)
Removes an element from the container with the specified key.
Definition: interfacecontainer.hxx:120
void disposeAndClear(const ::com::sun::star::lang::EventObject &rEvt)
Call disposing on all references in the container, that support XEventListener.
Definition: interfacecontainer.hxx:139
OInterfaceContainerHelper * getContainer(const key &) const
Return the container created under this key.
Definition: interfacecontainer.hxx:88
inline::com::sun::star::uno::Sequence< key > getContainedTypes() const
Return all id's under which at least one interface is added.
Definition: interfacecontainer.hxx:55
void clear()
Remove all elements of all containers.
Definition: interfacecontainer.hxx:177
~OMultiTypeInterfaceContainerHelperVar()
Deletes all containers.
Definition: interfacecontainer.hxx:38
void disposeAndClear(const ::com::sun::star::lang::EventObject &rEvt)
Call disposing on all object in the container that support XEventListener.
A helper class for mutex objects and interfaces.
Definition: mutex.hxx:123
A container of interfaces.
Definition: interfacecontainer.h:114
OMultiTypeInterfaceContainerHelperVar(::osl::Mutex &rMutex)
Create a container of interface containers.
Definition: interfacecontainer.hxx:29
A mutual exclusion synchronization object.
Definition: mutex.hxx:32
E * getArray()
Gets a pointer to elements array for reading and writing.
Definition: Sequence.hxx:134
Template C++ class representing an IDL sequence.
Definition: unotype.hxx:33
sal_Int32 addInterface(const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > &rxIFace)
Inserts an element into the container.
#define SAL_THROW(exc)
Definition of function throw clause macros.
Definition: types.h:358