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) 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 
40 #ifndef CONTROL_MEMENTO_TIE_H
41 #define CONTROL_MEMENTO_TIE_H
42 
46 #include "lib/replaceable-item.hpp"
47 #include "lib/format-obj.hpp"
48 #include "lib/util.hpp"
49 
50 #include <boost/operators.hpp>
51 #include <functional>
52 #include <string>
53 
54 
55 namespace steam {
56 namespace control {
57  namespace err = lumiera::error;
58 
59  using boost::equality_comparable;
62  using lib::meta::equals_safeInvoke;
64 
65 
87  template<typename SIG, typename MEM>
88  class MementoTie
89  : public equality_comparable<MementoTie<SIG,MEM>>
90  {
91  typedef typename CommandSignature<SIG,MEM>::CaptureSig SIG_cap;
92  typedef typename CommandSignature<SIG,MEM>::UndoOp_Sig SIG_undo;
93 
95 
96  bool isCaptured_;
97 
98  function<SIG_undo> undo_;
99  function<SIG_cap> capture_;
100 
101 
103  void capture (MEM const& mementoVal)
104  {
105  memento_ = mementoVal;
106  isCaptured_ = true;
107  }
108 
109 
110  public:
111  MementoTie()
112  : MementoTie (function<SIG_undo>(), function<SIG_cap>())
113  { }
114 
120  MementoTie (function<SIG_undo> const& undoFunc,
121  function<SIG_cap> const& captureFunc)
122  : memento_()
123  , isCaptured_(false)
124  , undo_(undoFunc)
125  , capture_(captureFunc)
126  { }
127 
128 
132  void
134  {
135  isCaptured_ = false;
136  memento_.clear();
137  }
138 
145  function<SIG>
147  {
148  using std::bind;
149 
150  return bindLast( undo_ // getState() bound to last argument of undo(...)
151  , bind (&MementoTie::getState, this)
152  );
153  }
154 
160  function<SIG>
162  {
163  return chained(capture_
164  ,[this](MEM const& mementoVal){ capture (mementoVal); }
165  );
166  }
167 
168 
172  MEM&
174  {
175  if (!isCaptured_)
176  throw err::State{"need to invoke memento state capturing beforehand"
177  , LERR_(MISSING_MEMENTO)};
178  return memento_;
179  }
180 
181 
186  explicit
187  operator bool() const
188  {
189  return isValid();
190  }
191 
192  bool
193  isValid () const
194  {
195  return undo_ && capture_ && isCaptured_;
196  }
197 
199  operator std::string() const;
200 
201  };
202 
203 
204  template<typename SIG, typename MEM>
205  MementoTie<SIG,MEM>::operator std::string() const
206  {
207  if (!undo_ || !capture_)
208  return "·noUNDO·";
209 
210  if (!isCaptured_)
211  return "<mem:missing>";
212 
213  return "<mem: "
214  + util::toString (memento_.get())
215  + ">";
216  }
217 
218 
219 
220 }} // namespace steam::control
221 #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:199
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
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:94
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:88