vtkbone
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
CommandStyleFileReader Class Referenceabstract

An abstract base class for reading data files that are structured as text with a fixed maximum line length, and consist of a series of commands. More...

#include <CommandStyleFileReader.h>

Inheritance diagram for CommandStyleFileReader:
Inheritance graph
[legend]

Public Types

typedef int(CommandStyleFileReader::* CommandHandler_t) ()
 
typedef std::map< std::string, CommandHandler_tCommandContext_t
 

Public Member Functions

 CommandStyleFileReader (std::istream &stream)
 
virtual int Read ()
 
virtual void DebugOn ()
 
virtual void DebugOff ()
 
virtual void SetDebug (int d)
 
int GetErrorStatus ()
 
const char * GetErrorMsg ()
 

Protected Member Functions

int RegisterCommandHandler (const char *commandName, CommandHandler_t handler)
 
virtual int CallCommandHandler (CommandHandler_t handler)
 
virtual int Finish ()
 
virtual int GetLine ()
 
virtual int IsCommand ()=0
 
virtual int IsCommentLine ()=0
 
virtual int FindCommand ()
 
virtual int ProcessCommand ()
 
virtual void SetError (const std::string &msg)
 
virtual void DebugMessage (const std::string &msg)
 
virtual void WarningMessage (const std::string &msg)
 

Protected Attributes

std::istream & stream
 
std::string commandName
 
long lineCount
 
int repeatLastCommand
 
std::string line
 
std::stack< CommandContext_tcommandContextStack
 
int debug
 
int errorStatus
 
std::string errorMsg
 

Detailed Description

An abstract base class for reading data files that are structured as text with a fixed maximum line length, and consist of a series of commands.

Error reporting functionality is also provided. The class works on a generic istream object, which can be either a file stream or a string stream.

You must provide the actual functionality to process any commands that you desire in a concrete derived class.

This class uses many virtual methods that can be reimplemented in your derived class in order to customize functionality.

BASIC INSTRUCTIONS on implementing a derived class to process a specific file type:

  1. Implement IsCommandBegin, IsCommandEnd and IsCommentLine in your derived class. These methods identify command beginnings and endings, as well as comment lines. There are no default implementations of these methods, because file syntax is so variable.
  2. Implement methods in your derived class to handle particular commands. Once implented, register them with the base class. This is done with a call like following, which you can put in your constructor:

    RegisterCommandHandler ("SOME SECTION NAME", reinterpret_cast<CommandHandler_t>(&MyClass::MyHandlerMethod));

  3. ("Somewhat" optional). Implement CallCommandHandler in your derived class. It only needs to cast its argument back to a member function pointer for your derived class, and call it. Like this:

    int MyClass::CallCommandHandler (CommandHandler_t handler) { typedef int (MyClass::*DerivedHandler_t)(); DerivedHandler_t f = reinterpret_cast<DerivedHandler_t>(handler); return (this->*f)(); }

    See the discussion below on why you can (usually) skip this step.

ADDITIONAL TIPS:

NOTE and WARNING about CallCommandHandler:

The Command Handlers table is implemented with member function pointers. This is a little-used (although occasionally useful) corner of C++, with some unfortunate quirks. One being, that according to the standard, member function pointers are specific to a particular class, and cannot even be converted between base and derived classes (this is controversial, since they would be more useful if they could be). Hence the necessity of reinterpret_cast<CommandHandler_t> in passing the method as an argument to RegisterCommandHandler.

