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) 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 
44 #ifndef CONTROL_HANDLING_PATTERN_H
45 #define CONTROL_HANDLING_PATTERN_H
46 
47 #include "lib/error.hpp"
48 #include "lib/symbol.hpp"
49 
50 #include <string>
51 
52 
53 
54 namespace steam {
55 namespace control {
56 
57  using std::string;
58  using lib::Symbol;
59 
60 
61  class CommandImpl;
62 
63 
71  class ExecResult
72  {
73  const string log_;
74 
75  public:
76  bool isValid() const;
77  void maybeThrow() const;
78  operator bool() const { return isValid(); }
79 
80  protected:
81  ExecResult () { }
82  ExecResult (lumiera::Error const&);
83 
84  friend class HandlingPattern;
85  };
86 
87 
88 
97  {
98  public:
99  virtual ~HandlingPattern() {}
100 
101  enum ID
102  { SYNC
103  , SYNC_THROW
104  , ASYNC
105  , DUMMY
106 
107  , NUM_IDS
108  };
109 
110  static ID defaultID() { return DUMMY; }
111 
113  static HandlingPattern const& get (ID id);
114 
115 
116 
121  ExecResult exec (CommandImpl& command, string) const;
122 
124  ExecResult undo (CommandImpl& command, string) const;
125 
126  explicit operator bool() const { return isValid(); }
127  virtual bool isValid() const =0;
128 
129  protected:
130 
131  virtual void performExec (CommandImpl& command) const =0;
132  virtual void performUndo (CommandImpl& command) const =0;
133 
134  private:
135  typedef void (HandlingPattern::*Action) (CommandImpl&) const;
136 
137  ExecResult invoke (CommandImpl&, string id, Action) const;
138  };
139 
140 
141 
142  inline ExecResult
143  HandlingPattern::exec (CommandImpl& command, string id) const
144  {
145  return this->invoke (command, id, &HandlingPattern::performExec);
146  }
147 
148  inline ExecResult
149  HandlingPattern::undo (CommandImpl& command, string id) const
150  {
151  return this->invoke (command, id, &HandlingPattern::performUndo);
152  }
153 
154 
155 
156 
157 }} // namespace steam::control
158 #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:126
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:71