LibreOffice Module connectivity (master) 1
pq_xcontainer.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*************************************************************************
3 *
4 * Effective License of whole file:
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License version 2.1, as published by the Free Software Foundation.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18 * MA 02111-1307 USA
19 *
20 * Parts "Copyright by Sun Microsystems, Inc" prior to August 2011:
21 *
22 * The Contents of this file are made available subject to the terms of
23 * the GNU Lesser General Public License Version 2.1
24 *
25 * Copyright: 2000 by Sun Microsystems, Inc.
26 *
27 * Contributor(s): Joerg Budischewski
28 *
29 * All parts contributed on or after August 2011:
30 *
31 * This Source Code Form is subject to the terms of the Mozilla Public
32 * License, v. 2.0. If a copy of the MPL was not distributed with this
33 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
34 *
35 ************************************************************************/
36
37#include <com/sun/star/container/ElementExistException.hpp>
38#include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
41#include <o3tl/safeint.hxx>
42#include <utility>
43
44#include "pq_xcontainer.hxx"
45#include "pq_statics.hxx"
46#include "pq_tools.hxx"
47
48using osl::MutexGuard;
49
51
52using com::sun::star::uno::Any;
53using com::sun::star::uno::Type;
54using com::sun::star::uno::XInterface;
57using com::sun::star::uno::RuntimeException;
58
59using com::sun::star::container::NoSuchElementException;
60using com::sun::star::container::XEnumeration;
61using com::sun::star::container::XContainerListener;
62using com::sun::star::container::ContainerEvent;
63using com::sun::star::lang::IndexOutOfBoundsException;
64using com::sun::star::lang::XEventListener;
65
66
67namespace pq_sdbc_driver
68{
69
70namespace {
71
72class ReplacedBroadcaster : public EventBroadcastHelper
73{
74 ContainerEvent m_event;
75public:
76 ReplacedBroadcaster(
77 const Reference< XInterface > & source,
78 const OUString & name,
79 const Any & newElement,
80 const OUString & oldElement ) :
81 m_event( source, Any( name ), newElement, Any(oldElement) )
82 {}
83
84 virtual void fire( XEventListener * listener ) const override
85 {
86 static_cast<XContainerListener*>(listener)->elementReplaced( m_event );
87 }
88 virtual Type getType() const override
89 {
91 }
92};
93
94class InsertedBroadcaster : public EventBroadcastHelper
95{
96public:
97 ContainerEvent m_event;
98 InsertedBroadcaster(
99 const Reference< XInterface > & source,
100 const OUString & name,
101 const Any & newElement ) :
102 m_event( source, Any( name ), newElement, Any() )
103 {}
104
105 virtual void fire( XEventListener * listener ) const override
106 {
107 static_cast<XContainerListener*>(listener)->elementInserted( m_event );
108 }
109
110 virtual Type getType() const override
111 {
113 }
114};
115
116class RemovedBroadcaster : public EventBroadcastHelper
117{
118public:
119 ContainerEvent m_event;
120 RemovedBroadcaster(
121 const Reference< XInterface > & source,
122 const OUString & name) :
123 m_event( source, Any( name ), Any(), Any() )
124 {}
125
126 virtual void fire( XEventListener * listener ) const override
127 {
128 static_cast<XContainerListener*>(listener)->elementRemoved( m_event );
129 }
130
131 virtual Type getType() const override
132 {
134 }
135};
136
137}
138
140 const ::rtl::Reference< comphelper::RefCountedMutex > & refMutex,
141 css::uno::Reference< css::sdbc::XConnection > origin,
142 ConnectionSettings *pSettings,
143 OUString type)
144 : ContainerBase( refMutex->GetMutex() ),
145 m_xMutex( refMutex ),
146 m_pSettings( pSettings ),
147 m_origin(std::move( origin )),
148 m_type(std::move( type ))
149{
150}
151
152Any Container::getByName( const OUString& aName )
153{
154 String2IntMap::const_iterator ii = m_name2index.find( aName );
155 if( ii == m_name2index.end() )
156 {
157 throw NoSuchElementException(
158 "Element " + aName + " unknown in " + m_type + "-Container",
159 *this );
160 }
161 OSL_ASSERT( ii->second >= 0 && o3tl::make_unsigned(ii->second) < m_values.size() );
162 return m_values[ ii->second ];
163}
164
166{
167 Sequence< OUString > ret( m_values.size() );
168 auto retRange = asNonConstRange(ret);
169 for( const auto& [rName, rIndex] : m_name2index )
170 {
171 // give element names in index order !
172 retRange[rIndex] = rName;
173 }
174 return ret;
175}
176
177sal_Bool Container::hasByName( const OUString& aName )
178{
179 return m_name2index.find( aName ) != m_name2index.end();
180}
181 // Methods
183{
184 return Type();
185}
186
188{
189 return ! m_name2index.empty();
190}
191
193{
194 if( Index < 0 || o3tl::make_unsigned(Index) >= m_values.size() )
195 {
196 throw IndexOutOfBoundsException(
197 "Index " + OUString::number( Index )
198 + " out of range for " + m_type + "-Container, expected 0 <= x <= "
199 + OUString::number(m_values.size() -1),
200 *this );
201 }
202 return m_values[Index];
203}
204
206{
207 return m_values.size();
208}
209
210namespace {
211
212class ContainerEnumeration : public ::cppu::WeakImplHelper< XEnumeration >
213{
214 std::vector< css::uno::Any > m_vec;
215 sal_Int32 m_index;
216public:
217 explicit ContainerEnumeration( std::vector< css::uno::Any >&& vec )
218 : m_vec( std::move(vec) ),
219 m_index( -1 )
220 {}
221
222public:
223 // XEnumeration
224 virtual sal_Bool SAL_CALL hasMoreElements( ) override;
225 virtual css::uno::Any SAL_CALL nextElement( ) override;
226
227};
228
229}
230
231sal_Bool ContainerEnumeration::hasMoreElements()
232{
233 return static_cast<int>(m_vec.size()) > m_index +1;
234}
235
236css::uno::Any ContainerEnumeration::nextElement()
237{
238 if( ! hasMoreElements() )
239 {
240 throw NoSuchElementException(
241 "NoSuchElementException during enumeration", *this );
242 }
243 m_index ++;
244 return m_vec[m_index];
245}
246
248{
249 return new ContainerEnumeration( std::vector(m_values) );
250}
251
253 const css::uno::Reference< css::util::XRefreshListener >& l )
254{
255 rBHelper.addListener( cppu::UnoType<decltype(l)>::get() , l );
256}
257
259 const css::uno::Reference< css::util::XRefreshListener >& l )
260{
261 rBHelper.removeListener( cppu::UnoType<decltype(l)>::get() , l );
262}
263
265{
266 m_origin.clear();
267}
268
269void Container::rename( const OUString &oldName, const OUString &newName )
270{
271 Any newValue;
272 {
273 osl::MutexGuard guard ( m_xMutex->GetMutex() );
274 String2IntMap::iterator ii = m_name2index.find( oldName );
275 if( ii != m_name2index.end() )
276 {
277 sal_Int32 nIndex = ii->second;
278 newValue = m_values[nIndex];
279 m_name2index.erase( ii );
281 }
282 }
283 fire( ReplacedBroadcaster( *this, newName, newValue, oldName ) );
284 fire( RefreshedBroadcaster( *this ) );
285}
286
287void Container::dropByName( const OUString& elementName )
288{
289 osl::MutexGuard guard( m_xMutex->GetMutex() );
290 String2IntMap::const_iterator ii = m_name2index.find( elementName );
291 if( ii == m_name2index.end() )
292 {
293 throw css::container::NoSuchElementException(
294 "Column " + elementName + " is unknown in "
295 + m_type + " container, so it can't be dropped",
296 *this );
297 }
298 dropByIndex( ii->second );
299}
300
301void Container::dropByIndex( sal_Int32 index )
302{
303 osl::MutexGuard guard( m_xMutex->GetMutex() );
304 if( index < 0 || o3tl::make_unsigned(index) >=m_values.size() )
305 {
306 throw css::lang::IndexOutOfBoundsException(
307 "Index out of range (allowed 0 to "
308 + OUString::number(m_values.size() -1)
309 + ", got " + OUString::number( index )
310 + ") in " + m_type,
311 *this );
312 }
313
314 OUString name;
315 String2IntMap::iterator ii = std::find_if(m_name2index.begin(), m_name2index.end(),
316 [&index](const String2IntMap::value_type& rEntry) { return rEntry.second == index; });
317 if (ii != m_name2index.end())
318 {
319 name = ii->first;
320 m_name2index.erase( ii );
321 }
322
323 for( int i = index +1 ; i < static_cast<int>(m_values.size()) ; i ++ )
324 {
325 m_values[i-1] = m_values[i];
326
327 // I know, this is expensive, but don't want to maintain another map ...
328 ii = std::find_if(m_name2index.begin(), m_name2index.end(),
329 [&i](const String2IntMap::value_type& rEntry) { return rEntry.second == i; });
330 if (ii != m_name2index.end())
331 {
332 ii->second = i-1;
333 }
334 }
335 m_values.resize( m_values.size() - 1 );
336
337 fire( RemovedBroadcaster( *this, name ) );
338}
339
341 const OUString & name,
342 const css::uno::Reference< css::beans::XPropertySet >& descriptor )
343
344{
345 osl::MutexGuard guard( m_xMutex->GetMutex() );
346
347 if( hasByName( name ) )
348 {
349 throw css::container::ElementExistException(
350 "a " + m_type + " with name " + name + " already exists in this container",
351 *this );
352 }
353
354 int index = m_values.size();
355 m_values.push_back( Any( descriptor ) );
357
358 fire( InsertedBroadcaster( *this, name, Any( descriptor ) ) );
359}
360
362 const css::uno::Reference< css::beans::XPropertySet >& descriptor)
363{
364 append( extractStringProperty( descriptor, getStatics().NAME ), descriptor );
365}
366
367
369 const css::uno::Reference< css::container::XContainerListener >& l )
370{
371 rBHelper.addListener( cppu::UnoType<decltype(l)>::get() , l );
372}
373
375 const css::uno::Reference< css::container::XContainerListener >& l )
376{
377 rBHelper.removeListener( cppu::UnoType<decltype(l)>::get() , l );
378}
379
380
382{
383 cppu::OInterfaceContainerHelper *container = rBHelper.getContainer( helper.getType() );
384 if( !container )
385 return;
386
387 cppu::OInterfaceIteratorHelper iterator( * container );
388 while( iterator.hasMoreElements() )
389 {
390 try
391 {
392 helper.fire( static_cast<XEventListener *>(iterator.next()) );
393 }
394 catch ( css::uno::RuntimeException & )
395 {
396 TOOLS_WARN_EXCEPTION( "connectivity.postgresql", "exception caught" );
397 // loose coupling, a runtime exception shall not break anything
398 // TODO: log away as warning !
399 }
400 catch( css::uno::Exception & )
401 {
402 TOOLS_WARN_EXCEPTION( "connectivity.postgresql", "exception from listener flying through" );
403 throw;
404 }
405 }
406
407}
408
409}
410
411/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
struct _ADOIndex Index
Definition: Awrapadox.hxx:45
NAME
css::uno::XInterface *SAL_CALL next()
bool SAL_CALL hasMoreElements() const
css::uno::Type const & get()
virtual css::uno::Any SAL_CALL getByIndex(sal_Int32 Index) override
void append(const OUString &str, const css::uno::Reference< css::beans::XPropertySet > &descriptor)
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
Container(const ::rtl::Reference< comphelper::RefCountedMutex > &refMutex, css::uno::Reference< css::sdbc::XConnection > origin, ConnectionSettings *pSettings, OUString type)
virtual css::uno::Sequence< OUString > SAL_CALL getElementNames() override
virtual void SAL_CALL addRefreshListener(const css::uno::Reference< css::util::XRefreshListener > &l) override
void fire(const EventBroadcastHelper &helper)
void rename(const OUString &oldName, const OUString &newName)
virtual sal_Bool SAL_CALL hasElements() override
virtual sal_Int32 SAL_CALL getCount() override
virtual void SAL_CALL dropByName(const OUString &elementName) override
css::uno::Reference< css::sdbc::XConnection > m_origin
virtual css::uno::Type SAL_CALL getElementType() override
virtual css::uno::Any SAL_CALL getByName(const OUString &aName) override
virtual void SAL_CALL disposing() override
virtual void SAL_CALL addContainerListener(const css::uno::Reference< css::container::XContainerListener > &xListener) override
virtual void SAL_CALL removeRefreshListener(const css::uno::Reference< css::util::XRefreshListener > &l) override
virtual sal_Bool SAL_CALL hasByName(const OUString &aName) override
::rtl::Reference< comphelper::RefCountedMutex > m_xMutex
virtual void SAL_CALL appendByDescriptor(const css::uno::Reference< css::beans::XPropertySet > &descriptor) override
virtual void SAL_CALL removeContainerListener(const css::uno::Reference< css::container::XContainerListener > &xListener) override
virtual void SAL_CALL dropByIndex(sal_Int32 index) override
std::vector< css::uno::Any > m_values
#define TOOLS_WARN_EXCEPTION(area, stream)
const Type m_type
sal_Int32 nIndex
OUString aName
void SAL_CALL elementReplaced(const css::container::ContainerEvent &Event) override
void SAL_CALL elementRemoved(const css::container::ContainerEvent &Event) override
DECL_LISTENERMULTIPLEXER_END void SAL_CALL elementInserted(const css::container::ContainerEvent &Event) override
class SAL_NO_VTABLE XPropertySet
Type
OUString newName(std::u16string_view aNewPrefix, std::u16string_view aOldPrefix, std::u16string_view old_Name)
int i
index
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
::cppu::WeakComponentImplHelper< css::container::XNameAccess, css::container::XIndexAccess, css::container::XEnumerationAccess, css::sdbcx::XAppend, css::sdbcx::XDrop, css::util::XRefreshable, css::sdbcx::XDataDescriptorFactory, css::container::XContainer > ContainerBase
Statics & getStatics()
Definition: pq_statics.cxx:112
OUString extractStringProperty(const Reference< XPropertySet > &descriptor, const OUString &name)
Definition: pq_tools.cxx:204
bool getType(BSTR name, Type &type)
OUString name
Definition: pq_statics.cxx:74
sal_Int32 type
Definition: pq_statics.cxx:60
sal_Int32 m_index
std::vector< css::uno::Any > m_vec
ContainerEvent m_event
unsigned char sal_Bool