X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Floaderaction.h;h=a6eb552e981e38f329ab626c34d2cae9c7afc504;hb=e4beb0453a1cfe200fc97607afab94c3ddee1c65;hp=ff478ec2df7aa8ed651914922516781e3f519278;hpb=db9c49893c2a9475cb5efa4a53bc481a5f66231f;p=libs%2Fdatafile.git diff --git a/source/loaderaction.h b/source/loaderaction.h index ff478ec..a6eb552 100644 --- a/source/loaderaction.h +++ b/source/loaderaction.h @@ -1,10 +1,3 @@ -/* $Id$ - -This file is part of libmspdatafile -Copyright © 2006-2008 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - #ifndef MSP_DATAFILE_LOADERACTION_H_ #define MSP_DATAFILE_LOADERACTION_H_ @@ -21,14 +14,15 @@ Base class for loader actions. */ class LoaderAction { -public: - /** - Called when a statement is to be loaded. - */ - virtual void execute(Loader &, const Statement &) const=0; - virtual ~LoaderAction() { } protected: LoaderAction() { } +public: + virtual ~LoaderAction() { } + + /** Called to process a statement. */ + virtual void execute(Loader &, const Statement &) const = 0; + + virtual std::string get_signature() const = 0; }; @@ -38,17 +32,21 @@ Loads a statement by calling a function that takes no arguments. template class LoaderFunc0: public LoaderAction { -public: +private: typedef void (L::*FuncType)(); + FuncType func; + +public: LoaderFunc0(FuncType f): func(f) { } - void execute(Loader &l, const Statement &st) const + + virtual void execute(Loader &l, const Statement &) const { - if(st.args.size()!=0) throw TypeError("Wrong number of arguments"); (dynamic_cast(l).*func)(); }; -private: - FuncType func; + + virtual std::string get_signature() const + { return std::string(); } }; @@ -58,17 +56,21 @@ Loads a statement by calling a function that takes one argument. template class LoaderFunc1: public LoaderAction { -public: +private: typedef void (L::*FuncType)(A0); + FuncType func; + +public: LoaderFunc1(FuncType f): func(f) { } - void execute(Loader &l, const Statement &st) const + + virtual void execute(Loader &l, const Statement &st) const { - if(st.args.size()!=1) throw TypeError("Wrong number of arguments"); (dynamic_cast(l).*func)(st.args[0].get()); } -private: - FuncType func; + + virtual std::string get_signature() const + { return std::string(1, TypeInfo::signature); } }; @@ -78,20 +80,30 @@ Loads a statement by calling a function that takes an array of values. template class LoaderFunc1 &>: public LoaderAction { -public: +private: typedef void (L::*FuncType)(const std::vector &); + FuncType func; + +public: LoaderFunc1(FuncType f): func(f) { } - void execute(Loader &l, const Statement &st) const + + virtual void execute(Loader &l, const Statement &st) const { std::vector values; values.reserve(st.args.size()); - for(ValueArray::const_iterator i=st.args.begin(); i!=st.args.end(); ++i) + for(Statement::Arguments::const_iterator i=st.args.begin(); i!=st.args.end(); ++i) values.push_back(i->get()); (dynamic_cast(l).*func)(values); } -private: - FuncType func; + + virtual std::string get_signature() const + { + std::string result; + result += TypeInfo::signature; + result += '*'; + return result; + } }; @@ -101,139 +113,203 @@ Loads a statement by calling a function with the statement itself as argument. template class LoaderFunc1: public LoaderAction { -public: +private: typedef void (L::*FuncType)(const Statement &); + FuncType func; + +public: LoaderFunc1(FuncType f): func(f) { } - void execute(Loader &l, const Statement &st) const + + virtual void execute(Loader &l, const Statement &st) const { (dynamic_cast(l).*func)(st); } -private: - FuncType func; + + virtual std::string get_signature() const + { return "*"; } }; template class LoaderFunc2: public LoaderAction { -public: +private: typedef void (L::*FuncType)(A0, A1); + FuncType func; + +public: LoaderFunc2(FuncType f): func(f) { } - void execute(Loader &l, const Statement &st) const + + virtual void execute(Loader &l, const Statement &st) const { - if(st.args.size()!=2) throw TypeError("Wrong number of arguments"); (dynamic_cast(l).*func)(st.args[0].get(), st.args[1].get()); } -private: - FuncType func; + + virtual std::string get_signature() const + { + std::string result; + result += TypeInfo::signature; + result += TypeInfo::signature; + return result; + } }; template class LoaderFunc3: public LoaderAction { -public: +private: typedef void (L::*FuncType)(A0, A1, A2); + FuncType func; + +public: LoaderFunc3(FuncType f): func(f) { } - void execute(Loader &l, const Statement &st) const + + virtual void execute(Loader &l, const Statement &st) const { - if(st.args.size()!=3) throw TypeError("Wrong number of arguments"); (dynamic_cast(l).*func)(st.args[0].get(), st.args[1].get(), st.args[2].get()); } -private: - FuncType func; + + virtual std::string get_signature() const + { + std::string result; + result += TypeInfo::signature; + result += TypeInfo::signature; + result += TypeInfo::signature; + return result; + } }; template class LoaderFunc4: public LoaderAction { -public: +private: typedef void (L::*FuncType)(A0, A1, A2, A3); + FuncType func; + +public: LoaderFunc4(FuncType f): func(f) { } - void execute(Loader &l, const Statement &st) const + + virtual void execute(Loader &l, const Statement &st) const { - if(st.args.size()!=4) throw TypeError("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: - FuncType func; + + virtual std::string get_signature() const + { + std::string result; + result += TypeInfo::signature; + result += TypeInfo::signature; + result += TypeInfo::signature; + result += TypeInfo::signature; + return result; + } }; template class LoaderFunc5: public LoaderAction { -public: +private: typedef void (L::*FuncType)(A0, A1, A2, A3, A4); + FuncType func; + +public: LoaderFunc5(FuncType f): func(f) { } - void execute(Loader &l, const Statement &st) const + + virtual void execute(Loader &l, const Statement &st) const { - if(st.args.size()!=5) throw TypeError("Wrong number of arguments"); (dynamic_cast(l).*func)(st.args[0].get(), st.args[1].get(), st.args[2].get(), st.args[3].get(), st.args[4].get()); } -private: - FuncType func; + + virtual std::string get_signature() const + { + std::string result; + result += TypeInfo::signature; + result += TypeInfo::signature; + result += TypeInfo::signature; + result += TypeInfo::signature; + result += TypeInfo::signature; + return result; + } }; template class LoadValue1: public LoaderAction { -public: +private: typedef T0 L::*Pointer0Type; + Pointer0Type ptr0; + +public: LoadValue1(Pointer0Type p0): ptr0(p0) { } - void execute(Loader &l, const Statement &st) const + + virtual void execute(Loader &l, const Statement &st) const { - if(st.args.size()!=1) throw TypeError("Wrong number of arguments"); - dynamic_cast(l).get_object().*ptr0=st.args[0].get(); + dynamic_cast(l).get_object().*ptr0 = st.args[0].get(); } -private: - Pointer0Type ptr0; + + virtual std::string get_signature() const + { return std::string(1, TypeInfo::signature); } }; template class LoadValue1: public LoaderAction { -public: +private: typedef T0 *L::*Pointer0Type; + Pointer0Type ptr0; + +public: LoadValue1(Pointer0Type p0): ptr0(p0) { } - void execute(Loader &l, const Statement &st) const + + virtual void execute(Loader &l, const Statement &st) const { - if(st.args.size()!=1) throw TypeError("Wrong number of arguments"); - typename L::Loader &ldr=dynamic_cast(l); - ldr.get_object().*ptr0=ldr.get_collection().template get(st.args[0].get()); + typename L::Loader &ldr = dynamic_cast(l); + ldr.get_object().*ptr0 = ldr.get_collection().template get(st.args[0].get()); } -private: - Pointer0Type ptr0; + + virtual std::string get_signature() const + { return std::string(1, TypeInfo::signature); } }; template class LoadValue2: public LoaderAction { -public: +private: typedef T0 L::*Pointer0Type; typedef T1 L::*Pointer1Type; + Pointer0Type ptr0; + Pointer1Type ptr1; + +public: LoadValue2(Pointer0Type p0, Pointer1Type p1): ptr0(p0), ptr1(p1) { } - void execute(Loader &l, const Statement &st) const + + virtual void execute(Loader &l, const Statement &st) const { - if(st.args.size()!=2) throw TypeError("Wrong number of arguments"); - dynamic_cast(l).get_object().*ptr0=st.args[0].get(); - dynamic_cast(l).get_object().*ptr1=st.args[1].get(); + dynamic_cast(l).get_object().*ptr0 = st.args[0].get(); + dynamic_cast(l).get_object().*ptr1 = st.args[1].get(); + } + + virtual std::string get_signature() const + { + std::string result; + result += TypeInfo::signature; + result += TypeInfo::signature; + return result; } -private: - Pointer0Type ptr0; - Pointer1Type ptr1; }; } // namespace DataFile