Library: first attempt to get the flexible VerbToken to work

...still not decided yet if this whole apporach is sound...
This commit is contained in:
Fischlurch 2019-04-17 18:32:21 +02:00
parent 9b5fdd39b8
commit 6fbd1021ba
3 changed files with 84 additions and 158 deletions

View file

@ -53,7 +53,6 @@
#include <utility> #include <utility>
#include <string> #include <string>
#include <array>
namespace lib { namespace lib {

View file

@ -50,94 +50,47 @@
#include "lib/meta/variadic-helper.hpp" #include "lib/meta/variadic-helper.hpp"
#include "lib/polymorphic-value.hpp" #include "lib/polymorphic-value.hpp"
#include "lib/verb-token.hpp"
#include "lib/symbol.hpp" #include "lib/symbol.hpp"
#include "lib/util.hpp" #include "lib/util.hpp"
#include <utility> #include <utility>
#include <string> #include <string>
#include <array>
#include <tuple> #include <tuple>
namespace lib { namespace lib {
using std::string; namespace {
using JustSomeIrrelvantType = struct{};
const size_t VERB_TOKEN_SIZE = sizeof(VerbToken<JustSomeIrrelvantType, void(void)>);
/**
* Action token implemented by double dispatch to a handler function, constexpr size_t
* as defined in the "receiver" interface (parameter \c REC). storageOverhead(size_t argStorage)
* The token is typically part of a DSL and can be applied
* to a concrete receiver subclass.
* @tparam REC the type receiving the verb invocations
* @tparam SIG signature of the actual verb function, expected
* to exist on the receiver (REC) interface
* @remarks while the included ID Literal is mostly for diagnostics,
* it also serves as identity for comparisons. Conceptually what
* we want is to compare the function "offset", but this leads
* into relying on implementation defined behaviour.
* @note the #VERB macro simplifies definition of actual tokens
*/
template<class REC, class SIG>
class VerbToken;
template<class REC, class RET, typename... ARGS>
class VerbToken<REC, RET(ARGS...)>
{ {
public: return argStorage + VERB_TOKEN_SIZE;
typedef RET (REC::*Handler) (ARGS...); }
/**
* Helper: pass a reference to an tuple element into a "perfect forwarding" call
* This is the equivalent of calling std::forward<TY> (TY&)
* ////////////////////////////////////////////////////////////////////TODO verify this reasoning is sound!!
*/
template<size_t idx, typename...ARGS>
decltype(auto) constexpr
get_ref (std::tuple<ARGS...>& tuple)
{
using Elm = std::remove_reference_t<
std::tuple_element_t<idx, std::tuple<ARGS...>>>;
private: return static_cast<Elm&&> (std::get<idx> (tuple));
Handler handler_; }
Literal token_; }
public:
RET
applyTo (REC& receiver, ARGS&& ...args)
{
REQUIRE ("NIL" != token_);
return (receiver.*handler_)(std::forward<ARGS>(args)...);
}
VerbToken(Handler handlerFunction, Literal token)
: handler_(handlerFunction)
, token_(token)
{ }
VerbToken()
: handler_{}
, token_("NIL")
{ }
/* default copyable */
operator string() const
{
return string(token_);
}
Literal const&
getID()
{
return token_;
}
/** equality of VerbToken, based on equality of the #token_ Literal
* @remarks member pointers to virtual functions aren't comparable, for good reason
*/
bool operator== (VerbToken const& o) const { return token_ == o.token_; }
bool operator!= (VerbToken const& o) const { return token_ != o.token_; }
};
#define VERB(RECEIVER, FUN) VERB_##FUN (&RECEIVER::FUN, STRINGIFY(FUN))
using JustSomeIrrelvantType = struct{};
const size_t VERB_TOKEN_SIZE = sizeof(VerbToken<JustSomeIrrelvantType, void(void)>);
template<class REC, class RET> template<class REC, class RET>
struct Hook struct VerbInvoker
{ {
virtual ~Hook() { } virtual ~VerbInvoker() { }
virtual RET applyTo (REC&) =0; virtual RET applyTo (REC&) =0;
}; };
@ -148,7 +101,7 @@ namespace lib {
template<class REC, class RET, typename... ARGS> template<class REC, class RET, typename... ARGS>
struct Holder<REC, RET(ARGS...)> struct Holder<REC, RET(ARGS...)>
: polyvalue::CopySupport< // mix-in virtual copy/move support : polyvalue::CopySupport< // mix-in virtual copy/move support
Hook<REC,RET>> // ...the common interface to use VerbInvoker<REC,RET>> // ...the common interface to use
{ {
using Verb = VerbToken<REC,RET(ARGS...)>; using Verb = VerbToken<REC,RET(ARGS...)>;
using Args = std::tuple<ARGS...>; using Args = std::tuple<ARGS...>;
@ -174,22 +127,17 @@ namespace lib {
RET RET
invokeVerb (REC& receiver, meta::IndexSeq<idx...>) invokeVerb (REC& receiver, meta::IndexSeq<idx...>)
{ {
return verb_.applyTo (receiver, std::get<idx> (args_)...); return verb_.applyTo (receiver, get_ref<idx> (args_)...);
} }
}; };
static constexpr size_t
storageOverhead(size_t argStorage)
{
return argStorage + VERB_TOKEN_SIZE;
}
template<class REC, class RET, size_t arg_storage> template<class REC, class RET, size_t arg_storage>
class VerbPack class VerbPack
: public PolymorphicValue<Hook<REC,RET>, storageOverhead(arg_storage)> : public PolymorphicValue<VerbInvoker<REC,RET>, storageOverhead(arg_storage)>
{ {
using PolyHolder = PolymorphicValue<Hook<REC,RET>, storageOverhead(arg_storage)>; using PolyHolder = PolymorphicValue<VerbInvoker<REC,RET>, storageOverhead(arg_storage)>;
template<typename...ARGS> template<typename...ARGS>
using PayloadType = Holder<REC, RET(ARGS...)>*; using PayloadType = Holder<REC, RET(ARGS...)>*;

View file

@ -27,16 +27,17 @@
#include "lib/test/run.hpp" #include "lib/test/run.hpp"
//#include "lib/verb-token.hpp"
#include "lib/verb-visitor.hpp" #include "lib/verb-visitor.hpp"
#include "lib/format-string.hpp" #include "lib/format-string.hpp"
#include "lib/format-cout.hpp" #include "lib/format-cout.hpp"
#include "lib/format-util.hpp"
#include <string> #include <string>
#include <vector> #include <vector>
using std::string; using std::string;
using util::_Fmt; using util::_Fmt;
using util::join;
using std::vector; using std::vector;
@ -49,23 +50,15 @@ namespace test{
public: public:
virtual ~Receiver() { } ///< this is an interface virtual ~Receiver() { } ///< this is an interface
virtual string woof() =0; virtual string woof (bool huge, uint cnt) =0;
virtual string honk() =0; virtual string honk (string) =0;
virtual string moo() =0; virtual string moo (size_t num) =0;
virtual string meh() =0; virtual string meh () =0;
}; };
namespace { namespace {
const string BEGINNING("silence"); using Token = VerbPack<Receiver, string, sizeof(string)>;
using TokenSeq = vector<Token>;
using Verb = VerbToken<Receiver, string(void)>;
using VerbSeq = vector<Verb>;
Verb VERB(Receiver, woof);
Verb VERB(Receiver, honk);
Verb VERB(Receiver, moo);
Verb VERB(Receiver, meh);
} }
@ -76,42 +69,29 @@ namespace test{
class VerboseRenderer class VerboseRenderer
: public Receiver : public Receiver
{ {
string woof() { return "Woof-Woof!"; }
string honk() { return "Honk-Honk!"; }
string moo() { return "Moo-Moo!"; }
string meh() { return "Meh!"; }
};
/**
* Statefull receiver of verb-tokens.
*/
class RecollectingReceiver
: public Receiver
{
string verb_;
_Fmt fmt_;
string string
buildResultTerm (string nextToken) woof (bool huge, uint cnt) override
{ {
string resultExpression (fmt_ % verb_ % nextToken); string woof{huge? "Woof..":"haw-haw"};
verb_ = nextToken; while (0 < cnt--)
return resultExpression; woof += woof;
return woof;
}
string
honk (string theHonk) override
{
return theHonk+"-"+theHonk+"!";
}
string
moo (size_t num) override
{
return join (vector<string>{num, "Moo"}, "__");
}
string
meh() override
{
return "Meh!";
} }
string woof() { return buildResultTerm (VERB_woof); }
string honk() { return buildResultTerm (VERB_honk); }
string moo() { return buildResultTerm (VERB_moo); }
string meh() { return buildResultTerm (VERB_meh); }
public:
RecollectingReceiver()
: verb_(BEGINNING)
, fmt_("%s followed by %s")
{ }
}; };
@ -119,6 +99,7 @@ namespace test{
/***********************************************************************//** /***********************************************************************//**
* @test Demonstration/Concept: dispatch a specific function * @test Demonstration/Concept: dispatch a specific function
* based on the given verbs of an embedded custom language. * based on the given verbs of an embedded custom language.
@ -134,19 +115,15 @@ namespace test{
virtual void virtual void
run (Arg) run (Arg)
{ {
VerbSeq tokens = build_test_feed(); TokenSeq tokens = build_and_copy_tokens();
render_verbose (tokens); render_verbose (tokens);
verify_dispatch (tokens);
VerbPack<Receiver, string, sizeof(void*)> woof(&Receiver::woof, "woof");
// profile.append_woof(1, 2); // profile.append_woof(1, 2);
} }
/** prepare a sequence of verbs /** prepare a sequence of verbs
* for the actual tests to work on */ * for the actual tests to work on */
VerbSeq /* VerbSeq
build_test_feed() build_test_feed()
{ {
return { return {
@ -156,7 +133,24 @@ namespace test{
VERB_meh VERB_meh
}; };
} }
*/
/** @test verify the correct individual dispatch
* through a computation specific for the given verb
*/
TokenSeq
build_and_copy_tokens ()
{
Token bigWoof(&Receiver::woof, "woof", true, 2u);
Token littleWoof(&Receiver::woof, "woof", false, 3u);
Token quack(&Receiver::honk, "honk", string{"quaack"});
Token honk(&Receiver::honk, "honk", string{"Hoonk"});
Token moo(&Receiver::moo, "moo", size_t(3));
Token meh(&Receiver::meh, "meh");
return TokenSeq{{littleWoof, quack,honk, bigWoof, moo, meh}};
}
/** @test demonstrate the dispatching /** @test demonstrate the dispatching
* based on the concrete verb token. * based on the concrete verb token.
@ -164,31 +158,16 @@ namespace test{
* the name of the invoked verb * the name of the invoked verb
*/ */
void void
render_verbose (VerbSeq tokens) render_verbose (TokenSeq& tokens)
{ {
VerboseRenderer receiver; VerboseRenderer receiver;
for (Verb verb : tokens) // for (Token tok : tokens)
cout << "consuming " << verb // cout << "dispatching " << tok
<< " -> '" // << " -> '"
<< verb.applyTo(receiver) // << tok.applyTo(receiver)
<< "'\n"; // << "'\n";
} }
/** @test verify the correct individual dispatch
* through a computation specific for the given verb
*/
void
verify_dispatch (VerbSeq tokens)
{
RecollectingReceiver receiver;
string previous = BEGINNING;
for (Verb verb : tokens)
{
CHECK (previous+" followed by "+string(verb) == verb.applyTo(receiver));
previous = string(verb);
}
}
}; };