Lumiera  0.pre.03
»edityourfreedom«
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
command-op-closure.hpp
Go to the documentation of this file.
1 /*
2  COMMAND-OP-CLOSURE.hpp - implementation the closure for a command operation
3 
4  Copyright (C) Lumiera.org
5  2009, 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 
36 #ifndef CONTROL_COMMAND_OP_CLOSURE_H
37 #define CONTROL_COMMAND_OP_CLOSURE_H
38 
39 #include "lib/meta/function.hpp"
47 #include "lib/format-cout.hpp"
48 
49 #include <memory>
50 #include <functional>
51 #include <sstream>
52 #include <string>
53 
54 
55 
56 
57 namespace proc {
58 namespace control {
59 
60  using lib::meta::_Fun;
61  using lib::meta::Tuple;
64  using lib::meta::NullType;
66 
69  using std::function;
70  using std::ostream;
71  using std::string;
72 
73 
74 
75 
76 
77 
79  template
80  < typename TY
81  , class BASE
82  , class TUP
83  , uint idx
84  >
86  : public BASE
87  {
88  TY & element() { return std::get<idx> (*this); }
89  TY const& element() const { return std::get<idx> (*this); }
90 
91  public:
92  using BASE::BASE;
93 
94 
96 
98 
99 
100  ostream&
101  dump (ostream& output) const
102  {
103  return BASE::dump (output << element() << ',');
104  }
105 
106  friend bool
107  compare (ParamAccessor const& p1, ParamAccessor const& p2)
108  {
109  return equals_safeInvoke (p1.element(), p2.element())
110  && compare ( static_cast<BASE>(p1)
111  , static_cast<BASE>(p2) );
112  }
113  };
114 
115  template<class TUP, uint n>
116  class ParamAccessor<NullType, TUP, TUP, n>
117  : public TUP
118  {
119  public:
120  ParamAccessor (TUP const& tup)
121  : TUP(tup)
122  { }
123 
125  ParamAccessor& operator= (ParamAccessor const&) =delete;
126  ParamAccessor& operator= (ParamAccessor&&) =delete;
127  ParamAccessor (ParamAccessor const&) =default;
128  ParamAccessor (ParamAccessor&&) =default;
129 
130 
132 
133  ostream&
134  dump (ostream& output) const
135  {
136  return output;
137  }
138 
139  friend bool
141  {
142  return true;
143  }
144  };
145 
146 
147 
152  template<typename SIG>
153  class OpClosure
154  {
155  using Args = typename _Fun<SIG>::Args;
157 
159 
162 
163  public:
165 
166 
168  : params_(Tuple<Args>())
169  , activated_(false)
170  { }
171 
172  explicit
173  OpClosure (ArgTuple const& args)
174  : params_(args)
175  , activated_(true)
176  { }
177 
179  OpClosure& operator= (OpClosure const&) =delete;
180  OpClosure& operator= (OpClosure&&) =delete;
181  OpClosure (OpClosure const&) =default;
182  OpClosure (OpClosure&&) =default;
183 
184 
185  bool
186  isValid () const
187  {
188  return activated_;
189  }
190 
191 
200  void
201  invoke (CmdFunctor const& unboundFunctor)
202  {
203  TupleApplicator<SIG> apply_this_arguments(params_);
204  apply_this_arguments (unboundFunctor.getFun<SIG>());
205  }
206 
207 
208 
209  operator string() const
210  {
211  std::ostringstream buff;
212  params_.dump (buff << "OpClosure(" );
213 
214  string dumped (buff.str());
215  if (10 < dumped.length())
216  // remove trailing comma...
217  return dumped.substr (0, dumped.length()-1) +")";
218  else
219  return dumped+")";
220  }
221 
222 
224  friend bool operator== (OpClosure const& c1, OpClosure const& c2) { return compare (c1.params_, c2.params_); }
225  friend bool operator!= (OpClosure const& c1, OpClosure const& c2) { return not (c1 == c2); }
226  };
227 
228 
229 
230 
231 }} // namespace proc::control
232 #endif /*CONTROL_COMMAND_OP_CLOSURE_H*/
Automatically use custom string conversion in C++ stream output.
Abstract foundation for building custom allocation managers.
helpers for fail-safe invocation of comparison operations from generic code.
friend bool compare(ParamAccessor const &, ParamAccessor const &)
friend bool operator!=(OpClosure const &c1, OpClosure const &c2)
Implementation helper to bind Proc-Layer commands with arbitrary argument tuples. ...
std::string dump(std::tuple< TYPES...> const &tuple)
convenience function to dump a given tuple's contents.
Helper for uniform access to function signature types.
Definition: function.hpp:108
Partial function application and building a complete function closure.
typename _Fun< SIG >::Args Args
_X_< Head, NextAccessor, TUP, i > Product
type of the product created by this template.
typename BuildTupleType< TYPES >::Type Tuple
Build a std::tuple from types given as type sequence.
Metaprogramming with tuples-of-types and the std::tuple record.
OpClosure & operator=(OpClosure const &)=delete
we deliberately support immutable types as command arguments
ostream & dump(ostream &output) const
friend bool operator==(OpClosure const &c1, OpClosure const &c2)
Supporting equality comparisons...
closure to deal with the actual command operation.
Specialised adapter to consume a record of GenNode entries to build a tuple.
Metaprogramming tools for transforming functor types.
bool equals_safeInvoke(X const &x1, X const &x2)
Generic wrapper carrying a function object while hiding the actual function signature.
void invoke(CmdFunctor const &unboundFunctor)
Core operation: use the embedded argument tuple for invoking a functor.
typename Builder::Product ParamStorageTuple
friend bool compare(ParamAccessor const &p1, ParamAccessor const &p2)
Foundation for a custom allocation manager, tracking the created objects by smart-ptrs.
Decorating a tuple type with auxiliary data access operations.
OpClosure(ArgTuple const &args)
A closure enabling self-contained execution of commands within the ProcDispatcher.
Tuple< TYPES > buildTuple(SRC values)
convenience shortcut to build a tuple from some suitable source data.
Closure-creating template.
Proc-Layer implementation namespace root.
Definition: id-scheme.hpp:63
Helper for accessing an individual function parameter.