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) 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 steam {
58 namespace control {
59 
60  using lib::meta::_Fun;
61  using lib::meta::Tuple;
64  using lib::meta::NullType;
66 
68  using std::function;
69  using std::ostream;
70  using std::string;
71 
72 
73 
74 
75 
76 
78  template
79  < typename TY
80  , class BASE
81  , class TUP
82  , uint idx
83  >
85  : public BASE
86  {
87  TY & element() { return std::get<idx> (*this); }
88  TY const& element() const { return std::get<idx> (*this); }
89 
90  public:
91  using BASE::BASE;
92 
93 
95 
97 
98  ostream&
99  dump (ostream& output) const
100  {
101  return BASE::dump (output << element() << ',');
102  }
103  };
104 
105  template<class TUP, uint n>
106  class ParamAccessor<NullType, TUP, TUP, n>
107  : public TUP
108  {
109  public:
110  ParamAccessor (TUP const& tup)
111  : TUP(tup)
112  { }
113 
115  ParamAccessor& operator= (ParamAccessor const&) =delete;
116  ParamAccessor& operator= (ParamAccessor&&) =delete;
117  ParamAccessor (ParamAccessor const&) =default;
118  ParamAccessor (ParamAccessor&&) =default;
119 
120 
122 
123  ostream&
124  dump (ostream& output) const
125  {
126  return output;
127  }
128  };
129 
130 
131 
136  template<typename SIG>
137  class OpClosure
138  {
139  using Args = typename _Fun<SIG>::Args;
141 
142  using ParamStorageTuple = typename Builder::Product;
143 
144  ParamStorageTuple params_;
145  bool activated_;
146 
147  public:
148  using ArgTuple = Tuple<Args>;
149 
150 
151  OpClosure()
152  : params_(Tuple<Args>())
153  , activated_(false)
154  { }
155 
156  explicit
157  OpClosure (ArgTuple const& args)
158  : params_(args)
159  , activated_(true)
160  { }
161 
163  OpClosure& operator= (OpClosure const&) =delete;
164  OpClosure& operator= (OpClosure&&) =delete;
165  OpClosure (OpClosure const&) =default;
166  OpClosure (OpClosure&&) =default;
167 
168 
169  bool
170  isValid () const
171  {
172  return activated_;
173  }
174 
175 
184  void
185  invoke (CmdFunctor const& unboundFunctor)
186  {
187  TupleApplicator<SIG> apply_this_arguments(params_);
188  apply_this_arguments (unboundFunctor.getFun<SIG>());
189  }
190 
191 
192 
193  operator string() const
194  {
195  std::ostringstream buff;
196  params_.dump (buff << "OpClosure(" );
197 
198  string dumped (buff.str());
199  if (10 < dumped.length())
200  // remove trailing comma...
201  return dumped.substr (0, dumped.length()-1) +")";
202  else
203  return dumped+")";
204  }
205  };
206 
207 
208 
209 
210 }} // namespace steam::control
211 #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:108
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.