1*b1cdbd2cSJim Jagielski /************************************************************** 2*b1cdbd2cSJim Jagielski * 3*b1cdbd2cSJim Jagielski * Licensed to the Apache Software Foundation (ASF) under one 4*b1cdbd2cSJim Jagielski * or more contributor license agreements. See the NOTICE file 5*b1cdbd2cSJim Jagielski * distributed with this work for additional information 6*b1cdbd2cSJim Jagielski * regarding copyright ownership. The ASF licenses this file 7*b1cdbd2cSJim Jagielski * to you under the Apache License, Version 2.0 (the 8*b1cdbd2cSJim Jagielski * "License"); you may not use this file except in compliance 9*b1cdbd2cSJim Jagielski * with the License. You may obtain a copy of the License at 10*b1cdbd2cSJim Jagielski * 11*b1cdbd2cSJim Jagielski * http://www.apache.org/licenses/LICENSE-2.0 12*b1cdbd2cSJim Jagielski * 13*b1cdbd2cSJim Jagielski * Unless required by applicable law or agreed to in writing, 14*b1cdbd2cSJim Jagielski * software distributed under the License is distributed on an 15*b1cdbd2cSJim Jagielski * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*b1cdbd2cSJim Jagielski * KIND, either express or implied. See the License for the 17*b1cdbd2cSJim Jagielski * specific language governing permissions and limitations 18*b1cdbd2cSJim Jagielski * under the License. 19*b1cdbd2cSJim Jagielski * 20*b1cdbd2cSJim Jagielski *************************************************************/ 21*b1cdbd2cSJim Jagielski 22*b1cdbd2cSJim Jagielski 23*b1cdbd2cSJim Jagielski 24*b1cdbd2cSJim Jagielski #ifndef VBAHELPER_COLLECTIONBASE_HXX 25*b1cdbd2cSJim Jagielski #define VBAHELPER_COLLECTIONBASE_HXX 26*b1cdbd2cSJim Jagielski 27*b1cdbd2cSJim Jagielski #include <vector> 28*b1cdbd2cSJim Jagielski #include <com/sun/star/beans/NamedValue.hpp> 29*b1cdbd2cSJim Jagielski #include <com/sun/star/container/XElementAccess.hpp> 30*b1cdbd2cSJim Jagielski #include <com/sun/star/container/XNamed.hpp> 31*b1cdbd2cSJim Jagielski #include <ooo/vba/XCollectionBase.hpp> 32*b1cdbd2cSJim Jagielski #include <cppuhelper/implbase1.hxx> 33*b1cdbd2cSJim Jagielski #include <vbahelper/vbahelper.hxx> 34*b1cdbd2cSJim Jagielski 35*b1cdbd2cSJim Jagielski namespace vbahelper { 36*b1cdbd2cSJim Jagielski 37*b1cdbd2cSJim Jagielski // ============================================================================ 38*b1cdbd2cSJim Jagielski 39*b1cdbd2cSJim Jagielski typedef ::cppu::WeakImplHelper1< ov::XCollectionBase > CollectionBase_BASE; 40*b1cdbd2cSJim Jagielski 41*b1cdbd2cSJim Jagielski /** Base class of VBA objects implementing the VBA collection concept. 42*b1cdbd2cSJim Jagielski 43*b1cdbd2cSJim Jagielski This base class intentionally does not include the interface 44*b1cdbd2cSJim Jagielski XHelperInterface supported by all application VBA object. There may be 45*b1cdbd2cSJim Jagielski other VBA objects that do not support the special methods provided by 46*b1cdbd2cSJim Jagielski XHelperInterface. 47*b1cdbd2cSJim Jagielski */ 48*b1cdbd2cSJim Jagielski class VBAHELPER_DLLPUBLIC CollectionBase : public CollectionBase_BASE 49*b1cdbd2cSJim Jagielski { 50*b1cdbd2cSJim Jagielski public: 51*b1cdbd2cSJim Jagielski /** Enumerates different container types a VBA collection can be based on. */ 52*b1cdbd2cSJim Jagielski enum ContainerType 53*b1cdbd2cSJim Jagielski { 54*b1cdbd2cSJim Jagielski /** Container elements are VBA items. 55*b1cdbd2cSJim Jagielski 56*b1cdbd2cSJim Jagielski The initial container contains the final VBA items provided by the 57*b1cdbd2cSJim Jagielski VBA collection. No conversion takes place on item access. 58*b1cdbd2cSJim Jagielski */ 59*b1cdbd2cSJim Jagielski CONTAINER_NATIVE_VBA, 60*b1cdbd2cSJim Jagielski 61*b1cdbd2cSJim Jagielski /** Container elements will be converted to VBA items on demand. 62*b1cdbd2cSJim Jagielski 63*b1cdbd2cSJim Jagielski The initial container contains intermediate objects (e.g. UNO 64*b1cdbd2cSJim Jagielski objects) which will be converted to VBA items everytime the item is 65*b1cdbd2cSJim Jagielski accessed (e.g. item access method, enumeration). Changes in the 66*b1cdbd2cSJim Jagielski initial container are reflected by the collection. 67*b1cdbd2cSJim Jagielski */ 68*b1cdbd2cSJim Jagielski CONTAINER_CONVERT_ON_DEMAND, 69*b1cdbd2cSJim Jagielski }; 70*b1cdbd2cSJim Jagielski 71*b1cdbd2cSJim Jagielski // ------------------------------------------------------------------------ 72*b1cdbd2cSJim Jagielski 73*b1cdbd2cSJim Jagielski CollectionBase( const css::uno::Type& rElementType ); 74*b1cdbd2cSJim Jagielski 75*b1cdbd2cSJim Jagielski // ------------------------------------------------------------------------ 76*b1cdbd2cSJim Jagielski 77*b1cdbd2cSJim Jagielski // attributes 78*b1cdbd2cSJim Jagielski virtual sal_Int32 SAL_CALL getCount() throw (css::uno::RuntimeException); 79*b1cdbd2cSJim Jagielski // XEnumerationAccess 80*b1cdbd2cSJim Jagielski virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() throw (css::uno::RuntimeException); 81*b1cdbd2cSJim Jagielski // XElementAccess 82*b1cdbd2cSJim Jagielski virtual css::uno::Type SAL_CALL getElementType() throw (css::uno::RuntimeException); 83*b1cdbd2cSJim Jagielski virtual sal_Bool SAL_CALL hasElements() throw (css::uno::RuntimeException); 84*b1cdbd2cSJim Jagielski // XDefaultMethod 85*b1cdbd2cSJim Jagielski virtual ::rtl::OUString SAL_CALL getDefaultMethodName() throw (css::uno::RuntimeException); 86*b1cdbd2cSJim Jagielski 87*b1cdbd2cSJim Jagielski // ------------------------------------------------------------------------ 88*b1cdbd2cSJim Jagielski 89*b1cdbd2cSJim Jagielski /** Associates this collection with the passed UNO container. 90*b1cdbd2cSJim Jagielski 91*b1cdbd2cSJim Jagielski @param rxElementAccess 92*b1cdbd2cSJim Jagielski The UNO container with the elements of this collection. Shall 93*b1cdbd2cSJim Jagielski support either XIndexAccess or XNameAccess, may support both. 94*b1cdbd2cSJim Jagielski 95*b1cdbd2cSJim Jagielski If the container does not support XIndexAccess, index access is 96*b1cdbd2cSJim Jagielski simulated based on the order returned by the function 97*b1cdbd2cSJim Jagielski XNameAccess::getElementNames(). 98*b1cdbd2cSJim Jagielski 99*b1cdbd2cSJim Jagielski If the container does not support XNameAccess, name access is 100*b1cdbd2cSJim Jagielski simulated by iterating the elements via index access and asking the 101*b1cdbd2cSJim Jagielski elements for their name via the interface XNamed. If the elements 102*b1cdbd2cSJim Jagielski do not support XNamed, the elements cannot be accessed by name. 103*b1cdbd2cSJim Jagielski 104*b1cdbd2cSJim Jagielski @param eContainerType 105*b1cdbd2cSJim Jagielski Specifies the type of the passed container. 106*b1cdbd2cSJim Jagielski */ 107*b1cdbd2cSJim Jagielski void initContainer( 108*b1cdbd2cSJim Jagielski const css::uno::Reference< css::container::XElementAccess >& rxElementAccess, 109*b1cdbd2cSJim Jagielski ContainerType eContainerType ) throw (css::uno::RuntimeException); 110*b1cdbd2cSJim Jagielski 111*b1cdbd2cSJim Jagielski /** Initializes this collection with copies of all elements in the passed 112*b1cdbd2cSJim Jagielski temporary STL vector. 113*b1cdbd2cSJim Jagielski 114*b1cdbd2cSJim Jagielski @param rElements 115*b1cdbd2cSJim Jagielski The STL vector with the named elements of this collection. 116*b1cdbd2cSJim Jagielski @param eContainerType 117*b1cdbd2cSJim Jagielski Specifies the type of the passed vector. 118*b1cdbd2cSJim Jagielski */ 119*b1cdbd2cSJim Jagielski void initElements( 120*b1cdbd2cSJim Jagielski const ::std::vector< css::uno::Reference< css::container::XNamed > >& rElements, 121*b1cdbd2cSJim Jagielski ContainerType eContainerType ) throw (css::uno::RuntimeException); 122*b1cdbd2cSJim Jagielski 123*b1cdbd2cSJim Jagielski /** Initializes this collection with copies of all elements in the passed 124*b1cdbd2cSJim Jagielski temporary STL vector. 125*b1cdbd2cSJim Jagielski 126*b1cdbd2cSJim Jagielski @param rElements 127*b1cdbd2cSJim Jagielski The STL vector with the named elements of this collection. 128*b1cdbd2cSJim Jagielski @param eContainerType 129*b1cdbd2cSJim Jagielski Specifies the type of the passed vector. 130*b1cdbd2cSJim Jagielski */ 131*b1cdbd2cSJim Jagielski void initElements( 132*b1cdbd2cSJim Jagielski const ::std::vector< css::beans::NamedValue >& rElements, 133*b1cdbd2cSJim Jagielski ContainerType eContainerType ) throw (css::uno::RuntimeException); 134*b1cdbd2cSJim Jagielski 135*b1cdbd2cSJim Jagielski /** Returns a VBA implementation object from the passed element. 136*b1cdbd2cSJim Jagielski 137*b1cdbd2cSJim Jagielski If the container type is CONTAINER_NATIVE_VBA, returns the passed 138*b1cdbd2cSJim Jagielski object unmodified. If the container type is CONTAINER_CONVERT_ON_DEMAND, 139*b1cdbd2cSJim Jagielski calls the virtual function implCreateCollectionItem() that implements 140*b1cdbd2cSJim Jagielski creation of the VBA implmentation object. 141*b1cdbd2cSJim Jagielski 142*b1cdbd2cSJim Jagielski @param rElement 143*b1cdbd2cSJim Jagielski The container element the VBA implementation object is based on. 144*b1cdbd2cSJim Jagielski 145*b1cdbd2cSJim Jagielski @param rIndex 146*b1cdbd2cSJim Jagielski The index or name that has been used to access the item. 147*b1cdbd2cSJim Jagielski */ 148*b1cdbd2cSJim Jagielski css::uno::Any createCollectionItem( 149*b1cdbd2cSJim Jagielski const css::uno::Any& rElement, 150*b1cdbd2cSJim Jagielski const css::uno::Any& rIndex ) throw (css::uno::RuntimeException); 151*b1cdbd2cSJim Jagielski 152*b1cdbd2cSJim Jagielski /** Returns a collection item specified by its one-based item index. 153*b1cdbd2cSJim Jagielski 154*b1cdbd2cSJim Jagielski @param nIndex 155*b1cdbd2cSJim Jagielski The one-based index of the collection item. 156*b1cdbd2cSJim Jagielski */ 157*b1cdbd2cSJim Jagielski css::uno::Any getItemByIndex( sal_Int32 nIndex ) throw (css::uno::RuntimeException); 158*b1cdbd2cSJim Jagielski 159*b1cdbd2cSJim Jagielski /** Returns a collection item specified by its name. 160*b1cdbd2cSJim Jagielski 161*b1cdbd2cSJim Jagielski @param rName 162*b1cdbd2cSJim Jagielski The name of the collection item. 163*b1cdbd2cSJim Jagielski */ 164*b1cdbd2cSJim Jagielski css::uno::Any getItemByName( const ::rtl::OUString& rName ) throw (css::uno::RuntimeException); 165*b1cdbd2cSJim Jagielski 166*b1cdbd2cSJim Jagielski /** Returns a collection item specified by its index or name. 167*b1cdbd2cSJim Jagielski 168*b1cdbd2cSJim Jagielski @param rIndex 169*b1cdbd2cSJim Jagielski The index or name of the collection item. May be empty, in that 170*b1cdbd2cSJim Jagielski case the entire collection is returned. 171*b1cdbd2cSJim Jagielski */ 172*b1cdbd2cSJim Jagielski css::uno::Any getAnyItemOrThis( const css::uno::Any& rIndex ) throw (css::uno::RuntimeException); 173*b1cdbd2cSJim Jagielski 174*b1cdbd2cSJim Jagielski /** Returns a collection item of a specific type specified by its index or 175*b1cdbd2cSJim Jagielski name. 176*b1cdbd2cSJim Jagielski 177*b1cdbd2cSJim Jagielski @param rIndex 178*b1cdbd2cSJim Jagielski The index or name of the collection item. 179*b1cdbd2cSJim Jagielski */ 180*b1cdbd2cSJim Jagielski template< typename XType > getAnyItem(const css::uno::Any & rIndex)181*b1cdbd2cSJim Jagielski inline css::uno::Reference< XType > getAnyItem( const css::uno::Any& rIndex ) throw (css::uno::RuntimeException) 182*b1cdbd2cSJim Jagielski { css::uno::Any aRet; if( rIndex.hasValue() ) aRet = getAnyItemOrThis( rIndex ); return css::uno::Reference< XType >( aRet, css::uno::UNO_QUERY_THROW ); } 183*b1cdbd2cSJim Jagielski 184*b1cdbd2cSJim Jagielski protected: 185*b1cdbd2cSJim Jagielski /** Derived classes implement creation of a VBA implementation object from 186*b1cdbd2cSJim Jagielski the passed intermediate container element. 187*b1cdbd2cSJim Jagielski 188*b1cdbd2cSJim Jagielski May be kept unimplemented if container type is CONTAINER_NATIVE_VBA. 189*b1cdbd2cSJim Jagielski 190*b1cdbd2cSJim Jagielski @param rElement 191*b1cdbd2cSJim Jagielski The container element the VBA implementation object is based on. 192*b1cdbd2cSJim Jagielski 193*b1cdbd2cSJim Jagielski @param rIndex 194*b1cdbd2cSJim Jagielski The index or name used to access the item. Can be used by 195*b1cdbd2cSJim Jagielski implementations as a hint how to find or convert the VBA object. 196*b1cdbd2cSJim Jagielski */ 197*b1cdbd2cSJim Jagielski virtual css::uno::Any implCreateCollectionItem( const css::uno::Any& rElement, const css::uno::Any& rIndex ) throw (css::uno::RuntimeException); 198*b1cdbd2cSJim Jagielski 199*b1cdbd2cSJim Jagielski private: 200*b1cdbd2cSJim Jagielski css::uno::Reference< css::container::XIndexAccess > mxIndexAccess; 201*b1cdbd2cSJim Jagielski css::uno::Reference< css::container::XNameAccess > mxNameAccess; 202*b1cdbd2cSJim Jagielski css::uno::Type maElementType; 203*b1cdbd2cSJim Jagielski bool mbConvertOnDemand; 204*b1cdbd2cSJim Jagielski }; 205*b1cdbd2cSJim Jagielski 206*b1cdbd2cSJim Jagielski // ============================================================================ 207*b1cdbd2cSJim Jagielski 208*b1cdbd2cSJim Jagielski } // namespace vbahelper 209*b1cdbd2cSJim Jagielski 210*b1cdbd2cSJim Jagielski #endif 211