The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
lua_common.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2016 by Chris Beck <[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  * Common callbacks and functions to manipulate config, vconfig, tstring
17  * in lua, and macros to get them from the stack.
18  */
19 
20 #ifndef LUA_COMMON_HPP_INCLUDED
21 #define LUA_COMMON_HPP_INCLUDED
22 
23 struct lua_State;
24 class t_string;
25 class vconfig;
26 
27 #include "config.hpp"
28 #include "scripting/lua_types.hpp"
29 #include "variable_info.hpp"
30 #include "map/location.hpp"
31 #include <vector>
32 
33 namespace lua_common {
34  int intf_textdomain(lua_State *L);
35  int intf_tovconfig(lua_State* L);
36 
40 
41 }
42 extern const char * tstringKey;
43 
44 /**
45  * Pushes a vconfig on the top of the stack.
46  */
47 void luaW_pushvconfig(lua_State *L, vconfig const &cfg);
48 
49 /**
50  * Pushes a t_string on the top of the stack.
51  */
52 void luaW_pushtstring(lua_State *L, t_string const &v);
53 
54 /**
55  * Converts an attribute value into a Lua object pushed at the top of the stack.
56  */
58 
59 /**
60  * Converts the value at the top of the stack to an attribute value
61  */
63 
64 
65 /**
66  * Returns true if the metatable of the object is the one found in the registry.
67  */
68 bool luaW_hasmetatable(lua_State *L, int index, luatypekey key);
69 
70 /**
71  * Converts a scalar to a translatable string.
72  */
73 bool luaW_totstring(lua_State *L, int index, t_string &str);
74 
75 /**
76  * Converts a scalar to a translatable string.
77  */
79 
80 /**
81  * Converts a config object to a Lua table.
82  * The destination table should be at the top of the stack on entry. It is
83  * still at the top on exit.
84  */
85 void luaW_filltable(lua_State *L, config const &cfg);
86 
87 /**
88  * Converts a map location object to a Lua table pushed at the top of the stack.
89  */
90 void luaW_pushlocation(lua_State *L, map_location const &loc);
91 
92 /**
93  * Converts an optional table or pair of integers to a map location object.
94  * @param index stack position of the table or first integer.
95  * @return false if a map location couldn't be matched.
96  */
97 bool luaW_tolocation(lua_State *L, int index, map_location &loc);
98 
99 /**
100  * Converts an optional table or pair of integers to a map location object.
101  * @note If a pair of integers was found, the first one will be removed
102  * from the stack when the function returns.
103  */
105 
106 /**
107  * Converts a config object to a Lua table pushed at the top of the stack.
108  */
109 void luaW_pushconfig(lua_State *L, config const &cfg);
110 
111 /**
112  * Converts an optional table or vconfig to a config object.
113  * @param index stack position of the table.
114  * @return false if some attributes had not the proper type.
115  * @note If the table has holes in the integer keys or floating-point keys,
116  * some keys will be ignored and the error will go undetected.
117  */
118 bool luaW_toconfig(lua_State *L, int index, config &cfg);
119 
120 /**
121  * Converts an optional table or vconfig to a config object.
122  */
124 
125 /**
126  * Gets an optional vconfig from either a table or a userdata.
127  * @return false in case of failure.
128  */
129 bool luaW_tovconfig(lua_State *L, int index, vconfig &vcfg);
130 
131 /**
132  * Gets an optional vconfig from either a table or a userdata.
133  * @param allow_missing true if missing values are allowed; the function
134  * then returns an unconstructed vconfig.
135  */
136 vconfig luaW_checkvconfig(lua_State *L, int index, bool allow_missing = false);
137 
138 /**
139  * Pushes the value found by following the variadic names (char *), if the
140  * value is not nil.
141  * @return true if an element was pushed.
142  */
143 bool luaW_getglobal(lua_State *L, const std::vector<std::string>& path);
144 
145 /**
146  * Pushes the value found by following the variadic names (char *), if the
147  * value is not nil.
148  * @return true if an element was pushed.
149  */
150 template<typename... T>
151 bool luaW_getglobal(lua_State *L, T... path) {
152  return luaW_getglobal(L, std::vector<std::string> {path...} );
153 }
154 
155 bool luaW_toboolean(lua_State *L, int n);
156 
157 
159 
161 
162 
163 #define return_tstring_attrib(name, accessor) \
164  if (strcmp(m, name) == 0) { \
165  luaW_pushtstring(L, accessor); \
166  return 1; \
167  }
168 
169 #define return_cstring_attrib(name, accessor) \
170  if (strcmp(m, name) == 0) { \
171  lua_pushstring(L, accessor); \
172  return 1; \
173  }
174 
175 #define return_string_attrib(name, accessor) \
176  return_cstring_attrib(name, (accessor).c_str())
177 
178 #define return_int_attrib(name, accessor) \
179  if (strcmp(m, name) == 0) { \
180  lua_pushinteger(L, accessor); \
181  return 1; \
182  }
183 
184 #define return_float_attrib(name, accessor) \
185  if (strcmp(m, name) == 0) { \
186  lua_pushnumber(L, accessor); \
187  return 1; \
188  }
189 
190 #define return_bool_attrib(name, accessor) \
191  if (strcmp(m, name) == 0) { \
192  lua_pushboolean(L, accessor); \
193  return 1; \
194  }
195 
196 #define return_cfg_attrib(name, accessor) \
197  if (strcmp(m, name) == 0) { \
198  config cfg; \
199  accessor; \
200  luaW_pushconfig(L, cfg); \
201  return 1; \
202  }
203 
204 #define return_cfgref_attrib(name, accessor) \
205  if (strcmp(m, name) == 0) { \
206  luaW_pushconfig(L, accessor); \
207  return 1; \
208  }
209 
210 #define return_vector_string_attrib(name, accessor) \
211  if (strcmp(m, name) == 0) { \
212  const std::vector<std::string>& vector = accessor; \
213  lua_createtable(L, vector.size(), 0); \
214  int i = 1; \
215  for (const std::string& s : vector) { \
216  lua_pushstring(L, s.c_str()); \
217  lua_rawseti(L, -2, i); \
218  ++i; \
219  } \
220  return 1; \
221  }
222 
223 #define modify_tstring_attrib(name, accessor) \
224  if (strcmp(m, name) == 0) { \
225  t_string value = luaW_checktstring(L, 3); \
226  accessor; \
227  return 0; \
228  }
229 
230 #define modify_string_attrib(name, accessor) \
231  if (strcmp(m, name) == 0) { \
232  const char *value = luaL_checkstring(L, 3); \
233  accessor; \
234  return 0; \
235  }
236 
237 #define modify_int_attrib(name, accessor) \
238  if (strcmp(m, name) == 0) { \
239  int value = luaL_checkinteger(L, 3); \
240  accessor; \
241  return 0; \
242  }
243 
244 #define modify_int_attrib_check_range(name, accessor, allowed_min, allowed_max) \
245  if (strcmp(m, name) == 0) { \
246  int value = luaL_checkinteger(L, 3); \
247  if (value < allowed_min || allowed_max < value) return luaL_argerror(L, 3, "out of bounds"); \
248  accessor; \
249  return 0; \
250  }
251 
252 #define modify_bool_attrib(name, accessor) \
253  if (strcmp(m, name) == 0) { \
254  bool value = luaW_toboolean(L, 3); \
255  accessor; \
256  return 0; \
257  }
258 
259 #define modify_vector_string_attrib(name, accessor) \
260  if (strcmp(m, name) == 0) { \
261  std::vector<std::string> vector; \
262  char const* message = "table with unnamed indices holding strings expected"; \
263  if (!lua_istable(L, 3)) return luaL_argerror(L, 3, message); \
264  unsigned length = lua_rawlen(L, 3); \
265  for (unsigned i = 1; i <= length; ++i) { \
266  lua_rawgeti(L, 3, i); \
267  char const* string = lua_tostring(L, 4); \
268  if(!string) return luaL_argerror(L, 2 + i, message); \
269  vector.push_back(string); \
270  lua_pop(L, 1); \
271  } \
272  accessor; \
273  return 0; \
274  }
275 
276 #endif
config luaW_checkconfig(lua_State *L, int index)
Converts an optional table or vconfig to a config object.
Definition: lua_common.cpp:749
const char * tstringKey
Definition: lua_common.cpp:48
std::string register_tstring_metatable(lua_State *L)
Adds the tstring metatable.
Definition: lua_common.cpp:395
bool luaW_toconfig(lua_State *L, int index, config &cfg)
Converts an optional table or vconfig to a config object.
Definition: lua_common.cpp:675
Extends variable_info with methods that can only be applied if vit != vit_const.
map_location luaW_checklocation(lua_State *L, int index)
Converts an optional table or pair of integers to a map location object.
Definition: lua_common.cpp:655
int intf_textdomain(lua_State *L)
Creates an interface for gettext.
Definition: lua_common.cpp:77
Definitions for the interface to Wesnoth Markup Language (WML).
Variant for storing WML attributes.
Definition: config.hpp:223
bool luaW_toscalar(lua_State *L, int index, config::attribute_value &v)
Converts the value at the top of the stack to an attribute value.
Definition: lua_common.cpp:497
void luaW_filltable(lua_State *L, config const &cfg)
Converts a config object to a Lua table.
Definition: lua_common.cpp:570
bool luaW_totstring(lua_State *L, int index, t_string &str)
Converts a scalar to a translatable string.
Definition: lua_common.cpp:537
GLsizei const char ** path
Definition: glew.h:4654
std::string register_vconfig_metatable(lua_State *L)
Adds the vconfig metatable.
Definition: lua_common.cpp:419
bool luaW_pushvariable(lua_State *L, variable_access_const &v)
Definition: lua_common.cpp:816
const GLdouble * v
Definition: glew.h:1359
void luaW_pushscalar(lua_State *L, config::attribute_value const &v)
Converts an attribute value into a Lua object pushed at the top of the stack.
Definition: lua_common.cpp:492
Encapsulates the map of the game.
Definition: location.hpp:38
bool luaW_getglobal(lua_State *L, const std::vector< std::string > &path)
Pushes the value found by following the variadic names (char *), if the value is not nil...
Definition: lua_common.cpp:792
bool luaW_toboolean(lua_State *L, int n)
Definition: lua_common.cpp:811
std::string register_gettext_metatable(lua_State *L)
Adds the gettext metatable.
Definition: lua_common.cpp:376
bool luaW_checkvariable(lua_State *L, variable_access_create &v, int n)
Definition: lua_common.cpp:845
GLuint index
Definition: glew.h:1782
vconfig luaW_checkvconfig(lua_State *L, int index, bool allow_missing=false)
Gets an optional vconfig from either a table or a userdata.
Definition: lua_common.cpp:784
bool luaW_hasmetatable(lua_State *L, int index, luatypekey key)
Returns true if the metatable of the object is the one found in the registry.
Definition: lua_common.cpp:524
void luaW_pushconfig(lua_State *L, config const &cfg)
Converts a config object to a Lua table pushed at the top of the stack.
Definition: lua_common.cpp:663
bool luaW_tolocation(lua_State *L, int index, map_location &loc)
Converts an optional table or pair of integers to a map location object.
Definition: lua_common.cpp:606
void * luatypekey
Definition: lua_types.hpp:18
GLclampd n
Definition: glew.h:5903
void luaW_pushvconfig(lua_State *L, vconfig const &cfg)
Pushes a vconfig on the top of the stack.
Definition: lua_common.cpp:456
void luaW_pushtstring(lua_State *L, t_string const &v)
Pushes a t_string on the top of the stack.
Definition: lua_common.cpp:462
A variable-expanding proxy for the config class.
Definition: variable.hpp:36
void luaW_pushlocation(lua_State *L, map_location const &loc)
Converts a map location object to a Lua table pushed at the top of the stack.
Definition: lua_common.cpp:593
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
int intf_tovconfig(lua_State *L)
Creates a vconfig containing the WML table.
Definition: lua_common.cpp:366
GLsizei const GLcharARB ** string
Definition: glew.h:4503
t_string luaW_checktstring(lua_State *L, int index)
Converts a scalar to a translatable string.
Definition: lua_common.cpp:562
bool luaW_tovconfig(lua_State *L, int index, vconfig &vcfg)
Gets an optional vconfig from either a table or a userdata.
Definition: lua_common.cpp:757