wml_exception.hpp

Go to the documentation of this file.
00001 /* $Id: wml_exception.hpp 26690 2008-05-18 13:47:04Z mordante $ */
00002 /*
00003    Copyright (C) 2007 - 2008 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 version 2
00008    or at your option any later version.
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY.
00011 
00012    See the COPYING file for more details.
00013 */
00014 
00015 /**
00016  * @file wml_exception.hpp
00017  * Add a special kind of assert to validate whether the input from WML
00018  * doesn't contain any problems that might crash the game.
00019  */
00020 
00021 #ifndef WML_EXCEPTION_HPP_INCLUDED
00022 #define WML_EXCEPTION_HPP_INCLUDED
00023 
00024 #include "tstring.hpp"
00025 
00026 #include <string>
00027 
00028 class display;
00029 
00030 /**
00031  * The macro to use for the validation of WML
00032  * 
00033  *  @param cond         The condition to test, if false and exception is generated.
00034  *  @param message      The translatable message to show at the user.
00035  */
00036 #ifdef _MSC_VER
00037  #if _MSC_VER < 1300
00038   #define __FUNCTION__ "(Unspecified)"
00039  #endif
00040 #endif
00041 
00042 // Sun Studio compilers call __func__ not __FUNCTION__
00043 #ifdef __SUNPRO_CC
00044 #define VALIDATE(cond, message) if(!(cond)) wml_exception(#cond, __FILE__, __LINE__, __func__, message)
00045 #else
00046 #define VALIDATE(cond, message) if(!(cond)) wml_exception(#cond, __FILE__, __LINE__, __FUNCTION__, message)
00047 #endif
00048 
00049 /**
00050  *  Helper function, don't call this directly.
00051  * 
00052  *  @param cond         The textual presentation of the test that failed.
00053  *  @param file         The file in which the test failed.
00054  *  @param line         The line at which the test failed.
00055  *  @param function     The funtion in which the test failed.
00056  *  @param message      The translatable message to show the user.
00057  */
00058 void wml_exception(const char* cond, const char* file, 
00059     const int line, const char* function, const t_string& message);
00060 
00061 /** Helper function, don't call this directly. */
00062 inline void wml_exception(const char* cond, const char* file, 
00063     const int line, const char* function, const char* message)
00064 {
00065     wml_exception(cond, file, line, function, t_string(message));
00066 }
00067 
00068 /** Helper function, don't call this directly. */
00069 inline void wml_exception(const char* cond, const char* file, 
00070     const int line, const char* function, const std::string& message)
00071 {
00072     wml_exception(cond, file, line, function, t_string(message));
00073 }
00074 
00075 /** Helper class, don't construct this directly. */
00076 struct twml_exception 
00077 {
00078     twml_exception(const t_string& user_msg, const std::string& dev_msg);
00079     
00080     /*
00081      *  The message for the user explaining what went wrong. This message can
00082      *  be translated so the user gets a explanation in his/her native tongue.
00083      */
00084     t_string user_message;
00085 
00086     /**
00087      *  The message for developers telling which problem was triggered, this
00088      *  shouldn't be translated. It's hard for a dev to parse errors in
00089      *  foreign tongues.
00090      */
00091     std::string dev_message;
00092 
00093     /** 
00094      * Shows the error in a dialog.
00095      *  @param disp         The display object to show the message on.
00096      */
00097     void show(display &disp);
00098 };
00099 
00100 /**
00101  *  Returns a standard message for a missing wml key.
00102  * 
00103  *  @param section      The section is which the key should appear.
00104  *  @param key          The ommitted key.
00105  *  @param primary_key  The primary key of the section.
00106  *  @param primary_value
00107  *                      The value of the primary key (mandatory if primary key
00108  *                      isn't empty).
00109  * 
00110  *  @return             The error message.
00111  */
00112 t_string missing_mandatory_wml_key(const std::string& section, const std::string& key,
00113         const std::string& primary_key = "", const std::string& primary_value = "");
00114 
00115 #endif
00116 

Generated by doxygen 1.5.5 on 23 May 2008 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs