1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #ifndef __FRAMEWORK_CLASSES_CHECKEDITERATOR_HXX_
29*cdf0e10cSrcweir #define __FRAMEWORK_CLASSES_CHECKEDITERATOR_HXX_
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
32*cdf0e10cSrcweir //	my own includes
33*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include <macros/debug.hxx>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
38*cdf0e10cSrcweir //	interface includes
39*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
42*cdf0e10cSrcweir //	other includes
43*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
44*cdf0e10cSrcweir #include <sal/types.h>
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir #ifndef __SGI_STL_ITERATOR
47*cdf0e10cSrcweir #include <iterator>
48*cdf0e10cSrcweir #endif
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
51*cdf0e10cSrcweir //	namespace
52*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir namespace framework{
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
57*cdf0e10cSrcweir //	exported const
58*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
61*cdf0e10cSrcweir //	exported definitions
62*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir /*-************************************************************************************************************//**
65*cdf0e10cSrcweir 	@short          implement a iterator which support 2 end states!
66*cdf0e10cSrcweir 	@descr			For our search methods we need a "walking" iterator object with special functionality!
67*cdf0e10cSrcweir 					We must check for 3 different states of an iterator - normal position, exact end, after end.
68*cdf0e10cSrcweir 					It's neccessary to detect if we have not found a entry and must return our default or
69*cdf0e10cSrcweir 					default already returned and we must break loop!
70*cdf0e10cSrcweir 					see using in class FilterCache too for further informations!
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir 	@Attention		If your wish to debug this inline code ...
73*cdf0e10cSrcweir 					under windows and msdev you can use "set ENVCFLAGS=/Ob0" to do that!
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir 	@implements		-
76*cdf0e10cSrcweir 	@base			-
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir 	@devstatus		ready to use
79*cdf0e10cSrcweir 	@threadsafe		no
80*cdf0e10cSrcweir *//*-*************************************************************************************************************/
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir template< class TContainer >
83*cdf0e10cSrcweir class CheckedIterator
84*cdf0e10cSrcweir {
85*cdf0e10cSrcweir 	//-------------------------------------------------------------------------------------------------------------
86*cdf0e10cSrcweir 	//	public methods
87*cdf0e10cSrcweir 	//-------------------------------------------------------------------------------------------------------------
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir 	public:
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir 		//---------------------------------------------------------------------------------------------------------
92*cdf0e10cSrcweir 		// constructor / destructor
93*cdf0e10cSrcweir 		//---------------------------------------------------------------------------------------------------------
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir 		/*-****************************************************************************************************//**
96*cdf0e10cSrcweir 			@short		standard constructor
97*cdf0e10cSrcweir 			@descr		Set default values on members.
98*cdf0e10cSrcweir 						We set it internal to E_UNKNOWN to detect uninitialized instances of this class.
99*cdf0e10cSrcweir 						If we found one - we know: "We must call initialize first!"
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir 			@seealso	-
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir 			@param		-
104*cdf0e10cSrcweir 			@return		-
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir 			@onerror	-
107*cdf0e10cSrcweir 		*//*-*****************************************************************************************************/
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir 		inline CheckedIterator()
110*cdf0e10cSrcweir                 :   m_eEndState ( E_UNKNOWN )
111*cdf0e10cSrcweir                 ,   m_pContainer( NULL      )
112*cdf0e10cSrcweir 		{
113*cdf0e10cSrcweir 		}
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir 		//---------------------------------------------------------------------------------------------------------
116*cdf0e10cSrcweir 		// interface methods
117*cdf0e10cSrcweir 		//---------------------------------------------------------------------------------------------------------
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir 		/*-****************************************************************************************************//**
120*cdf0e10cSrcweir 			@short		initialize instance with valid container
121*cdf0e10cSrcweir 			@descr		Set new container at an instance of this class. The other member will set automaticly!
122*cdf0e10cSrcweir 						m_pPosition = first element in container
123*cdf0e10cSrcweir 						m_eEndState = BEFOREEND
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir 			@seealso	-
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir             @param      "rContainer", must be a valid reference to an existing container.
128*cdf0e10cSrcweir 			@return		-
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir 			@onerror	An assertion is thrown.
131*cdf0e10cSrcweir 		*//*-*****************************************************************************************************/
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir         inline void initialize( const TContainer& rContainer )
134*cdf0e10cSrcweir 		{
135*cdf0e10cSrcweir 			// Check incoming parameter. We don't accept all!
136*cdf0e10cSrcweir             LOG_ASSERT2( &rContainer==NULL      , "CheckedIterator::initialize()", "Invalid parameter detected!"                        )
137*cdf0e10cSrcweir 			LOG_ASSERT2( m_eEndState!=E_UNKNOWN	, "CheckedIterator::initialize()", "Instance already initialized! Don't do it again."	)
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir 			if( m_eEndState == E_UNKNOWN )
140*cdf0e10cSrcweir 			{
141*cdf0e10cSrcweir 				// Set new container and update other member.
142*cdf0e10cSrcweir                 m_pContainer = &rContainer          ;
143*cdf0e10cSrcweir                 m_eEndState  = E_BEFOREEND          ;
144*cdf0e10cSrcweir                 m_pPosition  = m_pContainer->begin();
145*cdf0e10cSrcweir 			}
146*cdf0e10cSrcweir 		}
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir 		/*-****************************************************************************************************//**
149*cdf0e10cSrcweir 			@short		set internal states to E_END
150*cdf0e10cSrcweir 			@descr		Sometimes we need a "walking" check-iterator which is initialized with the END-state!
151*cdf0e10cSrcweir 						We need it to return one default value if no other ones exist ...
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir 			@seealso	using in class FilterCache!
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir 			@param		-
156*cdf0e10cSrcweir 			@return		-
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir 			@onerror	-
159*cdf0e10cSrcweir 		*//*-*****************************************************************************************************/
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir 		inline void setEnd()
162*cdf0e10cSrcweir 		{
163*cdf0e10cSrcweir             m_pContainer = NULL  ;
164*cdf0e10cSrcweir             m_eEndState  = E_END ;
165*cdf0e10cSrcweir 		}
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir 		/*-****************************************************************************************************//**
168*cdf0e10cSrcweir 			@short		set internal states to E_AFTEREND
169*cdf0e10cSrcweir 			@descr		Sometimes we need a "walking" check-iterator which is initialized with AFTEREND-state!
170*cdf0e10cSrcweir 						We need it if we don't have a container but must prevent us against further searching!
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir 			@seealso	using in class FilterCache!
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir 			@param		-
175*cdf0e10cSrcweir 			@return		-
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir 			@onerror	-
178*cdf0e10cSrcweir 		*//*-*****************************************************************************************************/
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir 		inline void setAfterEnd()
181*cdf0e10cSrcweir 		{
182*cdf0e10cSrcweir             m_pContainer = NULL       ;
183*cdf0e10cSrcweir             m_eEndState  = E_AFTEREND ;
184*cdf0e10cSrcweir 		}
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir         /*-****************************************************************************************************//**
187*cdf0e10cSrcweir             @short      reset this iterator
188*cdf0e10cSrcweir             @descr      It must be called on an already initialized iterator.
189*cdf0e10cSrcweir                         Means the member m_pContainer must be valid. Otherwhise the reaction
190*cdf0e10cSrcweir                         isn't defined.
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir             @param      -
193*cdf0e10cSrcweir             @return     -
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir             @onerror    -
196*cdf0e10cSrcweir         *//*-*****************************************************************************************************/
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir         inline void reset()
199*cdf0e10cSrcweir         {
200*cdf0e10cSrcweir             m_eEndState  = E_UNKNOWN;
201*cdf0e10cSrcweir             m_pContainer = NULL;
202*cdf0e10cSrcweir         }
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir 		/*-****************************************************************************************************//**
205*cdf0e10cSrcweir 			@short		step to next element in container.
206*cdf0e10cSrcweir 			@descr		If end of container is reached we change our internal "m_eEndState".
207*cdf0e10cSrcweir 						If end reached for first time; we set it to E_END;
208*cdf0e10cSrcweir 						If you step to next element again; we set it to E_AFTEREND.
209*cdf0e10cSrcweir 						So you have a chance to differ between "exact end" and "after end"!
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir 			@seealso	method isEnd()
212*cdf0e10cSrcweir 			@seealso	method isAfterEnd()
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir 			@param		-
215*cdf0e10cSrcweir 			@return		A reference to our changed object himself.
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir 			@onerror	-
218*cdf0e10cSrcweir 		*//*-*****************************************************************************************************/
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir 		inline CheckedIterator& operator++()
221*cdf0e10cSrcweir 		{
222*cdf0e10cSrcweir 			// Warn programmer if he forget to initailize object!
223*cdf0e10cSrcweir             LOG_ASSERT2( m_pContainer==NULL, "CheckedIterator::operator++()", "Object not initialized!" )
224*cdf0e10cSrcweir 			// Step to next element if any exist or set our end states.
225*cdf0e10cSrcweir 			switch( m_eEndState )
226*cdf0e10cSrcweir 			{
227*cdf0e10cSrcweir 				case E_BEFOREEND:	{
228*cdf0e10cSrcweir                                         ++m_pPosition;
229*cdf0e10cSrcweir                                         // If iterator reaching end ... set right state!
230*cdf0e10cSrcweir                                         if( m_pPosition == m_pContainer->end() )
231*cdf0e10cSrcweir 										{
232*cdf0e10cSrcweir 											m_eEndState = E_END;
233*cdf0e10cSrcweir 										}
234*cdf0e10cSrcweir 									}
235*cdf0e10cSrcweir 									break;
236*cdf0e10cSrcweir 				case E_END		:	{
237*cdf0e10cSrcweir                                         // Set state only ... iterator already points to end of container!
238*cdf0e10cSrcweir 										m_eEndState = E_AFTEREND;
239*cdf0e10cSrcweir 									}
240*cdf0e10cSrcweir 									break;
241*cdf0e10cSrcweir 			}
242*cdf0e10cSrcweir 			return *this;
243*cdf0e10cSrcweir 		}
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir 		/*-****************************************************************************************************//**
246*cdf0e10cSrcweir 			@short		return true if internal iterator was not initialized before
247*cdf0e10cSrcweir 			@descr		These will be true, if use start a new search by using these iterator mechanism!
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir 			@seealso	class FilterCache
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir 			@param		-
252*cdf0e10cSrcweir 			@return		True if internalk state E_UNKNOWN - false otherwise.
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir 			@onerror	-
255*cdf0e10cSrcweir 		*//*-*****************************************************************************************************/
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir 		inline sal_Bool isUninitialized()
258*cdf0e10cSrcweir 		{
259*cdf0e10cSrcweir 			return( m_eEndState == E_UNKNOWN );
260*cdf0e10cSrcweir 		}
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir 		/*-****************************************************************************************************//**
263*cdf0e10cSrcweir 			@short		return true if internal iterator reached end of container
264*cdf0e10cSrcweir 			@descr		These will be true if you step to the end of internal container.
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir 			@seealso	method isAfterEnd()
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir 			@param		-
269*cdf0e10cSrcweir 			@return		True if end reached; false otherwise.
270*cdf0e10cSrcweir 
271*cdf0e10cSrcweir 			@onerror	-
272*cdf0e10cSrcweir 		*//*-*****************************************************************************************************/
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir 		inline sal_Bool isEnd()
275*cdf0e10cSrcweir 		{
276*cdf0e10cSrcweir 			// Is true if one end state is set!
277*cdf0e10cSrcweir 			return	(
278*cdf0e10cSrcweir 						( m_eEndState == E_END		)	||
279*cdf0e10cSrcweir 						( m_eEndState == E_AFTEREND	)
280*cdf0e10cSrcweir 					);
281*cdf0e10cSrcweir 		}
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir 		/*-****************************************************************************************************//**
284*cdf0e10cSrcweir 			@short		return true if you call operator++ again and end already reached
285*cdf0e10cSrcweir 			@descr		These indicate, that end already reached but you call operator++ again and again!
286*cdf0e10cSrcweir 
287*cdf0e10cSrcweir 			@seealso	method isEnd()
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir 			@param		-
290*cdf0e10cSrcweir 			@return		True if end multiple reached; false otherwise.
291*cdf0e10cSrcweir 
292*cdf0e10cSrcweir 			@onerror	-
293*cdf0e10cSrcweir 		*//*-*****************************************************************************************************/
294*cdf0e10cSrcweir 
295*cdf0e10cSrcweir 		inline sal_Bool isAfterEnd()
296*cdf0e10cSrcweir 		{
297*cdf0e10cSrcweir 			// Is true only, if special end state is set!
298*cdf0e10cSrcweir 			return( m_eEndState == E_AFTEREND );
299*cdf0e10cSrcweir 		}
300*cdf0e10cSrcweir 
301*cdf0e10cSrcweir 		/*-****************************************************************************************************//**
302*cdf0e10cSrcweir 			@short		support readonly access to container entry
303*cdf0e10cSrcweir 			@descr		Use it to get the value of current container item.
304*cdf0e10cSrcweir 
305*cdf0e10cSrcweir 			@seealso	-
306*cdf0e10cSrcweir 
307*cdf0e10cSrcweir 			@param		-
308*cdf0e10cSrcweir 			@return		A reference to value of container entry.
309*cdf0e10cSrcweir 
310*cdf0e10cSrcweir 			@onerror	-
311*cdf0e10cSrcweir 		*//*-*****************************************************************************************************/
312*cdf0e10cSrcweir 
313*cdf0e10cSrcweir 		inline typename TContainer::const_iterator getEntry()
314*cdf0e10cSrcweir 		{
315*cdf0e10cSrcweir 			// Warn programmer if he forget to initialize these object ...
316*cdf0e10cSrcweir             LOG_ASSERT2( m_pContainer==NULL, "CheckedIterator::getEntry()", "Object not initialized!" )
317*cdf0e10cSrcweir 			// or try to read a non existing element!
318*cdf0e10cSrcweir             LOG_ASSERT2( m_eEndState!=E_BEFOREEND, "CheckedIterator::getEntry()", "Wrong using of class detected!" )
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir             return m_pPosition;
321*cdf0e10cSrcweir 		}
322*cdf0e10cSrcweir 
323*cdf0e10cSrcweir 	//-------------------------------------------------------------------------------------------------------------
324*cdf0e10cSrcweir 	//	private member
325*cdf0e10cSrcweir 	//-------------------------------------------------------------------------------------------------------------
326*cdf0e10cSrcweir 
327*cdf0e10cSrcweir 	private:
328*cdf0e10cSrcweir 
329*cdf0e10cSrcweir 		// These enum defines our four states for an iterator position in curent container.
330*cdf0e10cSrcweir 		enum EEndState
331*cdf0e10cSrcweir 		{
332*cdf0e10cSrcweir 			E_UNKNOWN	,
333*cdf0e10cSrcweir 			E_BEFOREEND	,
334*cdf0e10cSrcweir 			E_END		,
335*cdf0e10cSrcweir 			E_AFTEREND
336*cdf0e10cSrcweir 		};
337*cdf0e10cSrcweir 
338*cdf0e10cSrcweir         const TContainer*           m_pContainer    ;   // pointer to current container
339*cdf0e10cSrcweir         EEndState                   m_eEndState     ;   // "position state" of iterator!
340*cdf0e10cSrcweir         typename TContainer::const_iterator  m_pPosition     ;   // point to actual element in container
341*cdf0e10cSrcweir };
342*cdf0e10cSrcweir 
343*cdf0e10cSrcweir }		//	namespace framework
344*cdf0e10cSrcweir 
345*cdf0e10cSrcweir #endif	//	#ifndef __FRAMEWORK_CLASSES_CHECKEDITERATOR_HXX_
346