Attachment 'SimpleParserBase.hh'

Download

   1 #ifndef __SIMPLEPARSERBASE_HH__
   2 #define __SIMPLEPARSERBASE_HH__
   3 
   4 #include <string>
   5 #include <map>
   6 
   7 class SimpleParserBase
   8 {
   9 public:
  10   void parse( const char* filename );
  11   virtual ~SimpleParserBase();
  12 protected:
  13   /// split single line into "token" and "value"
  14   virtual bool parseLineBase( std::string& line );
  15   /// pass the "value" to token hanlding routine
  16   virtual bool parseLine( std::string& line );
  17   
  18   std::string _currentToken;
  19   std::string _currentValue;
  20 
  21   /// remove comments from line
  22   void trim( std::string& line ); 
  23 
  24 private:
  25   virtual void cleanLine( std::string& line );
  26 
  27 protected:
  28   template <class ParserT>
  29   bool registerTokenHandler( std::string, bool( ParserT::* )(std::string&) );
  30 
  31   class TokenHandlerBase;
  32   typedef std::map< std::string, TokenHandlerBase* > TokenHandlerMap;
  33   TokenHandlerMap tokenHandlerMap;
  34 
  35   class TokenHandlerBase
  36   {
  37   public:
  38     virtual bool process( SimpleParserBase*, std::string& )=0;
  39     virtual ~TokenHandlerBase();//bug in gcc 3.4.x?{};
  40   };
  41 
  42   template <class ParserT>
  43   class TokenHandler: public TokenHandlerBase
  44   {
  45   public:
  46     TokenHandler( bool (ParserT::*)( std::string&) );
  47     virtual bool process( SimpleParserBase*, std::string& );
  48   private:
  49     bool(ParserT::* _handler)(std::string&);
  50   };
  51 
  52 };
  53 
  54 
  55 #include <iostream>
  56 //======================== template definitions =========================
  57 template <class ParserT>
  58 bool SimpleParserBase::registerTokenHandler( std::string token, bool( ParserT::* handler)(std::string&) )
  59 {
  60   if ( tokenHandlerMap.count( token ) != 0 )
  61     {
  62       std::cerr << "SimpleParserBase: internal error. Token already registered. Please report this bug!" << std::endl;
  63       return false;
  64     }
  65   TokenHandlerBase* handlerClass = 
  66     new TokenHandler<ParserT>(handler);
  67   tokenHandlerMap[ token ] = handlerClass;
  68   return true;
  69 }
  70 
  71 template <class ParserT>
  72 SimpleParserBase::TokenHandler<ParserT>::TokenHandler
  73 ( bool (ParserT::*handler)( std::string& ) ):_handler(handler)
  74 { }
  75 
  76 template <class ParserT>
  77 bool SimpleParserBase::TokenHandler<ParserT>::process
  78 ( SimpleParserBase* parser, std::string& line )
  79 {
  80   ParserT* castedParser = static_cast<ParserT*>(parser);
  81   return (castedParser->*_handler)( line );
  82 }
  83 
  84 
  85 #endif //__SIMPLEPARSERBASE_HH__
  86 

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2009-04-15 11:34:30, 1.3 KB) [[attachment:ReaderParser.cc]]
  • [get | view] (2009-04-15 11:34:36, 0.6 KB) [[attachment:ReaderParser.hh]]
  • [get | view] (2009-04-15 11:33:32, 1.9 KB) [[attachment:SimpleParserBase.cc]]
  • [get | view] (2009-04-15 11:34:07, 2.2 KB) [[attachment:SimpleParserBase.hh]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.