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