Files CCore/inc/Tuple.h CCore/src/Tuple.cpp
Classes Tuple and ForwardTuple have a spartan design in CCore, because they were introduced to solve some particular problems.
template <class ... TT> struct Tuple { .... Tuple() {} Tuple(const TT & ...); template <class FuncInit> void call(FuncInit func_init); }; template <class ... TT> struct ForwardTuple { .... ForwardTuple() {} ForwardTuple(TT && ...); template <class FuncInit> void call(FuncInit func_init); };
The main problem is to capture an argument list and use it further in a functor call. The difference between Tuple and ForwardTuple is the first makes copies of the arguments, but the second holds rvalue references. Tuple is used when you need to store the arguments. If you have to capture them only temporary then ForwardTuple may be used.
Both classes use Functor Init Pattern.
The method call() builds and calls a functor with the argument list, captured by the tuple.
void func(int a,double b); .... int a; double b; auto tuple = MakeTuple(a,b) ; tuple.call(func); // calls func(a,b);
Tuple is used in the log implementation:
template <class Cat,class Stamp,class T,class ... TT> class LogMsg<Cat,Stamp,T,TT...> : public LogMsgBase<Cat,Stamp> { const char *format; Tuple<T,TT...> data; private: virtual void printBody(PrintBase &out) const { Printf(out,format,data); } public: LogMsg(Cat cat,const char *format_,const T &t,const TT & ... tt) : LogMsgBase<Cat,Stamp>(cat),format(format_),data(t,tt...) {} };
ForwardTuple is used in the array algorithms implementation:
template <class T,class ... SS> struct Creator_fill { enum NoThrowFlagType { NoThrow = false }; ForwardTuple<SS...> ss; explicit Creator_fill(SS && ... ss_) : ss( std::forward(ss_)... ) {} T * operator () (Place<void> place) { T *ret; ss.call( [&ret,place] (SS && ... ss) { ret=new(place) T( std::forward<SS>(ss)... ); } ); return ret; } };
MakeTuple() is a Creator function for Tuple:
template <class ... TT>
Tuple<TT...> MakeTuple(const TT & ... tt);