Strictly speaking, the member function pointer must be cast back to be a member function pointer of the derived class before being called. In practice however, providing your derived class uses only single inheritance (rather than multiple inheritance), it works to simply call the member function pointer cast to the base class (which is how it is stored in the table). This is convenient, because it can then be called directly from the base class. This works because if B inherits from A, then in practice all compilers store A at the beginning of B. This allows the same "this" pointer to be used for both. Obviously this no longer works in the case of multiple inheritance (B inherits from A and some other class C - they can't both be at the beginning of A).

Therefore, for lazy people (me included), I have provided a default CallCommandHandler in the base class that calls the command handler without the nominally required recast back to the derived class. If you are pedantic or want to safely use of multiple inheritance in your derived class, then you should add CallCommandHandler to your derived class using the template above.

One more annoyance is that Microsoft compilers, depending on compiler flags, might use different sizes for base and derived class member function pointers. (Yes! Really!) This breaks the C++ standard and it also breaks this code. The relevant compiler flag is /vmg, and it should be set - always.

For an example of a file reader implemented using this class, see AbaqusReaderHelper.

Definition at line 137 of file CommandStyleFileReader.h.

Member Typedef Documentation

◆ CommandHandler_t

typedef int(CommandStyleFileReader::* CommandStyleFileReader::CommandHandler_t) ()

Definition at line 143 of file CommandStyleFileReader.h.

◆ CommandContext_t

Definition at line 146 of file CommandStyleFileReader.h.

Constructor & Destructor Documentation

◆ CommandStyleFileReader()

CommandStyleFileReader::CommandStyleFileReader ( std::istream &  stream)

Member Function Documentation

◆ Read()

virtual int CommandStyleFileReader::Read ( )
virtual

◆ DebugOn()

virtual void CommandStyleFileReader::DebugOn ( )
inlinevirtual

Definition at line 157 of file CommandStyleFileReader.h.

◆ DebugOff()

virtual void CommandStyleFileReader::DebugOff ( )
inlinevirtual

Definition at line 158 of file CommandStyleFileReader.h.

◆ SetDebug()

virtual void CommandStyleFileReader::SetDebug ( int  d)
inlinevirtual

Definition at line 159 of file CommandStyleFileReader.h.

◆ GetErrorStatus()

int CommandStyleFileReader::GetErrorStatus ( )
inline

Definition at line 161 of file CommandStyleFileReader.h.

◆ GetErrorMsg()

const char * CommandStyleFileReader::GetErrorMsg ( )
inline

Definition at line 162 of file CommandStyleFileReader.h.

◆ RegisterCommandHandler()

int CommandStyleFileReader::RegisterCommandHandler ( const char *  commandName,
CommandHandler_t  handler 
)
protected

◆ CallCommandHandler()

virtual int CommandStyleFileReader::CallCommandHandler ( CommandHandler_t  handler)
protectedvirtual

◆ Finish()

virtual int CommandStyleFileReader::Finish ( )
inlineprotectedvirtual

Reimplemented in AbaqusInputReaderHelper.

Definition at line 181 of file CommandStyleFileReader.h.

◆ GetLine()

virtual int CommandStyleFileReader::GetLine ( )
protectedvirtual

Reimplemented in AbaqusInputReaderHelper.

◆ IsCommand()

virtual int CommandStyleFileReader::IsCommand ( )
protectedpure virtual

Implemented in AbaqusInputReaderHelper.

◆ IsCommentLine()

virtual int CommandStyleFileReader::IsCommentLine ( )
protectedpure virtual

Implemented in AbaqusInputReaderHelper.

◆ FindCommand()

virtual int CommandStyleFileReader::FindCommand ( )
protectedvirtual

◆ ProcessCommand()

virtual int CommandStyleFileReader::ProcessCommand ( )
protectedvirtual

◆ SetError()

virtual void CommandStyleFileReader::SetError ( const std::string &  msg)
protectedvirtual

◆ DebugMessage()

virtual void CommandStyleFileReader::DebugMessage ( const std::string &  msg)
protectedvirtual

Reimplemented in AbaqusInputReaderHelper.

◆ WarningMessage()

virtual void CommandStyleFileReader::WarningMessage ( const std::string &  msg)
protectedvirtual

Reimplemented in AbaqusInputReaderHelper.

Member Data Documentation

◆ stream

std::istream& CommandStyleFileReader::stream
protected

Definition at line 226 of file CommandStyleFileReader.h.

◆ commandName

std::string CommandStyleFileReader::commandName
protected

Definition at line 227 of file CommandStyleFileReader.h.

◆ lineCount

long CommandStyleFileReader::lineCount
protected

Definition at line 228 of file CommandStyleFileReader.h.

◆ repeatLastCommand

int CommandStyleFileReader::repeatLastCommand
protected

Definition at line 229 of file CommandStyleFileReader.h.

◆ line

std::string CommandStyleFileReader::line
protected

Definition at line 231 of file CommandStyleFileReader.h.

◆ commandContextStack

std::stack<CommandContext_t> CommandStyleFileReader::commandContextStack
protected

Definition at line 233 of file CommandStyleFileReader.h.

◆ debug

int CommandStyleFileReader::debug
protected

Definition at line 235 of file CommandStyleFileReader.h.

◆ errorStatus

int CommandStyleFileReader::errorStatus
protected

Definition at line 237 of file CommandStyleFileReader.h.

◆ errorMsg

std::string CommandStyleFileReader::errorMsg
protected

Definition at line 238 of file CommandStyleFileReader.h.


The documentation for this class was generated from the following file: