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) Lumiera.org
5  2014, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 */
22 
23 
47 #ifndef LIB_VERB_TOKEN_H
48 #define LIB_VERB_TOKEN_H
49 
50 
51 #include "lib/symbol.hpp"
52 #include "lib/util.hpp"
53 
54 #include <utility>
55 #include <string>
56 
57 
58 namespace lib {
59 
60  using std::string;
61 
62 
77  template<class REC, class SIG>
78  class VerbToken;
79 
80  template<class REC, class RET, typename... ARGS>
81  class VerbToken<REC, RET(ARGS...)>
82  {
83  public:
84  typedef RET (REC::*Handler) (ARGS...);
85 
86  protected:
87  Handler handler_;
88  Literal token_;
89 
90  public:
91  RET
92  applyTo (REC& receiver, ARGS&& ...args)
93  {
94  REQUIRE ("NIL" != token_);
95  return (receiver.*handler_)(std::forward<ARGS>(args)...);
96  }
97 
98  VerbToken(Handler handlerFunction, Literal token)
99  : handler_(handlerFunction)
100  , token_(token)
101  { }
102 
103  VerbToken()
104  : handler_{}
105  , token_("NIL")
106  { }
107 
108  /* default copyable */
109 
110  operator string() const
111  {
112  return string(token_);
113  }
114 
115  Literal const&
116  getID() const
117  {
118  return token_;
119  }
120 
124  bool operator== (VerbToken const& o) const { return token_ == o.token_; }
125  bool operator!= (VerbToken const& o) const { return token_ != o.token_; }
126  };
127 
128 #define VERB(RECEIVER, FUN) VERB_##FUN (&RECEIVER::FUN, STRINGIFY(FUN))
129 
130 
131 
132 
133 
134 } // namespace lib
135 #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:78
inline string literal This is a marker type to indicate that
Definition: symbol.hpp:85
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...