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