1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
---|
3 | // See LICENSE.txt for details. |
---|
4 | |
---|
5 | #ifndef _MULTIPARAMLOAD_H_ |
---|
6 | #define _MULTIPARAMLOAD_H_ |
---|
7 | |
---|
8 | #include <stdio.h> |
---|
9 | #include "param.h" |
---|
10 | #include <frams/virtfile/virtfile.h> |
---|
11 | #include <frams/util/extvalue.h> |
---|
12 | |
---|
13 | /** This is the general "framsticks-format" file parser for loading multiple objects. |
---|
14 | http://www.framsticks.com/common/formatspec.html |
---|
15 | The loader can be configured to recognize multiple object types from object headers |
---|
16 | and automatically call ParamInterface::load for the matching class. |
---|
17 | |
---|
18 | Your code should repeatedly call MultiParamLoader::go() method and check the status after each call, until the end of file. |
---|
19 | The loader pauses before and/or after each object giving you a chance to perform your application-specific actions (see MultiParamLoader::breakOn()). |
---|
20 | If your application does not require any special actions, then the simple MultiParamLoader:run() can be used. |
---|
21 | 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". |
---|
22 | |
---|
23 | Typical usage scenarios: |
---|
24 | 1. Loading a file that contains at most one object of any given class: |
---|
25 | - declare the object class(es) - MultiParamLoader::addClass() |
---|
26 | - call MultiParamLoader::run() |
---|
27 | - and that's all, the records from the file will be loaded into the corresponding objects |
---|
28 | |
---|
29 | 2. Loading multiple objects and adding them to a list (see loadtest.cpp for a sample code that demonstrates this scenario) |
---|
30 | - declare the object class giving the empty "template" object - MultiParamLoader::addClass() |
---|
31 | - set breakOn(AfterObject) |
---|
32 | - call MultiParamLoader::go() in a loop |
---|
33 | - 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 |
---|
34 | (alternatively, one could breakOn(BeforeObject) and use MultiParamLoader::loadObjectNow(ParamInterface*) to load the incoming object into a newly created object). |
---|
35 | */ |
---|
36 | class MultiParamLoader |
---|
37 | { |
---|
38 | VirtFILE *file; |
---|
39 | SListTempl<VirtFILE*> filestack; |
---|
40 | char ownfile; |
---|
41 | PtrListTempl<ExtObject*> objects; |
---|
42 | int status; |
---|
43 | SString lasterror, lastcomment, lastunknown; |
---|
44 | ExtObject lastobject; |
---|
45 | int breakcond; |
---|
46 | Param emptyparam; |
---|
47 | bool aborting; |
---|
48 | |
---|
49 | void init(); |
---|
50 | |
---|
51 | int maybeBreak(int cond) |
---|
52 | { status=cond; return breakcond & cond; } |
---|
53 | |
---|
54 | VirtFILE* popstack(); |
---|
55 | void clearstack(); |
---|
56 | |
---|
57 | public: |
---|
58 | MultiParamLoader() {init();} |
---|
59 | MultiParamLoader(VirtFILE *f) {init(); load(f);} |
---|
60 | MultiParamLoader(const char* filename) {init(); load(filename);} |
---|
61 | |
---|
62 | ~MultiParamLoader() {abort();clearObjects();} |
---|
63 | |
---|
64 | void reset(); |
---|
65 | |
---|
66 | void load(); //< use previously opened file |
---|
67 | void load(VirtFILE *f); |
---|
68 | void load(const char* filename); |
---|
69 | |
---|
70 | /** Register the object class. Class names will be matched with object headers ("xxx:" in the input file). |
---|
71 | Used with breakOn(BeforeObject) and/or breakOn(AfterObject). |
---|
72 | 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. |
---|
73 | */ |
---|
74 | void addObject(ParamInterface *pi) {objects+=new ExtObject(pi);} |
---|
75 | void removeObject(ParamInterface *pi) {removeObject(ExtObject(pi));} |
---|
76 | void addObject(const ExtObject &o) {objects+=new ExtObject(o);} |
---|
77 | void removeObject(const ExtObject &o); |
---|
78 | int findObject(const ExtObject &o); |
---|
79 | void clearObjects(); |
---|
80 | |
---|
81 | /** To be used in the main loop: while(event=loader.go()) { ... } |
---|
82 | loader.go() will return on specified events (@see breakOn(), noBreakOn()), |
---|
83 | then you can handle the event and resume loading. |
---|
84 | */ |
---|
85 | virtual int go(); |
---|
86 | /** same value as 'go()' */ |
---|
87 | int getStatus() {return status;} |
---|
88 | int finished() {return (status==Finished);} |
---|
89 | |
---|
90 | VirtFILE *getFile() {return file;} |
---|
91 | |
---|
92 | /** Abort immediately and close the file if needed */ |
---|
93 | void abort(); |
---|
94 | /** @param conditions can be combined bitwise, eg. MultiParamLoader::BeforeObject | MultiParamLoader::OnComment |
---|
95 | @see BreakConfitions |
---|
96 | */ |
---|
97 | void breakOn(int conditions) {breakcond|=conditions;} |
---|
98 | void noBreakOn(int conditions) {breakcond&=~conditions;} |
---|
99 | /** |
---|
100 | These constants are used as arguments in breakOn(), and as status values from go() and getStatus(). |
---|
101 | The user code can define some classes for automatic recognition (using addClass()); such records can be read without performing any additional actions. |
---|
102 | |
---|
103 | - 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). |
---|
104 | |
---|
105 | - 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). |
---|
106 | |
---|
107 | - BeforeUnknown: unknown (not registered) object header detected. Like in BeforeObject, the application can skipObject() or loadObjectNow(). |
---|
108 | |
---|
109 | @see getClass(), GetClassName() |
---|
110 | */ |
---|
111 | enum BreakConditions { Finished=0, BeforeObject=1, AfterObject=2, |
---|
112 | BeforeUnknown=4, OnComment=8, OnError=16, Loading=32 }; |
---|
113 | |
---|
114 | /** Can be used BeforeObject and AfterObject */ |
---|
115 | ExtObject &getObject() {return lastobject;} |
---|
116 | /** Can be used BeforeUnknown, BeforeObject, AfterObject */ |
---|
117 | const SString& getObjectName() {return lastunknown;} |
---|
118 | void setObjectName(SString n) {lastunknown=n;} |
---|
119 | /** Unknown object will be loaded if you set its class BeforeUnknown */ |
---|
120 | void setObject(ParamInterface *pi) {lastobject=ExtObject(pi);} |
---|
121 | void setObject(const ExtObject& o) {lastobject=o;} |
---|
122 | /** Can be used OnComment */ |
---|
123 | const SString& getComment() {return lastcomment;} |
---|
124 | /** Can be used OnError */ |
---|
125 | const SString& getError() {return lasterror;} |
---|
126 | /** Can be used BeforeObject and BeforeUnknown */ |
---|
127 | int loadObjectNow(ParamInterface *pi,bool warn_unknown_fields=true) {return loadObjectNow(ExtObject(pi),warn_unknown_fields);} |
---|
128 | int loadObjectNow(const ExtObject &o,bool warn_unknown_fields=true); |
---|
129 | /** Can be used BeforeObject */ |
---|
130 | int loadObjectNow() {return loadObjectNow(getObject());} |
---|
131 | /** Can be used BeforeObject and BeforeUnknown. |
---|
132 | Object data will not be loaded. */ |
---|
133 | int skipObject() {return loadObjectNow(&emptyparam,false);} |
---|
134 | /** @return 1 if no errors */ |
---|
135 | int run(); |
---|
136 | |
---|
137 | void includeFile(SString& filename); |
---|
138 | bool returnFromIncluded(); |
---|
139 | bool alreadyIncluded(const char* filename); |
---|
140 | |
---|
141 | }; |
---|
142 | |
---|
143 | #endif |
---|
144 | |
---|