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_HWPFILTER_SOURCE_LIST_HXX
21 #define INCLUDED_HWPFILTER_SOURCE_LIST_HXX
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.
41 typedef std::vector
<T
*> list_t
;
45 /// construct list with one element (pItem) or no element (pItem == NULL)
46 LinkedList( T
* pItem
= 0 );
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
70 class LinkedListIterator
72 // iterator state: reference list + position
73 LinkedList
<T
>* mpList
;
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
101 // (the implementation of template classes must be accessible to using
102 // code, hence this implementation is in the header.)
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)
113 LinkedList
<T
>::LinkedList( T
* pItem
)
116 maList
.push_back( pItem
);
120 LinkedList
<T
>::~LinkedList()
125 T
* LinkedList
<T
>::find( const int n
)
127 ASSERT( n
>= 0 && n
< static_cast<int>( maList
.size() ) );
132 T
* LinkedList
<T
>::first()
138 T
* LinkedList
<T
>::last()
140 return find( count() - 1 );
144 int LinkedList
<T
>::count() const
146 return static_cast<int>( maList
.size() );
150 int LinkedList
<T
>::empty() const
156 int LinkedList
<T
>::insert( T
* pItem
, int n
)
158 ASSERT( pItem
!= NULL
);
159 ASSERT( n
>= -1 && n
<= static_cast<int>( maList
.size() ));
163 maList
.push_back( pItem
);
167 maList
.insert( maList
.begin() + n
, pItem
);
170 return static_cast<int>( maList
.size() );
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
);
186 int LinkedList
<T
>::remove( T
* pItem
)
188 ASSERT( pItem
!= NULL
);
191 typename
list_t::iterator aIter
= maList
.begin();
192 typename
list_t::iterator aEnd
= maList
.end();
193 while( aIter
!= aEnd
&& *aIter
!= pItem
)
202 ASSERT( *aIter
== pItem
);
203 maList
.erase( aIter
);
217 LinkedListIterator
<T
>::LinkedListIterator( LinkedList
<T
>* pList
) :
221 ASSERT( pList
!= NULL
);
225 LinkedListIterator
<T
>::~LinkedListIterator()
230 T
* LinkedListIterator
<T
>::current()
232 return valid() ? mpList
->find( mnPosition
) : NULL
;
236 void LinkedListIterator
<T
>::set( const int nIndex
)
238 ASSERT( mpList
!= NULL
);
245 void LinkedListIterator
<T
>::reset()
247 ASSERT( mpList
!= NULL
);
253 void LinkedListIterator
<T
>::operator++( int )
255 ASSERT( mpList
!= NULL
);
257 // bug-compatible with LinkedList.cxx: ignore parameter!
262 void LinkedListIterator
<T
>::operator--( int )
264 ASSERT( mpList
!= NULL
);
266 // bug-compatible with LinkedList.cxx: ignore parameter!
271 void LinkedListIterator
<T
>::operator++()
273 ASSERT( mpList
!= NULL
);
278 void LinkedListIterator
<T
>::operator--()
280 ASSERT( mpList
!= NULL
);
285 bool LinkedListIterator
<T
>::valid()
287 return mpList
!= NULL
289 && mnPosition
< mpList
->count();
294 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */