00001 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- 00002 * vim:expandtab:autoindent:tabstop=4:shiftwidth=4:filetype=c:cindent:textwidth=0: 00003 * 00004 * Copyright (C) 2005 Dell Inc. 00005 * by Michael Brown <Michael_E_Brown@dell.com> 00006 * Licensed under the Open Software License version 2.1 00007 * 00008 * Alternatively, you can redistribute it and/or modify 00009 * it under the terms of the GNU General Public License as published 00010 * by the Free Software Foundation; either version 2 of the License, 00011 * or (at your option) any later version. 00012 00013 * This program is distributed in the hope that it will be useful, but 00014 * WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00016 * See the GNU General Public License for more details. 00017 */ 00018 00019 #define LIBSMBIOS_SOURCE 00020 #include "TokenImpl.h" 00021 00022 using namespace std; 00023 00024 namespace smbios 00025 { 00026 ConstTokenTableIterator::ConstTokenTableIterator (const ITokenTable * initialTable, int typeToMatch) 00027 : TokenTableIteratorBase( initialTable, typeToMatch ) 00028 {} 00029 00030 TokenTableIterator::TokenTableIterator (const ITokenTable *initialTable, int typeToMatch) 00031 : TokenTableIteratorBase( initialTable, typeToMatch ) 00032 {} 00033 00034 00035 TokenTableIteratorBase::TokenTableIteratorBase (const ITokenTable *initialTable, int typeToMatch) 00036 :matchType(typeToMatch), table(initialTable), current(-1) 00037 { 00038 if( table == 0 ) 00039 current = -2; 00040 00041 incrementIterator(); 00042 } 00043 00044 void TokenTableIteratorBase::reset() 00045 { 00046 current=0; 00047 incrementIterator(); 00048 } 00049 00050 bool TokenTableIteratorBase::eof() 00051 { 00052 return (current < 0); 00053 } 00054 00055 IToken& TokenTableIterator::operator * () const 00056 { 00057 return *(const_cast<TokenTableIterator *>(this)->dereference()); 00058 } 00059 00060 IToken* TokenTableIterator::operator -> () const 00061 { 00062 return const_cast<TokenTableIterator *>(this)->dereference(); 00063 } 00064 00065 const IToken& ConstTokenTableIterator::operator * () const 00066 { 00067 return *dereference(); 00068 } 00069 00070 const IToken* ConstTokenTableIterator::operator -> () const 00071 { 00072 return dereference(); 00073 } 00074 00075 00076 const IToken * TokenTableIteratorBase::dereference () const 00077 { 00078 return const_cast<TokenTableIteratorBase *>(this)->dereference(); 00079 } 00080 00081 IToken * TokenTableIteratorBase::dereference () 00082 { 00083 const TokenTable *CTTable = dynamic_cast<const TokenTable *>(table); 00084 if( current >= 0 && static_cast<unsigned int>(current) >= CTTable->tokenList.size() ) 00085 current = -2; // should _never_ get here. 00086 if( current > -1 ) 00087 { 00088 return CTTable->tokenList[current] ; 00089 } 00090 throw DerefNullPointerImpl("tried to dereference non-existent token"); 00091 } 00092 00093 void TokenTableIteratorBase::incrementIterator() 00094 { 00095 if( current == -2 ) 00096 return; 00097 00098 const TokenTable *CTTable = dynamic_cast<const TokenTable *>(table); 00099 size_t size = CTTable->tokenList.size(); 00100 do 00101 { 00102 ++current; 00103 } 00104 while( 00105 matchType != -1 && 00106 current >= 0 && 00107 static_cast<unsigned int>(current) < size && 00108 CTTable->tokenList[current]->getType() != static_cast<u32>(matchType) 00109 ); 00110 00111 // don't overflow the size of the table. 00112 // be careful about signed vs unsigned comparisons. 00113 if( current >= 0 && static_cast<unsigned int>(current) >= size ) 00114 current = -2; 00115 00116 return; 00117 } 00118 00119 //Prefix ++ 00120 TokenTableIterator & TokenTableIterator::operator ++ () 00121 { 00122 if( current > -1 ) 00123 incrementIterator(); 00124 return *this; 00125 } 00126 00127 //Postfix ++ 00128 const TokenTableIterator TokenTableIterator::operator ++ (int) 00129 { 00130 const TokenTableIterator oldValue = *this; 00131 ++(*this); 00132 return oldValue; 00133 } 00134 00135 //Prefix ++ 00136 ConstTokenTableIterator & ConstTokenTableIterator::operator ++ () 00137 { 00138 if( current > -1 ) 00139 incrementIterator(); 00140 return *this; 00141 } 00142 00143 //Postfix ++ 00144 const ConstTokenTableIterator ConstTokenTableIterator::operator ++ (int) 00145 { 00146 const ConstTokenTableIterator oldValue = *this; 00147 ++(*this); 00148 return oldValue; 00149 } 00150 00151 }