Lumiera  0.pre.03
»edit your freedom«
command-storage-holder.hpp
Go to the documentation of this file.
1 /*
2  COMMAND-STORAGE-HOLDER.hpp - specifically typed container for storage of command arguments
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 
44 #ifndef CONTROL_COMMAND_STORAGE_HOLDER_H
45 #define CONTROL_COMMAND_STORAGE_HOLDER_H
46 
51 #include "lib/opaque-holder.hpp"
52 
53 #include <string>
54 
55 
56 
57 namespace steam {
58 namespace control {
59  namespace err = lumiera::error;
60 
61  using lib::InPlaceBuffer;
62  using std::string;
63 
64 
65 
66 
67 
79  template<typename SIG, typename MEM>
81  : public CmdClosure
82  {
83  using ArgHolder = OpClosure<SIG>;
85 
88 
89  using ArgTuple = typename ArgHolder::ArgTuple;
90  using Args = typename lib::meta::RebindTupleTypes<ArgTuple>::Seq; // std::tuple<ARGS...> to Types<ARGS...>
91 
92 
93  /* ====== in-place storage buffers ====== */
94 
95  ArgumentBuff arguments_;
96  MementoBuff memento_;
97 
98 
99 
100  /* ==== proxied CmdClosure interface ==== */
101 
102  public:
103  virtual bool
104  isValid () const override
105  {
106  return arguments_->isValid();
107  }
108 
109  virtual bool
110  isCaptured() const override
111  {
112  return memento_->isValid();
113  }
114 
115 
116 
118  virtual void
119  bindArguments (Arguments& args) override
120  {
121  storeTuple (args.get<ArgTuple>());
122  }
123 
129  virtual void
130  bindArguments (lib::diff::Rec const& paramData) override
131  {
132  storeTuple (buildTuple<Args> (paramData));
133  }
134 
136  virtual void
137  unbindArguments() override
138  {
139  clearStorage();
140  }
141 
142 
143  virtual void
144  invoke (CmdFunctor const& func) override
145  {
146  if (!isValid())
147  throw err::State{"Lifecycle error: can't bind functor, "
148  "command arguments not yet provided"
149  , LERR_(UNBOUND_ARGUMENTS)};
150 
151  arguments_->invoke(func);
152  }
153 
154 
155  virtual
156  operator string() const override
157  {
158  return "Command-State{ arguments="
159  + (arguments_->isValid()? string(*arguments_) : "unbound")
160  + ", "+string(*memento_)+"}"
161  ;
162  }
163 
164 
165 
172  : arguments_()
173  , memento_()
174  { }
175 
181  : arguments_()
182  , memento_()
183  {
184  if (oAh.arguments_->isValid()) // don't clone garbage from invalid arguments
185  arguments_.template create<ArgHolder> (*oAh.arguments_);
186 
187  // memento can be cloned as-is, irrespective of activation state
188  memento_.template create<MemHolder> (*oAh.memento_);
189  }
190 
191 
193  StorageHolder& operator= (StorageHolder const&) = delete;
194 
195 
196 
199  void
200  accept (CommandImplCloneBuilder& visitor) const override
201  {
202  visitor.buildCloneContext (*this);
203  }
204 
205 
207  bool canUndo () const { return memento_->isValid(); }
208  bool empty () const { return !arguments_->isValid(); }
209 
210 
213  void
214  storeTuple (ArgTuple const& argTup)
215  {
216  arguments_.template create<ArgHolder> (argTup);
217  }
218 
219  void
220  clearStorage ()
221  {
222  arguments_.template create<ArgHolder>();
223  memento_->clear();
224  }
225 
226 
227  typedef typename CommandSignature<SIG,MEM>::OperateSig SIG_op;
228  typedef typename CommandSignature<SIG,MEM>::CaptureSig SIG_cap;
229  typedef typename CommandSignature<SIG,MEM>::UndoOp_Sig SIG_undo;
230 
235  tie (function<SIG_undo> const& undoFunc,
236  function<SIG_cap> const& captureFunc)
237  {
238  return memento_.template create<MemHolder> (undoFunc,captureFunc);
239  }
240 
245  {
246  return *memento_;
247  }
248 
249 
250 
254  MEM&
256  {
257  return memento_->getState();
258  }
259  };
260 
261 
262 
263 }} // namespace steam::control
264 #endif /*CONTROL_COMMAND_STORAGE_HOLDER_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?
Abstract foundation for building custom allocation managers.
void buildCloneContext(ARG const &origArgHolder)
to be executed from within the specifically typed context of a concrete command StorageHolder; alloca...
virtual void unbindArguments() override
discard any argument data and return to empty state
MEM & memento()
direct "backdoor" access to stored memento value.
StorageHolder(StorageHolder const &oAh)
copy construction allowed(but no assignment).
Steam-Layer implementation namespace root.
StorageHolder & operator=(StorageHolder const &)=delete
copy construction allowed(but no assignment)
MementoTie< SIG, MEM > & tie(function< SIG_undo > const &undoFunc, function< SIG_cap > const &captureFunc)
create a new memento storage wiring, discarding existing memento state.
closure to deal with the actual command operation.
A special binding used by Steam-Layer commands for capturing UNDO state information.
MementoTie< SIG, MEM > & getMementoWiring()
just re-access an existing memento storage wiring.
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:199
Generic wrapper carrying a function object while hiding the actual function signature.
virtual bool isValid() const override
does this closure hold a valid argument tuple?
Helper allowing type erasure while holding the actual object inline.
virtual void invoke(CmdFunctor const &func) override
invoke functor using the stored parameter values
Visitor to support creating a CommandImpl clone.
void storeTuple(ArgTuple const &argTup)
store a new argument tuple within this StorageHolder, discarding any previously stored arguments ...
Helper for creating an implementation clone, based on the visitor pattern.
virtual void bindArguments(lib::diff::Rec const &paramData) override
assign a new set of parameter values to this.
Buffer to place and maintain an object instance privately within another object.
virtual bool isCaptured() const override
does this closure hold captured UNDO state?
void accept(CommandImplCloneBuilder &visitor) const override
assist with creating a clone copy; this results in invocation of the copy ctor
virtual void bindArguments(Arguments &args) override
assign a new parameter tuple to this
object-like record of data.
Definition: record.hpp:150
Binding together state capturing and execution of the undo operation.
Definition: memento-tie.hpp:88
This is "the" top level CmdClosure implementation.
StorageHolder()
per default, all data within StorageHolder is set up in empty state.