|
|
A TSE3 utility class.
This class can be used by Serializable objects to ease writing a block parser. Provides facilities for reading a whole block (enclosed by "{" and "}") in from an input stream, and act on the data contained within it by either loading subblocks or passing on data lines to helper objects of type FileItemParser.
The advantage of using this class is that it saves a lot of tedious stream parsing code, and prevents silly mistakes like not handling blocks that are not recognised (in this verison of the TSE3 library the Metronome does not have any sub-blocks, but who's to say there won't be some in future versions?)
FileBlockParser ()
| FileBlockParser |
Creates a FileBlockParser set up to do absolutely nothing. Use the add methods to attach items and sub-blocks.
void add (const std::string &name, Serializable *block)
| add |
Add a sub-block to those understood by this parser. You specify a Serializable object to parse the sub-block, not another FileBlockParser. This is to implement a lazy form of object creation - the Serializable may create another FileBlockParser in it's load method, but only when it is called. If it's block is not present, this FileBlockParser will not be created. This prevents many many FileBlockParsers being created before any of the file is Parsed. It is also useful in the possible presence of cycles in the hierarchy of Serializable object.
Parameters:
name | Block identification name. |
block | Serializable object that will parse the block. |
void add (const std::string &name, FileItemParser *item)
| add |
Add an item to those recoginsed by this parser. These items consist of lines of the form "Identifier:Data".
The FileItemParser that handles a particular handle will be sent the "Data" part of the input line with the "Identifier:" part stripped off.
Parameters:
name | String that identifies the line as a particular item type. |
item | FileItemParser to call to handle the data from this line. |
void add (FileItemParser *item)
| add |
Add a catch-all FileItemParser to handle any lines not regnoised in the list of named items. There can only be one of these registered at one time. If there is no catch-all handler registered than unrecognised items in the block will be ignored.
The catch-all FileItemParser will be called with the contents of the entire line. Note that the "Identifier:" part IS NOT stipped.
Parameters:
item | The catch all FileItemParser |
void parse (std::istream &in, SerializableLoadInfo &info)
| parse |
Parse the block in the istream in.
The block tag will have been consumed by the enclosing block parser, and the next line to be parsed will be the opening "{". The parser will consume all lines up to the block's closing "}".
All sub-blocks that are recognised will be sent to the corresponding Serializable object for parsing. Any unrecognised sub-blocks will be skipped.
All items that are recognised will be sent to the corresponding FileItemParser. Any unrecognised items will be sent to the catch-all handler. If there is no catch-all handler then the line will be ignored.
Parameters:
in | istream to take input from |
info | Object holding information on the input stream |