Lumiera  0.pre.03
»edit your freedom«
command-impl.hpp
Go to the documentation of this file.
1 /*
2  COMMAND-IMPL.hpp - Steam-Layer command implementation (top level)
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_COMMAND_IMPL_H
32 #define CONTROL_COMMAND_IMPL_H
33 
37 #include "lib/format-string.hpp"
38 #include "lib/nocopy.hpp"
39 
40 #include <memory>
41 #include <functional>
42 
43 
44 namespace steam {
45 namespace control {
46 
47  using util::_Fmt;
48  using std::function;
49  using std::shared_ptr;
50 
51 
52 
70  {
71  Mutation do_;
72  UndoMutation undo_;
73 
75 
76  HandlingPattern::ID defaultPatt_;
77 
78 
79  template<typename ARG>
80  struct _Type
81  {
82  typedef typename ARG::SIG_op SIG_op;
83  typedef typename ARG::SIG_cap SIG_cap;
84  typedef typename ARG::SIG_undo SIG_undo;
85 
86  typedef function<SIG_op> Func_op;
87  typedef function<SIG_cap> Func_cap;
88  typedef function<SIG_undo> Func_undo;
89  };
90 #define _TY(_ID_) typename _Type<ARG>::_ID_
91 
92  public:
98  template<typename ARG>
99  CommandImpl (shared_ptr<ARG> pStorageHolder
100  ,_TY (Func_op) const& operFunctor
101  ,_TY (Func_cap) const& captFunctor
102  ,_TY (Func_undo) const& undoFunctor
103  )
104  : do_(operFunctor)
105  , undo_(pStorageHolder->tie (undoFunctor, captFunctor))
106  , pClo_(pStorageHolder)
107  , defaultPatt_(HandlingPattern::defaultID())
108  { }
109 
110 #undef _TY
111 
112 
113  ~CommandImpl();
114 
115 
127  ,UndoMutation const& newUndo
128  ,shared_ptr<CmdClosure> const& newClosure)
129  : do_{orig.do_}
130  , undo_{newUndo}
131  , pClo_{newClosure}
132  , defaultPatt_{orig.defaultPatt_}
133  , cmdID{orig.cmdID}
134  { }
135 
136  explicit operator bool() const { return isValid(); }
137 
138 
143 
144 
154  void
156  {
157  ASSERT (pClo_);
158  pClo_->accept(visitor);
159  }
160 
161 
162  public: /* === implementation of command functionality === */
163 
164  void
165  setArguments (Arguments& args)
166  {
167  pClo_->bindArguments (args);
168  }
169 
170  void
171  setArguments (lib::diff::Rec const& paramData)
172  {
173  pClo_->bindArguments (paramData);
174  }
175 
176  void
177  discardArguments()
178  {
179  pClo_->unbindArguments();
180  }
181 
182  void invokeOperation() { do_(*pClo_); }
183  void invokeCapture() { undo_.captureState(*pClo_); }
184  void invokeUndo() { undo_(*pClo_); }
185 
186 
187 
188  typedef HandlingPattern::ID PattID;
189 
190  PattID
191  getDefaultHandlingPattern() const
192  {
193  return defaultPatt_;
194  }
195 
198  PattID
200  {
201  PattID currID = defaultPatt_;
202  defaultPatt_ = newID;
203  return currID;
204  }
205 
206 
207 
208  /* === diagnostics === */
209 
210  bool
211  isValid() const
212  {
213  return bool(pClo_)
214  and HandlingPattern::get(defaultPatt_).isValid();
215  }
216 
217  bool
218  canExec() const
219  {
220  return isValid()
221  and pClo_->isValid();
222  }
223 
224  bool
225  canUndo() const
226  {
227  return isValid() and pClo_->isCaptured();
228  }
229 
230  operator string() const
231  {
232  return _Fmt("Cmd|valid:%s, exec:%s, undo:%s |%s")
233  % isValid()
234  % canExec()
235  % canUndo()
236  % (pClo_? string(*pClo_) : util::FAILURE_INDICATOR);
237  }
238  };
239 
240 
241 
242 
243 
244 }} // namespace steam::control
245 #endif
Core of a Steam-Layer command: functor containing the operation to be executed.
Any copy and copy construction prohibited.
Definition: nocopy.hpp:37
bool canExec() const
< state check: sufficiently defined to be invoked
Front-end for printf-style string template interpolation.
void prepareClone(CommandImplCloneBuilder &visitor) const
assist with building a clone copy of this CommandImpl.
bool isValid() const
< validity self-check: is basically usable.
PattID setHandlingPattern(PattID newID)
define a handling pattern to be used by default
Steam-Layer implementation namespace root.
A front-end for using printf-style formatting.
Specialised version of the command Mutation functor, used to implement the UNDO functionality.
Token or Atom with distinct identity.
Definition: symbol.hpp:117
Mix-Ins to allow or prohibit various degrees of copying and cloning.
Symbol cmdID
human-readable marker for diagnostics, will be (re)assigned when activating this CommandImpl ...
Steam-Layer command frontend.
Visitor to support creating a CommandImpl clone.
bool canUndo() const
< state check: has undo state been captured?
Unspecific command functor for implementing Steam-Layer Command.
A closure enabling self-contained execution of commands within the SteamDispatcher.
Steam-Layer Command implementation.
static HandlingPattern const & get(ID id)
retrieve the pre-configured pattern
CommandImpl(shared_ptr< ARG > pStorageHolder, _TY(Func_op) const &operFunctor, _TY(Func_cap) const &captFunctor, _TY(Func_undo) const &undoFunctor)
build a new implementation frame, and do the initial wiring.
object-like record of data.
Definition: record.hpp:141
CommandImpl(CommandImpl const &orig, UndoMutation const &newUndo, shared_ptr< CmdClosure > const &newClosure)
Interface: Operation Skeleton how to invoke or undo a command.