Lumiera  0.pre.03
»edit your freedom«
handling-pattern.cpp
Go to the documentation of this file.
1 /*
2  HandlingPattern - A skeleton for executing commands, including standard implementations
3 
4  Copyright (C)
5  2008, 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 
22 #include "lib/error.hpp"
25 #include "lib/format-string.hpp"
26 #include "lib/util.hpp"
27 
28 
29 using util::isnil;
30 using util::_Fmt;
31 
32 
33 namespace steam {
34 namespace control {
35  namespace error = lumiera::error;
36 
37  HandlingPattern const&
39  {
40  return getPatternInstance(id);
41  }
42 
43 
46  HandlingPattern::invoke (CommandImpl& command, string id, Action action) const
47  {
48  const char* cmdName = cStr(id);
49  TRACE (proc_dbg, "invoking %s...", cmdName);
50  static _Fmt err_pre ("Error state detected, %s *NOT* invoked.");
51  static _Fmt err_post ("Error state after %s invocation.");
52  static _Fmt err_fatal ("Execution of %s raised unknown error.");
53  try
54  {
55  Symbol errID_pre = lumiera_error();
56  if (errID_pre)
57  return ExecResult (error::Logic (err_pre % command, errID_pre));
58 
59  // execute or undo it...
60  (this->*action) (command);
61 
62  Symbol errID = lumiera_error();
63  if (errID)
64  return ExecResult (error::State (err_post % command, errID));
65  else
66  return ExecResult();
67  }
68 
69 
70  catch (lumiera::Error& problem)
71  {
72  Symbol errID = lumiera_error();
73  WARN (command, "Invocation of %s failed: %s", cmdName, problem.what());
74  TRACE (proc_dbg, "Error flag was: %s", errID.c());
75  return ExecResult (problem);
76  }
77  catch (std::exception& library_problem)
78  {
79  Symbol errID = lumiera_error();
80  WARN (command, "Invocation of %s failed: %s", cmdName, library_problem.what());
81  TRACE (proc_dbg, "Error flag was: %s", errID.c());
82  return ExecResult (error::External (library_problem));
83  }
84  catch (...)
85  {
86  Symbol errID = lumiera_error();
87  ERROR (command, "Invocation of %s failed with unknown exception; error flag is: %s", cmdName, errID.c());
88  throw error::Fatal (err_fatal % command, errID);
89  }
90  }
91 
92 
93 
94  /* ====== execution result state object ======= */
95 
96 
101  : log_(problem.what())
102  {
103  lumiera_error(); // ensure error flag is cleared
104  }
105 
106 
107  bool
108  ExecResult::isValid() const
109  {
110  return isnil(log_);
111  }
112 
113 
114  void
115  ExecResult::maybeThrow() const
116  {
117  if (!isnil (log_))
118  throw error::Logic ("Command execution failed: "+log_);
119  }
120 
121 
122 }} // namespace steam::control
CStr cStr(std::string const &rendered)
convenience shortcut: forced conversion to c-String via string.
Definition: symbol.hpp:59
ExecResult()
default: command executed successfully
virtual CStr what() const noexcept override
std::exception interface : yield a diagnostic message
Front-end for printf-style string template interpolation.
Steam-Layer implementation namespace root.
A front-end for using printf-style formatting.
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:190
Token or Atom with distinct identity.
Definition: symbol.hpp:117
Result (Status) of command execution.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
lumiera_err lumiera_error(void)
Get and clear current error state.
Definition: error-state.c:115
HandlingPattern const & getPatternInstance(HandlingPattern::ID id)
access the singleton instance for a given ID
Pre-defined command execution skeletons.
void maybeThrow(string description="")
Check the lumiera error state and throw a specific exception in case a non-cleared errorflag is detec...
Definition: error.hpp:249
Lumiera error handling (C++ interface).
Steam-Layer Command implementation.
static HandlingPattern const & get(ID id)
retrieve the pre-configured pattern
ExecResult invoke(CommandImpl &, string id, Action) const
Interface and Base definition for all Lumiera Exceptions.
Definition: error.hpp:62
A hard wired collection of predefined command handling patterns.