bump product version to 5.0.4.1
[LibreOffice.git] / forms / source / xforms / collection.hxx
blob13ad182ccb3435131172679566eacd7567e1d9eb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_FORMS_SOURCE_XFORMS_COLLECTION_HXX
21 #define INCLUDED_FORMS_SOURCE_XFORMS_COLLECTION_HXX
23 #include "enumeration.hxx"
25 #include <cppuhelper/implbase3.hxx>
26 #include <com/sun/star/container/ElementExistException.hpp>
27 #include <com/sun/star/container/NoSuchElementException.hpp>
28 #include <com/sun/star/container/XEnumeration.hpp>
29 #include <com/sun/star/container/XIndexReplace.hpp>
30 #include <com/sun/star/container/XSet.hpp>
31 #include <com/sun/star/container/XContainer.hpp>
32 #include <com/sun/star/container/XContainerListener.hpp>
33 #include <com/sun/star/lang/IllegalArgumentException.hpp>
34 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
35 #include <com/sun/star/lang/WrappedTargetException.hpp>
36 #include <com/sun/star/uno/Any.hxx>
37 #include <com/sun/star/uno/Reference.hxx>
38 #include <com/sun/star/uno/RuntimeException.hpp>
39 #include <com/sun/star/uno/Type.hxx>
40 #include <vector>
41 #include <algorithm>
44 typedef cppu::WeakImplHelper3<
45 com::sun::star::container::XIndexReplace,
46 com::sun::star::container::XSet,
47 com::sun::star::container::XContainer>
48 Collection_t;
50 template<class ELEMENT_TYPE>
51 class Collection : public Collection_t
53 public:
54 typedef ELEMENT_TYPE T;
55 typedef com::sun::star::uno::Reference<com::sun::star::container::XContainerListener> XContainerListener_t;
56 typedef std::vector<XContainerListener_t> Listeners_t;
58 protected:
59 std::vector<T> maItems;
60 Listeners_t maListeners;
62 public:
64 Collection() {}
65 virtual ~Collection() {}
67 const T& getItem( sal_Int32 n ) const
69 OSL_ENSURE( isValidIndex(n), "invalid index" );
70 OSL_ENSURE( isValid( maItems[n] ), "invalid item found" );
71 return maItems[n];
74 void setItem( sal_Int32 n, const T& t)
76 OSL_ENSURE( isValidIndex(n), "invalid index" );
77 OSL_ENSURE( isValid ( t ), "invalid item" );
79 T& aRef = maItems[ n ];
80 _elementReplaced( n, t );
81 _remove( aRef );
82 aRef = t;
83 _insert( t );
86 bool hasItem( const T& t ) const
88 return maItems.end() != std::find( maItems.begin(), maItems.end(), t );
91 sal_Int32 addItem( const T& t )
93 OSL_ENSURE( !hasItem( t ), "item to be added already present" );
94 OSL_ENSURE( isValid( t ), "invalid item" );
96 maItems.push_back( t );
97 _insert( t );
98 _elementInserted( maItems.size() - 1 );
99 return ( maItems.size() - 1 );
102 void removeItem( const T& t )
104 OSL_ENSURE( hasItem( t ), "item to be removed not present" );
105 OSL_ENSURE( isValid( t ), "an invalid item, funny that!" );
107 _elementRemoved( t );
108 _remove( t );
109 maItems.erase( std::find( maItems.begin(), maItems.end(), t ) );
112 bool hasItems() const
114 return maItems.size() != 0;
117 sal_Int32 countItems() const
119 return static_cast<sal_Int32>( maItems.size() );
122 bool isValidIndex( sal_Int32 n ) const
124 return n >= 0 && n < static_cast<sal_Int32>( maItems.size() );
128 // the following method may be overridden by sub-classes for
129 // customized behaviour
131 /// called before insertion to determine whether item is valid
132 virtual bool isValid( const T& ) const { return true; }
135 protected:
137 // the following methods may be overridden by sub-classes for
138 // customized behaviour
140 /// called after item has been inserted into the collection
141 virtual void _insert( const T& ) { }
143 /// called before item is removed from the collection
144 virtual void _remove( const T& ) { }
146 public:
148 typedef com::sun::star::uno::Type Type_t;
149 typedef com::sun::star::uno::Any Any_t;
150 typedef com::sun::star::uno::RuntimeException RuntimeException_t;
151 typedef com::sun::star::lang::IllegalArgumentException IllegalArgumentException_t;
152 typedef com::sun::star::container::NoSuchElementException NoSuchElementException_t;
153 typedef com::sun::star::lang::IndexOutOfBoundsException IndexOutOfBoundsException_t;
154 typedef com::sun::star::uno::Reference<com::sun::star::container::XEnumeration> XEnumeration_t;
155 typedef com::sun::star::lang::WrappedTargetException WrappedTargetException_t;
156 typedef com::sun::star::container::ElementExistException ElementExistException_t;
159 // XElementAccess
160 virtual Type_t SAL_CALL getElementType()
161 throw( RuntimeException_t, std::exception ) SAL_OVERRIDE
163 return cppu::UnoType<T>::get();
166 virtual sal_Bool SAL_CALL hasElements()
167 throw( RuntimeException_t, std::exception ) SAL_OVERRIDE
169 return hasItems();
172 // XIndexAccess : XElementAccess
173 virtual sal_Int32 SAL_CALL getCount()
174 throw( RuntimeException_t, std::exception ) SAL_OVERRIDE
176 return countItems();
179 virtual Any_t SAL_CALL getByIndex( sal_Int32 nIndex )
180 throw( IndexOutOfBoundsException_t,
181 WrappedTargetException_t,
182 RuntimeException_t, std::exception) SAL_OVERRIDE
184 if( isValidIndex( nIndex ) )
185 return com::sun::star::uno::makeAny( getItem( nIndex ) );
186 else
187 throw IndexOutOfBoundsException_t();
190 // XIndexReplace : XIndexAccess
191 virtual void SAL_CALL replaceByIndex( sal_Int32 nIndex,
192 const Any_t& aElement )
193 throw( IllegalArgumentException_t,
194 IndexOutOfBoundsException_t,
195 WrappedTargetException_t,
196 RuntimeException_t, std::exception) SAL_OVERRIDE
198 T t;
199 if( isValidIndex( nIndex) )
200 if( ( aElement >>= t ) && isValid( t ) )
201 setItem( nIndex, t );
202 else
203 throw IllegalArgumentException_t();
204 else
205 throw IndexOutOfBoundsException_t();
208 // XEnumerationAccess : XElementAccess
209 virtual XEnumeration_t SAL_CALL createEnumeration()
210 throw( RuntimeException_t, std::exception ) SAL_OVERRIDE
212 return new Enumeration( this );
216 // XSet : XEnumerationAccess
217 virtual sal_Bool SAL_CALL has( const Any_t& aElement )
218 throw( RuntimeException_t, std::exception ) SAL_OVERRIDE
220 T t;
221 return ( aElement >>= t ) ? hasItem( t ) : sal_False;
224 virtual void SAL_CALL insert( const Any_t& aElement )
225 throw( IllegalArgumentException_t,
226 ElementExistException_t,
227 RuntimeException_t, std::exception ) SAL_OVERRIDE
229 T t;
230 if( ( aElement >>= t ) && isValid( t ) )
231 if( ! hasItem( t ) )
232 addItem( t );
233 else
234 throw ElementExistException_t();
235 else
236 throw IllegalArgumentException_t();
239 virtual void SAL_CALL remove( const Any_t& aElement )
240 throw( IllegalArgumentException_t,
241 NoSuchElementException_t,
242 RuntimeException_t, std::exception ) SAL_OVERRIDE
244 T t;
245 if( aElement >>= t )
246 if( hasItem( t ) )
247 removeItem( t );
248 else
249 throw NoSuchElementException_t();
250 else
251 throw IllegalArgumentException_t();
255 // XContainer
256 virtual void SAL_CALL addContainerListener(
257 const XContainerListener_t& xListener )
258 throw( RuntimeException_t, std::exception ) SAL_OVERRIDE
260 OSL_ENSURE( xListener.is(), "need listener!" );
261 if( std::find( maListeners.begin(), maListeners.end(), xListener)
262 == maListeners.end() )
263 maListeners.push_back( xListener );
266 virtual void SAL_CALL removeContainerListener(
267 const XContainerListener_t& xListener )
268 throw( RuntimeException_t, std::exception ) SAL_OVERRIDE
270 OSL_ENSURE( xListener.is(), "need listener!" );
271 Listeners_t::iterator aIter =
272 std::find( maListeners.begin(), maListeners.end(), xListener );
273 if( aIter != maListeners.end() )
274 maListeners.erase( aIter );
277 protected:
279 // call listeners:
280 void _elementInserted( sal_Int32 nPos )
282 OSL_ENSURE( isValidIndex(nPos), "invalid index" );
283 com::sun::star::container::ContainerEvent aEvent(
284 static_cast<com::sun::star::container::XIndexReplace*>( this ),
285 com::sun::star::uno::makeAny( nPos ),
286 com::sun::star::uno::makeAny( getItem( nPos ) ),
287 com::sun::star::uno::Any() );
288 for( Listeners_t::iterator aIter = maListeners.begin();
289 aIter != maListeners.end();
290 ++aIter )
292 (*aIter)->elementInserted( aEvent );
296 void _elementRemoved( const T& aOld )
298 com::sun::star::container::ContainerEvent aEvent(
299 static_cast<com::sun::star::container::XIndexReplace*>( this ),
300 com::sun::star::uno::Any(),
301 com::sun::star::uno::makeAny( aOld ),
302 com::sun::star::uno::Any() );
303 for( Listeners_t::iterator aIter = maListeners.begin();
304 aIter != maListeners.end();
305 ++aIter )
307 (*aIter)->elementRemoved( aEvent );
311 void _elementReplaced( const sal_Int32 nPos, const T& aNew )
313 OSL_ENSURE( isValidIndex(nPos), "invalid index" );
314 com::sun::star::container::ContainerEvent aEvent(
315 static_cast<com::sun::star::container::XIndexReplace*>( this ),
316 com::sun::star::uno::makeAny( nPos ),
317 com::sun::star::uno::makeAny( getItem( nPos ) ),
318 com::sun::star::uno::makeAny( aNew ) );
319 for( Listeners_t::iterator aIter = maListeners.begin();
320 aIter != maListeners.end();
321 ++aIter )
323 (*aIter)->elementReplaced( aEvent );
329 #endif
331 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */