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 | */ |
---|
34 | class MultiParamLoader |
---|
35 | { |
---|
36 | VirtFILE *file; |
---|
37 | SListTempl<VirtFILE*> filestack; |
---|
38 | char ownfile; |
---|
39 | SList params; |
---|
40 | int status; |
---|
41 | SString lasterror, lastcomment, lastunknown; |
---|
42 | ParamInterface *lastclass; |
---|
43 | int breakcond; |
---|
44 | Param emptyparam; |
---|
45 | |
---|
46 | void init(); |
---|
47 | void load(); |
---|
48 | |
---|
49 | int maybeBreak(int cond) |
---|
50 | { status=cond; return breakcond & cond; } |
---|
51 | |
---|
52 | VirtFILE* popstack(); |
---|
53 | void clearstack(); |
---|
54 | |
---|
55 | public: |
---|
56 | MultiParamLoader() {init();} |
---|
57 | MultiParamLoader(VirtFILE *f) {init(); load(f);} |
---|
58 | MultiParamLoader(const char* filename) {init(); load(filename);} |
---|
59 | |
---|
60 | ~MultiParamLoader() {abort();} |
---|
61 | |
---|
62 | void reset(); |
---|
63 | |
---|
64 | void load(VirtFILE *f); |
---|
65 | void 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 | */ |
---|
71 | void addClass(ParamInterface *pi) {params+=pi;} |
---|
72 | void removeClass(ParamInterface *pi) {params-=pi;} |
---|
73 | void 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 | */ |
---|
79 | virtual int go(); |
---|
80 | /** same value as 'go()' */ |
---|
81 | int getStatus() {return status;} |
---|
82 | int finished() {return (status==Finished);} |
---|
83 | |
---|
84 | VirtFILE *getFile() {return file;} |
---|
85 | |
---|
86 | /** Abort immediately and close the file if needed */ |
---|
87 | void abort(); |
---|
88 | /** @param conditions can be combined bitwise, eg. MultiParamLoader::BeforeObject | MultiParamLoader::OnComment |
---|
89 | @see BreakConfitions |
---|
90 | */ |
---|
91 | void breakOn(int conditions) {breakcond|=conditions;} |
---|
92 | void 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 | */ |
---|
105 | enum BreakConditions { Finished=0, BeforeObject=1, AfterObject=2, |
---|
106 | BeforeUnknown=4, OnComment=8, OnError=16, Loading=32 }; |
---|
107 | |
---|
108 | /** Can be used BeforeObject and AfterObject */ |
---|
109 | ParamInterface *getClass() {return lastclass;} |
---|
110 | /** Can be used BeforeUnknown, BeforeObject, AfterObject */ |
---|
111 | const SString& getClassName() {return lastunknown;} |
---|
112 | const void setClassName(SString n) {lastunknown=n;} |
---|
113 | /** Unknown object will be loaded if you set its class BeforeUnknown */ |
---|
114 | void setClass(ParamInterface *pi) {lastclass=pi;} |
---|
115 | /** Can be used OnComment */ |
---|
116 | const SString& getComment() {return lastcomment;} |
---|
117 | /** Can be used OnError */ |
---|
118 | const SString& getError() {return lasterror;} |
---|
119 | /** Can be used BeforeObject and BeforeUnknown */ |
---|
120 | int loadObjectNow(ParamInterface *pi); |
---|
121 | /** Can be used BeforeObject */ |
---|
122 | int loadObjectNow() {return loadObjectNow(getClass());} |
---|
123 | /** Can be used BeforeObject and BeforeUnknown. |
---|
124 | Object data will not be loaded. */ |
---|
125 | int skipObject() {return loadObjectNow(&emptyparam);} |
---|
126 | /** @return 1 if no errors */ |
---|
127 | int run(); |
---|
128 | |
---|
129 | void include(SString& filename); |
---|
130 | bool returnFromIncluded(); |
---|
131 | bool alreadyIncluded(const char* filename); |
---|
132 | |
---|
133 | }; |
---|
134 | |
---|
135 | #endif |
---|
136 | |
---|