Lumiera  0.pre.03
»edit your freedom«
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)
5  2009, 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 
27 #ifndef CONTROL_COMMAND_OP_CLOSURE_H
28 #define CONTROL_COMMAND_OP_CLOSURE_H
29 
30 #include "lib/meta/function.hpp"
38 #include "lib/format-cout.hpp"
39 
40 #include <memory>
41 #include <functional>
42 #include <sstream>
43 #include <string>
44 
45 
46 
47 
48 namespace steam {
49 namespace control {
50 
51  using lib::meta::_Fun;
52  using lib::meta::Tuple;
55  using lib::meta::NullType;
57 
59  using std::function;
60  using std::ostream;
61  using std::string;
62 
63 
64 
65 
66 
67 
69  template
70  < typename TY
71  , class BASE
72  , class TUP
73  , uint idx
74  >
76  : public BASE
77  {
78  TY & element() { return std::get<idx> (*this); }
79  TY const& element() const { return std::get<idx> (*this); }
80 
81  public:
82  using BASE::BASE;
83 
84 
86 
88 
89  ostream&
90  dump (ostream& output) const
91  {
92  return BASE::dump (output << element() << ',');
93  }
94  };
95 
96  template<class TUP, uint n>
97  class ParamAccessor<NullType, TUP, TUP, n>
98  : public TUP
99  {
100  public:
101  ParamAccessor (TUP const& tup)
102  : TUP(tup)
103  { }
104 
106  ParamAccessor& operator= (ParamAccessor const&) =delete;
107  ParamAccessor& operator= (ParamAccessor&&) =delete;
108  ParamAccessor (ParamAccessor const&) =default;
109  ParamAccessor (ParamAccessor&&) =default;
110 
111 
113 
114  ostream&
115  dump (ostream& output) const
116  {
117  return output;
118  }
119  };
120 
121 
122 
127  template<typename SIG>
128  class OpClosure
129  {
130  using Args = typename _Fun<SIG>::Args;
132 
133  using ParamStorageTuple = typename Builder::Product;
134 
135  ParamStorageTuple params_;
136  bool activated_;
137 
138  public:
139  using ArgTuple = Tuple<Args>;
140 
141 
142  OpClosure()
143  : params_(Tuple<Args>())
144  , activated_(false)
145  { }
146 
147  explicit
148  OpClosure (ArgTuple const& args)
149  : params_(args)
150  , activated_(true)
151  { }
152 
154  OpClosure& operator= (OpClosure const&) =delete;
155  OpClosure& operator= (OpClosure&&) =delete;
156  OpClosure (OpClosure const&) =default;
157  OpClosure (OpClosure&&) =default;
158 
159 
160  bool
161  isValid () const
162  {
163  return activated_;
164  }
165 
166 
175  void
176  invoke (CmdFunctor const& unboundFunctor)
177  {
178  TupleApplicator<SIG> apply_this_arguments(params_);
179  apply_this_arguments (unboundFunctor.getFun<SIG>());
180  }
181 
182 
183 
184  operator string() const
185  {
186  std::ostringstream buff;
187  params_.dump (buff << "OpClosure(" );
188 
189  string dumped (buff.str());
190  if (10 < dumped.length())
191  // remove trailing comma...
192  return dumped.substr (0, dumped.length()-1) +")";
193  else
194  return dumped+")";
195  }
196  };
197 
198 
199 
200 
201 }} // namespace steam::control
202 #endif /*CONTROL_COMMAND_OP_CLOSURE_H*/
Automatically use custom string conversion in C++ stream output.
Abstract foundation for building custom allocation managers.
typename BuildTupleType< TYPES >::Type Tuple
Build a std::tuple from types given as type sequence.
helpers for fail-safe invocation of comparison operations from generic code.
Implementation helper to bind Steam-Layer commands with arbitrary argument tuples.
Helper for uniform access to function signature types.
Definition: function.hpp:99
Partial function application and building a complete function closure.
_X_< Head, NextAccessor, TUP, i > Product
type of the product created by this template.
Tuple< TYPES > buildTuple(SRC values)
convenience shortcut to build a tuple from some suitable source data.
Steam-Layer implementation namespace root.
closure to deal with the actual command operation.
Metaprogramming with tuples-of-types and the std::tuple record.
Specialised adapter to consume a record of GenNode entries to build a tuple.
Metaprogramming tools for transforming functor types.
Generic wrapper carrying a function object while hiding the actual function signature.
Helper for accessing an individual function parameter.
Foundation for a custom allocation manager, tracking the created objects by smart-ptrs.
Decorating a tuple type with auxiliary data access operations.
A closure enabling self-contained execution of commands within the SteamDispatcher.
Closure-creating template.
void invoke(CmdFunctor const &unboundFunctor)
Core operation: use the embedded argument tuple for invoking a functor.
std::string dump(std::tuple< TYPES... > const &tuple)
convenience function to dump a given tuple&#39;s contents.