LibreOffice
LibreOffice 4.4 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  : rMutex( rMutex_ )
31 {
32  m_pMap = new InterfaceMap;
33 }
34 
35 
36 template< class key , class hashImpl , class equalImpl >
38 {
39  typename InterfaceMap::iterator iter = m_pMap->begin();
40  typename InterfaceMap::iterator end = m_pMap->end();
41 
42  while( iter != end )
43  {
44  delete static_cast<OInterfaceContainerHelper*>((*iter).second);
45  (*iter).second = 0;
46  ++iter;
47  }
48  delete m_pMap;
49 }
50 
51 
52 template< class key , class hashImpl , class equalImpl >
54 {
55  ::osl::MutexGuard aGuard( rMutex );
56  typename InterfaceMap::size_type nSize = m_pMap->size();
57  if( nSize != 0 )
58  {
59  ::com::sun::star::uno::Sequence< key > aInterfaceTypes( nSize );
60  key * pArray = aInterfaceTypes.getArray();
61 
62  typename InterfaceMap::iterator iter = m_pMap->begin();
63  typename InterfaceMap::iterator end = m_pMap->end();
64 
65  sal_uInt32 i = 0;
66  while( iter != end )
67  {
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++;
73  }
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 );
77  }
78  return aInterfaceTypes;
79  }
80  return ::com::sun::star::uno::Sequence<key>();
81 }
82 
83 
84 template< class key , class hashImpl , class equalImpl >
86  const key & rKey ) const
87 {
88  ::osl::MutexGuard aGuard( rMutex );
89 
90  typename InterfaceMap::iterator iter = find( rKey );
91  if( iter != m_pMap->end() )
92  return static_cast<OInterfaceContainerHelper*>( (*iter).second );
93  return 0;
94 }
95 
96 
97 template< class key , class hashImpl , class equalImpl >
99  const key & rKey,
100  const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & rListener )
101 {
102  ::osl::MutexGuard aGuard( rMutex );
103  typename InterfaceMap::iterator iter = find( rKey );
104  if( iter == m_pMap->end() )
105  {
107  m_pMap->push_back(std::pair<key, void*>(rKey, pLC));
108  return pLC->addInterface( rListener );
109  }
110  else
111  return static_cast<OInterfaceContainerHelper*>((*iter).second)->addInterface( rListener );
112 }
113 
114 
115 template< class key , class hashImpl , class equalImpl >
117  const key & rKey,
118  const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & rListener )
119 {
120  ::osl::MutexGuard aGuard( rMutex );
121 
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 );
127 
128  // no container with this id. Always return 0
129  return 0;
130 }
131 
132 
133 template< class key , class hashImpl , class equalImpl >
135  const ::com::sun::star::lang::EventObject & rEvt )
136 {
137  typename InterfaceMap::size_type nSize = 0;
138  OInterfaceContainerHelper ** ppListenerContainers = NULL;
139  {
140  ::osl::MutexGuard aGuard( rMutex );
141  nSize = m_pMap->size();
142  if( nSize )
143  {
144  typedef OInterfaceContainerHelper* ppp;
145  ppListenerContainers = new ppp[nSize];
146 
147  typename InterfaceMap::iterator iter = m_pMap->begin();
148  typename InterfaceMap::iterator end = m_pMap->end();
149 
150  typename InterfaceMap::size_type i = 0;
151  while( iter != end )
152  {
153  ppListenerContainers[i++] = static_cast<OInterfaceContainerHelper*>((*iter).second);
154  ++iter;
155  }
156  }
157  }
158 
159  // create a copy, because do not fire event in a guarded section
160  for( typename InterfaceMap::size_type i = 0; i < nSize; i++ )
161  {
162  if( ppListenerContainers[i] )
163  ppListenerContainers[i]->disposeAndClear( rEvt );
164  }
165 
166  delete [] ppListenerContainers;
167 }
168 
169 
170 template< class key , class hashImpl , class equalImpl >
172 {
173  ::osl::MutexGuard aGuard( rMutex );
174  typename InterfaceMap::iterator iter = m_pMap->begin();
175  typename InterfaceMap::iterator end = m_pMap->end();
176 
177  while( iter != end )
178  {
179  static_cast<OInterfaceContainerHelper*>((*iter).second)->clear();
180  ++iter;
181  }
182 }
183 
184 
185 }
186 
187 #endif
188 
189 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
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:116
A helper class for mutex objects and interfaces.
Definition: mutex.hxx:121
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:98
Definition: Enterable.hxx:26
void disposeAndClear(const ::com::sun::star::lang::EventObject &rEvt)
Call disposing on all object in the container that support XEventListener.
~OMultiTypeInterfaceContainerHelperVar()
Deletes all containers.
Definition: interfacecontainer.hxx:37
A mutual exclusion synchronization object.
Definition: mutex.hxx:30
inline::com::sun::star::uno::Sequence< key > getContainedTypes() const
Return all id's under which at least one interface is added.
Definition: interfacecontainer.hxx:53
OMultiTypeInterfaceContainerHelperVar(::osl::Mutex &rMutex)
Create a container of interface containers.
Definition: interfacecontainer.hxx:29
OInterfaceContainerHelper * getContainer(const key &) const
Return the container created under this key.
Definition: interfacecontainer.hxx:85
Template C++ class representing an IDL sequence.
Definition: unotype.hxx:33
void clear()
Remove all elements of all containers.
Definition: interfacecontainer.hxx:171
sal_Int32 addInterface(const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > &rxIFace)
Inserts an element into the container.
A container of interfaces.
Definition: interfacecontainer.h:120
void disposeAndClear(const ::com::sun::star::lang::EventObject &rEvt)
Call disposing on all references in the container, that support XEventListener.
Definition: interfacecontainer.hxx:134
E * getArray()
Gets a pointer to elements array for reading and writing.
Definition: Sequence.hxx:135