xref: /aoo42x/main/forms/source/xforms/collection.hxx (revision 1738ad43)
12d785d7eSAndrew Rist /**************************************************************
2*1738ad43Smseidel  *
32d785d7eSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
42d785d7eSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
52d785d7eSAndrew Rist  * distributed with this work for additional information
62d785d7eSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
72d785d7eSAndrew Rist  * to you under the Apache License, Version 2.0 (the
82d785d7eSAndrew Rist  * "License"); you may not use this file except in compliance
92d785d7eSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*1738ad43Smseidel  *
112d785d7eSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*1738ad43Smseidel  *
132d785d7eSAndrew Rist  * Unless required by applicable law or agreed to in writing,
142d785d7eSAndrew Rist  * software distributed under the License is distributed on an
152d785d7eSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
162d785d7eSAndrew Rist  * KIND, either express or implied.  See the License for the
172d785d7eSAndrew Rist  * specific language governing permissions and limitations
182d785d7eSAndrew Rist  * under the License.
19*1738ad43Smseidel  *
202d785d7eSAndrew Rist  *************************************************************/
212d785d7eSAndrew Rist 
222d785d7eSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef _COLLECTION_HXX
25cdf0e10cSrcweir #define _COLLECTION_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "enumeration.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <cppuhelper/implbase3.hxx>
30cdf0e10cSrcweir #include <com/sun/star/container/ElementExistException.hpp>
31cdf0e10cSrcweir #include <com/sun/star/container/NoSuchElementException.hpp>
32cdf0e10cSrcweir #include <com/sun/star/container/XEnumeration.hpp>
33cdf0e10cSrcweir #include <com/sun/star/container/XIndexReplace.hpp>
34cdf0e10cSrcweir #include <com/sun/star/container/XSet.hpp>
35cdf0e10cSrcweir #include <com/sun/star/container/XContainer.hpp>
36cdf0e10cSrcweir #include <com/sun/star/container/XContainerListener.hpp>
37cdf0e10cSrcweir #include <com/sun/star/lang/IllegalArgumentException.hpp>
38cdf0e10cSrcweir #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
39cdf0e10cSrcweir #include <com/sun/star/lang/WrappedTargetException.hpp>
40cdf0e10cSrcweir #include <com/sun/star/uno/Any.hxx>
41cdf0e10cSrcweir #include <com/sun/star/uno/Reference.hxx>
42cdf0e10cSrcweir #include <com/sun/star/uno/RuntimeException.hpp>
43cdf0e10cSrcweir #include <com/sun/star/uno/Type.hxx>
44cdf0e10cSrcweir #include <vector>
45cdf0e10cSrcweir #include <algorithm>
46cdf0e10cSrcweir 
47cdf0e10cSrcweir 
48cdf0e10cSrcweir typedef cppu::WeakImplHelper3<
49cdf0e10cSrcweir     com::sun::star::container::XIndexReplace,
50cdf0e10cSrcweir     com::sun::star::container::XSet,
51cdf0e10cSrcweir     com::sun::star::container::XContainer>
52cdf0e10cSrcweir Collection_t;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir template<class ELEMENT_TYPE>
55cdf0e10cSrcweir class Collection : public Collection_t
56cdf0e10cSrcweir {
57cdf0e10cSrcweir public:
58cdf0e10cSrcweir     typedef ELEMENT_TYPE T;
59cdf0e10cSrcweir     typedef com::sun::star::uno::Reference<com::sun::star::container::XContainerListener> XContainerListener_t;
60cdf0e10cSrcweir     typedef std::vector<XContainerListener_t> Listeners_t;
61cdf0e10cSrcweir 
62cdf0e10cSrcweir protected:
63cdf0e10cSrcweir     std::vector<T> maItems;
64cdf0e10cSrcweir     Listeners_t maListeners;
65cdf0e10cSrcweir 
66cdf0e10cSrcweir public:
67cdf0e10cSrcweir 
Collection()68cdf0e10cSrcweir     Collection() {}
~Collection()69cdf0e10cSrcweir     virtual ~Collection() {}
70cdf0e10cSrcweir 
getItem(sal_Int32 n) const71cdf0e10cSrcweir     const T& getItem( sal_Int32 n ) const
72cdf0e10cSrcweir     {
73cdf0e10cSrcweir         OSL_ENSURE( isValidIndex(n), "invalid index" );
74cdf0e10cSrcweir         OSL_ENSURE( isValid( maItems[n] ), "invalid item found" );
75cdf0e10cSrcweir         return maItems[n];
76cdf0e10cSrcweir     }
77cdf0e10cSrcweir 
setItem(sal_Int32 n,const T & t)78cdf0e10cSrcweir     void setItem( sal_Int32 n, const T& t)
79cdf0e10cSrcweir     {
80cdf0e10cSrcweir         OSL_ENSURE( isValidIndex(n), "invalid index" );
81cdf0e10cSrcweir         OSL_ENSURE( isValid ( t ), "invalid item" );
82cdf0e10cSrcweir 
83cdf0e10cSrcweir         T& aRef = maItems[ n ];
84cdf0e10cSrcweir         _elementReplaced( n, t );
85cdf0e10cSrcweir         _remove( aRef );
86cdf0e10cSrcweir         aRef = t;
87cdf0e10cSrcweir         _insert( t );
88cdf0e10cSrcweir     }
89cdf0e10cSrcweir 
hasItem(const T & t) const90cdf0e10cSrcweir     bool hasItem( const T& t ) const
91cdf0e10cSrcweir     {
92cdf0e10cSrcweir         return maItems.end() != std::find( maItems.begin(), maItems.end(), t );
93cdf0e10cSrcweir     }
94cdf0e10cSrcweir 
addItem(const T & t)95cdf0e10cSrcweir     sal_Int32 addItem( const T& t )
96cdf0e10cSrcweir     {
97cdf0e10cSrcweir         OSL_ENSURE( !hasItem( t ), "item to be added already present" );
98cdf0e10cSrcweir         OSL_ENSURE( isValid( t ), "invalid item" );
99cdf0e10cSrcweir 
100cdf0e10cSrcweir         maItems.push_back( t );
101cdf0e10cSrcweir         _insert( t );
102cdf0e10cSrcweir         _elementInserted( maItems.size() - 1 );
103cdf0e10cSrcweir         return ( maItems.size() - 1 );
104cdf0e10cSrcweir     }
105cdf0e10cSrcweir 
removeItem(const T & t)106cdf0e10cSrcweir     void removeItem( const T& t )
107cdf0e10cSrcweir     {
108cdf0e10cSrcweir         OSL_ENSURE( hasItem( t ), "item to be removed not present" );
109cdf0e10cSrcweir         OSL_ENSURE( isValid( t ), "an invalid item, funny that!" );
110cdf0e10cSrcweir 
111cdf0e10cSrcweir         _elementRemoved( t );
112cdf0e10cSrcweir         _remove( t );
113cdf0e10cSrcweir         maItems.erase( std::find( maItems.begin(), maItems.end(), t ) );
114cdf0e10cSrcweir     }
115cdf0e10cSrcweir 
hasItems() const116cdf0e10cSrcweir     bool hasItems() const
117cdf0e10cSrcweir     {
118cdf0e10cSrcweir         return maItems.size() != 0;
119cdf0e10cSrcweir     }
120cdf0e10cSrcweir 
countItems() const121cdf0e10cSrcweir     sal_Int32 countItems() const
122cdf0e10cSrcweir     {
123cdf0e10cSrcweir         return static_cast<sal_Int32>( maItems.size() );
124cdf0e10cSrcweir     }
125cdf0e10cSrcweir 
isValidIndex(sal_Int32 n) const126cdf0e10cSrcweir     bool isValidIndex( sal_Int32 n ) const
127cdf0e10cSrcweir     {
128*1738ad43Smseidel         return n >= 0 && n < static_cast<sal_Int32>( maItems.size() );
129cdf0e10cSrcweir     }
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 
132*1738ad43Smseidel     // the following method may be overridden by sub-classes for
133*1738ad43Smseidel     // customized behavior
134cdf0e10cSrcweir 
135cdf0e10cSrcweir     /// called before insertion to determine whether item is valid
isValid(const T &) const136cdf0e10cSrcweir     virtual bool isValid( const T& ) const { return true; }
137cdf0e10cSrcweir 
138cdf0e10cSrcweir 
139cdf0e10cSrcweir protected:
140cdf0e10cSrcweir 
141*1738ad43Smseidel     // the following methods may be overridden by sub-classes for
142*1738ad43Smseidel     // customized behavior
143cdf0e10cSrcweir 
144cdf0e10cSrcweir     /// called after item has been inserted into the collection
_insert(const T &)145cdf0e10cSrcweir     virtual void _insert( const T& ) { }
146cdf0e10cSrcweir 
147cdf0e10cSrcweir     /// called before item is removed from the collection
_remove(const T &)148cdf0e10cSrcweir     virtual void _remove( const T& ) { }
149cdf0e10cSrcweir 
150cdf0e10cSrcweir public:
151cdf0e10cSrcweir 
152cdf0e10cSrcweir     typedef com::sun::star::uno::Type Type_t;
153cdf0e10cSrcweir     typedef com::sun::star::uno::Any Any_t;
154cdf0e10cSrcweir     typedef com::sun::star::uno::RuntimeException RuntimeException_t;
155cdf0e10cSrcweir     typedef com::sun::star::lang::IllegalArgumentException IllegalArgumentException_t;
156cdf0e10cSrcweir     typedef com::sun::star::container::NoSuchElementException NoSuchElementException_t;
157cdf0e10cSrcweir     typedef com::sun::star::lang::IndexOutOfBoundsException IndexOutOfBoundsException_t;
158cdf0e10cSrcweir     typedef com::sun::star::uno::Reference<com::sun::star::container::XEnumeration> XEnumeration_t;
159cdf0e10cSrcweir     typedef com::sun::star::lang::WrappedTargetException WrappedTargetException_t;
160cdf0e10cSrcweir     typedef com::sun::star::container::ElementExistException ElementExistException_t;
161cdf0e10cSrcweir 
162cdf0e10cSrcweir 
163cdf0e10cSrcweir     // XElementAccess
getElementType()164*1738ad43Smseidel     virtual Type_t SAL_CALL getElementType()
165cdf0e10cSrcweir         throw( RuntimeException_t )
166cdf0e10cSrcweir     {
167cdf0e10cSrcweir         return getCppuType( static_cast<T*>( NULL ) );
168cdf0e10cSrcweir     }
169cdf0e10cSrcweir 
hasElements()170*1738ad43Smseidel     virtual sal_Bool SAL_CALL hasElements()
171cdf0e10cSrcweir         throw( RuntimeException_t )
172cdf0e10cSrcweir     {
173cdf0e10cSrcweir         return hasItems();
174cdf0e10cSrcweir     }
175cdf0e10cSrcweir 
176cdf0e10cSrcweir     // XIndexAccess : XElementAccess
getCount()177*1738ad43Smseidel     virtual sal_Int32 SAL_CALL getCount()
178cdf0e10cSrcweir         throw( RuntimeException_t )
179cdf0e10cSrcweir     {
180cdf0e10cSrcweir         return countItems();
181cdf0e10cSrcweir     }
182cdf0e10cSrcweir 
getByIndex(sal_Int32 nIndex)183*1738ad43Smseidel     virtual Any_t SAL_CALL getByIndex( sal_Int32 nIndex )
184*1738ad43Smseidel         throw( IndexOutOfBoundsException_t,
185*1738ad43Smseidel                WrappedTargetException_t,
186cdf0e10cSrcweir                RuntimeException_t)
187cdf0e10cSrcweir     {
188cdf0e10cSrcweir         if( isValidIndex( nIndex ) )
189cdf0e10cSrcweir             return com::sun::star::uno::makeAny( getItem( nIndex ) );
190cdf0e10cSrcweir         else
191cdf0e10cSrcweir             throw IndexOutOfBoundsException_t();
192cdf0e10cSrcweir     }
193cdf0e10cSrcweir 
194cdf0e10cSrcweir     // XIndexReplace : XIndexAccess
replaceByIndex(sal_Int32 nIndex,const Any_t & aElement)195*1738ad43Smseidel     virtual void SAL_CALL replaceByIndex( sal_Int32 nIndex,
196*1738ad43Smseidel                                           const Any_t& aElement )
197*1738ad43Smseidel         throw( IllegalArgumentException_t,
198*1738ad43Smseidel                IndexOutOfBoundsException_t,
199*1738ad43Smseidel                WrappedTargetException_t,
200cdf0e10cSrcweir                RuntimeException_t)
201cdf0e10cSrcweir     {
202cdf0e10cSrcweir         T t;
203cdf0e10cSrcweir         if( isValidIndex( nIndex) )
204*1738ad43Smseidel             if( ( aElement >>= t ) && isValid( t ) )
205cdf0e10cSrcweir                 setItem( nIndex, t );
206cdf0e10cSrcweir             else
207cdf0e10cSrcweir                 throw IllegalArgumentException_t();
208cdf0e10cSrcweir         else
209cdf0e10cSrcweir             throw IndexOutOfBoundsException_t();
210cdf0e10cSrcweir     }
211cdf0e10cSrcweir 
212cdf0e10cSrcweir     // XEnumerationAccess : XElementAccess
createEnumeration()213cdf0e10cSrcweir     virtual XEnumeration_t SAL_CALL createEnumeration()
214cdf0e10cSrcweir         throw( RuntimeException_t )
215cdf0e10cSrcweir     {
216cdf0e10cSrcweir         return new Enumeration( this );
217cdf0e10cSrcweir     }
218cdf0e10cSrcweir 
219*1738ad43Smseidel 
220cdf0e10cSrcweir     // XSet : XEnumerationAccess
has(const Any_t & aElement)221*1738ad43Smseidel     virtual sal_Bool SAL_CALL has( const Any_t& aElement )
222cdf0e10cSrcweir         throw( RuntimeException_t )
223cdf0e10cSrcweir     {
224cdf0e10cSrcweir         T t;
225cdf0e10cSrcweir         return ( aElement >>= t ) ? hasItem( t ) : sal_False;
226cdf0e10cSrcweir     }
227*1738ad43Smseidel 
insert(const Any_t & aElement)228cdf0e10cSrcweir     virtual void SAL_CALL insert( const Any_t& aElement )
229cdf0e10cSrcweir         throw( IllegalArgumentException_t,
230*1738ad43Smseidel                ElementExistException_t,
231cdf0e10cSrcweir                RuntimeException_t )
232cdf0e10cSrcweir     {
233cdf0e10cSrcweir         T t;
234*1738ad43Smseidel         if( ( aElement >>= t ) && isValid( t ) )
235cdf0e10cSrcweir             if( ! hasItem( t ) )
236cdf0e10cSrcweir                 addItem( t );
237cdf0e10cSrcweir             else
238cdf0e10cSrcweir                 throw ElementExistException_t();
239cdf0e10cSrcweir         else
240cdf0e10cSrcweir             throw IllegalArgumentException_t();
241cdf0e10cSrcweir     }
242cdf0e10cSrcweir 
remove(const Any_t & aElement)243cdf0e10cSrcweir     virtual void SAL_CALL remove( const Any_t& aElement )
244cdf0e10cSrcweir         throw( IllegalArgumentException_t,
245cdf0e10cSrcweir                NoSuchElementException_t,
246cdf0e10cSrcweir                RuntimeException_t )
247cdf0e10cSrcweir     {
248cdf0e10cSrcweir         T t;
249cdf0e10cSrcweir         if( aElement >>= t )
250cdf0e10cSrcweir             if( hasItem( t ) )
251cdf0e10cSrcweir                 removeItem( t );
252cdf0e10cSrcweir             else
253cdf0e10cSrcweir                 throw NoSuchElementException_t();
254cdf0e10cSrcweir         else
255cdf0e10cSrcweir             throw IllegalArgumentException_t();
256cdf0e10cSrcweir     }
257cdf0e10cSrcweir 
258cdf0e10cSrcweir 
259cdf0e10cSrcweir     // XContainer
addContainerListener(const XContainerListener_t & xListener)260*1738ad43Smseidel     virtual void SAL_CALL addContainerListener(
261cdf0e10cSrcweir         const XContainerListener_t& xListener )
262cdf0e10cSrcweir         throw( RuntimeException_t )
263cdf0e10cSrcweir     {
264cdf0e10cSrcweir         OSL_ENSURE( xListener.is(), "need listener!" );
265cdf0e10cSrcweir         if( std::find( maListeners.begin(), maListeners.end(), xListener)
266cdf0e10cSrcweir             == maListeners.end() )
267cdf0e10cSrcweir             maListeners.push_back( xListener );
268cdf0e10cSrcweir     }
269cdf0e10cSrcweir 
removeContainerListener(const XContainerListener_t & xListener)270cdf0e10cSrcweir     virtual void SAL_CALL removeContainerListener(
271cdf0e10cSrcweir         const XContainerListener_t& xListener )
272cdf0e10cSrcweir         throw( RuntimeException_t )
273cdf0e10cSrcweir     {
274cdf0e10cSrcweir         OSL_ENSURE( xListener.is(), "need listener!" );
275*1738ad43Smseidel         Listeners_t::iterator aIter =
276cdf0e10cSrcweir             std::find( maListeners.begin(), maListeners.end(), xListener );
277cdf0e10cSrcweir         if( aIter != maListeners.end() )
278cdf0e10cSrcweir             maListeners.erase( aIter );
279cdf0e10cSrcweir     }
280cdf0e10cSrcweir 
281cdf0e10cSrcweir protected:
282cdf0e10cSrcweir 
283cdf0e10cSrcweir     // call listeners:
_elementInserted(sal_Int32 nPos)284cdf0e10cSrcweir     void _elementInserted( sal_Int32 nPos )
285cdf0e10cSrcweir     {
286cdf0e10cSrcweir         OSL_ENSURE( isValidIndex(nPos), "invalid index" );
287*1738ad43Smseidel         com::sun::star::container::ContainerEvent aEvent(
288cdf0e10cSrcweir             static_cast<com::sun::star::container::XIndexReplace*>( this ),
289cdf0e10cSrcweir             com::sun::star::uno::makeAny( nPos ),
290cdf0e10cSrcweir             com::sun::star::uno::makeAny( getItem( nPos ) ),
291cdf0e10cSrcweir             com::sun::star::uno::Any() );
292cdf0e10cSrcweir         for( Listeners_t::iterator aIter = maListeners.begin();
293cdf0e10cSrcweir              aIter != maListeners.end();
294cdf0e10cSrcweir              aIter++ )
295cdf0e10cSrcweir         {
296cdf0e10cSrcweir             (*aIter)->elementInserted( aEvent );
297cdf0e10cSrcweir         }
298cdf0e10cSrcweir     }
299cdf0e10cSrcweir 
_elementRemoved(const T & aOld)300cdf0e10cSrcweir     void _elementRemoved( const T& aOld )
301cdf0e10cSrcweir     {
302*1738ad43Smseidel         com::sun::star::container::ContainerEvent aEvent(
303cdf0e10cSrcweir             static_cast<com::sun::star::container::XIndexReplace*>( this ),
304cdf0e10cSrcweir             com::sun::star::uno::Any(),
305cdf0e10cSrcweir             com::sun::star::uno::makeAny( aOld ),
306cdf0e10cSrcweir             com::sun::star::uno::Any() );
307cdf0e10cSrcweir         for( Listeners_t::iterator aIter = maListeners.begin();
308cdf0e10cSrcweir              aIter != maListeners.end();
309cdf0e10cSrcweir              aIter++ )
310cdf0e10cSrcweir         {
311cdf0e10cSrcweir             (*aIter)->elementRemoved( aEvent );
312cdf0e10cSrcweir         }
313cdf0e10cSrcweir     }
314cdf0e10cSrcweir 
_elementReplaced(const sal_Int32 nPos,const T & aNew)315cdf0e10cSrcweir     void _elementReplaced( const sal_Int32 nPos, const T& aNew )
316cdf0e10cSrcweir     {
317cdf0e10cSrcweir         OSL_ENSURE( isValidIndex(nPos), "invalid index" );
318*1738ad43Smseidel         com::sun::star::container::ContainerEvent aEvent(
319cdf0e10cSrcweir             static_cast<com::sun::star::container::XIndexReplace*>( this ),
320cdf0e10cSrcweir             com::sun::star::uno::makeAny( nPos ),
321cdf0e10cSrcweir             com::sun::star::uno::makeAny( getItem( nPos ) ),
322cdf0e10cSrcweir             com::sun::star::uno::makeAny( aNew ) );
323cdf0e10cSrcweir         for( Listeners_t::iterator aIter = maListeners.begin();
324cdf0e10cSrcweir              aIter != maListeners.end();
325cdf0e10cSrcweir              aIter++ )
326cdf0e10cSrcweir         {
327cdf0e10cSrcweir             (*aIter)->elementReplaced( aEvent );
328cdf0e10cSrcweir         }
329cdf0e10cSrcweir     }
330cdf0e10cSrcweir 
331cdf0e10cSrcweir };
332cdf0e10cSrcweir 
333cdf0e10cSrcweir #endif
334