LibreOffice Module forms (master) 1
collection.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#pragma once
21
22#include "enumeration.hxx"
23
25#include <osl/diagnose.h>
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/uno/Any.hxx>
36#include <com/sun/star/uno/Reference.hxx>
37#include <com/sun/star/uno/Type.hxx>
38#include <vector>
39
40
41typedef cppu::WeakImplHelper<
42 css::container::XIndexReplace,
43 css::container::XSet,
44 css::container::XContainer>
46
47template<class ELEMENT_TYPE>
49{
50public:
51 typedef ELEMENT_TYPE T;
52 typedef std::vector<css::uno::Reference<css::container::XContainerListener> > Listeners_t;
53
54protected:
55 std::vector<T> maItems;
57
58public:
59
61
62 const T& getItem( sal_Int32 n ) const
63 {
64 OSL_ENSURE( isValidIndex(n), "invalid index" );
65 OSL_ENSURE( isValid( maItems[n] ), "invalid item found" );
66 return maItems[n];
67 }
68
69 void setItem( sal_Int32 n, const T& t)
70 {
71 OSL_ENSURE( isValidIndex(n), "invalid index" );
72 OSL_ENSURE( isValid ( t ), "invalid item" );
73
74 T& aRef = maItems[ n ];
76 _remove( aRef );
77 aRef = t;
78 _insert( t );
79 }
80
81 bool hasItem( const T& t ) const
82 {
83 return maItems.end() != std::find( maItems.begin(), maItems.end(), t );
84 }
85
86 sal_Int32 addItem( const T& t )
87 {
88 OSL_ENSURE( !hasItem( t ), "item to be added already present" );
89 OSL_ENSURE( isValid( t ), "invalid item" );
90
91 maItems.push_back( t );
92 _insert( t );
93 _elementInserted( maItems.size() - 1 );
94 return ( maItems.size() - 1 );
95 }
96
97 void removeItem( const T& t )
98 {
99 OSL_ENSURE( hasItem( t ), "item to be removed not present" );
100 OSL_ENSURE( isValid( t ), "an invalid item, funny that!" );
101
103 _remove( t );
104 maItems.erase( std::find( maItems.begin(), maItems.end(), t ) );
105 }
106
107 bool hasItems() const
108 {
109 return maItems.size() != 0;
110 }
111
112 sal_Int32 countItems() const
113 {
114 return static_cast<sal_Int32>( maItems.size() );
115 }
116
117 bool isValidIndex( sal_Int32 n ) const
118 {
119 return n >= 0 && n < static_cast<sal_Int32>( maItems.size() );
120 }
121
122
123 // the following method may be overridden by sub-classes for
124 // customized behaviour
125
127 virtual bool isValid( const T& ) const { return true; }
128
129
130protected:
131
132 // the following methods may be overridden by sub-classes for
133 // customized behaviour
134
136 virtual void _insert( const T& ) { }
137
139 virtual void _remove( const T& ) { }
140
141public:
142
143 // XElementAccess
144 virtual css::uno::Type SAL_CALL getElementType() override
145 {
146 return cppu::UnoType<T>::get();
147 }
148
149 virtual sal_Bool SAL_CALL hasElements() override
150 {
151 return hasItems();
152 }
153
154 // XIndexAccess : XElementAccess
155 virtual sal_Int32 SAL_CALL getCount() override
156 {
157 return countItems();
158 }
159
160 virtual css::uno::Any SAL_CALL getByIndex( sal_Int32 nIndex ) override
161 {
162 if( !isValidIndex( nIndex ) )
163 throw css::lang::IndexOutOfBoundsException();
164 return css::uno::Any( getItem( nIndex ) );
165 }
166
167 // XIndexReplace : XIndexAccess
168 virtual void SAL_CALL replaceByIndex( sal_Int32 nIndex,
169 const css::uno::Any& aElement ) override
170 {
171 T t;
172 if( !isValidIndex( nIndex) )
173 throw css::lang::IndexOutOfBoundsException();
174 if( !( aElement >>= t ) || !isValid( t ) )
175 throw css::lang::IllegalArgumentException();
176 setItem( nIndex, t );
177 }
178
179 // XEnumerationAccess : XElementAccess
180 virtual css::uno::Reference<css::container::XEnumeration> SAL_CALL createEnumeration() override
181 {
182 return new Enumeration( this );
183 }
184
185
186 // XSet : XEnumerationAccess
187 virtual sal_Bool SAL_CALL has( const css::uno::Any& aElement ) override
188 {
189 T t;
190 return ( aElement >>= t ) && hasItem( t );
191 }
192
193 virtual void SAL_CALL insert( const css::uno::Any& aElement ) override
194 {
195 T t;
196 if( !( aElement >>= t ) || !isValid( t ) )
197 throw css::lang::IllegalArgumentException();
198 if( hasItem( t ) )
199 throw css::container::ElementExistException();
200 addItem( t );
201 }
202
203 virtual void SAL_CALL remove( const css::uno::Any& aElement ) override
204 {
205 T t;
206 if( !(aElement >>= t) )
207 throw css::lang::IllegalArgumentException();
208 if( !hasItem( t ) )
209 throw css::container::NoSuchElementException();
210 removeItem( t );
211 }
212
213
214 // XContainer
215 virtual void SAL_CALL addContainerListener(
216 const css::uno::Reference<css::container::XContainerListener>& xListener ) override
217 {
218 OSL_ENSURE( xListener.is(), "need listener!" );
219 if( std::find( maListeners.begin(), maListeners.end(), xListener)
220 == maListeners.end() )
221 maListeners.push_back( xListener );
222 }
223
224 virtual void SAL_CALL removeContainerListener(
225 const css::uno::Reference<css::container::XContainerListener>& xListener ) override
226 {
227 OSL_ENSURE( xListener.is(), "need listener!" );
228 Listeners_t::iterator aIter =
229 std::find( maListeners.begin(), maListeners.end(), xListener );
230 if( aIter != maListeners.end() )
231 maListeners.erase( aIter );
232 }
233
234protected:
235
236 // call listeners:
237 void _elementInserted( sal_Int32 nPos )
238 {
239 OSL_ENSURE( isValidIndex(nPos), "invalid index" );
240 css::container::ContainerEvent aEvent(
241 static_cast<css::container::XIndexReplace*>( this ),
242 css::uno::Any( nPos ),
243 css::uno::Any( getItem( nPos ) ),
244 css::uno::Any() );
245 for (auto const& listener : maListeners)
246 {
247 listener->elementInserted( aEvent );
248 }
249 }
250
251 void _elementRemoved( const T& aOld )
252 {
253 css::container::ContainerEvent aEvent(
254 static_cast<css::container::XIndexReplace*>( this ),
255 css::uno::Any(),
256 css::uno::Any( aOld ),
257 css::uno::Any() );
258 for (auto const& listener : maListeners)
259 {
260 listener->elementRemoved( aEvent );
261 }
262 }
263
264 void _elementReplaced( const sal_Int32 nPos, const T& aNew )
265 {
266 OSL_ENSURE( isValidIndex(nPos), "invalid index" );
267 css::container::ContainerEvent aEvent(
268 static_cast<css::container::XIndexReplace*>( this ),
269 css::uno::Any( nPos ),
270 css::uno::Any( getItem( nPos ) ),
271 css::uno::Any( aNew ) );
272 for (auto const& listener : maListeners)
273 {
274 listener->elementReplaced( aEvent );
275 }
276 }
277
278};
279
280/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
XPropertyListType t
AnyEventRef aEvent
sal_Int32 countItems() const
Definition: collection.hxx:112
virtual void _remove(const T &)
called before item is removed from the collection
Definition: collection.hxx:139
virtual void SAL_CALL remove(const css::uno::Any &aElement) override
Definition: collection.hxx:203
std::vector< css::uno::Reference< css::container::XContainerListener > > Listeners_t
Definition: collection.hxx:52
const T & getItem(sal_Int32 n) const
Definition: collection.hxx:62
virtual void SAL_CALL addContainerListener(const css::uno::Reference< css::container::XContainerListener > &xListener) override
Definition: collection.hxx:215
void removeItem(const T &t)
Definition: collection.hxx:97
virtual void SAL_CALL replaceByIndex(sal_Int32 nIndex, const css::uno::Any &aElement) override
Definition: collection.hxx:168
sal_Int32 addItem(const T &t)
Definition: collection.hxx:86
virtual sal_Int32 SAL_CALL getCount() override
Definition: collection.hxx:155
virtual bool isValid(const T &) const
called before insertion to determine whether item is valid
Definition: collection.hxx:127
void _elementRemoved(const T &aOld)
Definition: collection.hxx:251
bool hasItems() const
Definition: collection.hxx:107
bool hasItem(const T &t) const
Definition: collection.hxx:81
void setItem(sal_Int32 n, const T &t)
Definition: collection.hxx:69
void _elementReplaced(const sal_Int32 nPos, const T &aNew)
Definition: collection.hxx:264
virtual css::uno::Any SAL_CALL getByIndex(sal_Int32 nIndex) override
Definition: collection.hxx:160
bool isValidIndex(sal_Int32 n) const
Definition: collection.hxx:117
virtual void SAL_CALL insert(const css::uno::Any &aElement) override
Definition: collection.hxx:193
virtual sal_Bool SAL_CALL hasElements() override
Definition: collection.hxx:149
virtual void _insert(const T &)
called after item has been inserted into the collection
Definition: collection.hxx:136
virtual sal_Bool SAL_CALL has(const css::uno::Any &aElement) override
Definition: collection.hxx:187
virtual css::uno::Type SAL_CALL getElementType() override
Definition: collection.hxx:144
std::vector< T > maItems
Definition: collection.hxx:55
ELEMENT_TYPE T
Definition: collection.hxx:51
Listeners_t maListeners
Definition: collection.hxx:56
void _elementInserted(sal_Int32 nPos)
Definition: collection.hxx:237
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
Definition: collection.hxx:180
virtual void SAL_CALL removeContainerListener(const css::uno::Reference< css::container::XContainerListener > &xListener) override
Definition: collection.hxx:224
implement XEnumeration based on container::XIndexAccess
Definition: enumeration.hxx:38
css::uno::Type const & get()
cppu::WeakImplHelper< css::container::XIndexReplace, css::container::XSet, css::container::XContainer > Collection_t
Definition: collection.hxx:45
sal_Int32 nIndex
sal_Int64 n
sal_uInt16 nPos
unsigned char sal_Bool