X-Git-Url: http://git.tdb.fi/?p=libs%2Fdatafile.git;a=blobdiff_plain;f=source%2Floaderaction.h;h=c7c593829a1b24a3ccd161dad400f14b06d207e8;hp=ff478ec2df7aa8ed651914922516781e3f519278;hb=9c95942d24a92abea14bb9e11d33daae2d017321;hpb=db9c49893c2a9475cb5efa4a53bc481a5f66231f diff --git a/source/loaderaction.h b/source/loaderaction.h index ff478ec..c7c5938 100644 --- a/source/loaderaction.h +++ b/source/loaderaction.h @@ -1,19 +1,21 @@ -/* $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_ -#include "except.h" +#include +#include "argumentstore.h" #include "statement.h" namespace Msp { namespace DataFile { +template +std::string create_signature(const std::string &prefix = std::string()) +{ return prefix+std::string(1, TypeInfo::signature); } + +template +std::string create_signature(const std::string &prefix = std::string()) +{ return create_signature(prefix+std::string(1, TypeInfo::signature)); } + class Loader; /** @@ -21,14 +23,17 @@ 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 void execute(Loader &, const ArgumentStore &) const = 0; + + virtual std::string get_signature() const = 0; }; @@ -38,17 +43,26 @@ 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 void execute(Loader &l, const ArgumentStore &) const + { + (dynamic_cast(l).*func)(); + }; + + virtual std::string get_signature() const + { return std::string(); } }; @@ -58,17 +72,26 @@ 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 void execute(Loader &l, const ArgumentStore &as) const + { + (dynamic_cast(l).*func)(as.get(0)); + } + + virtual std::string get_signature() const + { return std::string(1, TypeInfo::signature); } }; @@ -78,20 +101,40 @@ 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 void execute(Loader &l, const ArgumentStore &as) const + { + std::vector values; + unsigned n_args = as.get_info().key.signature.size(); + values.reserve(n_args); + for(unsigned i=0; i(i)); + (dynamic_cast(l).*func)(values); + } + + virtual std::string get_signature() const + { + std::string result; + result += TypeInfo::signature; + result += '*'; + return result; + } }; @@ -101,139 +144,205 @@ 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 void execute(Loader &, const ArgumentStore &) const + { + throw std::logic_error("incompatible format"); + } + + virtual std::string get_signature() const + { return "*"; } }; -template -class LoaderFunc2: public LoaderAction +template +struct Apply; + +template +struct Apply { -public: - typedef void (L::*FuncType)(A0, A1); + template + static void apply(L &l, F func, const Statement &, Args... args) + { + (l.*func)(args...); + } - LoaderFunc2(FuncType f): func(f) { } - void execute(Loader &l, const Statement &st) const + template + static void apply(L &l, F func, const ArgumentStore &, Args... args) { - if(st.args.size()!=2) throw TypeError("Wrong number of arguments"); - (dynamic_cast(l).*func)(st.args[0].get(), st.args[1].get()); + (l.*func)(args...); } -private: - FuncType func; }; - -template -class LoaderFunc3: public LoaderAction +template +struct Apply { -public: - typedef void (L::*FuncType)(A0, A1, A2); + template + static void apply(L &l, F func, const Statement &st, Args... args) + { + Apply::apply(l, func, st, args..., st.args[I].get()); + } - LoaderFunc3(FuncType f): func(f) { } - void execute(Loader &l, const Statement &st) const + template + static void apply(L &l, F func, const ArgumentStore &as, Args... args) { - 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()); + Apply::apply(l, func, as, args..., as.get(I)); } -private: - FuncType func; }; -template -class LoaderFunc4: public LoaderAction +template +class LoaderFuncN: public LoaderAction { +protected: + typedef void (L::*FuncType)(Args...); + + FuncType func; + public: - typedef void (L::*FuncType)(A0, A1, A2, A3); + LoaderFuncN(FuncType f): func(f) { } - 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()); + Apply<0, Args...>::apply(dynamic_cast(l), func, st); } -private: - FuncType func; + + virtual void execute(Loader &l, const ArgumentStore &as) const + { + Apply<0, Args...>::apply(dynamic_cast(l), func, as); + } + + virtual std::string get_signature() const + { return create_signature(); } }; -template -class LoaderFunc5: public LoaderAction +template +class LoaderFuncNBound1: public LoaderAction { +protected: + typedef void (L::*FuncType)(B0, Args...); + typedef typename RemoveReference::Type Bound0Type; + + FuncType func; + Bound0Type bound0; + public: - typedef void (L::*FuncType)(A0, A1, A2, A3, A4); + LoaderFuncNBound1(FuncType f, const Bound0Type &b0): func(f), bound0(b0) { } - 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()); + Apply<0, Args...>::apply(dynamic_cast(l), func, st, bound0); } -private: - FuncType func; + + virtual void execute(Loader &l, const ArgumentStore &as) const + { + Apply<0, Args...>::apply(dynamic_cast(l), func, as, bound0); + } + + virtual std::string get_signature() const + { return create_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"); - dynamic_cast(l).get_object().*ptr0=st.args[0].get(); + dynamic_cast(l).get_object().*ptr0 = st.args[0].get(); } -private: - Pointer0Type ptr0; + + virtual void execute(Loader &l, const ArgumentStore &as) const + { + dynamic_cast(l).get_object().*ptr0 = as.get(0); + } + + 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 void execute(Loader &l, const ArgumentStore &as) const + { + typename L::Loader &ldr = dynamic_cast(l); + ldr.get_object().*ptr0 = &ldr.get_collection().template get(as.get(0)); + } + + 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 void execute(Loader &l, const ArgumentStore &as) const + { + dynamic_cast(l).get_object().*ptr0 = as.get(0); + dynamic_cast(l).get_object().*ptr1 = as.get(1); + } + + virtual std::string get_signature() const + { + std::string result; + result += TypeInfo::signature; + result += TypeInfo::signature; + return result; } -private: - Pointer0Type ptr0; - Pointer1Type ptr1; }; } // namespace DataFile