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