1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#ifndef INCLUDED_VBAHELPER_VBACOLLECTIONIMPL_HXX
#define INCLUDED_VBAHELPER_VBACOLLECTIONIMPL_HXX
#include <ooo/vba/XCollection.hpp>
#include <com/sun/star/container/XEnumerationAccess.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>
#include <com/sun/star/script/XDefaultMethod.hpp>
#include <com/sun/star/container/XIndexAccess.hpp>
#include <com/sun/star/container/XNameAccess.hpp>
#include <com/sun/star/container/XNamed.hpp>
#include <cppuhelper/implbase3.hxx>
#include <cppuhelper/implbase2.hxx>
#include <cppuhelper/implbase1.hxx>
#include <vbahelper/vbahelper.hxx>
#include <vbahelper/vbahelperinterface.hxx>
#include <vector>
// ============================================================================
typedef ::cppu::WeakImplHelper1< css::container::XEnumeration > EnumerationHelper_BASE;
// ============================================================================
/** A wrapper that holds a com.sun.star.container.XIndexAccess and provides a
com.sun.star.container.XEnumeration.
Can be used to provide an enumeration from an index container that contains
completely constructed/initialized VBA implementation objects. CANNOT be
used to provide an enumeration from an index container with other objects
(e.g. UNO objects) where construction of the VBA objects is needed first.
*/
class VBAHELPER_DLLPUBLIC SimpleIndexAccessToEnumeration : public EnumerationHelper_BASE
{
public:
explicit SimpleIndexAccessToEnumeration(
const css::uno::Reference< css::container::XIndexAccess >& rxIndexAccess ) throw (css::uno::RuntimeException) :
mxIndexAccess( rxIndexAccess ), mnIndex( 0 ) {}
virtual sal_Bool SAL_CALL hasMoreElements() throw (css::uno::RuntimeException, std::exception)
{
return mnIndex < mxIndexAccess->getCount();
}
virtual css::uno::Any SAL_CALL nextElement() throw (css::container::NoSuchElementException, css::lang::WrappedTargetException, css::uno::RuntimeException, std::exception)
{
if( !hasMoreElements() )
throw css::container::NoSuchElementException();
return mxIndexAccess->getByIndex( mnIndex++ );
}
private:
css::uno::Reference< css::container::XIndexAccess > mxIndexAccess;
sal_Int32 mnIndex;
};
// ============================================================================
/** A wrapper that holds a com.sun.star.container.XEnumeration or a
com.sun.star.container.XIndexAccess and provides an enumeration of VBA objects.
The method createCollectionObject() needs to be implemented by the derived
class. This class can be used to convert an enumeration or an index container
containing UNO objects to an enumeration providing the related VBA objects.
*/
class VBAHELPER_DLLPUBLIC SimpleEnumerationBase : public EnumerationHelper_BASE
{
public:
explicit SimpleEnumerationBase(
const css::uno::Reference< ov::XHelperInterface >& rxParent,
const css::uno::Reference< css::uno::XComponentContext >& rxContext,
const css::uno::Reference< css::container::XEnumeration >& rxEnumeration ) throw (css::uno::RuntimeException) :
mxParent( rxParent ), mxContext( rxContext ), mxEnumeration( rxEnumeration ) {}
explicit SimpleEnumerationBase(
const css::uno::Reference< ov::XHelperInterface >& rxParent,
const css::uno::Reference< css::uno::XComponentContext >& rxContext,
const css::uno::Reference< css::container::XIndexAccess >& rxIndexAccess ) throw (css::uno::RuntimeException) :
mxParent( rxParent ), mxContext( rxContext ), mxEnumeration( new SimpleIndexAccessToEnumeration( rxIndexAccess ) ) {}
virtual sal_Bool SAL_CALL hasMoreElements() throw (css::uno::RuntimeException, std::exception)
{
return mxEnumeration->hasMoreElements();
}
virtual css::uno::Any SAL_CALL nextElement() throw (css::container::NoSuchElementException, css::lang::WrappedTargetException, css::uno::RuntimeException, std::exception)
{
return createCollectionObject( mxEnumeration->nextElement() );
}
/** Derived classes implement creation of a VBA implementation object from
the passed container element. */
virtual css::uno::Any createCollectionObject( const css::uno::Any& rSource ) = 0;
protected:
css::uno::Reference< ov::XHelperInterface > mxParent;
css::uno::Reference< css::uno::XComponentContext > mxContext;
css::uno::Reference< css::container::XEnumeration > mxEnumeration;
};
// ============================================================================
// deprecated, use SimpleEnumerationBase instead!
class VBAHELPER_DLLPUBLIC EnumerationHelperImpl : public EnumerationHelper_BASE
{
protected:
css::uno::WeakReference< ov::XHelperInterface > m_xParent;
css::uno::Reference< css::uno::XComponentContext > m_xContext;
css::uno::Reference< css::container::XEnumeration > m_xEnumeration;
public:
EnumerationHelperImpl( const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext, const css::uno::Reference< css::container::XEnumeration >& xEnumeration ) throw ( css::uno::RuntimeException ) : m_xParent( xParent ), m_xContext( xContext ), m_xEnumeration( xEnumeration ) { }
virtual ::sal_Bool SAL_CALL hasMoreElements( ) throw (css::uno::RuntimeException, std::exception) { return m_xEnumeration->hasMoreElements(); }
};
// a wrapper class for a providing a XIndexAccess, XNameAccess, XEnumerationAccess impl based on providing a vector of interfaces
// only requirement is the object needs to implement XName
typedef ::cppu::WeakImplHelper3< css::container::XNameAccess, css::container::XIndexAccess, css::container::XEnumerationAccess > XNamedCollectionHelper_BASE;
template< typename Ifc1 >
class XNamedObjectCollectionHelper : public XNamedCollectionHelper_BASE
{
public:
typedef std::vector< css::uno::Reference< Ifc1 > > XNamedVec;
private:
class XNamedEnumerationHelper : public EnumerationHelper_BASE
{
XNamedVec mXNamedVec;
typename XNamedVec::iterator mIt;
public:
XNamedEnumerationHelper( const XNamedVec& sMap ) : mXNamedVec( sMap ), mIt( mXNamedVec.begin() ) {}
virtual ::sal_Bool SAL_CALL hasMoreElements( ) throw (css::uno::RuntimeException, std::exception)
{
return ( mIt != mXNamedVec.end() );
}
virtual css::uno::Any SAL_CALL nextElement( ) throw (css::container::NoSuchElementException, css::lang::WrappedTargetException, css::uno::RuntimeException, std::exception)
{
if ( hasMoreElements() )
return css::uno::makeAny( *mIt++ );
throw css::container::NoSuchElementException();
}
};
protected:
XNamedVec mXNamedVec;
typename XNamedVec::iterator cachePos;
public:
XNamedObjectCollectionHelper( const XNamedVec& sMap ) : mXNamedVec( sMap ), cachePos(mXNamedVec.begin()) {}
// XElementAccess
virtual css::uno::Type SAL_CALL getElementType( ) throw (css::uno::RuntimeException, std::exception) { return cppu::UnoType<Ifc1>::get(); }
virtual ::sal_Bool SAL_CALL hasElements( ) throw (css::uno::RuntimeException, std::exception) { return ( mXNamedVec.size() > 0 ); }
// XNameAcess
virtual css::uno::Any SAL_CALL getByName( const OUString& aName ) throw (css::container::NoSuchElementException, css::lang::WrappedTargetException, css::uno::RuntimeException, std::exception)
{
if ( !hasByName(aName) )
throw css::container::NoSuchElementException();
return css::uno::makeAny( *cachePos );
}
virtual css::uno::Sequence< OUString > SAL_CALL getElementNames( ) throw (css::uno::RuntimeException, std::exception)
{
css::uno::Sequence< OUString > sNames( mXNamedVec.size() );
OUString* pString = sNames.getArray();
typename XNamedVec::iterator it = mXNamedVec.begin();
typename XNamedVec::iterator it_end = mXNamedVec.end();
for ( ; it != it_end; ++it, ++pString )
{
css::uno::Reference< css::container::XNamed > xName( *it, css::uno::UNO_QUERY_THROW );
*pString = xName->getName();
}
return sNames;
}
virtual ::sal_Bool SAL_CALL hasByName( const OUString& aName ) throw (css::uno::RuntimeException, std::exception)
{
cachePos = mXNamedVec.begin();
typename XNamedVec::iterator it_end = mXNamedVec.end();
for ( ; cachePos != it_end; ++cachePos )
{
css::uno::Reference< css::container::XNamed > xName( *cachePos, css::uno::UNO_QUERY_THROW );
if ( aName.equals( xName->getName() ) )
break;
}
return ( cachePos != it_end );
}
// XElementAccess
virtual ::sal_Int32 SAL_CALL getCount( ) throw (css::uno::RuntimeException, std::exception) { return mXNamedVec.size(); }
virtual css::uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) throw (css::lang::IndexOutOfBoundsException, css::lang::WrappedTargetException, css::uno::RuntimeException, std::exception )
{
if ( Index < 0 || Index >= getCount() )
throw css::lang::IndexOutOfBoundsException();
return css::uno::makeAny( mXNamedVec[ Index ] );
}
// XEnumerationAccess
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration( ) throw (css::uno::RuntimeException, std::exception)
{
return new XNamedEnumerationHelper( mXNamedVec );
}
};
// including a HelperInterface implementation
template< typename Ifc1 >
class ScVbaCollectionBase : public InheritedHelperInterfaceImpl< Ifc1 >
{
typedef InheritedHelperInterfaceImpl< Ifc1 > BaseColBase;
protected:
css::uno::Reference< css::container::XIndexAccess > m_xIndexAccess;
css::uno::Reference< css::container::XNameAccess > m_xNameAccess;
sal_Bool mbIgnoreCase;
virtual css::uno::Any getItemByStringIndex( const OUString& sIndex ) throw (css::uno::RuntimeException)
{
if ( !m_xNameAccess.is() )
throw css::uno::RuntimeException("ScVbaCollectionBase string index access not supported by this object", css::uno::Reference< css::uno::XInterface >() );
if( mbIgnoreCase )
{
css::uno::Sequence< OUString > sElementNames = m_xNameAccess->getElementNames();
for( sal_Int32 i = 0; i < sElementNames.getLength(); i++ )
{
OUString aName = sElementNames[i];
if( aName.equalsIgnoreAsciiCase( sIndex ) )
{
return createCollectionObject( m_xNameAccess->getByName( aName ) );
}
}
}
return createCollectionObject( m_xNameAccess->getByName( sIndex ) );
}
virtual css::uno::Any getItemByIntIndex( const sal_Int32 nIndex ) throw (css::uno::RuntimeException)
{
if ( !m_xIndexAccess.is() )
throw css::uno::RuntimeException("ScVbaCollectionBase numeric index access not supported by this object", css::uno::Reference< css::uno::XInterface >() );
if ( nIndex <= 0 )
{
throw css::lang::IndexOutOfBoundsException(
OUString( "index is 0 or negative" ),
css::uno::Reference< css::uno::XInterface >() );
}
// need to adjust for vba index ( for which first element is 1 )
return createCollectionObject( m_xIndexAccess->getByIndex( nIndex - 1 ) );
}
virtual void UpdateCollectionIndex( const css::uno::Reference< css::container::XIndexAccess >& xIndexAccess )
{
css::uno::Reference< css::container::XNameAccess > xNameAccess( xIndexAccess, css::uno::UNO_QUERY_THROW );
m_xIndexAccess = xIndexAccess;
m_xNameAccess = xNameAccess;
}
public:
ScVbaCollectionBase( const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext, const css::uno::Reference< css::container::XIndexAccess >& xIndexAccess, sal_Bool bIgnoreCase = sal_False ) : BaseColBase( xParent, xContext ), m_xIndexAccess( xIndexAccess ), mbIgnoreCase( bIgnoreCase ) { m_xNameAccess.set(m_xIndexAccess, css::uno::UNO_QUERY); }
//XCollection
virtual ::sal_Int32 SAL_CALL getCount() throw (css::uno::RuntimeException)
{
return m_xIndexAccess->getCount();
}
virtual css::uno::Any SAL_CALL Item( const css::uno::Any& Index1, const css::uno::Any& /*not processed in this base class*/ ) throw (css::uno::RuntimeException)
{
if ( Index1.getValueTypeClass() != css::uno::TypeClass_STRING )
{
sal_Int32 nIndex = 0;
if ( !( Index1 >>= nIndex ) )
{
OUString message;
message = OUString( "Couldn't convert index to Int32");
throw css::lang::IndexOutOfBoundsException( message,
css::uno::Reference< css::uno::XInterface >() );
}
return getItemByIntIndex( nIndex );
}
OUString aStringSheet;
Index1 >>= aStringSheet;
return getItemByStringIndex( aStringSheet );
}
// XDefaultMethod
OUString SAL_CALL getDefaultMethodName( ) throw (css::uno::RuntimeException)
{
const static OUString sName( "Item" );
return sName;
}
// XEnumerationAccess
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() throw (css::uno::RuntimeException) = 0;
// XElementAccess
virtual css::uno::Type SAL_CALL getElementType() throw (css::uno::RuntimeException) = 0;
// XElementAccess
virtual ::sal_Bool SAL_CALL hasElements() throw (css::uno::RuntimeException)
{
return ( m_xIndexAccess->getCount() > 0 );
}
virtual css::uno::Any createCollectionObject( const css::uno::Any& aSource ) = 0;
};
typedef ::cppu::WeakImplHelper1<ov::XCollection> XCollection_InterfacesBASE;
typedef ScVbaCollectionBase< XCollection_InterfacesBASE > CollImplBase1;
// compatible with the old collections ( pre XHelperInterface base class ) ( some internal objects still use this )
class VBAHELPER_DLLPUBLIC ScVbaCollectionBaseImpl : public CollImplBase1
{
public:
ScVbaCollectionBaseImpl( const css::uno::Reference< ov::XHelperInterface > xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext, const css::uno::Reference< css::container::XIndexAccess >& xIndexAccess ) throw( css::uno::RuntimeException ) : CollImplBase1( xParent, xContext, xIndexAccess){}
};
template <typename Ifc> // where Ifc must implement XCollectionTest
class CollTestImplHelper : public ScVbaCollectionBase< ::cppu::WeakImplHelper1< Ifc > >
{
typedef ScVbaCollectionBase< ::cppu::WeakImplHelper1< Ifc > > ImplBase1;
public:
CollTestImplHelper( const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext, const css::uno::Reference< css::container::XIndexAccess >& xIndexAccess, sal_Bool bIgnoreCase = sal_False ) throw( css::uno::RuntimeException ) : ImplBase1( xParent, xContext, xIndexAccess, bIgnoreCase ) {}
};
#endif //SC_VBA_COLLECTION_IMPL_HXX
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|