namespace DataFile {
Loader::Loader():
+ actions(0),
cur_st(0),
direct(false),
check_sub_loads(false)
Loader::~Loader()
{
- for(ActionMap::iterator i=actions.begin(); i!=actions.end(); ++i)
+ for(ActionMap::iterator i=local_actions.begin(); i!=local_actions.end(); ++i)
delete i->second;
}
void Loader::load(Parser &p)
{
+ if(!actions)
+ throw logic_error("no actions");
+
while(p)
{
if(p.peek(0))
void Loader::load(const Statement &st)
{
+ if(!actions)
+ throw logic_error("no actions");
+
for(list<Statement>::const_iterator i=st.sub.begin(); i!=st.sub.end(); ++i)
load_statement(*i);
finish();
throw logic_error("no current statement");
}
+void Loader::set_actions(ActionMap &a)
+{
+ if(actions)
+ throw logic_error("actions already set");
+
+ actions = &a;
+ if(a.empty())
+ init_actions();
+}
+
void Loader::add(const string &kwd, LoaderAction *act)
{
+ if(!actions)
+ actions = &local_actions;
+
StatementKey key(kwd, (act ? act->get_signature() : "*"));
- ActionMap::iterator i = actions.find(key);
- if(i!=actions.end())
+ ActionMap::iterator i = actions->find(key);
+ if(i!=actions->end())
{
delete i->second;
i->second = act;
}
else
- actions[key] = act;
+ (*actions)[key] = act;
}
void Loader::add_auxiliary_loader(Loader &ldr)
bool Loader::has_action(const StatementKey &key) const
{
- ActionMap::const_iterator i = actions.lower_bound(StatementKey(key.keyword, string()));
- for(; (i!=actions.end() && i->first.keyword==key.keyword); ++i)
+ if(!actions)
+ return false;
+
+ ActionMap::const_iterator i = actions->lower_bound(StatementKey(key.keyword, string()));
+ for(; (i!=actions->end() && i->first.keyword==key.keyword); ++i)
if(signature_match(key.signature, i->first.signature))
return true;
return false;
LoaderAction *Loader::find_action(const StatementKey &key) const
{
- ActionMap::const_iterator begin = actions.lower_bound(StatementKey(key.keyword, string()));
- ActionMap::const_iterator end = actions.upper_bound(StatementKey(key.keyword, "~"));
+ if(!actions)
+ return 0;
+
+ ActionMap::const_iterator begin = actions->lower_bound(StatementKey(key.keyword, string()));
+ ActionMap::const_iterator end = actions->upper_bound(StatementKey(key.keyword, "~"));
if(begin==end)
throw unknown_keyword(key.keyword);
*/
class Loader
{
-private:
+protected:
typedef std::map<StatementKey, LoaderAction *> ActionMap;
- ActionMap actions;
+private:
+ ActionMap local_actions;
+ ActionMap *actions;
Parser *cur_parser;
unsigned cur_level;
const Statement *cur_st;
/** Processes the current statement's substatements with another Loader. */
void load_sub_with(Loader &);
+ /** Sets the actions to be used when loading. If the map is empty,
+ init_actions will be called. */
+ void set_actions(ActionMap &);
+ virtual void init_actions() { }
+
/** Adds a keyword that is loaded by calling a function. */
template<typename L>
void add(const std::string &k, void (L::*func)())