X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Floader.h;h=39a50d0b7fe4a56b88a28ec1be68c82e78ed0d5e;hb=9094bc6a657e5587e7e17827c35e3fd388fb91ec;hp=6e9c4f8012e42c952a36bc21f392a7559ef22e47;hpb=80d6c4f64ad4d293cc23e845f7013d6d45c309c5;p=libs%2Fdatafile.git diff --git a/source/loader.h b/source/loader.h index 6e9c4f8..39a50d0 100644 --- a/source/loader.h +++ b/source/loader.h @@ -6,8 +6,9 @@ Distributed under the LGPL #ifndef MSP_PARSER_LOADER_H_ #define MSP_PARSER_LOADER_H_ +#include #include -#include +#include "error.h" #include "parser.h" #include "statement.h" #include "value.h" @@ -36,7 +37,7 @@ public: LoaderFunc0(FuncType f): func(f) { } void execute(Loader &l, const Statement &st) const { - if(st.args.size()!=0) throw TypeError("Wrong number of arguments"); + if(st.args.size()!=0) throw TypeError(st.get_location()+": Wrong number of arguments"); (dynamic_cast(l).*func)(); }; private: @@ -52,7 +53,7 @@ public: LoaderFunc1(FuncType f): func(f) { } void execute(Loader &l, const Statement &st) const { - if(st.args.size()!=1) throw TypeError("Wrong number of arguments"); + if(st.args.size()!=1) throw TypeError(st.get_location()+": Wrong number of arguments"); (dynamic_cast(l).*func)(st.args[0].get()); } private: @@ -68,7 +69,7 @@ public: LoaderFunc2(FuncType f): func(f) { } void execute(Loader &l, const Statement &st) const { - if(st.args.size()!=2) throw TypeError("Wrong number of arguments"); + if(st.args.size()!=2) throw TypeError(st.get_location()+": Wrong number of arguments"); (dynamic_cast(l).*func)(st.args[0].get(), st.args[1].get()); } private: @@ -84,7 +85,7 @@ public: LoaderFunc3(FuncType f): func(f) { } void execute(Loader &l, const Statement &st) const { - if(st.args.size()!=3) throw TypeError("Wrong number of arguments"); + if(st.args.size()!=3) throw TypeError(st.get_location()+": Wrong number of arguments"); (dynamic_cast(l).*func)(st.args[0].get(), st.args[1].get(), st.args[2].get()); } private: @@ -100,7 +101,7 @@ public: LoaderFunc4(FuncType f): func(f) { } void execute(Loader &l, const Statement &st) const { - if(st.args.size()!=4) throw TypeError("Wrong number of arguments"); + if(st.args.size()!=4) throw TypeError(st.get_location()+": Wrong number of arguments"); (dynamic_cast(l).*func)(st.args[0].get(), st.args[1].get(), st.args[2].get(), st.args[3].get()); } private: @@ -116,7 +117,7 @@ public: LoadValue(PointerType p): ptr(p) { } void execute(Loader &l, const Statement &st) const { - if(st.args.size()!=1) throw TypeError("Wrong number of arguments"); + if(st.args.size()!=1) throw TypeError(st.get_location()+": Wrong number of arguments"); dynamic_cast(l).get_object().*ptr=st.args[0].get(); } private: @@ -205,6 +206,18 @@ private: } }; +template +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 Parser } // namespace Msp