1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
44 typedef cppu::WeakImplHelper3
<
45 com::sun::star::container::XIndexReplace
,
46 com::sun::star::container::XSet
,
47 com::sun::star::container::XContainer
>
50 template<class ELEMENT_TYPE
>
51 class Collection
: public Collection_t
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
;
59 std::vector
<T
> maItems
;
60 Listeners_t maListeners
;
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" );
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
);
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
);
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
);
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; }
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
& ) { }
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
;
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
172 // XIndexAccess : XElementAccess
173 virtual sal_Int32 SAL_CALL
getCount()
174 throw( RuntimeException_t
, std::exception
) SAL_OVERRIDE
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
) );
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
199 if( isValidIndex( nIndex
) )
200 if( ( aElement
>>= t
) && isValid( t
) )
201 setItem( nIndex
, t
);
203 throw IllegalArgumentException_t();
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
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
230 if( ( aElement
>>= t
) && isValid( t
) )
234 throw ElementExistException_t();
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
249 throw NoSuchElementException_t();
251 throw IllegalArgumentException_t();
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
);
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();
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();
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();
323 (*aIter
)->elementReplaced( aEvent
);
331 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */