Lumiera  0.pre.03
»edityourfreedom«
command-simple-closure.hpp
Go to the documentation of this file.
1 /*
2  COMMAND-SIMPLE-CLOSURE.hpp - demo implementation of command closure
3 
4  Copyright (C) Lumiera.org
5  2016, 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 
46 #ifndef CONTROL_COMMAND_SIMPLE_CLOSURE_H
47 #define CONTROL_COMMAND_SIMPLE_CLOSURE_H
48 
51 #include "lib/opaque-holder.hpp"
52 
53 #include <string>
54 
55 
56 
57 namespace proc {
58 namespace control {
59 
60  using lib::InPlaceBuffer;
61  using std::string;
62 
63 
64 
65 
66 
75  template<typename SIG>
77  : public CmdClosure
78  {
81 
82  using ArgTuple = typename ArgHolder::ArgTuple;
83  using Args = typename lib::meta::RebindTySeq<ArgTuple>::Seq; // std::tuple<ARGS...> to Types<ARGS...>
84 
85 
86  /* ====== in-place argument storage ====== */
87 
89 
90 
91 
92  /* ==== proxied CmdClosure interface ==== */
93 
94  public:
95  virtual bool
96  isValid () const override
97  {
98  return arguments_->isValid();
99  }
100 
101  virtual bool
102  isCaptured() const override
103  {
104  return false;
105  }
106 
107 
108 
110  virtual void
111  bindArguments (Arguments& args) override
112  {
113  storeTuple (args.get<ArgTuple>());
114  }
115 
121  virtual void
122  bindArguments (lib::diff::Rec const& paramData) override
123  {
124  storeTuple (buildTuple<Args> (paramData));
125  }
126 
128  virtual void
129  unbindArguments() override
130  {
131  clearStorage();
132  }
133 
134 
135  virtual void
136  invoke (CmdFunctor const& func) override
137  {
138  if (!isValid())
139  throw lumiera::error::State ("Lifecycle error: can't bind functor, "
140  "command arguments not yet provided",
141  LERR_(UNBOUND_ARGUMENTS));
142 
143  arguments_->invoke(func);
144  }
145 
146 
147  virtual
148  operator string() const override
149  {
150  return "Command-Closure{ arguments="
151  + (arguments_->isValid()? string(*arguments_) : "unbound")
152  + " }"
153  ;
154  }
155 
156 
157 
164  : arguments_()
165  { }
166 
167  explicit
168  SimpleClosure (ArgTuple const& args)
169  : arguments_()
170  {
171  storeTuple (args);
172  }
173 
175  : arguments_()
176  {
177  if (oAh.arguments_->isValid()) // don't clone garbage from invalid arguments
178  arguments_.template create<ArgHolder> (*oAh.arguments_);
179  }
180 
181 
182  void
183  accept (CommandImplCloneBuilder&) const override
184  {
185  NOTREACHED();
186  }
187 
188 
190  bool canUndo () const { return false; }
191  bool empty () const { return !arguments_->isValid(); }
192 
193 
196  void
197  storeTuple (ArgTuple const& argTup)
198  {
199  arguments_.template create<ArgHolder> (argTup);
200  }
201 
202  void
204  {
205  arguments_.template create<ArgHolder>();
206  }
207 
208 
209  bool
210  equals (CmdClosure const& other) const override
211  {
212  const SimpleClosure* toCompare = dynamic_cast<const SimpleClosure*> (&other);
213  return (toCompare)
214  and (*this == *toCompare);
215  }
216 
218  friend bool
220  {
221  return (a1.arguments_->isValid() == a2.arguments_->isValid())
222  and (*a1.arguments_ == *a2.arguments_)
223  ;
224  }
225 
226  friend bool
228  {
229  return not (a1 == a2);
230  }
231  };
232 
233 
234 
235 }} // namespace proc::control
236 #endif /*CONTROL_COMMAND_SIMPLE_CLOSURE_H*/
Implementation of the concrete (sub)-closure of a command, responsible for invoking the actual comman...
bool canUndo() const
has undo state capturing been invoked?
virtual void invoke(CmdFunctor const &func) override
invoke functor using the stored parameter values
void accept(CommandImplCloneBuilder &) const override
assist with creating clone closure without disclosing concrete type
Abstract foundation for building custom allocation managers.
virtual void bindArguments(Arguments &args) override
assign a new parameter tuple to this
friend bool operator==(SimpleClosure const &a1, SimpleClosure const &a2)
Supporting equality comparisons...
friend bool operator!=(SimpleClosure const &a1, SimpleClosure const &a2)
typename ArgHolder::ArgTuple ArgTuple
closure to deal with the actual command operation.
virtual bool isCaptured() const override
does this closure hold captured UNDO state?
#define LERR_(_NAME_)
Definition: error.hpp:58
Generic wrapper carrying a function object while hiding the actual function signature.
LumieraError< LERR_(STATE)> State
Definition: error.hpp:214
SimpleClosure(SimpleClosure const &oAh)
virtual void unbindArguments() override
discard any argument data and return to empty state
Helper allowing type erasure while holding the actual object inline.
typename Types< TYPES... >::Seq Seq
Visitor to support creating a CommandImpl clone.
typename lib::meta::RebindTySeq< ArgTuple >::Seq Args
bool equals(CmdClosure const &other) const override
is equivalent to the given other closure?
void storeTuple(ArgTuple const &argTup)
store a new argument tuple within this StorageHolder, discarding any previously stored arguments ...
Buffer to place and maintain an object instance privately within another object.
Dummy / proof-of-concept implementation of CmdClosure.
Proc-Layer implementation namespace root.
Definition: id-scheme.hpp:63
object-like record of data.
Definition: record.hpp:143
virtual void bindArguments(lib::diff::Rec const &paramData) override
assign a new set of parameter values to this.
SimpleClosure()
per default, all data within StorageHolder is set up in empty state.
virtual bool isValid() const override
does this closure hold a valid argument tuple?