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