Lumiera  0.pre.03
»edit your freedom«
handling-pattern.hpp
Go to the documentation of this file.
1 /*
2  HANDLILNG-PATTERN.hpp - A skeleton for executing commands, including standard implementations
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 
35 #ifndef CONTROL_HANDLING_PATTERN_H
36 #define CONTROL_HANDLING_PATTERN_H
37 
38 #include "lib/error.hpp"
39 #include "lib/symbol.hpp"
40 
41 #include <string>
42 
43 
44 
45 namespace steam {
46 namespace control {
47 
48  using std::string;
49  using lib::Symbol;
50 
51 
52  class CommandImpl;
53 
54 
62  class ExecResult
63  {
64  const string log_;
65 
66  public:
67  bool isValid() const;
68  void maybeThrow() const;
69  operator bool() const { return isValid(); }
70 
71  protected:
72  ExecResult () { }
73  ExecResult (lumiera::Error const&);
74 
75  friend class HandlingPattern;
76  };
77 
78 
79 
88  {
89  public:
90  virtual ~HandlingPattern() {}
91 
92  enum ID
93  { SYNC
94  , SYNC_THROW
95  , ASYNC
96  , DUMMY
97 
98  , NUM_IDS
99  };
100 
101  static ID defaultID() { return DUMMY; }
102 
104  static HandlingPattern const& get (ID id);
105 
106 
107 
112  ExecResult exec (CommandImpl& command, string) const;
113 
115  ExecResult undo (CommandImpl& command, string) const;
116 
117  explicit operator bool() const { return isValid(); }
118  virtual bool isValid() const =0;
119 
120  protected:
121 
122  virtual void performExec (CommandImpl& command) const =0;
123  virtual void performUndo (CommandImpl& command) const =0;
124 
125  private:
126  typedef void (HandlingPattern::*Action) (CommandImpl&) const;
127 
128  ExecResult invoke (CommandImpl&, string id, Action) const;
129  };
130 
131 
132 
133  inline ExecResult
134  HandlingPattern::exec (CommandImpl& command, string id) const
135  {
136  return this->invoke (command, id, &HandlingPattern::performExec);
137  }
138 
139  inline ExecResult
140  HandlingPattern::undo (CommandImpl& command, string id) const
141  {
142  return this->invoke (command, id, &HandlingPattern::performUndo);
143  }
144 
145 
146 
147 
148 }} // namespace steam::control
149 #endif
ExecResult undo(CommandImpl &command, string) const
likewise invoke the configured UNDO operation
ExecResult exec(CommandImpl &command, string) const
main functionality: invoke a command, detect errors.
ExecResult()
default: command executed successfully
virtual ~HandlingPattern()
this is an interface
Steam-Layer implementation namespace root.
Token or Atom with distinct identity.
Definition: symbol.hpp:117
Marker types to indicate a literal string and a Symbol.
Result (Status) of command execution.
Lumiera error handling (C++ interface).
Steam-Layer Command implementation.
Interface: Operation Skeleton how to invoke or undo a command.
Interface and Base definition for all Lumiera Exceptions.
Definition: error.hpp:62