X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Floaderaction.h;h=4666687e71534fd15d63499898ce9217250f4ee9;hb=256b44a5009467171af53316141277027bcc0ba4;hp=03a9da7bcc1aa8fc33823f404a0426f704aaeaed;hpb=e2a4cefe59dd3e6e1b2fac2fb7232326bb2b0787;p=libs%2Fdatafile.git diff --git a/source/loaderaction.h b/source/loaderaction.h index 03a9da7..4666687 100644 --- a/source/loaderaction.h +++ b/source/loaderaction.h @@ -1,19 +1,21 @@ -/* $Id$ - -This file is part of libmspdatafile -Copyright © 2008, 2010 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; /** @@ -29,6 +31,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; }; @@ -52,6 +56,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(); } }; @@ -76,6 +85,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); } }; @@ -99,8 +113,18 @@ public: { 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); + } + + 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); } @@ -133,118 +157,103 @@ 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 "*"; } }; -template -class LoaderFunc2: public LoaderAction -{ -private: - typedef void (L::*FuncType)(A0, A1); - - FuncType func; - -public: - LoaderFunc2(FuncType f): func(f) { } +template +struct Apply; - 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)(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)(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, args..., 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, args..., 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 { - (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 + virtual void execute(Loader &l, const ArgumentStore &as) const { - 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); } + + virtual std::string get_signature() const + { 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 Bound0Type &b0): func(f), bound0(b0) { } virtual void execute(Loader &l, const Statement &st) const { - (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 + virtual void execute(Loader &l, const ArgumentStore &as) const { - 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); } + + virtual std::string get_signature() const + { return create_signature(); } }; @@ -264,6 +273,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); } }; @@ -283,9 +297,13 @@ public: virtual void execute(Loader &l, const Statement &st) const { 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 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 @@ -312,6 +330,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;