wml_exception.hpp

Go to the documentation of this file.
00001 /* $Id: wml_exception.hpp 54252 2012-05-20 09:56:05Z mordante $ */
00002 /*
00003    Copyright (C) 2007 - 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 /**
00017  * @file
00018  * Add a special kind of assert to validate whether the input from WML
00019  * doesn't contain any problems that might crash the game.
00020  */
00021 
00022 #ifndef WML_EXCEPTION_HPP_INCLUDED
00023 #define WML_EXCEPTION_HPP_INCLUDED
00024 
00025 #include "config.hpp"
00026 #include "lua_jailbreak_exception.hpp"
00027 
00028 #include <string>
00029 
00030 class display;
00031 
00032 /**
00033  * The macro to use for the validation of WML
00034  *
00035  *  @param cond         The condition to test, if false and exception is generated.
00036  *  @param message      The translatable message to show at the user.
00037  */
00038 #ifdef _MSC_VER
00039  #if _MSC_VER < 1300
00040   #define __FUNCTION__ "(Unspecified)"
00041  #endif
00042 #endif
00043 
00044 #ifndef __func__
00045  #ifdef __FUNCTION__
00046   #define __func__ __FUNCTION__
00047  #endif
00048 #endif
00049 
00050 #define VALIDATE(cond, message)                                           \
00051     do {                                                                  \
00052         if(!(cond)) {                                                     \
00053             wml_exception(#cond, __FILE__, __LINE__, __func__, message);  \
00054         }                                                                 \
00055     } while(0)
00056 
00057 #define VALIDATE_WITH_DEV_MESSAGE(cond, message, dev_message)             \
00058     do {                                                                  \
00059         if(!(cond)) {                                                     \
00060             wml_exception(#cond                                           \
00061                     , __FILE__                                            \
00062                     , __LINE__                                            \
00063                     , __func__                                            \
00064                     , message                                             \
00065                     , dev_message);                                       \
00066         }                                                                 \
00067     } while(0)
00068 
00069 #define FAIL(message)                                                     \
00070     do {                                                                  \
00071         wml_exception(NULL, __FILE__, __LINE__, __func__, message);       \
00072         /* wml_exception never returns. */                                \
00073         /* Help the compiler to figure that out */                        \
00074         throw 42;                                                         \
00075     } while(0)
00076 
00077 #define FAIL_WITH_DEV_MESSAGE(message, dev_message)                       \
00078     do {                                                                  \
00079         wml_exception(NULL                                                \
00080                 , __FILE__                                                \
00081                 , __LINE__                                                \
00082                 , __func__                                                \
00083                 , message                                                 \
00084                 , dev_message);                                           \
00085         /* wml_exception never returns. */                                \
00086         /* Help the compiler to figure that out */                        \
00087         throw 42;                                                         \
00088     } while(0)
00089 
00090 /**
00091  *  Helper function, don't call this directly.
00092  *
00093  *  @param cond         The textual presentation of the test that failed.
00094  *  @param file         The file in which the test failed.
00095  *  @param line         The line at which the test failed.
00096  *  @param function     The function in which the test failed.
00097  *  @param message      The translated message to show the user.
00098  */
00099 void wml_exception(
00100           const char* cond
00101         , const char* file
00102         , int line
00103         , const char *function
00104         , const std::string& message
00105         , const std::string& dev_message = "");
00106 
00107 /** Helper class, don't construct this directly. */
00108 struct twml_exception
00109     : public tlua_jailbreak_exception
00110 {
00111     twml_exception(const std::string& user_msg, const std::string& dev_msg)
00112         : user_message(user_msg)
00113         , dev_message(dev_msg)
00114     {
00115     }
00116 
00117     ~twml_exception() throw() {}
00118 
00119     /**
00120      *  The message for the user explaining what went wrong. This message can
00121      *  be translated so the user gets a explanation in his/her native tongue.
00122      */
00123     std::string user_message;
00124 
00125     /**
00126      *  The message for developers telling which problem was triggered, this
00127      *  shouldn't be translated. It's hard for a dev to parse errors in
00128      *  foreign tongues.
00129      */
00130     std::string dev_message;
00131 
00132     /**
00133      * Shows the error in a dialog.
00134      *  @param disp         The display object to show the message on.
00135      */
00136     void show(display &disp);
00137 private:
00138     IMPLEMENT_LUA_JAILBREAK_EXCEPTION(twml_exception)
00139 };
00140 
00141 /**
00142  * Returns a standard message for a missing wml key.
00143  *
00144  * @param section                 The section is which the key should appear
00145  *                                (this should include the section brackets).
00146  *                                It may contain parent sections to make it
00147  *                                easier to find the wanted sections. They are
00148  *                                listed like [parent][child][section].
00149  * @param key                     The ommitted key.
00150  * @param primary_key             The primary key of the section.
00151  * @param primary_value           The value of the primary key (mandatory if
00152  *                                primary key isn't empty).
00153  *
00154  * @returns                       The error message.
00155  */
00156 std::string missing_mandatory_wml_key(
00157           const std::string& section
00158         , const std::string& key
00159         , const std::string& primary_key = ""
00160         , const std::string& primary_value = "");
00161 /**
00162  * Returns a standard warning message for using a deprecated wml key.
00163  *
00164  * @param key                     The deprecated key.
00165  * @param removal_version         The version in which the key will be
00166  *                                removed key.
00167  *
00168  * @returns                       The warning message.
00169  */
00170 std::string deprecate_wml_key_warning(
00171           const std::string& key
00172         , const std::string& removal_version);
00173 
00174 /**
00175  * Returns a standard warning message for using a deprecated renamed wml key.
00176  *
00177  * @param deprecated_key          The deprecated key.
00178  * @param key                     The new key to be used.
00179  * @param removal_version         The version in which the key will be
00180  *                                removed key.
00181  *
00182  * @returns                       The warning message.
00183  */
00184 std::string deprecated_renamed_wml_key_warning(
00185           const std::string& deprecated_key
00186         , const std::string& key
00187         , const std::string& removal_version);
00188 
00189 /**
00190  * Returns a config attribute, using either the old name or the new one.
00191  *
00192  * The function first tries the find the attribute using @p key and if that
00193  * doesn't find the attribute it tries @p deprecated_key. If that test finds
00194  * an attribute it will issue a warning and return the result. Else returns
00195  * an empty attribute.
00196  *
00197  * @note This function is not a member of @ref config, since that would add
00198  * additional dependencies to the core library.
00199  *
00200  * @param cfg                     The config to get the attribute from.
00201  * @param deprecated_key          The deprecated key.
00202  * @param key                     The new key to be used.
00203  * @param removal_version         The version in which the key will be
00204  *                                removed key.
00205  *
00206  * @returns                       The attribute found as described above.
00207  */
00208 const config::attribute_value& get_renamed_config_attribute(
00209           const config& cfg
00210         , const std::string& deprecated_key
00211         , const std::string& key
00212         , const std::string& removal_version);
00213 
00214 #endif
00215 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

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