X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Floaderaction.h;h=a3b04437d279e3e6835bd20538d2c99311721064;hb=b0186e8878204a29e25ca4063c1dac4dea908508;hp=5b2f0b0f022016cc97dbce52bb3d09aab7887bf0;hpb=302f73123da1194dd91b43138cd880cae9318a14;p=libs%2Fdatafile.git diff --git a/source/loaderaction.h b/source/loaderaction.h index 5b2f0b0..a3b0443 100644 --- a/source/loaderaction.h +++ b/source/loaderaction.h @@ -1,11 +1,23 @@ #ifndef MSP_DATAFILE_LOADERACTION_H_ #define MSP_DATAFILE_LOADERACTION_H_ +#include +#include "argumentstore.h" #include "statement.h" namespace Msp { namespace DataFile { +#if __cplusplus>=201103L +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)); } +#endif + class Loader; /** @@ -21,6 +33,8 @@ public: /** 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; }; @@ -44,6 +58,11 @@ public: (dynamic_cast(l).*func)(); }; + virtual void execute(Loader &l, const ArgumentStore &) const + { + (dynamic_cast(l).*func)(); + }; + virtual std::string get_signature() const { return std::string(); } }; @@ -68,6 +87,11 @@ public: (dynamic_cast(l).*func)(st.args[0].get()); } + 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); } }; @@ -96,6 +120,16 @@ public: (dynamic_cast(l).*func)(values); } + 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; @@ -125,6 +159,11 @@ public: (dynamic_cast(l).*func)(st); } + virtual void execute(Loader &, const ArgumentStore &) const + { + throw std::logic_error("incompatible format"); + } + virtual std::string get_signature() const { return "*"; } }; @@ -146,6 +185,11 @@ public: (dynamic_cast(l).*func)(st.args[0].get(), st.args[1].get()); } + virtual void execute(Loader &l, const ArgumentStore &as) const + { + (dynamic_cast(l).*func)(as.get(0), as.get(1)); + } + virtual std::string get_signature() const { std::string result; @@ -172,6 +216,11 @@ public: (dynamic_cast(l).*func)(st.args[0].get(), st.args[1].get(), st.args[2].get()); } + virtual void execute(Loader &l, const ArgumentStore &as) const + { + (dynamic_cast(l).*func)(as.get(0), as.get(1), as.get(2)); + } + virtual std::string get_signature() const { std::string result; @@ -199,6 +248,11 @@ public: (dynamic_cast(l).*func)(st.args[0].get(), st.args[1].get(), st.args[2].get(), st.args[3].get()); } + virtual void execute(Loader &l, const ArgumentStore &as) const + { + (dynamic_cast(l).*func)(as.get(0), as.get(1), as.get(2), as.get(3)); + } + virtual std::string get_signature() const { std::string result; @@ -227,6 +281,11 @@ public: (dynamic_cast(l).*func)(st.args[0].get(), st.args[1].get(), st.args[2].get(), st.args[3].get(), st.args[4].get()); } + virtual void execute(Loader &l, const ArgumentStore &as) const + { + (dynamic_cast(l).*func)(as.get(0), as.get(1), as.get(2), as.get(3), as.get(4)); + } + virtual std::string get_signature() const { std::string result; @@ -240,6 +299,98 @@ public: }; +#if __cplusplus>=201103L +template +struct Apply; + +template +struct Apply +{ + template + static void apply(L &l, F func, const Statement &, Args... args) + { + (l.*func)(args...); + } + + template + static void apply(L &l, F func, const ArgumentStore &, Args... args) + { + (l.*func)(args...); + } +}; + +template +struct Apply +{ + template + static void apply(L &l, F func, const Statement &st, Args... args) + { + Apply::apply(l, func, st, args..., st.args[I].get()); + } + + template + static void apply(L &l, F func, const ArgumentStore &as, Args... args) + { + Apply::apply(l, func, as, args..., as.get(I)); + } +}; + + +template +class LoaderFuncN: public LoaderAction +{ +protected: + typedef void (L::*FuncType)(Args...); + + FuncType func; + +public: + LoaderFuncN(FuncType f): func(f) { } + + virtual void execute(Loader &l, const Statement &st) const + { + Apply<0, Args...>::apply(dynamic_cast(l), func, st); + } + + 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 LoaderFuncNBound1: public LoaderAction +{ +protected: + typedef void (L::*FuncType)(B0, Args...); + typedef typename RemoveReference::Type Bound0Type; + + FuncType func; + Bound0Type bound0; + +public: + LoaderFuncNBound1(FuncType f, const Bound0Type &b0): func(f), bound0(b0) { } + + virtual void execute(Loader &l, const Statement &st) const + { + Apply<0, Args...>::apply(dynamic_cast(l), func, st, bound0); + } + + 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(); } +}; +#endif + + template class LoadValue1: public LoaderAction { @@ -256,6 +407,11 @@ public: dynamic_cast(l).get_object().*ptr0 = st.args[0].get(); } + 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); } }; @@ -275,7 +431,13 @@ public: virtual void execute(Loader &l, const Statement &st) const { typename L::Loader &ldr = dynamic_cast(l); - ldr.get_object().*ptr0 = ldr.get_collection().template get(st.args[0].get()); + ldr.get_object().*ptr0 = &ldr.get_collection().template get(st.args[0].get()); + } + + 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 @@ -302,6 +464,12 @@ public: 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;