fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / hwpfilter / source / list.hxx
blobe205657a7f48ac7aca501a80d7d59906a3323134
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_HWPFILTER_SOURCE_LIST_HXX
21 #define INCLUDED_HWPFILTER_SOURCE_LIST_HXX
23 /**
24 * Re-implement a simple container: LinkedList + LinkedListIterator
26 * DO NOT USE EXCEPT FOR REPLACING THE ORIGINAL LinkedList/LinkedListIterator!
27 * USE STL CONTAINERS FOR NEW CODE!
29 * The classes LinkedList and LinkedListIterator were originally
30 * implemented in two files LinkedList.cxx/.h, whose license would not
31 * allow re-distribution through OpenOffice.org. This file
32 * re-implements the same functionality, based on the STL.
35 #include <cstddef>
36 #include <vector>
38 template<class T>
39 class LinkedList
41 typedef std::vector<T*> list_t;
42 list_t maList;
44 public:
45 /// construct list with one element (pItem) or no element (pItem == NULL)
46 LinkedList( T* pItem = 0 );
47 ~LinkedList();
49 T* find( const int n ); /// return nth element in list
50 T* first(); /// return first element in list
51 T* last(); /// return last element in list
53 int count() const; /// return number of elements in list
54 int empty() const; /// determine whether list contains any elements
56 /// insert pItem into list at position n; at end if n == -1; return count()
57 int insert( T* pItem, int n = -1 );
59 /// remove nth element from list
60 T* remove( const int n );
62 /// remove given element from list
63 int remove( T* pItem );
66 /** iterator class for LinkedList<T>. Iterator may travel outside of
67 * list using operator++/--, in which case current() must return
68 * NULL. */
69 template<class T>
70 class LinkedListIterator
72 // iterator state: reference list + position
73 LinkedList<T>* mpList;
74 int mnPosition;
76 public:
77 /// construct list with single element
78 LinkedListIterator( LinkedList<T>* pList = 0 );
79 ~LinkedListIterator();
81 T* current(); /// return current element, or NULL if invalid
82 void set( const int n ); /// set iterator to position n
84 void reset( ); /// set iterator to first element
86 // bug-compatible with original LinkedList.h/cxx: Ignore parameter!
87 void operator++( int ); /// advance iterator by one step (ignore n !!!)
88 void operator--( int ); /// go one step backwards (ignore n !!!)
89 void operator++(); /// advance iterator by one step
90 void operator--(); /// go one step backwards
92 private:
93 bool valid();
99 // IMPLEMENTATION
101 // (the implementation of template classes must be accessible to using
102 // code, hence this implementation is in the header.)
105 #include <algorithm>
107 // define assert based on SAL, so we do not introduce a tools dependency
108 #include <osl/diagnose.h>
109 #define ASSERT(x) OSL_ENSURE((x), " HWP FILTER: " #x)
112 template<class T>
113 LinkedList<T>::LinkedList( T* pItem )
115 if( pItem != NULL )
116 maList.push_back( pItem );
119 template<class T>
120 LinkedList<T>::~LinkedList()
124 template<class T>
125 T* LinkedList<T>::find( const int n )
127 ASSERT( n >= 0 && n < static_cast<int>( maList.size() ) );
128 return maList[n];
131 template<class T>
132 T* LinkedList<T>::first()
134 return find( 0 );
137 template<class T>
138 T* LinkedList<T>::last()
140 return find( count() - 1 );
143 template<class T>
144 int LinkedList<T>::count() const
146 return static_cast<int>( maList.size() );
149 template<class T>
150 int LinkedList<T>::empty() const
152 return count() == 0;
155 template<class T>
156 int LinkedList<T>::insert( T* pItem, int n )
158 ASSERT( pItem != NULL );
159 ASSERT( n >= -1 && n <= static_cast<int>( maList.size() ));
161 if( n == -1 )
163 maList.push_back( pItem );
165 else
167 maList.insert( maList.begin() + n, pItem );
170 return static_cast<int>( maList.size() );
173 template<class T>
174 T* LinkedList<T>::remove( const int n )
176 ASSERT( n >= -1 && n <= static_cast<int>( maList.size() ) );
178 T* pItem = maList[ n ];
179 ASSERT( pItem != NULL );
181 maList.erase( maList.begin() + n );
182 return pItem;
185 template<class T>
186 int LinkedList<T>::remove( T* pItem )
188 ASSERT( pItem != NULL );
190 int i = 0;
191 typename list_t::iterator aIter = maList.begin();
192 typename list_t::iterator aEnd = maList.end();
193 while( aIter != aEnd && *aIter != pItem )
195 ++i;
196 ++aIter;
199 if( aIter != aEnd )
201 // found!
202 ASSERT( *aIter == pItem );
203 maList.erase( aIter );
205 else
207 // else: not found
208 i = -1;
211 return i;
216 template<class T>
217 LinkedListIterator<T>::LinkedListIterator( LinkedList<T>* pList ) :
218 mpList( pList ),
219 mnPosition( 0 )
221 ASSERT( pList != NULL );
224 template<class T>
225 LinkedListIterator<T>::~LinkedListIterator()
229 template<class T>
230 T* LinkedListIterator<T>::current()
232 return valid() ? mpList->find( mnPosition ) : NULL;
235 template<class T>
236 void LinkedListIterator<T>::set( const int nIndex )
238 ASSERT( mpList != NULL );
239 mnPosition = nIndex;
240 ASSERT( valid() );
244 template<class T>
245 void LinkedListIterator<T>::reset()
247 ASSERT( mpList != NULL );
248 mnPosition = 0;
249 ASSERT( valid() );
252 template<class T>
253 void LinkedListIterator<T>::operator++( int )
255 ASSERT( mpList != NULL );
257 // bug-compatible with LinkedList.cxx: ignore parameter!
258 mnPosition ++;
261 template<class T>
262 void LinkedListIterator<T>::operator--( int )
264 ASSERT( mpList != NULL );
266 // bug-compatible with LinkedList.cxx: ignore parameter!
267 mnPosition --;
270 template<class T>
271 void LinkedListIterator<T>::operator++()
273 ASSERT( mpList != NULL );
274 mnPosition ++;
277 template<class T>
278 void LinkedListIterator<T>::operator--()
280 ASSERT( mpList != NULL );
281 mnPosition --;
284 template<class T>
285 bool LinkedListIterator<T>::valid()
287 return mpList != NULL
288 && mnPosition >= 0
289 && mnPosition < mpList->count();
292 #endif
294 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */