2010-10-27 13:11:31 +01:00
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2012-06-22 16:25:46 +01:00
/*
* 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 .
*/
2010-06-15 20:02:53 +02:00
2013-10-23 19:18:54 +02:00
# ifndef INCLUDED_VBAHELPER_VBACOLLECTIONIMPL_HXX
# define INCLUDED_VBAHELPER_VBACOLLECTIONIMPL_HXX
2009-09-18 15:35:47 +00:00
# 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>
2013-11-09 15:33:26 -06:00
# include <vbahelper/vbahelper.hxx>
# include <vbahelper/vbahelperinterface.hxx>
2009-09-18 15:35:47 +00:00
# include <vector>
2014-02-25 19:06:16 +01:00
2010-06-15 20:02:53 +02:00
2009-09-18 15:35:47 +00:00
typedef : : cppu : : WeakImplHelper1 < css : : container : : XEnumeration > EnumerationHelper_BASE ;
2014-02-25 19:06:16 +01:00
2010-06-15 20:02:53 +02:00
/** 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 ) { }
2014-03-27 18:12:18 +01:00
virtual sal_Bool SAL_CALL hasMoreElements ( ) throw ( css : : uno : : RuntimeException , std : : exception ) SAL_OVERRIDE
2010-06-15 20:02:53 +02:00
{
return mnIndex < mxIndexAccess - > getCount ( ) ;
}
2014-03-27 18:12:18 +01:00
virtual css : : uno : : Any SAL_CALL nextElement ( ) throw ( css : : container : : NoSuchElementException , css : : lang : : WrappedTargetException , css : : uno : : RuntimeException , std : : exception ) SAL_OVERRIDE
2010-06-15 20:02:53 +02:00
{
if ( ! hasMoreElements ( ) )
throw css : : container : : NoSuchElementException ( ) ;
return mxIndexAccess - > getByIndex ( mnIndex + + ) ;
}
private :
css : : uno : : Reference < css : : container : : XIndexAccess > mxIndexAccess ;
sal_Int32 mnIndex ;
} ;
2014-02-25 19:06:16 +01:00
2010-06-15 20:02:53 +02:00
/** A wrapper that holds a com.sun.star.container.XEnumeration or a
com . sun . star . container . XIndexAccess and provides an enumeration of VBA objects .
2011-03-25 10:40:25 +01:00
The method createCollectionObject ( ) needs to be implemented by the derived
class . This class can be used to convert an enumeration or an index container
2010-06-15 20:02:53 +02:00
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 ) ) { }
2014-03-27 18:12:18 +01:00
virtual sal_Bool SAL_CALL hasMoreElements ( ) throw ( css : : uno : : RuntimeException , std : : exception ) SAL_OVERRIDE
2010-06-15 20:02:53 +02:00
{
return mxEnumeration - > hasMoreElements ( ) ;
}
2014-03-27 18:12:18 +01:00
virtual css : : uno : : Any SAL_CALL nextElement ( ) throw ( css : : container : : NoSuchElementException , css : : lang : : WrappedTargetException , css : : uno : : RuntimeException , std : : exception ) SAL_OVERRIDE
2010-06-15 20:02:53 +02:00
{
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 ;
} ;
2014-02-25 19:06:16 +01:00
2010-06-15 20:02:53 +02:00
// deprecated, use SimpleEnumerationBase instead!
2009-09-18 15:35:47 +00:00
class VBAHELPER_DLLPUBLIC EnumerationHelperImpl : public EnumerationHelper_BASE
{
protected :
2010-07-01 16:23:26 +02:00
css : : uno : : WeakReference < ov : : XHelperInterface > m_xParent ;
2009-09-18 15:35:47 +00:00
css : : uno : : Reference < css : : uno : : XComponentContext > m_xContext ;
css : : uno : : Reference < css : : container : : XEnumeration > m_xEnumeration ;
public :
2010-07-01 16:23:26 +02:00
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 ) { }
2014-04-03 13:52:06 +02:00
virtual sal_Bool SAL_CALL hasMoreElements ( ) throw ( css : : uno : : RuntimeException , std : : exception ) SAL_OVERRIDE { return m_xEnumeration - > hasMoreElements ( ) ; }
2009-09-18 15:35:47 +00:00
} ;
// 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 >
2011-01-25 16:17:23 +01:00
class XNamedObjectCollectionHelper : public XNamedCollectionHelper_BASE
2009-09-18 15:35:47 +00:00
{
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 ( ) ) { }
2014-04-03 13:52:06 +02:00
virtual sal_Bool SAL_CALL hasMoreElements ( ) throw ( css : : uno : : RuntimeException , std : : exception ) SAL_OVERRIDE
2009-09-18 15:35:47 +00:00
{
return ( mIt ! = mXNamedVec . end ( ) ) ;
}
2014-03-27 18:12:18 +01:00
virtual css : : uno : : Any SAL_CALL nextElement ( ) throw ( css : : container : : NoSuchElementException , css : : lang : : WrappedTargetException , css : : uno : : RuntimeException , std : : exception ) SAL_OVERRIDE
2009-09-18 15:35:47 +00:00
{
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
2014-03-27 18:12:18 +01:00
virtual css : : uno : : Type SAL_CALL getElementType ( ) throw ( css : : uno : : RuntimeException , std : : exception ) SAL_OVERRIDE { return cppu : : UnoType < Ifc1 > : : get ( ) ; }
2014-04-03 13:52:06 +02:00
virtual sal_Bool SAL_CALL hasElements ( ) throw ( css : : uno : : RuntimeException , std : : exception ) SAL_OVERRIDE { return ( mXNamedVec . size ( ) > 0 ) ; }
2009-09-18 15:35:47 +00:00
// XNameAcess
2014-03-27 18:12:18 +01:00
virtual css : : uno : : Any SAL_CALL getByName ( const OUString & aName ) throw ( css : : container : : NoSuchElementException , css : : lang : : WrappedTargetException , css : : uno : : RuntimeException , std : : exception ) SAL_OVERRIDE
2009-09-18 15:35:47 +00:00
{
if ( ! hasByName ( aName ) )
throw css : : container : : NoSuchElementException ( ) ;
return css : : uno : : makeAny ( * cachePos ) ;
}
2014-03-27 18:12:18 +01:00
virtual css : : uno : : Sequence < OUString > SAL_CALL getElementNames ( ) throw ( css : : uno : : RuntimeException , std : : exception ) SAL_OVERRIDE
2009-09-18 15:35:47 +00:00
{
2013-04-07 12:06:47 +02:00
css : : uno : : Sequence < OUString > sNames ( mXNamedVec . size ( ) ) ;
OUString * pString = sNames . getArray ( ) ;
2009-09-18 15:35:47 +00:00
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 ;
}
2014-04-03 13:52:06 +02:00
virtual sal_Bool SAL_CALL hasByName ( const OUString & aName ) throw ( css : : uno : : RuntimeException , std : : exception ) SAL_OVERRIDE
2009-09-18 15:35:47 +00:00
{
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
2014-03-27 18:12:18 +01:00
virtual : : sal_Int32 SAL_CALL getCount ( ) throw ( css : : uno : : RuntimeException , std : : exception ) SAL_OVERRIDE { 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 ) SAL_OVERRIDE
2009-09-18 15:35:47 +00:00
{
if ( Index < 0 | | Index > = getCount ( ) )
throw css : : lang : : IndexOutOfBoundsException ( ) ;
return css : : uno : : makeAny ( mXNamedVec [ Index ] ) ;
}
// XEnumerationAccess
2014-03-27 18:12:18 +01:00
virtual css : : uno : : Reference < css : : container : : XEnumeration > SAL_CALL createEnumeration ( ) throw ( css : : uno : : RuntimeException , std : : exception ) SAL_OVERRIDE
2009-09-18 15:35:47 +00:00
{
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 ;
2014-04-01 14:35:59 +02:00
bool mbIgnoreCase ;
2009-09-18 15:35:47 +00:00
2013-04-07 12:06:47 +02:00
virtual css : : uno : : Any getItemByStringIndex ( const OUString & sIndex ) throw ( css : : uno : : RuntimeException )
2009-09-18 15:35:47 +00:00
{
if ( ! m_xNameAccess . is ( ) )
2013-06-29 21:24:12 +02:00
throw css : : uno : : RuntimeException ( " ScVbaCollectionBase string index access not supported by this object " , css : : uno : : Reference < css : : uno : : XInterface > ( ) ) ;
2009-09-18 15:35:47 +00:00
2010-10-06 10:16:27 +01:00
if ( mbIgnoreCase )
{
2013-04-07 12:06:47 +02:00
css : : uno : : Sequence < OUString > sElementNames = m_xNameAccess - > getElementNames ( ) ;
2010-10-06 10:16:27 +01:00
for ( sal_Int32 i = 0 ; i < sElementNames . getLength ( ) ; i + + )
{
2013-04-07 12:06:47 +02:00
OUString aName = sElementNames [ i ] ;
2010-10-06 10:16:27 +01:00
if ( aName . equalsIgnoreAsciiCase ( sIndex ) )
{
return createCollectionObject ( m_xNameAccess - > getByName ( aName ) ) ;
}
}
}
2009-09-18 15:35:47 +00:00
return createCollectionObject ( m_xNameAccess - > getByName ( sIndex ) ) ;
}
virtual css : : uno : : Any getItemByIntIndex ( const sal_Int32 nIndex ) throw ( css : : uno : : RuntimeException )
{
if ( ! m_xIndexAccess . is ( ) )
2013-06-29 21:24:12 +02:00
throw css : : uno : : RuntimeException ( " ScVbaCollectionBase numeric index access not supported by this object " , css : : uno : : Reference < css : : uno : : XInterface > ( ) ) ;
2009-09-18 15:35:47 +00:00
if ( nIndex < = 0 )
{
throw css : : lang : : IndexOutOfBoundsException (
2013-04-07 12:06:47 +02:00
OUString ( " index is 0 or negative " ) ,
2009-09-18 15:35:47 +00:00
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 ) ) ;
}
2010-06-15 20:02:53 +02:00
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 ;
}
2009-09-18 15:35:47 +00:00
public :
2014-04-01 14:35:59 +02:00
ScVbaCollectionBase ( 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 ) : BaseColBase ( xParent , xContext ) , m_xIndexAccess ( xIndexAccess ) , mbIgnoreCase ( bIgnoreCase ) { m_xNameAccess . set ( m_xIndexAccess , css : : uno : : UNO_QUERY ) ; }
2009-09-18 15:35:47 +00:00
//XCollection
virtual : : sal_Int32 SAL_CALL getCount ( ) throw ( css : : uno : : RuntimeException )
{
return m_xIndexAccess - > getCount ( ) ;
}
2014-04-05 20:48:43 +01:00
virtual css : : uno : : Any SAL_CALL Item ( const css : : uno : : Any & Index1 , const css : : uno : : Any & /*not processed in this base class*/ ) throw ( css : : script : : BasicErrorException , css : : uno : : RuntimeException )
2009-09-18 15:35:47 +00:00
{
if ( Index1 . getValueTypeClass ( ) ! = css : : uno : : TypeClass_STRING )
{
sal_Int32 nIndex = 0 ;
2014-01-21 23:17:22 +01:00
if ( ! ( Index1 > > = nIndex ) )
2009-09-18 15:35:47 +00:00
{
2013-04-07 12:06:47 +02:00
OUString message ;
message = OUString ( " Couldn't convert index to Int32 " ) ;
2009-09-18 15:35:47 +00:00
throw css : : lang : : IndexOutOfBoundsException ( message ,
css : : uno : : Reference < css : : uno : : XInterface > ( ) ) ;
}
return getItemByIntIndex ( nIndex ) ;
}
2013-04-07 12:06:47 +02:00
OUString aStringSheet ;
2009-09-18 15:35:47 +00:00
Index1 > > = aStringSheet ;
return getItemByStringIndex ( aStringSheet ) ;
}
// XDefaultMethod
2013-04-07 12:06:47 +02:00
OUString SAL_CALL getDefaultMethodName ( ) throw ( css : : uno : : RuntimeException )
2009-09-18 15:35:47 +00:00
{
2014-04-06 17:16:41 +02:00
return " Item " ;
2009-09-18 15:35:47 +00:00
}
// 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
2014-04-03 13:52:06 +02:00
virtual sal_Bool SAL_CALL hasElements ( ) throw ( css : : uno : : RuntimeException )
2009-09-18 15:35:47 +00:00
{
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
2011-01-25 16:17:23 +01:00
class CollTestImplHelper : public ScVbaCollectionBase < : : cppu : : WeakImplHelper1 < Ifc > >
2009-09-18 15:35:47 +00:00
{
typedef ScVbaCollectionBase < : : cppu : : WeakImplHelper1 < Ifc > > ImplBase1 ;
public :
2014-04-01 14:35:59 +02:00
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 ) throw ( css : : uno : : RuntimeException ) : ImplBase1 ( xParent , xContext , xIndexAccess , bIgnoreCase ) { }
2009-09-18 15:35:47 +00:00
} ;
# endif //SC_VBA_COLLECTION_IMPL_HXX
2010-10-27 13:11:31 +01:00
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */