From 941fe3a9b92faf1120faaf64204a4790be7c850b Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Sat, 12 Dec 2015 23:18:25 +0100 Subject: [PATCH] factor out util function to "stringify" arbitrary arguments this function is of use also for creating a vector of strings from a bunch of C-Strings, but it could also be used to construct other stuff initialised by strings (e.g. RegExps) --- src/lib/format-util.hpp | 68 ++++++++++++++++++++++++++++++++++++++ src/lib/test/event-log.hpp | 43 ++++++------------------ 2 files changed, 79 insertions(+), 32 deletions(-) diff --git a/src/lib/format-util.hpp b/src/lib/format-util.hpp index 0fcef6ec4..4af0f391c 100644 --- a/src/lib/format-util.hpp +++ b/src/lib/format-util.hpp @@ -46,6 +46,7 @@ #include #include #include +#include #include #include #include @@ -66,6 +67,7 @@ namespace util { using lib::Symbol; using util::isnil; using std::string; + using std::move; namespace { // we need to guard the string conversion @@ -184,6 +186,72 @@ namespace util { : tyStr(val); } + + + namespace { // helper to convert arbitrary elements toString + + template + inline void + do_stringify(CON&) + { /* do nothing */ } + + template + inline void + do_stringify(CON& container, X const& elm, ELMS const& ...args) + { + container += util::str(elm); + do_stringify (container, args...); + } + + + template + struct SeqContainer + : CON + { + void + operator+= (string&& s) + { + CON::push_back (move(s)); + } + }; + + // most common case: use a vector container... + using std::vector; + + template + struct SeqContainer, ELMS...> + :vector + { + SeqContainer() + { + this->reserve(sizeof...(ELMS)); + } + + void + operator+= (string&& s) + { + this->emplace_back (move(s)); + } + }; + } + + + /** convert a sequence of elements to string + * @param elms sequence of arbitrary elements + * @return a collection of type CON, initialised by the + * string representation of the given elements + */ + template + inline CON + stringify(ELMS const& ...elms) + { + SeqContainer storage; + do_stringify (storage, elms...); + return CON {move(storage)}; + } + + + namespace { // helper to build range iterator on demand template struct _RangeIter diff --git a/src/lib/test/event-log.hpp b/src/lib/test/event-log.hpp index f66e52a4d..c6072a8b1 100644 --- a/src/lib/test/event-log.hpp +++ b/src/lib/test/event-log.hpp @@ -68,29 +68,13 @@ namespace test{ // using lib::Literal; using std::string; + using util::stringify; using util::contains; using util::isnil; using util::_Fmt; using lib::Symbol; // using std::rand; - namespace { - - template - inline void - stringify(CON&) - { - /* NOP */ - } - - template - inline void - stringify(CON& container, X const& elm, ARGS const& ...args) - { - container.emplace_back (util::str(elm)); - stringify (container, args...); - } - } /** @@ -390,19 +374,20 @@ namespace test{ EventMatch& arg (ARGS const& ...args) { - ArgSeq argSeq; - argSeq.reserve(sizeof...(ARGS)); - stringify (argSeq, args...); + ArgSeq argSeq(stringify (args...)); + string argList(util::join(argSeq)); - solution_.andFilter (matchArguments(std::move(argSeq))); - evaluateQuery ("match-arguments("+util::join(argSeq)+")"); + solution_.andFilter (matchArguments(move(argSeq))); + evaluateQuery ("match-arguments("+argList+")"); return *this; } - template + template EventMatch& - argMatch (string regExp, MORE...args) + argMatch (ARGS const& ...regExps) { + stringify (regExps...); + UNIMPLEMENTED("process regular expression match on call argument list"); } @@ -584,10 +569,7 @@ namespace test{ EventLog& call (string target, string function, ARGS const& ...args) { - ArgSeq argSeq; - argSeq.reserve(sizeof...(ARGS)); - stringify (argSeq, args...); - return call (target, function, std::move(argSeq)); + return call (target, function, stringify(args...)); } /** Log a function call on given object ("`this`")... */ @@ -609,10 +591,7 @@ namespace test{ EventLog& note (ELMS const& ...initialiser) { - ArgSeq args; - args.reserve(sizeof...(ELMS)); - stringify (args, initialiser...); - log_->emplace_back(args); + log_->emplace_back(stringify (initialiser...)); return *this; }