-/*
-This file is part of libmspparser
-Copyright © 2006 Mikko Rasa, Mikkosoft Productions
+/* $Id$
+
+This file is part of libmspdatafile
+Copyright © 2006 Mikko Rasa, Mikkosoft Productions
Distributed under the LGPL
*/
-#ifndef MSP_PARSER_LOADER_H_
-#define MSP_PARSER_LOADER_H_
+#ifndef MSP_DATAFILE_LOADER_H_
+#define MSP_DATAFILE_LOADER_H_
+
+#include <fstream>
#include <map>
-#include <msp/error.h>
+#include "error.h"
#include "parser.h"
#include "statement.h"
#include "value.h"
namespace Msp {
-namespace Parser {
+namespace DataFile {
+class Loader;
class Statement;
-template<typename L>
+/**
+Base class for loader actions.
+*/
class LoaderAction
{
public:
- virtual void execute(L &, const Statement &) const=0;
+ /**
+ Called when a statement is to be loaded.
+ */
+ virtual void execute(Loader &, const Statement &) const=0;
virtual ~LoaderAction() { }
protected:
LoaderAction() { }
};
+
+/**
+Loads a statement by calling a function that takes no arguments.
+*/
template<typename L>
-class LoaderFunc0: public LoaderAction<L>
+class LoaderFunc0: public LoaderAction
{
public:
typedef void (L::*FuncType)();
-
+
LoaderFunc0(FuncType f): func(f) { }
- void execute(L &l, const Statement &st) const
+ void execute(Loader &l, const Statement &st) const
{
- if(st.args.size()!=0) throw TypeError("Wrong number of arguments");
- (l.*func)();
+ if(st.args.size()!=0) throw TypeError(st.get_location()+": Wrong number of arguments");
+ (dynamic_cast<L &>(l).*func)();
};
private:
FuncType func;
};
+
+/**
+Loads a statement by calling a function that takes one argument.
+*/
template<typename L, typename A0>
-class LoaderFunc1: public LoaderAction<L>
+class LoaderFunc1: public LoaderAction
{
public:
typedef void (L::*FuncType)(A0);
-
+
LoaderFunc1(FuncType f): func(f) { }
- void execute(L &l, const Statement &st) const
+ void execute(Loader &l, const Statement &st) const
{
- if(st.args.size()!=1) throw TypeError("Wrong number of arguments");
- (l.*func)(st.args[0].get<A0>());
+ if(st.args.size()!=1) throw TypeError(st.get_location()+": Wrong number of arguments");
+ (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>());
}
private:
FuncType func;
};
+
+/**
+Loads a statement by calling a function that takes an array of values.
+*/
+template<typename L, typename A0>
+class LoaderFunc1<L, const std::vector<A0> &>: public LoaderAction
+{
+public:
+ typedef void (L::*FuncType)(const std::vector<A0> &);
+
+ LoaderFunc1(FuncType f): func(f) { }
+ void execute(Loader &l, const Statement &st) const
+ {
+ std::vector<A0> values;
+ values.reserve(st.args.size());
+ for(ValueArray::const_iterator i=st.args.begin(); i!=st.args.end(); ++i)
+ values.push_back(i->get<A0>());
+ (dynamic_cast<L &>(l).*func)(values);
+ }
+private:
+ FuncType func;
+};
+
+
template<typename L, typename A0, typename A1>
-class LoaderFunc2: public LoaderAction<L>
+class LoaderFunc2: public LoaderAction
{
public:
typedef void (L::*FuncType)(A0, A1);
-
+
LoaderFunc2(FuncType f): func(f) { }
- void execute(L &l, const Statement &st) const
+ void execute(Loader &l, const Statement &st) const
{
- if(st.args.size()!=2) throw TypeError("Wrong number of arguments");
- (l.*func)(st.args[0].get<A0>(), st.args[1].get<A1>());
+ if(st.args.size()!=2) throw TypeError(st.get_location()+": Wrong number of arguments");
+ (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>());
}
private:
FuncType func;
};
+
template<typename L, typename A0, typename A1, typename A2>
-class LoaderFunc3: public LoaderAction<L>
+class LoaderFunc3: public LoaderAction
{
public:
typedef void (L::*FuncType)(A0, A1, A2);
-
+
LoaderFunc3(FuncType f): func(f) { }
- void execute(L &l, const Statement &st) const
+ void execute(Loader &l, const Statement &st) const
{
- if(st.args.size()!=3) throw TypeError("Wrong number of arguments");
- (l.*func)(st.args[0].get<A0>(), st.args[1].get<A1>(), st.args[2].get<A2>());
+ if(st.args.size()!=3) throw TypeError(st.get_location()+": Wrong number of arguments");
+ (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>(), st.args[2].get<A2>());
}
private:
FuncType func;
};
+
template<typename L, typename A0, typename A1, typename A2, typename A3>
-class LoaderFunc4: public LoaderAction<L>
+class LoaderFunc4: public LoaderAction
{
public:
typedef void (L::*FuncType)(A0, A1, A2, A3);
-
+
LoaderFunc4(FuncType f): func(f) { }
- void execute(L &l, const Statement &st) const
+ void execute(Loader &l, const Statement &st) const
{
- if(st.args.size()!=4) throw TypeError("Wrong number of arguments");
- (l.*func)(st.args[0].get<A0>(), st.args[1].get<A1>(), st.args[2].get<A2>(), st.args[3].get<A3>());
+ if(st.args.size()!=4) throw TypeError(st.get_location()+": Wrong number of arguments");
+ (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>(), st.args[2].get<A2>(), st.args[3].get<A3>());
}
private:
FuncType func;
};
-template<typename L, typename T>
-class LoadValue: public LoaderAction<L>
-{
-public:
- typedef T L::ObjectType::*PointerType;
-
- LoadValue(PointerType p): ptr(p) { }
- void execute(L &l, const Statement &st) const
- {
- if(st.args.size()!=1) throw TypeError("Wrong number of arguments");
- l.get_object().*ptr=st.args[0].get<T>();
- }
-private:
- PointerType ptr;
-};
-template<typename L, typename S>
-class SubLoader0: public LoaderAction<L>
+template<typename L, typename T0>
+class LoadValue1: public LoaderAction
{
public:
- typedef typename S::ObjectType &(L::*FuncType)();
+ typedef T0 L::*Pointer0Type;
- SubLoader0(FuncType f): func(f) { }
- void execute(L &l, const Statement &st) const
+ LoadValue1(Pointer0Type p0): ptr0(p0) { }
+ void execute(Loader &l, const Statement &st) const
{
- if(st.args.size()!=0) throw TypeError("Wrong number of arguments");
- typename S::ObjectType &obj=(l.*func)();
- S sl(obj);
- sl.load(st);
+ if(st.args.size()!=1) throw TypeError(st.get_location()+": Wrong number of arguments");
+ dynamic_cast<typename L::Loader &>(l).get_object().*ptr0=st.args[0].get<T0>();
}
private:
- FuncType func;
+ Pointer0Type ptr0;
};
-template<typename L, typename S, typename A0>
-class SubLoader1: public LoaderAction<L>
+
+template<typename L, typename T0, typename T1>
+class LoadValue2: public LoaderAction
{
public:
- typedef typename S::ObjectType &(L::*FuncType)(A0);
+ typedef T0 L::*Pointer0Type;
+ typedef T1 L::*Pointer1Type;
- SubLoader1(FuncType f): func(f) { }
- void execute(L &l, const Statement &st) const
+ LoadValue2(Pointer0Type p0, Pointer1Type p1): ptr0(p0), ptr1(p1) { }
+ void execute(Loader &l, const Statement &st) const
{
- if(st.args.size()!=1) throw TypeError("Wrong number of arguments");
- typename S::ObjectType &obj=(l.*func)(st.args[0].get<A0>());
- S sl(obj);
- sl.load(st);
+ if(st.args.size()!=2) throw TypeError(st.get_location()+": Wrong number of arguments");
+ dynamic_cast<typename L::Loader &>(l).get_object().*ptr0=st.args[0].get<T0>();
+ dynamic_cast<typename L::Loader &>(l).get_object().*ptr1=st.args[1].get<T1>();
}
private:
- FuncType func;
+ Pointer0Type ptr0;
+ Pointer1Type ptr1;
};
-template<typename C, typename L>
+
+/**
+Base class for data loaders. To enable objects of a certain class to be loaded
+from datafiles, create a public Loader class in it, derived from this class.
+Typically the Loader class contains a reference to the object being loaded. If
+you want to load data members of the object directly, the Loader class must
+have a member function get_object() returning that reference.
+*/
class Loader
{
public:
- typedef C ObjectType;
-
- C &get_object() { return obj; }
- void load(const Statement &st)
- {
- for(std::list<Statement>::const_iterator i=st.sub.begin(); i!=st.sub.end(); ++i)
- load_statement(*i);
- }
- void load(Parser &p)
- {
- while(p)
- {
- Statement st=p.parse();
- if(st.valid)
- load_statement(st);
- }
- }
- virtual ~Loader()
- {
- for(typename ActionMap::iterator i=actions.begin(); i!=actions.end(); ++i)
- delete i->second;
- }
+ /**
+ Loads data from a statement. This is normally only used by the Loader class
+ itself for loading sub-items, but needs to be public so it can be accessed
+ in derived objects.
+ */
+ void load(const Statement &st);
+
+ /**
+ Loads statements from a parser.
+ */
+ void load(Parser &p);
+
+ virtual ~Loader();
protected:
- typedef std::map<std::string, LoaderAction<L > *> ActionMap;
+ Loader(): cur_st(0) { }
- C &obj;
-
- Loader(C &o): obj(o) { }
-
+ template<typename L>
void add(const std::string &k, void (L::*func)())
{ actions.insert(typename ActionMap::value_type(k, new LoaderFunc0<L>(func))); }
-
- template<typename A0>
+
+ template<typename L, typename A0>
void add(const std::string &k, void (L::*func)(A0))
{ actions.insert(typename ActionMap::value_type(k, new LoaderFunc1<L, A0>(func))); }
-
- template<typename A0, typename A1>
+
+ template<typename L, typename A0, typename A1>
void add(const std::string &k, void (L::*func)(A0, A1))
{ actions.insert(typename ActionMap::value_type(k, new LoaderFunc2<L, A0, A1>(func))); }
-
- template<typename A0, typename A1, typename A2>
+
+ template<typename L, typename A0, typename A1, typename A2>
void add(const std::string &k, void (L::*func)(A0, A1, A2))
{ actions.insert(typename ActionMap::value_type(k, new LoaderFunc3<L, A0, A1, A2>(func))); }
-
- template<typename A0, typename A1, typename A2, typename A3>
+
+ template<typename L, typename A0, typename A1, typename A2, typename A3>
void add(const std::string &k, void (L::*func)(A0, A1, A2, A3))
{ actions.insert(typename ActionMap::value_type(k, new LoaderFunc4<L, A0, A1, A2, A3>(func))); }
- template<typename S>
- void add(const std::string &k, typename S::ObjectType &(L::*func)())
- { actions.insert(typename ActionMap::value_type(k, new SubLoader0<L, S>(func))); }
+ template<typename L, typename T0>
+ void add(const std::string &k, T0 L::*p0)
+ { actions.insert(typename ActionMap::value_type(k, new LoadValue1<L, T0>(p0))); }
- template<typename S, typename A0>
- void add(const std::string &k, typename S::ObjectType &(L::*func)(A0))
- { actions.insert(typename ActionMap::value_type(k, new SubLoader1<L, S, A0>(func))); }
+ template<typename L, typename T0, typename T1>
+ void add(const std::string &k, T0 L::*p0, T1 L::*p1)
+ { actions.insert(typename ActionMap::value_type(k, new LoadValue2<L, T0, T1>(p0, p1))); }
- template<typename T>
- void add(const std::string &k, T C::*p)
- { actions.insert(typename ActionMap::value_type(k, new LoadValue<L, T>(p))); }
-private:
- ActionMap actions;
+ void add(const std::string &k)
+ { actions.insert(ActionMap::value_type(k, 0)); }
+
+ /**
+ Loads a sub-object from the statement being currently processed. The Loader
+ class of the sub-object is automatically used.
+ */
+ template<typename S>
+ void load_sub(S &s)
+ { load_sub<typename S::Loader, S>(s); }
+
+ /**
+ Loads a sub-object with a custom Loader class.
+ */
+ template<typename L, typename S>
+ void load_sub(S &s)
+ {
+ if(!cur_st)
+ throw InvalidState("load_sub called without current statement");
+ L loader(s);
+ loader.load(*cur_st);
+ }
- void load_statement(const Statement &st)
+ /**
+ Loads a sub-object with a custom Loader class that takes one argument in
+ addition to to object to be loaded.
+ */
+ template<typename L, typename S, typename T>
+ void load_sub(S &s, T &p)
{
- typename ActionMap::iterator j=actions.find(st.keyword);
- if(j==actions.end())
- throw Exception(st.get_location()+": Unknown keyword '"+st.keyword+"'");
- j->second->execute(dynamic_cast<L &>(*this), st);
+ if(!cur_st)
+ throw InvalidState("load_sub called without current statement");
+ L loader(s, p);
+ loader.load(*cur_st);
}
+private:
+ typedef std::map<std::string, LoaderAction *> ActionMap;
+
+ ActionMap actions;
+ const Statement *cur_st;
+
+ void load_statement(const Statement &st);
};
-} // namespace Parser
+/**
+Loads an object from a file. The object must have a public Loader class.
+*/
+template<typename T>
+void load(T &obj, const std::string &fn)
+{
+ std::ifstream in(fn.c_str());
+ if(!in)
+ throw Exception("Couldn't open "+fn);
+
+ Parser parser(in, fn);
+ typename T::Loader loader(obj);
+ loader.load(parser);
+}
+
+} // namespace DataFile
} // namespace Msp
#endif