Lumiera  0.pre.03
»edit your freedom«
verb-token.hpp
Go to the documentation of this file.
1 /*
2  VERB-TOKEN.hpp - double dispatch based on DSL tokens
3 
4  Copyright (C)
5  2014, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 */
13 
14 
38 #ifndef LIB_VERB_TOKEN_H
39 #define LIB_VERB_TOKEN_H
40 
41 
42 #include "lib/symbol.hpp"
43 #include "lib/util.hpp"
44 
45 #include <utility>
46 #include <string>
47 
48 
49 namespace lib {
50 
51  using std::string;
52 
53 
68  template<class REC, class SIG>
69  class VerbToken;
70 
71  template<class REC, class RET, typename... ARGS>
72  class VerbToken<REC, RET(ARGS...)>
73  {
74  public:
75  typedef RET (REC::*Handler) (ARGS...);
76 
77  protected:
78  Handler handler_;
79  Literal token_;
80 
81  public:
82  RET
83  applyTo (REC& receiver, ARGS&& ...args)
84  {
85  REQUIRE ("NIL" != token_);
86  return (receiver.*handler_)(std::forward<ARGS>(args)...);
87  }
88 
89  VerbToken(Handler handlerFunction, Literal token)
90  : handler_(handlerFunction)
91  , token_(token)
92  { }
93 
94  VerbToken()
95  : handler_{}
96  , token_("NIL")
97  { }
98 
99  /* default copyable */
100 
101  operator string() const
102  {
103  return string(token_);
104  }
105 
106  Literal const&
107  getID() const
108  {
109  return token_;
110  }
111 
115  bool operator== (VerbToken const& o) const { return token_ == o.token_; }
116  bool operator!= (VerbToken const& o) const { return token_ != o.token_; }
117  };
118 
119 #define VERB(RECEIVER, FUN) VERB_##FUN (&RECEIVER::FUN, STRINGIFY(FUN))
120 
121 
122 
123 
124 
125 } // namespace lib
126 #endif /*LIB_VERB_TOKEN_H*/
Action token implemented by double dispatch to a handler function, as defined in the "receiver" inter...
Definition: verb-token.hpp:69
inline string literal This is a marker type to indicate that
Definition: symbol.hpp:76
bool operator==(PtrDerefIter< I1 > const &il, PtrDerefIter< I2 > const &ir)
Supporting equality comparisons...
Implementation namespace for support and library code.
Marker types to indicate a literal string and a Symbol.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...