lua_jailbreak_exception.hpp

Go to the documentation of this file.
00001 /* $Id: lua_jailbreak_exception.hpp 52869 2012-02-03 20:18:33Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2011 - 2012 by Mark de Wever <koraq@xs4all.nl>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 #ifndef LUA_JAILBREAK_EXCEPTION
00017 #define LUA_JAILBREAK_EXCEPTION
00018 
00019 #include "global.hpp"
00020 
00021 /**
00022  * Base class for exceptions that want to be thrown 'through' lua.
00023  *
00024  * Classes inheriting from this class need to use the @ref
00025  * IMPLEMENT_LUA_JAILBREAK_EXCEPTION macro in the class definition.
00026  */
00027 class tlua_jailbreak_exception
00028 {
00029 public:
00030     virtual ~tlua_jailbreak_exception() throw() {}
00031 
00032     /** Stores a copy the current exception to be rethrown. */
00033     void store() const throw();
00034 
00035     /**
00036      * Rethrows the stored exception.
00037      *
00038      * It is safe to call this function is no exception is stored.
00039      */
00040     static void rethrow();
00041 
00042 protected:
00043 
00044     /** The exception to be rethrown. */
00045     static tlua_jailbreak_exception* jailbreak_exception;
00046 
00047 private:
00048 
00049     /** Clears the current exception. */
00050     static void clear() throw();
00051 
00052     /**
00053      * Creates a copy of the current exception.
00054      *
00055      * The copy is allocated with @p new and is implemented by the @ref
00056      * IMPLEMENT_LUA_JAILBREAK_EXCEPTION macro.
00057      *
00058      * @note it's implemented by the subclass to avoid slicing.
00059      *
00060      * @returns                   A pointer to a copy of the class on the heap.
00061      */
00062     virtual tlua_jailbreak_exception* clone() const = 0;
00063 
00064     /**
00065      * Executes the exception.
00066      *
00067      * Throws a copy of the stored @ref jailbreak_exception. The caller is
00068      * responsible for clearing @ref jailbreak_exception after the throw.
00069      * The function is implemented by the @ref
00070      * IMPLEMENT_LUA_JAILBREAK_EXCEPTION macro.
00071      *
00072      * @note it's implemented by the subclass to avoid slicing.
00073      *
00074      * @pre                       jailbreak_exception != NULL
00075      */
00076     virtual void execute() = 0;
00077 };
00078 
00079 /**
00080  * Helper macro for classes deriving from @ref tlua_jailbreak_exception.
00081  *
00082  * @ref tlua_jailbreak_exception has several pure virtual functions, this
00083  * macro implements them properly. This macro needs to be placed in the
00084  * definition of the most derived class, which uses @ref
00085  * tlua_jailbreak_exception as baseclass.
00086  *
00087  * @param type                    The type of the class whc
00088  */
00089 #define IMPLEMENT_LUA_JAILBREAK_EXCEPTION(type)                      \
00090                                                                      \
00091     virtual type* clone() const { return new type(*this); }          \
00092                                                                      \
00093     virtual void execute()                                           \
00094     {                                                                \
00095         type exception(dynamic_cast<type&>(*jailbreak_exception));   \
00096         throw exception;                                             \
00097     }
00098 
00099 #endif
00100 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:05 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs