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 
58  using boost::equality_comparable;
61  using lib::meta::equals_safeInvoke;
63 
64  LUMIERA_ERROR_DECLARE (MISSING_MEMENTO);
65 
66 
88  template<typename SIG, typename MEM>
89  class MementoTie
90  : public equality_comparable<MementoTie<SIG,MEM>>
91  {
92  typedef typename CommandSignature<SIG,MEM>::CaptureSig SIG_cap;
93  typedef typename CommandSignature<SIG,MEM>::UndoOp_Sig SIG_undo;
94 
96 
97  bool isCaptured_;
98 
99  function<SIG_undo> undo_;
100  function<SIG_cap> capture_;
101 
102 
104  void capture (MEM const& mementoVal)
105  {
106  memento_ = mementoVal;
107  isCaptured_ = true;
108  }
109 
110 
111  public:
112  MementoTie()
113  : MementoTie (function<SIG_undo>(), function<SIG_cap>())
114  { }
115 
121  MementoTie (function<SIG_undo> const& undoFunc,
122  function<SIG_cap> const& captureFunc)
123  : memento_()
124  , isCaptured_(false)
125  , undo_(undoFunc)
126  , capture_(captureFunc)
127  { }
128 
129 
133  void
135  {
136  isCaptured_ = false;
137  memento_.clear();
138  }
139 
146  function<SIG>
148  {
149  using std::bind;
150 
151  return bindLast( undo_ // getState() bound to last argument of undo(...)
152  , bind (&MementoTie::getState, this)
153  );
154  }
155 
161  function<SIG>
163  {
164  using std::placeholders::_1;
165 
166  function<void(MEM)> doCaptureMemento = bind (&MementoTie::capture, this, _1 );
167 
168  return chained(capture_, doCaptureMemento);
169  }
170 
171 
175  MEM&
177  {
178  if (!isCaptured_)
179  throw lumiera::error::State ("need to invoke memento state capturing beforehand",
180  LERR_(MISSING_MEMENTO));
181  return memento_;
182  }
183 
184 
189  explicit
190  operator bool() const
191  {
192  return isValid();
193  }
194 
195  bool
196  isValid () const
197  {
198  return undo_ && capture_ && isCaptured_;
199  }
200 
202  operator std::string() const;
203 
204  };
205 
206 
207  template<typename SIG, typename MEM>
208  MementoTie<SIG,MEM>::operator std::string() const
209  {
210  if (!undo_ || !capture_)
211  return "·noUNDO·";
212 
213  if (!isCaptured_)
214  return "<mem:missing>";
215 
216  return "<mem: "
217  + util::toString (memento_.get())
218  + ">";
219  }
220 
221 
222 
223 }} // namespace steam::control
224 #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.
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:196
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
LUMIERA_ERROR_DECLARE(INVALID_ARGUMENTS)
Arguments provided for binding doesn&#39;t match stored command function parameters.
Simple functions to represent objects, for debugging and diagnostics.
_PapE< SIG >::Function bindLast(SIG &f, TERM const &arg)
bind the last function argument to an arbitrary term, which especially might be a (nested) binder...
void clear()
reverses the effect of capturing state and returns this memento holder into pristine state ...
_Chain< SIG1, SIG2 >::Function chained(SIG1 &f1, SIG2 &f2)
build a functor chaining the given functions
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:95
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:89