Hosting courtesy of Sourceforge

SourceForge Logo
Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

ListParser.h

Go to the documentation of this file.
00001 //
00002 //      Copyright (C) 2002 Robert Renaud
00003 //
00004 //      This program is free software; you can redistribute it and/or
00005 //      modify it under the terms of the GNU General Public License
00006 //      as published by the Free Software Foundation; either version 2
00007 //      of the License, or (at your option) any later version.
00008 //
00009 //      This program is distributed in the hope that it will be useful,
00010 //      but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00012 //
00013 //      See the GNU General Public License for more details.
00014 //
00015 //      You should have received a copy of the GNU General Public License
00016 //      along with this program; if not, write to the Free Software
00017 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 //
00019 
00020 #ifndef LIST_PARSER_H_
00021 #define LIST_PARSER_H_
00022 
00023 #include "../Config.h"
00024 
00025 #include <list>
00026 #include <vector>
00027 
00028 #include "ParseDebug.h"
00029 #include "ParseAction.h"
00030 #include "Parse.h"
00031 
00032 using namespace std;
00033 
00034 namespace Parse {
00035         
00037         class Parseable {
00038         public:
00042                 virtual Parser* makeParserToParseMe(istream &inputFile)=0;
00043                 virtual ~Parseable() { }
00044         };
00045         
00050         template <class T>      class ParserGenerator {
00051         public:
00058                 virtual Parser* generateParser(istream &inputFile, T& item)=0;
00059                 virtual ~ParserGenerator() { }
00060         };
00061 
00062 
00069         template <class Base, class Derived> class NaturalParser : public ParseAction {
00070         public:
00074                 NaturalParser(Base*& pointerRef) :
00075                         containerPtr(pointerRef)
00076                 { }
00077                 
00081                 void getValue(std::istream& in) {
00082                         containerPtr = new Derived();
00083                         Parser* p = containerPtr->makeParserToParseMe(in);
00084                         p->read();
00085                         delete p;
00086                 }                                       
00087         private:
00088                 Base*& containerPtr;
00089         };
00091         template <class Base> class ListParser : public ParseAction {
00092         public:
00096                 ListParser(list<Base*> & listToAddTo) :
00097                   listOfBaseptr(listToAddTo) { }
00098 
00099         protected:
00100                 /* reference to external list of Base* passed to it, used in ctor */
00101                 list<Base*>& listOfBaseptr;
00102         }; // class ListParser
00103 
00109         template <class Base> class GeneratedListParser : public ListParser<Base> {
00110         public:
00118                 GeneratedListParser(list<Base*>& listToAddTo, ParserGenerator<Base>* dynPtrParseGen)
00119                         :         ListParser<Base>(listToAddTo),
00120                         pg(dynPtrParseGen)
00121                 {   }
00122 
00127                 virtual void getValue(istream& input) {
00128                         Base* basePtr = new Base();
00129                         Parser* p= pg->generateParser(input, *basePtr);
00130                         p->read();
00131                         listOfBaseptr.push_back(basePtr);
00132                         delete p;
00133                 }
00134 
00136                 virtual ~GeneratedListParser() {
00137                         delete pg;
00138                 }
00139         protected:
00140                 ParserGenerator<Base>* pg;
00141         }; // class GeneratedListParser<T>
00142 
00144         template <class Base> class NaturalListParser : public ListParser<Base> {
00145         public:
00148                 NaturalListParser(list<Base*>& listToAddTo) :
00149                   ListParser<Base>(listToAddTo)   { }
00150                   
00154                   virtual void getValue(istream& input) {
00155                           Base* basePtr = new Base();
00156                           Parser* p = basePtr->makeParserToParseMe(input);      
00157                           p->read();
00158                           listOfBaseptr.push_back(basePtr);
00159                           delete p;
00160                   }
00161         }; // class GeneratedListParser<T>
00162         
00164         template <class Base> class VectorParser : public ParseAction {
00165         public:
00169                 VectorParser(vector<Base*> & vectorToAddTo) :
00170                   vectorOfBaseptr(vectorToAddTo) { }
00171                   
00172         protected:              
00173                 /* reference to external vector of T* passed to it, used in ctor */
00174                 vector<Base*>& vectorOfBaseptr;
00175         }; // class VectorParser
00176         
00177         
00181         template <class Base, class Derived = Base> class NaturalVectorParser : public VectorParser<Base> {
00182         public:
00187                 NaturalVectorParser(vector<Base*>& vectorToAddTo) :
00188                   VectorParser<Base>(vectorToAddTo)   { }
00189                   
00197                 virtual void getValue(istream& input) {
00198                         Base* basePtr = new Derived;
00199                         Parser* p = basePtr->makeParserToParseMe(input);                                
00200 
00201                         size_t index;
00202                         bool indexWasRead=false;
00203                         p->addAction("index", new ReadAndCheck<size_t>(index, indexWasRead));
00204 
00205                         p->read();
00206 
00207                         if (indexWasRead) {
00208                                 if (index >= vectorOfBaseptr.size()) vectorOfBaseptr.resize(index+1, NULL);
00209                                 vectorOfBaseptr[index] = basePtr;
00210                         } else {
00211                                 vectorOfBaseptr.push_back(basePtr);
00212                         }
00213                           
00214                         delete p;
00215                 }
00216         }; // class GeneratedListParser<Base, Derived>  
00217         
00223         template <class Base, class Derived=Base> class GeneratedVectorParser : public VectorParser<Base> {
00224         public:
00231                 GeneratedVectorParser(vector<Base*>& vectorToAddTo, ParserGenerator<Derived>* dynPtrParseGen)
00232                         :         VectorParser<Base>(vectorToAddTo),    
00233                         pg(dynPtrParseGen)
00234                 {   }
00235                 
00242                 virtual void getValue(istream& input) {
00243                         Derived* derivedPtr = new Derived();
00244                         Parser* p= pg->generateParser(input, *derivedPtr);
00245                         int index;
00246                         bool indexWasRead=false;
00247                         p->addAction("index", new ReadAndCheck<int>(index, indexWasRead));
00248                         p->read();
00249                         if (indexWasRead) {
00250                                 vectorOfBaseptr[index] = derivedPtr;
00251                         } else {
00252                                 vectorOfBaseptr.push_back(derivedPtr);
00253                         }
00254                         delete p;
00255                 }       
00256                 
00258                 ~GeneratedVectorParser() {
00259                         delete pg;
00260                 }
00261         protected:
00262                 ParserGenerator<Derived>* pg;
00263         }; // class GeneratedVectorParser
00264 
00265         
00266 }; // namespace Parse
00267 
00268 #endif // ifndef LIST_PARSER_H_
00269 

Generated on Tue May 21 07:26:51 2002 for BomberLAN by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001