The Battle for Wesnoth  1.15.1+dev
lua_jailbreak_exception.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2018 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
16 
17 #include <cassert>
18 
20 
21 void lua_jailbreak_exception::store() const noexcept
22 {
23  /*
24  * It should not be possible to call this function with an exception still
25  * pending. It could happen if the code doesn't call
26  * lua_jailbreak_exception::rethrow() or a logic error in the code.
27  */
28  assert(!jailbreak_exception);
29 
30  jailbreak_exception = this->clone();
31 }
32 
34 {
35  if(!jailbreak_exception) {
36  return;
37  }
38 
39  /*
40  * We need to call lua_jailbreak_exception::clear() after the exception
41  * is thrown. The most straightforward approach would be a small helper
42  * class calling clear in its destructor, but alas g++ then complains about
43  * an unused variable. Since we're sure there will be something thrown use
44  * that fact to make sure the the function is called.
45  */
46  try {
48  } catch(...) {
49  clear();
50  throw;
51  }
52 
53  /* We never should reach this point. */
54  assert(false);
55 }
56 
58 {
59  delete jailbreak_exception;
60  jailbreak_exception = nullptr;
61 }
static void clear() noexcept
Clears the current exception.
static lua_jailbreak_exception * jailbreak_exception
The exception to be rethrown.
static void rethrow()
Rethrows the stored exception.
void store() const noexcept
Stores a copy the current exception to be rethrown.
virtual void execute()=0
Executes the exception.
virtual lua_jailbreak_exception * clone() const =0
Creates a copy of the current exception.
Base class for exceptions that want to be thrown &#39;through&#39; lua.