source: cpp/gdk/multiparamload.h @ 93

Last change on this file since 93 was 74, checked in by Maciej Komosinski, 13 years ago

improved documentation, compilation, and precision of numbers parsed

  • Property svn:eol-style set to native
File size: 6.1 KB
Line 
1// This file is a part of the Framsticks GDK library.
2// Copyright (C) 2002-2011  Szymon Ulatowski.  See LICENSE.txt for details.
3// Refer to http://www.framsticks.com/ for further information.
4
5#ifndef _MULTIPARAMLOAD_H_
6#define _MULTIPARAMLOAD_H_
7
8#include <stdio.h>
9#include "param.h"
10#include "virtfile.h"
11
12/** This is the general "framsticks-format" file parser for loading multiple objects.
13    The loader can be configured to recognize multiple object types from object headers
14    and automatically call ParamInterface::load for the matching class.
15   
16    Your code should repeatedly call MultiParamLoader::go() method and check the status after each call, until the end of file.
17    The loader pauses before and/or after each object giving you a chance to perform your application-specific actions (see MultiParamLoader::breakOn()).
18    If your application does not require any special actions, then the simple MultiParamLoader:run() can be used.
19    The word "record" (and "record type") used in this description refer to the textual form of a serialized object - this is to avoid confusion with 'live' objects passed to the loader methods. "Record type" can be the same as the class name, but it does not have to be the same. For example, the most common record type for storing the Genotype object is called "org" (think: organism) instead of "Genotype".
20
21    Typical usage scenarios:
22    1. Loading a file that contains at most one object of any given class:
23    - declare the object class(es) - MultiParamLoader::addClass()
24    - call MultiParamLoader::run()
25    - and that's all, the records from the file will be loaded into the corresponding objects
26
27    2. Loading multiple objects and adding them to a list (see loadtest.cpp for a sample code that demonstrates this scenario)
28    - declare the object class giving the empty "template" object - MultiParamLoader::addClass()
29    - set breakOn(AfterObject)
30    - call MultiParamLoader::go() in a loop
31    - the returned status will be equal to AfterObject each time an object is loaded. One can detect this condition and create the real object from our template object
32    (alternatively, one could breakOn(BeforeObject) and use MultiParamLoader::loadObjectNow(ParamInterface*) to load the incoming object into a newly created object).   
33 */
34class MultiParamLoader
35{
36VirtFILE *file;
37SListTempl<VirtFILE*> filestack;
38char ownfile;
39SList params;
40int status;
41SString lasterror, lastcomment, lastunknown;
42ParamInterface *lastclass;
43int breakcond;
44Param emptyparam;
45
46void init();
47void load();
48
49int maybeBreak(int cond)
50{ status=cond; return breakcond & cond; }
51
52VirtFILE* popstack();
53void clearstack();
54
55  public:
56MultiParamLoader() {init();}
57MultiParamLoader(VirtFILE *f) {init(); load(f);}
58MultiParamLoader(const char* filename) {init(); load(filename);}
59
60~MultiParamLoader() {abort();}
61
62void reset();
63
64void load(VirtFILE *f);
65void load(const char* filename);
66
67/** Register the object class. Class names will be matched with object headers ("xxx:" in the input file).
68    Used with breakOn(BeforeObject) and/or breakOn(AfterObject).
69    Note that registered classes will only work when the record name matches the class name, otherwise breakOn(BeforeUnknown) must be used and then getClassName() to check for the expected record.
70 */
71void addClass(ParamInterface *pi) {params+=pi;}
72void removeClass(ParamInterface *pi) {params-=pi;}
73void clearClasses() {params.clear();}
74
75/** To be used in the main loop: while(event=loader.go()) { ... }
76    loader.go() will return on specified events (@see breakOn(), noBreakOn()),
77    then you can handle the event and resume loading.
78 */
79virtual int go();
80/** same value as 'go()' */
81int getStatus() {return status;}
82int finished() {return (status==Finished);}
83
84VirtFILE *getFile() {return file;}
85
86/** Abort immediately and close the file if needed */
87void abort();
88/** @param conditions can be combined bitwise, eg. MultiParamLoader::BeforeObject |  MultiParamLoader::OnComment
89    @see BreakConfitions
90*/
91void breakOn(int conditions) {breakcond|=conditions;}
92void noBreakOn(int conditions) {breakcond&=~conditions;}
93/**
94   These constants are used as arguments in breakOn(), and as status values from go() and getStatus().
95   The user code can define some classes for automatic recognition (using addClass()); such records can be read without performing any additional actions.
96   
97   - BeforeObject: found an object with recognized classname (addClass()). Application code can choose to skip the incoming record (skipObject()), redirect the incoming data into a different object (loadObjectNow(ParamInterface*)), or do nothing for default behavior (loading into previously registered object).
98
99   - AfterObject: the object was loaded into the registered class interface (addClass()). This is to allow for additional actions after loading the object (e.g. data validation).
100
101   - BeforeUnknown: unknown (not registered) object header detected. Like in BeforeObject, the application can skipObject() or loadObjectNow().
102
103@see getClass(), GetClassName()
104 */
105enum BreakConditions { Finished=0, BeforeObject=1, AfterObject=2,
106                       BeforeUnknown=4, OnComment=8, OnError=16, Loading=32 };
107
108/** Can be used BeforeObject and AfterObject */
109ParamInterface *getClass() {return lastclass;}
110/** Can be used BeforeUnknown, BeforeObject, AfterObject */
111const SString& getClassName() {return lastunknown;}
112const void setClassName(SString n) {lastunknown=n;}
113/** Unknown object will be loaded if you set its class BeforeUnknown */
114void setClass(ParamInterface *pi) {lastclass=pi;}
115/** Can be used OnComment */
116const SString& getComment() {return lastcomment;}
117/** Can be used OnError */
118const SString& getError() {return lasterror;}
119/** Can be used BeforeObject and BeforeUnknown */
120int loadObjectNow(ParamInterface *pi);
121/** Can be used BeforeObject */
122int loadObjectNow() {return loadObjectNow(getClass());}
123/** Can be used BeforeObject and BeforeUnknown.
124    Object data will not be loaded. */
125int skipObject() {return loadObjectNow(&emptyparam);}
126/** @return 1 if no errors */
127int run();
128
129void include(SString& filename);
130bool returnFromIncluded();
131bool alreadyIncluded(const char* filename);
132
133};
134
135#endif
136
Note: See TracBrowser for help on using the repository browser.