The Battle for Wesnoth  1.17.0-dev
lua_jailbreak_exception.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2021
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 
21 
22 void lua_jailbreak_exception::store() const noexcept
23 {
24  /*
25  * It should not be possible to call this function with an exception still
26  * pending. It could happen if the code doesn't call
27  * lua_jailbreak_exception::rethrow() or a logic error in the code.
28  */
29  assert(!jailbreak_exception);
30 
31  jailbreak_exception = this->clone();
32 }
33 
35 {
36  if(!jailbreak_exception) {
37  return;
38  }
39 
40  /*
41  * We need to call lua_jailbreak_exception::clear() after the exception
42  * is thrown. The most straightforward approach would be a small helper
43  * class calling clear in its destructor, but alas g++ then complains about
44  * an unused variable. Since we're sure there will be something thrown use
45  * that fact to make sure the the function is called.
46  */
47  try {
49  } catch(...) {
50  clear();
51  throw;
52  }
53 
54  /* We never should reach this point. */
55  assert(false);
56 }
57 
59 {
60  delete jailbreak_exception;
61  jailbreak_exception = nullptr;
62 }
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.