From: Mikko Rasa Date: Sun, 19 Apr 2020 14:23:50 +0000 (+0300) Subject: Support N-ary loader functions if compiling in C++11 mode X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=c308213edfde6e137fde273678c4af2520566826;p=libs%2Fdatafile.git Support N-ary loader functions if compiling in C++11 mode --- diff --git a/source/loader.h b/source/loader.h index de0b32d..02caaaf 100644 --- a/source/loader.h +++ b/source/loader.h @@ -97,6 +97,12 @@ protected: void add(const std::string &k, void (L::*func)(A0)) { add(k, new LoaderFunc1(func)); } +#if __cplusplus>=201103L + template + void add(const std::string &k, void (L::*func)(Args...)) + { add(k, new LoaderFuncN(func)); } + +#else template void add(const std::string &k, void (L::*func)(A0, A1)) { add(k, new LoaderFunc2(func)); } @@ -112,6 +118,7 @@ protected: template void add(const std::string &k, void (L::*func)(A0, A1, A2, A3, A4)) { add(k, new LoaderFunc5(func)); } +#endif /** Adds a keyword that is loaded into a member of the loaded object. */ template diff --git a/source/loaderaction.h b/source/loaderaction.h index 82fd3b4..7e4feac 100644 --- a/source/loaderaction.h +++ b/source/loaderaction.h @@ -1,12 +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; /** @@ -288,6 +299,71 @@ 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(); } +}; + +#endif + + template class LoadValue1: public LoaderAction {