merge the formfield patch from ooo-build
[ooovba.git] / hwpfilter / source / list.hxx
blobc9cbf9e9b404eaec158e66c6cb09322b151d01c5
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: list.hxx,v $
10 * $Revision: 1.6.10.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef list_hxx
32 #define list_hxx
34 /**
35 * Re-implement a simple container: LinkedList + LinkedListIterator
37 * DO NOT USE EXCEPT FOR REPLACING THE ORIGINAL LinkedList/LinkedListIterator!
38 * USE STL CONTAINERS FOR NEW CODE!
40 * The classes LinkedList and LinkedListIterator were originally
41 * implemented in two files LinkedList.cpp/.h, whose license would not
42 * allow re-distribution through OpenOffice.org. This file
43 * re-implements the same functionality, based on the STL.
46 #include <vector>
48 template<class T>
49 class LinkedList
51 typedef std::vector<T*> list_t;
52 list_t maList;
54 public:
55 /// construct list with one element (pItem) or no element (pItem == NULL)
56 LinkedList( T* pItem = NULL );
57 ~LinkedList();
59 T* find( const int n ); /// return nth element in list
60 T* first(); /// return first element in list
61 T* last(); /// return last element in list
63 int count() const; /// return number of elements in list
64 int empty() const; /// determine whether list contains any elements
66 /// insert pItem into list at position n; at end if n == -1; return count()
67 int insert( T* pItem, int n = -1 );
69 /// remove nth element from list
70 T* remove( const int n );
72 /// remove given element from list
73 int remove( T* pItem );
76 /** iterator class for LinkedList<T>. Iterator may travel outside of
77 * list using operator++/--, in which case current() must return
78 * NULL. */
79 template<class T>
80 class LinkedListIterator
82 // iterator state: reference list + position
83 LinkedList<T>* mpList;
84 int mnPosition;
86 public:
87 /// construct list with single element
88 LinkedListIterator( LinkedList<T>* pList = NULL );
89 ~LinkedListIterator();
91 T* current(); /// return current element, or NULL if invalid
92 void set( const int n ); /// set iterator to position n
94 void reset( ); /// set iterator to first element
96 // bug-compatible with original LinkedList.h/cpp: Ignore parameter!
97 void operator++( int ); /// advance iterator by one step (ignore n !!!)
98 void operator--( int ); /// go one step backwards (ignore n !!!)
100 private:
101 bool valid();
107 // IMPLEMENTATION
109 // (the implementation of template classes must be accessible to using
110 // code, hence this implementation is in the header.)
113 #include <algorithm>
115 // define assert based on SAL, so we do not introduce a tools dependency
116 #include <osl/diagnose.h>
117 #define ASSERT(x) OSL_ENSURE((x), " HWP FILTER: " #x)
120 template<class T>
121 LinkedList<T>::LinkedList( T* pItem )
123 if( pItem != NULL )
124 maList.push_back( pItem );
127 template<class T>
128 LinkedList<T>::~LinkedList()
132 template<class T>
133 T* LinkedList<T>::find( const int n )
135 ASSERT( n >= 0 && n < static_cast<int>( maList.size() ) );
136 return maList[n];
139 template<class T>
140 T* LinkedList<T>::first()
142 return find( 0 );
145 template<class T>
146 T* LinkedList<T>::last()
148 return find( count() - 1 );
151 template<class T>
152 int LinkedList<T>::count() const
154 return static_cast<int>( maList.size() );
157 template<class T>
158 int LinkedList<T>::empty() const
160 return count() == 0;
163 template<class T>
164 int LinkedList<T>::insert( T* pItem, int n )
166 ASSERT( pItem != NULL );
167 ASSERT( n >= -1 && n <= static_cast<int>( maList.size() ));
169 if( n == -1 )
171 maList.push_back( pItem );
173 else
175 maList.insert( maList.begin() + n, pItem );
178 return static_cast<int>( maList.size() );
181 template<class T>
182 T* LinkedList<T>::remove( const int n )
184 ASSERT( n >= -1 && n <= static_cast<int>( maList.size() ) );
186 T* pItem = maList[ n ];
187 ASSERT( pItem != NULL );
189 maList.erase( maList.begin() + n );
190 return pItem;
193 template<class T>
194 int LinkedList<T>::remove( T* pItem )
196 ASSERT( pItem != NULL );
198 int i = 0;
199 typename list_t::iterator aIter = maList.begin();
200 typename list_t::iterator aEnd = maList.end();
201 while( aIter != aEnd && *aIter != pItem )
203 i++;
204 aIter++;
207 if( aIter != aEnd )
209 // found!
210 ASSERT( *aIter == pItem );
211 maList.erase( aIter );
213 else
215 // else: not found
216 i = -1;
219 return i;
224 template<class T>
225 LinkedListIterator<T>::LinkedListIterator( LinkedList<T>* pList ) :
226 mpList( pList ),
227 mnPosition( 0 )
229 ASSERT( pList != NULL );
232 template<class T>
233 LinkedListIterator<T>::~LinkedListIterator()
237 template<class T>
238 T* LinkedListIterator<T>::current()
240 return valid() ? mpList->find( mnPosition ) : NULL;
243 template<class T>
244 void LinkedListIterator<T>::set( const int nIndex )
246 ASSERT( mpList != NULL );
247 mnPosition = nIndex;
248 ASSERT( valid() );
252 template<class T>
253 void LinkedListIterator<T>::reset()
255 ASSERT( mpList != NULL );
256 mnPosition = 0;
257 ASSERT( valid() );
260 template<class T>
261 void LinkedListIterator<T>::operator++( int )
263 ASSERT( mpList != NULL );
265 // bug-compatible with LinkedList.cpp: ignore parameter!
266 mnPosition ++;
269 template<class T>
270 void LinkedListIterator<T>::operator--( int )
272 ASSERT( mpList != NULL );
274 // bug-compatible with LinkedList.cpp: ignore parameter!
275 mnPosition --;
278 template<class T>
279 bool LinkedListIterator<T>::valid()
281 return mpList != NULL
282 && mnPosition >= 0
283 && mnPosition < mpList->count();
286 #endif