1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: checkediterator.hxx,v $
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 __FRAMEWORK_CLASSES_CHECKEDITERATOR_HXX_
32 #define __FRAMEWORK_CLASSES_CHECKEDITERATOR_HXX_
34 //_________________________________________________________________________________________________________________
36 //_________________________________________________________________________________________________________________
38 #include <macros/debug.hxx>
40 //_________________________________________________________________________________________________________________
42 //_________________________________________________________________________________________________________________
44 //_________________________________________________________________________________________________________________
46 //_________________________________________________________________________________________________________________
47 #include <sal/types.h>
49 #ifndef __SGI_STL_ITERATOR
53 //_________________________________________________________________________________________________________________
55 //_________________________________________________________________________________________________________________
59 //_________________________________________________________________________________________________________________
61 //_________________________________________________________________________________________________________________
63 //_________________________________________________________________________________________________________________
64 // exported definitions
65 //_________________________________________________________________________________________________________________
67 /*-************************************************************************************************************//**
68 @short implement a iterator which support 2 end states!
69 @descr For our search methods we need a "walking" iterator object with special functionality!
70 We must check for 3 different states of an iterator - normal position, exact end, after end.
71 It's neccessary to detect if we have not found a entry and must return our default or
72 default already returned and we must break loop!
73 see using in class FilterCache too for further informations!
75 @Attention If your wish to debug this inline code ...
76 under windows and msdev you can use "set ENVCFLAGS=/Ob0" to do that!
81 @devstatus ready to use
83 *//*-*************************************************************************************************************/
85 template< class TContainer
>
88 //-------------------------------------------------------------------------------------------------------------
90 //-------------------------------------------------------------------------------------------------------------
94 //---------------------------------------------------------------------------------------------------------
95 // constructor / destructor
96 //---------------------------------------------------------------------------------------------------------
98 /*-****************************************************************************************************//**
99 @short standard constructor
100 @descr Set default values on members.
101 We set it internal to E_UNKNOWN to detect uninitialized instances of this class.
102 If we found one - we know: "We must call initialize first!"
110 *//*-*****************************************************************************************************/
112 inline CheckedIterator()
113 : m_eEndState ( E_UNKNOWN
)
114 , m_pContainer( NULL
)
118 //---------------------------------------------------------------------------------------------------------
120 //---------------------------------------------------------------------------------------------------------
122 /*-****************************************************************************************************//**
123 @short initialize instance with valid container
124 @descr Set new container at an instance of this class. The other member will set automaticly!
125 m_pPosition = first element in container
126 m_eEndState = BEFOREEND
130 @param "rContainer", must be a valid reference to an existing container.
133 @onerror An assertion is thrown.
134 *//*-*****************************************************************************************************/
136 inline void initialize( const TContainer
& rContainer
)
138 // Check incoming parameter. We don't accept all!
139 LOG_ASSERT2( &rContainer
==NULL
, "CheckedIterator::initialize()", "Invalid parameter detected!" )
140 LOG_ASSERT2( m_eEndState
!=E_UNKNOWN
, "CheckedIterator::initialize()", "Instance already initialized! Don't do it again." )
142 if( m_eEndState
== E_UNKNOWN
)
144 // Set new container and actualize other member.
145 m_pContainer
= &rContainer
;
146 m_eEndState
= E_BEFOREEND
;
147 m_pPosition
= m_pContainer
->begin();
151 /*-****************************************************************************************************//**
152 @short set internal states to E_END
153 @descr Sometimes we need a "walking" check-iterator which is initialized with the END-state!
154 We need it to return one default value if no other ones exist ...
156 @seealso using in class FilterCache!
162 *//*-*****************************************************************************************************/
166 m_pContainer
= NULL
;
167 m_eEndState
= E_END
;
170 /*-****************************************************************************************************//**
171 @short set internal states to E_AFTEREND
172 @descr Sometimes we need a "walking" check-iterator which is initialized with AFTEREND-state!
173 We need it if we don't have a container but must prevent us against further searching!
175 @seealso using in class FilterCache!
181 *//*-*****************************************************************************************************/
183 inline void setAfterEnd()
185 m_pContainer
= NULL
;
186 m_eEndState
= E_AFTEREND
;
189 /*-****************************************************************************************************//**
190 @short reset this iterator
191 @descr It must be called on an already initialized iterator.
192 Means the member m_pContainer must be valid. Otherwhise the reaction
199 *//*-*****************************************************************************************************/
203 m_eEndState
= E_UNKNOWN
;
207 /*-****************************************************************************************************//**
208 @short step to next element in container.
209 @descr If end of container is reached we change our internal "m_eEndState".
210 If end reached for first time; we set it to E_END;
211 If you step to next element again; we set it to E_AFTEREND.
212 So you have a chance to differ between "exact end" and "after end"!
214 @seealso method isEnd()
215 @seealso method isAfterEnd()
218 @return A reference to our changed object himself.
221 *//*-*****************************************************************************************************/
223 inline CheckedIterator
& operator++()
225 // Warn programmer if he forget to initailize object!
226 LOG_ASSERT2( m_pContainer
==NULL
, "CheckedIterator::operator++()", "Object not initialized!" )
227 // Step to next element if any exist or set our end states.
228 switch( m_eEndState
)
232 // If iterator reaching end ... set right state!
233 if( m_pPosition
== m_pContainer
->end() )
240 // Set state only ... iterator already points to end of container!
241 m_eEndState
= E_AFTEREND
;
248 /*-****************************************************************************************************//**
249 @short return true if internal iterator was not initialized before
250 @descr These will be true, if use start a new search by using these iterator mechanism!
252 @seealso class FilterCache
255 @return True if internalk state E_UNKNOWN - false otherwise.
258 *//*-*****************************************************************************************************/
260 inline sal_Bool
isUninitialized()
262 return( m_eEndState
== E_UNKNOWN
);
265 /*-****************************************************************************************************//**
266 @short return true if internal iterator reached end of container
267 @descr These will be true if you step to the end of internal container.
269 @seealso method isAfterEnd()
272 @return True if end reached; false otherwise.
275 *//*-*****************************************************************************************************/
277 inline sal_Bool
isEnd()
279 // Is true if one end state is set!
281 ( m_eEndState
== E_END
) ||
282 ( m_eEndState
== E_AFTEREND
)
286 /*-****************************************************************************************************//**
287 @short return true if you call operator++ again and end already reached
288 @descr These indicate, that end already reached but you call operator++ again and again!
290 @seealso method isEnd()
293 @return True if end multiple reached; false otherwise.
296 *//*-*****************************************************************************************************/
298 inline sal_Bool
isAfterEnd()
300 // Is true only, if special end state is set!
301 return( m_eEndState
== E_AFTEREND
);
304 /*-****************************************************************************************************//**
305 @short support readonly access to container entry
306 @descr Use it to get the value of current container item.
311 @return A reference to value of container entry.
314 *//*-*****************************************************************************************************/
316 inline typename
TContainer::const_iterator
getEntry()
318 // Warn programmer if he forget to initialize these object ...
319 LOG_ASSERT2( m_pContainer
==NULL
, "CheckedIterator::getEntry()", "Object not initialized!" )
320 // or try to read a non existing element!
321 LOG_ASSERT2( m_eEndState
!=E_BEFOREEND
, "CheckedIterator::getEntry()", "Wrong using of class detected!" )
326 //-------------------------------------------------------------------------------------------------------------
328 //-------------------------------------------------------------------------------------------------------------
332 // These enum defines our four states for an iterator position in curent container.
341 const TContainer
* m_pContainer
; // pointer to current container
342 EEndState m_eEndState
; // "position state" of iterator!
343 typename
TContainer::const_iterator m_pPosition
; // point to actual element in container
346 } // namespace framework
348 #endif // #ifndef __FRAMEWORK_CLASSES_CHECKEDITERATOR_HXX_