The Battle for Wesnoth  1.15.11+dev
lua_jailbreak_exception.hpp
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 
15 #pragma once
16 
17 /**
18  * Base class for exceptions that want to be thrown 'through' lua.
19  *
20  * Classes inheriting from this class need to use the @ref
21  * IMPLEMENT_LUA_JAILBREAK_EXCEPTION macro in the class definition.
22  */
24 {
25 public:
26  virtual ~lua_jailbreak_exception() noexcept {}
27 
28  /** Stores a copy the current exception to be rethrown. */
29  void store() const noexcept;
30 
31  /**
32  * Rethrows the stored exception.
33  *
34  * It is safe to call this function is no exception is stored.
35  */
36  static void rethrow();
37 
38 protected:
39 
40  /** The exception to be rethrown. */
42 
43 private:
44 
45  /** Clears the current exception. */
46  static void clear() noexcept;
47 
48  /**
49  * Creates a copy of the current exception.
50  *
51  * The copy is allocated with @p new and is implemented by the @ref
52  * IMPLEMENT_LUA_JAILBREAK_EXCEPTION macro.
53  *
54  * @note it's implemented by the subclass to avoid slicing.
55  *
56  * @returns A pointer to a copy of the class on the heap.
57  */
58  virtual lua_jailbreak_exception* clone() const = 0;
59 
60  /**
61  * Executes the exception.
62  *
63  * Throws a copy of the stored @ref jailbreak_exception. The caller is
64  * responsible for clearing @ref jailbreak_exception after the throw.
65  * The function is implemented by the @ref
66  * IMPLEMENT_LUA_JAILBREAK_EXCEPTION macro.
67  *
68  * @note it's implemented by the subclass to avoid slicing.
69  *
70  * @pre jailbreak_exception != nullptr
71  */
72  virtual void execute() = 0;
73 };
74 
75 /**
76  * Helper macro for classes deriving from @ref lua_jailbreak_exception.
77  *
78  * @ref lua_jailbreak_exception has several pure virtual functions, this
79  * macro implements them properly. This macro needs to be placed in the
80  * definition of the most derived class, which uses @ref
81  * lua_jailbreak_exception as baseclass.
82  *
83  * @param type The type of the class whc
84  */
85 #define IMPLEMENT_LUA_JAILBREAK_EXCEPTION(type) \
86  \
87  virtual type* clone() const { return new type(*this); } \
88  \
89  virtual void execute() \
90  { \
91  type exception(dynamic_cast<type&>(*jailbreak_exception)); \
92  throw exception; \
93  }
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 ~lua_jailbreak_exception() noexcept
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.