The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
wml_exception.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2007 - 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 /**
16  * @file
17  * Add a special kind of assert to validate whether the input from WML
18  * doesn't contain any problems that might crash the game.
19  */
20 
21 #ifndef WML_EXCEPTION_HPP_INCLUDED
22 #define WML_EXCEPTION_HPP_INCLUDED
23 
24 #include "config.hpp"
26 
27 #include <string>
28 
29 class CVideo;
30 
31 /**
32  * The macro to use for the validation of WML
33  *
34  * @param cond The condition to test, if false and exception is generated.
35  * @param message The translatable message to show at the user.
36  */
37 #ifdef _MSC_VER
38  #if _MSC_VER < 1300
39  #define __FUNCTION__ "(Unspecified)"
40  #endif
41 #endif
42 
43 #ifndef __func__
44  #ifdef __FUNCTION__
45  #define __func__ __FUNCTION__
46  #endif
47 #endif
48 
49 #define VALIDATE(cond, message) \
50  do { \
51  if(!(cond)) { \
52  wml_exception(#cond, __FILE__, __LINE__, __func__, message); \
53  } \
54  } while(0)
55 
56 #define VALIDATE_WITH_DEV_MESSAGE(cond, message, dev_message) \
57  do { \
58  if(!(cond)) { \
59  wml_exception(#cond \
60  , __FILE__ \
61  , __LINE__ \
62  , __func__ \
63  , message \
64  , dev_message); \
65  } \
66  } while(0)
67 
68 #define FAIL(message) \
69  do { \
70  wml_exception(nullptr, __FILE__, __LINE__, __func__, message); \
71  /* wml_exception never returns. */ \
72  /* Help the compiler to figure that out */ \
73  throw 42; \
74  } while(0)
75 
76 #define FAIL_WITH_DEV_MESSAGE(message, dev_message) \
77  do { \
78  wml_exception(nullptr \
79  , __FILE__ \
80  , __LINE__ \
81  , __func__ \
82  , message \
83  , dev_message); \
84  /* wml_exception never returns. */ \
85  /* Help the compiler to figure that out */ \
86  throw 42; \
87  } while(0)
88 
89 /**
90  * Helper function, don't call this directly.
91  *
92  * @param cond The textual presentation of the test that failed.
93  * @param file The file in which the test failed.
94  * @param line The line at which the test failed.
95  * @param function The function in which the test failed.
96  * @param message The translated message to show the user.
97  */
98 void wml_exception(
99  const char* cond
100  , const char* file
101  , int line
102  , const char *function
103  , const std::string& message
104  , const std::string& dev_message = "");
105 
106 /** Helper class, don't construct this directly. */
108  : public tlua_jailbreak_exception
109 {
110  twml_exception(const std::string& user_msg, const std::string& dev_msg)
111  : user_message(user_msg)
112  , dev_message(dev_msg)
113  {
114  }
115 
116  ~twml_exception() throw() {}
117 
118  /**
119  * The message for the user explaining what went wrong. This message can
120  * be translated so the user gets a explanation in his/her native tongue.
121  */
123 
124  /**
125  * The message for developers telling which problem was triggered, this
126  * shouldn't be translated. It's hard for a dev to parse errors in
127  * foreign tongues.
128  */
130 
131  /**
132  * Shows the error in a dialog.
133  *
134  * @param video Target for rendering the UI message.
135  */
136  void show(CVideo& video);
137 private:
139 };
140 
141 /**
142  * Returns a standard message for a missing wml key.
143  *
144  * @param section The section is which the key should appear
145  * (this should include the section brackets).
146  * It may contain parent sections to make it
147  * easier to find the wanted sections. They are
148  * listed like [parent][child][section].
149  * @param key The omitted key.
150  * @param primary_key The primary key of the section.
151  * @param primary_value The value of the primary key (mandatory if
152  * primary key isn't empty).
153  *
154  * @returns The error message.
155  */
157  const std::string& section
158  , const std::string& key
159  , const std::string& primary_key = ""
160  , const std::string& primary_value = "");
161 /**
162  * Returns a standard warning message for using a deprecated wml key.
163  *
164  * @param key The deprecated key.
165  * @param removal_version The version in which the key will be
166  * removed key.
167  *
168  * @returns The warning message.
169  */
171  const std::string& key
172  , const std::string& removal_version);
173 
174 /**
175  * Returns a standard warning message for using a deprecated renamed wml key.
176  *
177  * @param deprecated_key The deprecated key.
178  * @param key The new key to be used.
179  * @param removal_version The version in which the key will be
180  * removed key.
181  *
182  * @returns The warning message.
183  */
185  const std::string& deprecated_key
186  , const std::string& key
187  , const std::string& removal_version);
188 
189 /**
190  * Returns a config attribute, using either the old name or the new one.
191  *
192  * The function first tries the find the attribute using @p key and if that
193  * doesn't find the attribute it tries @p deprecated_key. If that test finds
194  * an attribute it will issue a warning and return the result. Else returns
195  * an empty attribute.
196  *
197  * @note This function is not a member of @ref config, since that would add
198  * additional dependencies to the core library.
199  *
200  * @param cfg The config to get the attribute from.
201  * @param deprecated_key The deprecated key.
202  * @param key The new key to be used.
203  * @param removal_version The version in which the key will be
204  * removed key.
205  *
206  * @returns The attribute found as described above.
207  */
209  const config& cfg
210  , const std::string& deprecated_key
211  , const std::string& key
212  , const std::string& removal_version);
213 
214 #endif
215 
twml_exception(const std::string &user_msg, const std::string &dev_msg)
#define IMPLEMENT_LUA_JAILBREAK_EXCEPTION(type)
Helper macro for classes deriving from tlua_jailbreak_exception.
Definition: video.hpp:58
void show(CVideo &video)
Shows the error in a dialog.
std::string deprecate_wml_key_warning(const std::string &key, const std::string &removal_version)
Returns a standard warning message for using a deprecated wml key.
Definitions for the interface to Wesnoth Markup Language (WML).
Variant for storing WML attributes.
Definition: config.hpp:223
std::string user_message
The message for the user explaining what went wrong.
std::string dev_message
The message for developers telling which problem was triggered, this shouldn't be translated...
const config::attribute_value & get_renamed_config_attribute(const config &cfg, const std::string &deprecated_key, const std::string &key, const std::string &removal_version)
Returns a config attribute, using either the old name or the new one.
std::string deprecated_renamed_wml_key_warning(const std::string &deprecated_key, const std::string &key, const std::string &removal_version)
Returns a standard warning message for using a deprecated renamed wml key.
void wml_exception(const char *cond, const char *file, int line, const char *function, const std::string &message, const std::string &dev_message="")
Helper function, don't call this directly.
Base class for exceptions that want to be thrown 'through' lua.
static int cond(LexState *ls)
Definition: lparser.cpp:1168
GLsizei GLenum GLuint GLuint GLsizei char * message
Definition: glew.h:2499
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
Helper class, don't construct this directly.
std::string missing_mandatory_wml_key(const std::string &section, const std::string &key, const std::string &primary_key="", const std::string &primary_value="")
Returns a standard message for a missing wml key.
GLsizei const GLcharARB ** string
Definition: glew.h:4503