AlterOffice
AlterOffice 3.4 SDK C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 #ifndef INCLUDED_CPPUHELPER_INTERFACECONTAINER_HXX
4 #define INCLUDED_CPPUHELPER_INTERFACECONTAINER_HXX
5 
6 #include "sal/config.h"
7 
8 #include <cstddef>
9 
11 
12 
13 namespace cppu
14 {
15 
16 template< class key , class hashImpl , class equalImpl >
18  : rMutex( rMutex_ )
19 {
20  m_pMap = new InterfaceMap;
21 }
22 
23 
24 template< class key , class hashImpl , class equalImpl >
26 {
27  typename InterfaceMap::iterator iter = m_pMap->begin();
28  typename InterfaceMap::iterator end = m_pMap->end();
29 
30  while( iter != end )
31  {
32  delete static_cast<OInterfaceContainerHelper*>((*iter).second);
33  (*iter).second = NULL;
34  ++iter;
35  }
36  delete m_pMap;
37 }
38 
39 
40 template< class key , class hashImpl , class equalImpl >
42 {
43  ::osl::MutexGuard aGuard( rMutex );
44  typename InterfaceMap::size_type nSize = m_pMap->size();
45  if( nSize != 0 )
46  {
47  css::uno::Sequence< key > aInterfaceTypes( nSize );
48  key * pArray = aInterfaceTypes.getArray();
49 
50  typename InterfaceMap::iterator iter = m_pMap->begin();
51  typename InterfaceMap::iterator end = m_pMap->end();
52 
53  sal_uInt32 i = 0;
54  while( iter != end )
55  {
56  // are interfaces added to this container?
57  if( static_cast<OInterfaceContainerHelper*>((*iter).second)->getLength() )
58  // yes, put the type in the array
59  pArray[i++] = (*iter).first;
60  ++iter;
61  }
62  if( i != nSize ) {
63  // may be empty container, reduce the sequence to the right size
64  aInterfaceTypes = css::uno::Sequence<key>( pArray, i );
65  }
66  return aInterfaceTypes;
67  }
68  return css::uno::Sequence<key>();
69 }
70 
71 
72 template< class key , class hashImpl , class equalImpl >
74  const key & rKey ) const
75 {
76  ::osl::MutexGuard aGuard( rMutex );
77 
78  typename InterfaceMap::iterator iter = find( rKey );
79  if( iter != m_pMap->end() )
80  return static_cast<OInterfaceContainerHelper*>( (*iter).second );
81  return NULL;
82 }
83 
84 
85 template< class key , class hashImpl , class equalImpl >
87  const key & rKey,
88  const css::uno::Reference< css::uno::XInterface > & rListener )
89 {
90  ::osl::MutexGuard aGuard( rMutex );
91  typename InterfaceMap::iterator iter = find( rKey );
92  if( iter == m_pMap->end() )
93  {
95  m_pMap->push_back(std::pair<key, void*>(rKey, pLC));
96  return pLC->addInterface( rListener );
97  }
98  else
99  return static_cast<OInterfaceContainerHelper*>((*iter).second)->addInterface( rListener );
100 }
101 
102 
103 template< class key , class hashImpl , class equalImpl >
105  const key & rKey,
106  const css::uno::Reference< css::uno::XInterface > & rListener )
107 {
108  ::osl::MutexGuard aGuard( rMutex );
109 
110  // search container with id nUik
111  typename InterfaceMap::iterator iter = find( rKey );
112  // container found?
113  if( iter != m_pMap->end() )
114  return static_cast<OInterfaceContainerHelper*>((*iter).second)->removeInterface( rListener );
115 
116  // no container with this id. Always return 0
117  return 0;
118 }
119 
120 
121 template< class key , class hashImpl , class equalImpl >
123  const css::lang::EventObject & rEvt )
124 {
125  typename InterfaceMap::size_type nSize = 0;
126  OInterfaceContainerHelper ** ppListenerContainers = NULL;
127  {
128  ::osl::MutexGuard aGuard( rMutex );
129  nSize = m_pMap->size();
130  if( nSize )
131  {
132  typedef OInterfaceContainerHelper* ppp;
133  ppListenerContainers = new ppp[nSize];
134 
135  typename InterfaceMap::iterator iter = m_pMap->begin();
136  typename InterfaceMap::iterator end = m_pMap->end();
137 
138  typename InterfaceMap::size_type i = 0;
139  while( iter != end )
140  {
141  ppListenerContainers[i++] = static_cast<OInterfaceContainerHelper*>((*iter).second);
142  ++iter;
143  }
144  }
145  }
146 
147  // create a copy, because do not fire event in a guarded section
148  for( typename InterfaceMap::size_type i = 0; i < nSize; i++ )
149  {
150  if( ppListenerContainers[i] )
151  ppListenerContainers[i]->disposeAndClear( rEvt );
152  }
153 
154  delete [] ppListenerContainers;
155 }
156 
157 
158 template< class key , class hashImpl , class equalImpl >
160 {
161  ::osl::MutexGuard aGuard( rMutex );
162  typename InterfaceMap::iterator iter = m_pMap->begin();
163  typename InterfaceMap::iterator end = m_pMap->end();
164 
165  while( iter != end )
166  {
167  static_cast<OInterfaceContainerHelper*>((*iter).second)->clear();
168  ++iter;
169  }
170 }
171 
172 
173 }
174 
175 #endif
176 
177 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 removeInterface(const key &rKey, const css::uno::Reference< css::uno::XInterface > &rxIFace)
Removes an element from the container with the specified key.
Definition: interfacecontainer.hxx:104
OInterfaceContainerHelper * getContainer(const key &) const
Return the container created under this key.
Definition: interfacecontainer.hxx:73
~OMultiTypeInterfaceContainerHelperVar()
Deletes all containers.
Definition: interfacecontainer.hxx:25
sal_Int32 addInterface(const css::uno::Reference< css::uno::XInterface > &rxIFace)
Inserts an element into the container.
void clear()
Remove all elements of all containers.
Definition: interfacecontainer.hxx:159
A container of interfaces.
Definition: interfacecontainer.h:111
void disposeAndClear(const css::lang::EventObject &rEvt)
Call disposing on all references in the container, that support XEventListener.
Definition: interfacecontainer.hxx:122
A mutual exclusion synchronization object.
Definition: mutex.hxx:15
Object lifetime scoped mutex object or interface lock.
Definition: mutex.hxx:103
void disposeAndClear(const css::lang::EventObject &rEvt)
Call disposing on all object in the container that support XEventListener.
OMultiTypeInterfaceContainerHelperVar(::osl::Mutex &rMutex)
Create a container of interface containers.
Definition: interfacecontainer.hxx:17
css::uno::Sequence< key > getContainedTypes() const
Return all id&#39;s under which at least one interface is added.
Definition: interfacecontainer.hxx:41
sal_Int32 addInterface(const key &rKey, const css::uno::Reference< css::uno::XInterface > &r)
Inserts an element into the container with the specified key.
Definition: interfacecontainer.hxx:86