Lumiera 0.pre.04~rc.1
»edit your freedom«
Loading...
Searching...
No Matches
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/item-wrapper.hpp"
38#include "lib/format-obj.hpp"
39#include "lib/util.hpp"
40
41#include <functional>
42#include <string>
43
44
45namespace steam {
46namespace control {
47 namespace err = lumiera::error;
48
53
54
76 template<typename SIG, typename MEM>
78 {
81
83
85
88
89
91 void capture (MEM const& mementoVal)
92 {
94 isCaptured_ = true;
95 }
96
97
98 public:
100 : MementoTie (function<SIG_undo>(), function<SIG_cap>())
101 { }
102
115
116
120 void
122 {
123 isCaptured_ = false;
124 memento_.reset();
125 }
126
135 {
136 using std::bind;
137
138 return bindLast( undo_ // getState() bound to last argument of undo(...)
139 , bind (&MementoTie::getState, this)
140 );
141 }
142
150 {
151 return chained(capture_
152 ,[this](MEM const& mementoVal){ capture (mementoVal); }
153 );
154 }
155
156
160 MEM&
162 {
163 if (not isCaptured_)
164 throw err::State{"need to invoke memento state capturing beforehand"
165 , LERR_(MISSING_MEMENTO)};
166 return *memento_;
167 }
168
169
174 explicit
175 operator bool() const
176 {
177 return isValid();
178 }
179
180 bool
181 isValid () const
182 {
184 }
185
187 operator std::string() const;
188
189 };
190
191
192 template<typename SIG, typename MEM>
194 {
195 if (not undo_ or not capture_)
196 return "·noUNDO·";
197
198 if (not isCaptured_)
199 return "<mem:missing>";
200
201 return "<mem: "
202 + util::toString (*memento_)
203 + ">";
204 }
205
206
207
208}} // namespace steam::control
209#endif
Universal value/ref wrapper accessible similar to a pointer.
BuildFunType< void, ExtendedArgs >::Sig UndoOp_Sig
BuildFunType< MEM, Args >::Sig CaptureSig
Binding together state capturing and execution of the undo operation.
CommandSignature< SIG, MEM >::CaptureSig SIG_cap
ItemWrapper< MEM > memento_
storage holding the captured state for undo
function< SIG > tieUndoFunc()
bind the undo function to the internal memento store within this object.
MementoTie(function< SIG_undo > const &undoFunc, function< SIG_cap > const &captureFunc)
creates an execution context tying together the provided functions.
MEM & getState()
access the currently captured memento state value
void capture(MEM const &mementoVal)
to be chained behind the capture function
CommandSignature< SIG, MEM >::UndoOp_Sig SIG_undo
function< SIG_undo > undo_
void clear()
reverses the effect of capturing state and returns this memento holder into pristine state
function< SIG_cap > capture_
function< SIG > tieCaptureFunc()
bind the capturing function to the internal memento store within this object.
Metaprogramming helpers for deriving the precise function signatures necessary to implement a given c...
#define LERR_(_NAME_)
Definition error.hpp:45
Simple functions to represent objects, for debugging and diagnostics.
Partial function application and building a complete function closure.
Adapter to store and hold an element of arbitrary type in local storage.
helpers for fail-safe invocation of comparison operations from generic code.
auto bindLast(FUN &&f, TERM &&arg)
auto chained(FUN1 &&f1, FUN2 &&f2)
build a functor chaining the given functions: feed the result of f1 into f2.
enable_if_c< Cond::value, T >::type enable_if
SFINAE helper to control the visibility of specialisations and overloads.
Definition meta/util.hpp:87
bool equals_safeInvoke(X const &x1, X const &x2)
LumieraError< LERR_(STATE)> State
Definition error.hpp:209
Steam-Layer implementation namespace root.
std::string toString(TY const &val) noexcept
get some string representation of any object, reliably.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...