Lumiera  0.pre.03
»edit your freedom«
memento-tie.hpp
Go to the documentation of this file.
1 /*
2  MEMENTO-TIE.hpp - capturing and providing state for undoing commands
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 
31 #ifndef CONTROL_MEMENTO_TIE_H
32 #define CONTROL_MEMENTO_TIE_H
33 
37 #include "lib/replaceable-item.hpp"
38 #include "lib/format-obj.hpp"
39 #include "lib/util.hpp"
40 
41 #include <boost/operators.hpp>
42 #include <functional>
43 #include <string>
44 
45 
46 namespace steam {
47 namespace control {
48  namespace err = lumiera::error;
49 
50  using boost::equality_comparable;
53  using lib::meta::equals_safeInvoke;
55 
56 
78  template<typename SIG, typename MEM>
79  class MementoTie
80  : public equality_comparable<MementoTie<SIG,MEM>>
81  {
82  typedef typename CommandSignature<SIG,MEM>::CaptureSig SIG_cap;
83  typedef typename CommandSignature<SIG,MEM>::UndoOp_Sig SIG_undo;
84 
86 
87  bool isCaptured_;
88 
89  function<SIG_undo> undo_;
90  function<SIG_cap> capture_;
91 
92 
94  void capture (MEM const& mementoVal)
95  {
96  memento_ = mementoVal;
97  isCaptured_ = true;
98  }
99 
100 
101  public:
102  MementoTie()
103  : MementoTie (function<SIG_undo>(), function<SIG_cap>())
104  { }
105 
111  MementoTie (function<SIG_undo> const& undoFunc,
112  function<SIG_cap> const& captureFunc)
113  : memento_()
114  , isCaptured_(false)
115  , undo_(undoFunc)
116  , capture_(captureFunc)
117  { }
118 
119 
123  void
125  {
126  isCaptured_ = false;
127  memento_.clear();
128  }
129 
136  function<SIG>
138  {
139  using std::bind;
140 
141  return bindLast( undo_ // getState() bound to last argument of undo(...)
142  , bind (&MementoTie::getState, this)
143  );
144  }
145 
151  function<SIG>
153  {
154  return chained(capture_
155  ,[this](MEM const& mementoVal){ capture (mementoVal); }
156  );
157  }
158 
159 
163  MEM&
165  {
166  if (!isCaptured_)
167  throw err::State{"need to invoke memento state capturing beforehand"
168  , LERR_(MISSING_MEMENTO)};
169  return memento_;
170  }
171 
172 
177  explicit
178  operator bool() const
179  {
180  return isValid();
181  }
182 
183  bool
184  isValid () const
185  {
186  return undo_ && capture_ && isCaptured_;
187  }
188 
190  operator std::string() const;
191 
192  };
193 
194 
195  template<typename SIG, typename MEM>
196  MementoTie<SIG,MEM>::operator std::string() const
197  {
198  if (!undo_ || !capture_)
199  return "·noUNDO·";
200 
201  if (!isCaptured_)
202  return "<mem:missing>";
203 
204  return "<mem: "
205  + util::toString (memento_.get())
206  + ">";
207  }
208 
209 
210 
211 }} // namespace steam::control
212 #endif
function< SIG > tieCaptureFunc()
bind the capturing function to the internal memento store within this object.
helpers for fail-safe invocation of comparison operations from generic code.
function< SIG > tieUndoFunc()
bind the undo function to the internal memento store within this object.
Partial function application and building a complete function closure.
Steam-Layer implementation namespace root.
_PapE< SIG >::FunType::Functor bindLast(SIG &f, TERM const &arg)
bind the last function argument to an arbitrary term, which especially might be a (nested) binder...
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:190
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
void capture(MEM const &mementoVal)
to be chained behind the capture function
Definition: memento-tie.hpp:94
auto chained(FUN1 &&f1, FUN2 &&f2)
build a functor chaining the given functions: feed the result of f1 into f2.
Simple functions to represent objects, for debugging and diagnostics.
void clear()
reverses the effect of capturing state and returns this memento holder into pristine state ...
Adapter container to take snapshots from non-assignable values.
MementoTie(function< SIG_undo > const &undoFunc, function< SIG_cap > const &captureFunc)
creates an execution context tying together the provided functions.
Metaprogramming helpers for deriving the precise function signatures necessary to implement a given c...
ReplaceableItem< MEM > memento_
storage holding the captured state for undo
Definition: memento-tie.hpp:85
MEM & getState()
access the currently captured memento state value
Adapter wrapper to treat non-assignable values as if they were assignable.
Binding together state capturing and execution of the undo operation.
Definition: memento-tie.hpp:79