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