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) 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_COMMAND_IMPL_H
41 #define CONTROL_COMMAND_IMPL_H
42 
46 #include "lib/format-string.hpp"
47 #include "lib/nocopy.hpp"
48 
49 #include <memory>
50 #include <functional>
51 
52 
53 namespace steam {
54 namespace control {
55 
56  using util::_Fmt;
57  using std::function;
58  using std::shared_ptr;
59 
60 
61 
79  {
80  Mutation do_;
81  UndoMutation undo_;
82 
84 
85  HandlingPattern::ID defaultPatt_;
86 
87 
88  template<typename ARG>
89  struct _Type
90  {
91  typedef typename ARG::SIG_op SIG_op;
92  typedef typename ARG::SIG_cap SIG_cap;
93  typedef typename ARG::SIG_undo SIG_undo;
94 
95  typedef function<SIG_op> Func_op;
96  typedef function<SIG_cap> Func_cap;
97  typedef function<SIG_undo> Func_undo;
98  };
99 #define _TY(_ID_) typename _Type<ARG>::_ID_
100 
101  public:
107  template<typename ARG>
108  CommandImpl (shared_ptr<ARG> pStorageHolder
109  ,_TY (Func_op) const& operFunctor
110  ,_TY (Func_cap) const& captFunctor
111  ,_TY (Func_undo) const& undoFunctor
112  )
113  : do_(operFunctor)
114  , undo_(pStorageHolder->tie (undoFunctor, captFunctor))
115  , pClo_(pStorageHolder)
116  , defaultPatt_(HandlingPattern::defaultID())
117  { }
118 
119 #undef _TY
120 
121 
122  ~CommandImpl();
123 
124 
136  ,UndoMutation const& newUndo
137  ,shared_ptr<CmdClosure> const& newClosure)
138  : do_{orig.do_}
139  , undo_{newUndo}
140  , pClo_{newClosure}
141  , defaultPatt_{orig.defaultPatt_}
142  , cmdID{orig.cmdID}
143  { }
144 
145  explicit operator bool() const { return isValid(); }
146 
147 
152 
153 
163  void
165  {
166  ASSERT (pClo_);
167  pClo_->accept(visitor);
168  }
169 
170 
171  public: /* === implementation of command functionality === */
172 
173  void
174  setArguments (Arguments& args)
175  {
176  pClo_->bindArguments (args);
177  }
178 
179  void
180  setArguments (lib::diff::Rec const& paramData)
181  {
182  pClo_->bindArguments (paramData);
183  }
184 
185  void
186  discardArguments()
187  {
188  pClo_->unbindArguments();
189  }
190 
191  void invokeOperation() { do_(*pClo_); }
192  void invokeCapture() { undo_.captureState(*pClo_); }
193  void invokeUndo() { undo_(*pClo_); }
194 
195 
196 
197  typedef HandlingPattern::ID PattID;
198 
199  PattID
200  getDefaultHandlingPattern() const
201  {
202  return defaultPatt_;
203  }
204 
207  PattID
209  {
210  PattID currID = defaultPatt_;
211  defaultPatt_ = newID;
212  return currID;
213  }
214 
215 
216 
217  /* === diagnostics === */
218 
219  bool
220  isValid() const
221  {
222  return bool(pClo_)
223  and HandlingPattern::get(defaultPatt_).isValid();
224  }
225 
226  bool
227  canExec() const
228  {
229  return isValid()
230  and pClo_->isValid();
231  }
232 
233  bool
234  canUndo() const
235  {
236  return isValid() and pClo_->isCaptured();
237  }
238 
239  operator string() const
240  {
241  return _Fmt("Cmd|valid:%s, exec:%s, undo:%s |%s")
242  % isValid()
243  % canExec()
244  % canUndo()
245  % (pClo_? string(*pClo_) : util::FAILURE_INDICATOR);
246  }
247  };
248 
249 
250 
251 
252 
253 }} // namespace steam::control
254 #endif
Core of a Steam-Layer command: functor containing the operation to be executed.
Any copy and copy construction prohibited.
Definition: nocopy.hpp:46
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:116
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:150
CommandImpl(CommandImpl const &orig, UndoMutation const &newUndo, shared_ptr< CmdClosure > const &newClosure)
Interface: Operation Skeleton how to invoke or undo a command.