X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Floaderaction.h;h=a3b04437d279e3e6835bd20538d2c99311721064;hb=HEAD;hp=22fb82ad13691209b484383fa421c48fd188b024;hpb=7df5e45c7f414f6a07681dc4ec2abb63b091a309;p=libs%2Fdatafile.git diff --git a/source/loaderaction.h b/source/loaderaction.h index 22fb82a..9e08dac 100644 --- a/source/loaderaction.h +++ b/source/loaderaction.h @@ -1,12 +1,21 @@ #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; /** @@ -15,13 +24,15 @@ Base class for loader actions. class LoaderAction { protected: - LoaderAction() { } + LoaderAction() = default; public: - virtual ~LoaderAction() { } + virtual ~LoaderAction() = default; /** 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; }; @@ -40,12 +51,17 @@ private: public: LoaderFunc0(FuncType f): func(f) { } - virtual void execute(Loader &l, const Statement &) const + void execute(Loader &l, const Statement &) const override + { + (dynamic_cast(l).*func)(); + }; + + void execute(Loader &l, const ArgumentStore &) const override { (dynamic_cast(l).*func)(); }; - virtual std::string get_signature() const + std::string get_signature() const override { return std::string(); } }; @@ -64,12 +80,17 @@ private: public: LoaderFunc1(FuncType f): func(f) { } - virtual void execute(Loader &l, const Statement &st) const + void execute(Loader &l, const Statement &st) const override { (dynamic_cast(l).*func)(st.args[0].get()); } - virtual std::string get_signature() const + void execute(Loader &l, const ArgumentStore &as) const override + { + (dynamic_cast(l).*func)(as.get(0)); + } + + std::string get_signature() const override { return std::string(1, TypeInfo::signature); } }; @@ -88,16 +109,26 @@ private: public: LoaderFunc1(FuncType f): func(f) { } - virtual void execute(Loader &l, const Statement &st) const + void execute(Loader &l, const Statement &st) const override { std::vector values; values.reserve(st.args.size()); - for(Statement::Arguments::const_iterator i=st.args.begin(); i!=st.args.end(); ++i) - values.push_back(i->get()); + for(const Value &a: st.args) + values.push_back(a.get()); + (dynamic_cast(l).*func)(values); + } + + void execute(Loader &l, const ArgumentStore &as) const override + { + 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 get_signature() const override { std::string result; result += TypeInfo::signature; @@ -121,123 +152,109 @@ private: public: LoaderFunc1(FuncType f): func(f) { } - virtual void execute(Loader &l, const Statement &st) const + void execute(Loader &l, const Statement &st) const override { (dynamic_cast(l).*func)(st); } - virtual std::string get_signature() const + void execute(Loader &, const ArgumentStore &) const override + { + throw std::logic_error("incompatible format"); + } + + std::string get_signature() const override { return "*"; } }; -template -class LoaderFunc2: public LoaderAction -{ -private: - typedef void (L::*FuncType)(A0, A1); - - FuncType func; +template +struct Apply; -public: - LoaderFunc2(FuncType f): func(f) { } - - virtual void execute(Loader &l, const Statement &st) const +template +struct Apply +{ + template + static void apply(L &l, F func, const Statement &, Args &&... args) { - (dynamic_cast(l).*func)(st.args[0].get(), st.args[1].get()); + (l.*func)(std::forward(args)...); } - virtual std::string get_signature() const + template + static void apply(L &l, F func, const ArgumentStore &, Args &&... args) { - std::string result; - result += TypeInfo::signature; - result += TypeInfo::signature; - return result; + (l.*func)(std::forward(args)...); } }; - -template -class LoaderFunc3: public LoaderAction +template +struct Apply { -private: - typedef void (L::*FuncType)(A0, A1, A2); - - FuncType func; - -public: - LoaderFunc3(FuncType f): func(f) { } - - virtual void execute(Loader &l, const Statement &st) const + template + static void apply(L &l, F func, const Statement &st, Args &&... args) { - (dynamic_cast(l).*func)(st.args[0].get(), st.args[1].get(), st.args[2].get()); + Apply::apply(l, func, st, std::forward(args)..., std::move(st.args[I].get())); } - virtual std::string get_signature() const + template + static void apply(L &l, F func, const ArgumentStore &as, Args &&... args) { - std::string result; - result += TypeInfo::signature; - result += TypeInfo::signature; - result += TypeInfo::signature; - return result; + Apply::apply(l, func, as, std::forward(args)..., std::move(as.get(I))); } }; -template -class LoaderFunc4: public LoaderAction +template +class LoaderFuncN: public LoaderAction { -private: - typedef void (L::*FuncType)(A0, A1, A2, A3); +protected: + typedef void (L::*FuncType)(Args...); FuncType func; public: - LoaderFunc4(FuncType f): func(f) { } + LoaderFuncN(FuncType f): func(f) { } - virtual void execute(Loader &l, const Statement &st) const + void execute(Loader &l, const Statement &st) const override { - (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); } - virtual std::string get_signature() const + void execute(Loader &l, const ArgumentStore &as) const override { - std::string result; - result += TypeInfo::signature; - result += TypeInfo::signature; - result += TypeInfo::signature; - result += TypeInfo::signature; - return result; + Apply<0, Args...>::apply(dynamic_cast(l), func, as); } + + std::string get_signature() const override + { return create_signature(); } }; -template -class LoaderFunc5: public LoaderAction +template +class LoaderFuncNBound1: public LoaderAction { -private: - typedef void (L::*FuncType)(A0, A1, A2, A3, A4); +protected: + typedef void (L::*FuncType)(B0, Args...); + typedef typename std::remove_reference::type Bound0Type; FuncType func; + Bound0Type bound0; public: - LoaderFunc5(FuncType f): func(f) { } + LoaderFuncNBound1(FuncType f, const B0 &b0): func(f), bound0(b0) { } + LoaderFuncNBound1(FuncType f, B0 &&b0): func(f), bound0(std::move(b0)) { } - virtual void execute(Loader &l, const Statement &st) const + void execute(Loader &l, const Statement &st) const override { - (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); } - virtual std::string get_signature() const + void execute(Loader &l, const ArgumentStore &as) const override { - std::string result; - result += TypeInfo::signature; - result += TypeInfo::signature; - result += TypeInfo::signature; - result += TypeInfo::signature; - result += TypeInfo::signature; - return result; + Apply<0, Args...>::apply(dynamic_cast(l), func, as, bound0); } + + std::string get_signature() const override + { return create_signature(); } }; @@ -252,12 +269,17 @@ private: public: LoadValue1(Pointer0Type p0): ptr0(p0) { } - virtual void execute(Loader &l, const Statement &st) const + void execute(Loader &l, const Statement &st) const override { dynamic_cast(l).get_object().*ptr0 = st.args[0].get(); } - virtual std::string get_signature() const + void execute(Loader &l, const ArgumentStore &as) const override + { + dynamic_cast(l).get_object().*ptr0 = as.get(0); + } + + std::string get_signature() const override { return std::string(1, TypeInfo::signature); } }; @@ -273,15 +295,19 @@ private: public: LoadValue1(Pointer0Type p0): ptr0(p0) { } - virtual void execute(Loader &l, const Statement &st) const + void execute(Loader &l, const Statement &st) const override { typename L::Loader &ldr = dynamic_cast(l); - if(!ldr.is_pointer_reload_allowed() && ldr.get_object().*ptr0) - throw InvalidState("The pointer has already been loaded"); - 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 std::string get_signature() const + void execute(Loader &l, const ArgumentStore &as) const override + { + typename L::Loader &ldr = dynamic_cast(l); + ldr.get_object().*ptr0 = &ldr.get_collection().template get(as.get(0)); + } + + std::string get_signature() const override { return std::string(1, TypeInfo::signature); } }; @@ -299,13 +325,19 @@ private: public: LoadValue2(Pointer0Type p0, Pointer1Type p1): ptr0(p0), ptr1(p1) { } - virtual void execute(Loader &l, const Statement &st) const + void execute(Loader &l, const Statement &st) const override { 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 + void execute(Loader &l, const ArgumentStore &as) const override + { + dynamic_cast(l).get_object().*ptr0 = as.get(0); + dynamic_cast(l).get_object().*ptr1 = as.get(1); + } + + std::string get_signature() const override { std::string result; result += TypeInfo::signature;