LibreOffice Module vbahelper (master) 1
vbacollectionimpl.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
20#ifndef INCLUDED_VBAHELPER_VBACOLLECTIONIMPL_HXX
21#define INCLUDED_VBAHELPER_VBACOLLECTIONIMPL_HXX
22
23#include <exception>
24#include <utility>
25#include <vector>
26
27#include <com/sun/star/container/NoSuchElementException.hpp>
28#include <com/sun/star/container/XEnumeration.hpp>
29#include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
30#include <com/sun/star/lang/WrappedTargetException.hpp>
31#include <com/sun/star/uno/Any.hxx>
32#include <com/sun/star/uno/Reference.hxx>
33#include <com/sun/star/uno/RuntimeException.hpp>
34#include <com/sun/star/uno/Sequence.hxx>
35#include <com/sun/star/uno/Type.hxx>
36#include <com/sun/star/uno/TypeClass.hpp>
37#include <cppu/unotype.hxx>
40#include <ooo/vba/XCollection.hpp>
41#include <com/sun/star/container/XIndexAccess.hpp>
42#include <com/sun/star/container/XNameAccess.hpp>
43#include <com/sun/star/container/XNamed.hpp>
44#include <rtl/ustring.hxx>
45#include <sal/types.h>
46#include <vbahelper/vbadllapi.h>
49
50namespace com::sun::star {
51 namespace container { class XEnumerationAccess; }
52 namespace uno { class XComponentContext; }
53}
54
55namespace ooo::vba {
56 class XHelperInterface;
57}
58
59typedef ::cppu::WeakImplHelper< css::container::XEnumeration > EnumerationHelper_BASE;
60
61
71{
72public:
75 css::uno::Reference< css::container::XIndexAccess > xIndexAccess ) :
76 mxIndexAccess(std::move( xIndexAccess )), mnIndex( 0 ) {}
77
78 virtual sal_Bool SAL_CALL hasMoreElements() override
79 {
80 return mnIndex < mxIndexAccess->getCount();
81 }
82
83 virtual css::uno::Any SAL_CALL nextElement() override
84 {
85 if( !hasMoreElements() )
86 throw css::container::NoSuchElementException();
87 return mxIndexAccess->getByIndex( mnIndex++ );
88 }
89
90private:
91 css::uno::Reference< css::container::XIndexAccess > mxIndexAccess;
92 sal_Int32 mnIndex;
93};
94
95
104{
105public:
108 const css::uno::Reference< css::container::XIndexAccess >& rxIndexAccess ) :
109 mxEnumeration( new SimpleIndexAccessToEnumeration( rxIndexAccess ) ) {}
110
111 virtual sal_Bool SAL_CALL hasMoreElements() override
112 {
113 return mxEnumeration->hasMoreElements();
114 }
115
116 virtual css::uno::Any SAL_CALL nextElement() override
117 {
118 return createCollectionObject( mxEnumeration->nextElement() );
119 }
120
123 virtual css::uno::Any createCollectionObject( const css::uno::Any& rSource ) = 0;
124
125private:
126 css::uno::Reference< css::container::XEnumeration > mxEnumeration;
127};
128
129
130// deprecated, use SimpleEnumerationBase instead!
132{
133protected:
134 css::uno::WeakReference< ov::XHelperInterface > m_xParent;
135 css::uno::Reference< css::uno::XComponentContext > m_xContext;
136 css::uno::Reference< css::container::XEnumeration > m_xEnumeration;
137public:
139 EnumerationHelperImpl( const css::uno::Reference< ov::XHelperInterface >& xParent, css::uno::Reference< css::uno::XComponentContext > xContext, css::uno::Reference< css::container::XEnumeration > xEnumeration ) : m_xParent( xParent ), m_xContext(std::move( xContext )), m_xEnumeration(std::move( xEnumeration )) { }
140 virtual sal_Bool SAL_CALL hasMoreElements( ) override { return m_xEnumeration->hasMoreElements(); }
141};
142
143// a wrapper class for a providing a XIndexAccess, XNameAccess, XEnumerationAccess impl based on providing a vector of interfaces
144// only requirement is the object needs to implement XName
145
146
147template< typename OneIfc >
148class XNamedObjectCollectionHelper final : public ::cppu::WeakImplHelper< css::container::XNameAccess,
149 css::container::XIndexAccess,
150 css::container::XEnumerationAccess >
151{
152public:
153typedef std::vector< css::uno::Reference< OneIfc > > XNamedVec;
154private:
155
157 {
159 typename XNamedVec::iterator mIt;
160 public:
162
163 virtual sal_Bool SAL_CALL hasMoreElements( ) override
164 {
165 return ( mIt != mXNamedVec.end() );
166 }
167
168 virtual css::uno::Any SAL_CALL nextElement( ) override
169 {
170 if ( hasMoreElements() )
171 return css::uno::Any( *mIt++ );
172 throw css::container::NoSuchElementException();
173 }
174 };
175
177 typename XNamedVec::iterator cachePos;
178public:
180 // XElementAccess
181 virtual css::uno::Type SAL_CALL getElementType( ) override { return cppu::UnoType< OneIfc >::get(); }
182 virtual sal_Bool SAL_CALL hasElements( ) override { return ( mXNamedVec.size() > 0 ); }
183 // XNameAccess
184 virtual css::uno::Any SAL_CALL getByName( const OUString& aName ) override
185 {
186 if ( !hasByName(aName) )
187 throw css::container::NoSuchElementException();
188 return css::uno::Any( *cachePos );
189 }
190 virtual css::uno::Sequence< OUString > SAL_CALL getElementNames( ) override
191 {
192 css::uno::Sequence< OUString > sNames( mXNamedVec.size() );
193 OUString* pString = sNames.getArray();
194 typename XNamedVec::iterator it = mXNamedVec.begin();
195 typename XNamedVec::iterator it_end = mXNamedVec.end();
196
197 for ( ; it != it_end; ++it, ++pString )
198 {
199 css::uno::Reference< css::container::XNamed > xName( *it, css::uno::UNO_QUERY_THROW );
200 *pString = xName->getName();
201 }
202 return sNames;
203 }
204 virtual sal_Bool SAL_CALL hasByName( const OUString& aName ) override
205 {
206 cachePos = mXNamedVec.begin();
207 typename XNamedVec::iterator it_end = mXNamedVec.end();
208 for ( ; cachePos != it_end; ++cachePos )
209 {
210 css::uno::Reference< css::container::XNamed > xName( *cachePos, css::uno::UNO_QUERY_THROW );
211 if ( aName == xName->getName() )
212 break;
213 }
214 return ( cachePos != it_end );
215 }
216
217 // XElementAccess
218 virtual ::sal_Int32 SAL_CALL getCount( ) override { return mXNamedVec.size(); }
219 virtual css::uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) override
220 {
222 throw css::lang::IndexOutOfBoundsException();
223
224 return css::uno::Any( mXNamedVec[ Index ] );
225
226 }
227 // XEnumerationAccess
228 virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration( ) override
229 {
231 }
232};
233
234// including a HelperInterface implementation
235template< typename... Ifc >
236class SAL_DLLPUBLIC_RTTI ScVbaCollectionBase : public InheritedHelperInterfaceImpl< Ifc... >
237{
239protected:
240 css::uno::Reference< css::container::XIndexAccess > m_xIndexAccess;
241 css::uno::Reference< css::container::XNameAccess > m_xNameAccess;
243
245 virtual css::uno::Any getItemByStringIndex( const OUString& sIndex )
246 {
247 if ( !m_xNameAccess.is() )
248 throw css::uno::RuntimeException("ScVbaCollectionBase string index access not supported by this object" );
249
250 if( mbIgnoreCase )
251 {
252 const css::uno::Sequence< OUString > sElementNames = m_xNameAccess->getElementNames();
253 for( const OUString& rName : sElementNames )
254 {
255 if( rName.equalsIgnoreAsciiCase( sIndex ) )
256 {
257 return createCollectionObject( m_xNameAccess->getByName( rName ) );
258 }
259 }
260 }
261 return createCollectionObject( m_xNameAccess->getByName( sIndex ) );
262 }
263
266 virtual css::uno::Any getItemByIntIndex( const sal_Int32 nIndex )
267 {
268 if ( !m_xIndexAccess.is() )
269 throw css::uno::RuntimeException("ScVbaCollectionBase numeric index access not supported by this object" );
270 if ( nIndex <= 0 )
271 {
272 throw css::lang::IndexOutOfBoundsException(
273 "index is 0 or negative" );
274 }
275 // need to adjust for vba index ( for which first element is 1 )
276 return createCollectionObject( m_xIndexAccess->getByIndex( nIndex - 1 ) );
277 }
278
279 void UpdateCollectionIndex( const css::uno::Reference< css::container::XIndexAccess >& xIndexAccess )
280 {
281 css::uno::Reference< css::container::XNameAccess > xNameAccess( xIndexAccess, css::uno::UNO_QUERY_THROW );
282 m_xIndexAccess = xIndexAccess;
283 m_xNameAccess = xNameAccess;
284 }
285
286public:
287 ScVbaCollectionBase( const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext, css::uno::Reference< css::container::XIndexAccess > xIndexAccess, bool bIgnoreCase = false ) : BaseColBase( xParent, xContext ), m_xIndexAccess(std::move( xIndexAccess )), mbIgnoreCase( bIgnoreCase ) { m_xNameAccess.set(m_xIndexAccess, css::uno::UNO_QUERY); }
288
289 //XCollection
290 virtual ::sal_Int32 SAL_CALL getCount() override
291 {
292 return m_xIndexAccess->getCount();
293 }
294
295 virtual css::uno::Any SAL_CALL Item(const css::uno::Any& Index1, const css::uno::Any& /*not processed in this base class*/) override
296 {
297 OUString aStringSheet;
298 if (Index1.getValueTypeClass() == css::uno::TypeClass_DOUBLE)
299 {
300 // This is needed for ContentControls, where the unique integer ID
301 // can be passed as float to simulate a "by name" lookup.
302 double fIndex = 0;
303 Index1 >>= fIndex;
304 aStringSheet = OUString::number(fIndex);
305 }
306 else if (Index1.getValueTypeClass() != css::uno::TypeClass_STRING)
307 {
308 sal_Int32 nIndex = 0;
309 if ( !( Index1 >>= nIndex ) )
310 {
311 throw css::lang::IndexOutOfBoundsException( "Couldn't convert index to Int32" );
312 }
313
314 return getItemByIntIndex( nIndex );
315 }
316 else
317 Index1 >>= aStringSheet;
318
319 return getItemByStringIndex( aStringSheet );
320 }
321
322 // XDefaultMethod
323 OUString SAL_CALL getDefaultMethodName( ) override
324 {
325 return "Item";
326 }
327 // XEnumerationAccess
328 virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override = 0;
329
330 // XElementAccess
331 virtual css::uno::Type SAL_CALL getElementType() override = 0;
332 // XElementAccess
333 virtual sal_Bool SAL_CALL hasElements() override
334 {
335 return ( m_xIndexAccess->getCount() > 0 );
336 }
337 virtual css::uno::Any createCollectionObject( const css::uno::Any& aSource ) = 0;
338
339};
340
342// compatible with the old collections ( pre XHelperInterface base class ) ( some internal objects still use this )
344{
345public:
347 ScVbaCollectionBaseImpl( const css::uno::Reference< ov::XHelperInterface > & xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext, const css::uno::Reference< css::container::XIndexAccess >& xIndexAccess ) : CollImplBase( xParent, xContext, xIndexAccess){}
348
349};
350
351template < typename... Ifc > // where Ifc must implement XCollectionTest
352class SAL_DLLPUBLIC_RTTI CollTestImplHelper : public ScVbaCollectionBase< ::cppu::WeakImplHelper< Ifc... > >
353{
354typedef ScVbaCollectionBase< ::cppu::WeakImplHelper< Ifc... > > ImplBase;
355
356public:
358 CollTestImplHelper( const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext, const css::uno::Reference< css::container::XIndexAccess >& xIndexAccess, bool bIgnoreCase = false ) : ImplBase( xParent, xContext, xIndexAccess, bIgnoreCase ) {}
359};
360
361
362#endif //SC_VBA_COLLECTION_IMPL_HXX
363
364/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
struct _ADOIndex Index
Reference< XComponentContext > m_xContext
ScVbaCollectionBase< ::cppu::WeakImplHelper< Ifc... > > ImplBase
CollTestImplHelper(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Reference< css::container::XIndexAccess > &xIndexAccess, bool bIgnoreCase=false)
virtual sal_Bool SAL_CALL hasMoreElements() override
css::uno::Reference< css::container::XEnumeration > m_xEnumeration
css::uno::WeakReference< ov::XHelperInterface > m_xParent
css::uno::Reference< css::uno::XComponentContext > m_xContext
EnumerationHelperImpl(const css::uno::Reference< ov::XHelperInterface > &xParent, css::uno::Reference< css::uno::XComponentContext > xContext, css::uno::Reference< css::container::XEnumeration > xEnumeration)
ScVbaCollectionBaseImpl(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Reference< css::container::XIndexAccess > &xIndexAccess)
css::uno::Reference< css::container::XNameAccess > m_xNameAccess
OUString SAL_CALL getDefaultMethodName() override
ScVbaCollectionBase(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, css::uno::Reference< css::container::XIndexAccess > xIndexAccess, bool bIgnoreCase=false)
virtual sal_Bool SAL_CALL hasElements() override
void UpdateCollectionIndex(const css::uno::Reference< css::container::XIndexAccess > &xIndexAccess)
virtual css::uno::Any createCollectionObject(const css::uno::Any &aSource)=0
virtual css::uno::Any SAL_CALL Item(const css::uno::Any &Index1, const css::uno::Any &) override
virtual css::uno::Any getItemByIntIndex(const sal_Int32 nIndex)
InheritedHelperInterfaceImpl< Ifc... > BaseColBase
css::uno::Reference< css::container::XIndexAccess > m_xIndexAccess
virtual ::sal_Int32 SAL_CALL getCount() override
virtual css::uno::Any getItemByStringIndex(const OUString &sIndex)
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override=0
virtual css::uno::Type SAL_CALL getElementType() override=0
A wrapper that holds a com.sun.star.container.XEnumeration or a com.sun.star.container....
virtual sal_Bool SAL_CALL hasMoreElements() override
SimpleEnumerationBase(const css::uno::Reference< css::container::XIndexAccess > &rxIndexAccess)
css::uno::Reference< css::container::XEnumeration > mxEnumeration
virtual css::uno::Any createCollectionObject(const css::uno::Any &rSource)=0
Derived classes implement creation of a VBA implementation object from the passed container element.
virtual css::uno::Any SAL_CALL nextElement() override
A wrapper that holds a com.sun.star.container.XIndexAccess and provides a com.sun....
SimpleIndexAccessToEnumeration(css::uno::Reference< css::container::XIndexAccess > xIndexAccess)
virtual sal_Bool SAL_CALL hasMoreElements() override
virtual css::uno::Any SAL_CALL nextElement() override
css::uno::Reference< css::container::XIndexAccess > mxIndexAccess
virtual css::uno::Any SAL_CALL nextElement() override
virtual sal_Bool SAL_CALL hasMoreElements() override
virtual css::uno::Any SAL_CALL getByName(const OUString &aName) override
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
virtual css::uno::Type SAL_CALL getElementType() override
std::vector< css::uno::Reference< OneIfc > > XNamedVec
virtual css::uno::Any SAL_CALL getByIndex(::sal_Int32 Index) override
virtual ::sal_Int32 SAL_CALL getCount() override
virtual css::uno::Sequence< OUString > SAL_CALL getElementNames() override
virtual sal_Bool SAL_CALL hasElements() override
XNamedObjectCollectionHelper(XNamedVec sMap)
virtual sal_Bool SAL_CALL hasByName(const OUString &aName) override
css::uno::Type const & get()
sal_Int32 nIndex
OUString aName
enumrange< T >::Iterator begin(enumrange< T >)
sal_uInt32 mnIndex
unsigned char sal_Bool
::cppu::WeakImplHelper< css::container::XEnumeration > EnumerationHelper_BASE
ScVbaCollectionBase< ::cppu::WeakImplHelper< ov::XCollection > > CollImplBase
#define VBAHELPER_DLLPUBLIC
Definition: vbadllapi.h:28