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) Lumiera.org
5  2008, 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 
31 #include "lib/error.hpp"
34 #include "lib/format-string.hpp"
35 #include "lib/util.hpp"
36 
37 
38 using util::isnil;
39 using util::_Fmt;
40 
41 
42 namespace steam {
43 namespace control {
44  namespace error = lumiera::error;
45 
46  HandlingPattern const&
48  {
49  return getPatternInstance(id);
50  }
51 
52 
55  HandlingPattern::invoke (CommandImpl& command, string id, Action action) const
56  {
57  const char* cmdName = cStr(id);
58  TRACE (proc_dbg, "invoking %s...", cmdName);
59  static _Fmt err_pre ("Error state detected, %s *NOT* invoked.");
60  static _Fmt err_post ("Error state after %s invocation.");
61  static _Fmt err_fatal ("Execution of %s raised unknown error.");
62  try
63  {
64  Symbol errID_pre = lumiera_error();
65  if (errID_pre)
66  return ExecResult (error::Logic (err_pre % command, errID_pre));
67 
68  // execute or undo it...
69  (this->*action) (command);
70 
71  Symbol errID = lumiera_error();
72  if (errID)
73  return ExecResult (error::State (err_post % command, errID));
74  else
75  return ExecResult();
76  }
77 
78 
79  catch (lumiera::Error& problem)
80  {
81  Symbol errID = lumiera_error();
82  WARN (command, "Invocation of %s failed: %s", cmdName, problem.what());
83  TRACE (proc_dbg, "Error flag was: %s", errID.c());
84  return ExecResult (problem);
85  }
86  catch (std::exception& library_problem)
87  {
88  Symbol errID = lumiera_error();
89  WARN (command, "Invocation of %s failed: %s", cmdName, library_problem.what());
90  TRACE (proc_dbg, "Error flag was: %s", errID.c());
91  return ExecResult (error::External (library_problem));
92  }
93  catch (...)
94  {
95  Symbol errID = lumiera_error();
96  ERROR (command, "Invocation of %s failed with unknown exception; error flag is: %s", cmdName, errID.c());
97  throw error::Fatal (err_fatal % command, errID);
98  }
99  }
100 
101 
102 
103  /* ====== execution result state object ======= */
104 
105 
110  : log_(problem.what())
111  {
112  lumiera_error(); // ensure error flag is cleared
113  }
114 
115 
116  bool
117  ExecResult::isValid() const
118  {
119  return isnil(log_);
120  }
121 
122 
123  void
124  ExecResult::maybeThrow() const
125  {
126  if (!isnil (log_))
127  throw error::Logic ("Command execution failed: "+log_);
128  }
129 
130 
131 }} // namespace steam::control
CStr cStr(std::string const &rendered)
convenience shortcut: forced conversion to c-String via string.
Definition: symbol.hpp:68
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:199
Token or Atom with distinct identity.
Definition: symbol.hpp:126
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:124
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:258
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:71
A hard wired collection of predefined command handling patterns.