The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Namespaces | Macros | Functions | Variables
lua_common.hpp File Reference
#include "config.hpp"
#include "scripting/lua_types.hpp"
#include "variable_info.hpp"
#include "map/location.hpp"
#include <vector>
Include dependency graph for lua_common.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Namespaces

 lua_common
 

Macros

#define return_tstring_attrib(name, accessor)
 
#define return_cstring_attrib(name, accessor)
 
#define return_string_attrib(name, accessor)   return_cstring_attrib(name, (accessor).c_str())
 
#define return_int_attrib(name, accessor)
 
#define return_float_attrib(name, accessor)
 
#define return_bool_attrib(name, accessor)
 
#define return_cfg_attrib(name, accessor)
 
#define return_cfgref_attrib(name, accessor)
 
#define return_vector_string_attrib(name, accessor)
 
#define modify_tstring_attrib(name, accessor)
 
#define modify_string_attrib(name, accessor)
 
#define modify_int_attrib(name, accessor)
 
#define modify_int_attrib_check_range(name, accessor, allowed_min, allowed_max)
 
#define modify_bool_attrib(name, accessor)
 
#define modify_vector_string_attrib(name, accessor)
 

Functions

int lua_common::intf_textdomain (lua_State *L)
 Creates an interface for gettext. More...
 
int lua_common::intf_tovconfig (lua_State *L)
 Creates a vconfig containing the WML table. More...
 
std::string lua_common::register_gettext_metatable (lua_State *L)
 Adds the gettext metatable. More...
 
std::string lua_common::register_tstring_metatable (lua_State *L)
 Adds the tstring metatable. More...
 
std::string lua_common::register_vconfig_metatable (lua_State *L)
 Adds the vconfig metatable. More...
 
void luaW_pushvconfig (lua_State *L, vconfig const &cfg)
 Pushes a vconfig on the top of the stack. More...
 
void luaW_pushtstring (lua_State *L, t_string const &v)
 Pushes a t_string on the top of the stack. More...
 
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. More...
 
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. More...
 
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. More...
 
bool luaW_totstring (lua_State *L, int index, t_string &str)
 Converts a scalar to a translatable string. More...
 
t_string luaW_checktstring (lua_State *L, int index)
 Converts a scalar to a translatable string. More...
 
void luaW_filltable (lua_State *L, config const &cfg)
 Converts a config object to a Lua table. More...
 
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. More...
 
bool luaW_tolocation (lua_State *L, int index, map_location &loc)
 Converts an optional table or pair of integers to a map location object. More...
 
map_location luaW_checklocation (lua_State *L, int index)
 Converts an optional table or pair of integers to a map location object. More...
 
void luaW_pushconfig (lua_State *L, config const &cfg)
 Converts a config object to a Lua table pushed at the top of the stack. More...
 
bool luaW_toconfig (lua_State *L, int index, config &cfg)
 Converts an optional table or vconfig to a config object. More...
 
config luaW_checkconfig (lua_State *L, int index)
 Converts an optional table or vconfig to a config object. More...
 
bool luaW_tovconfig (lua_State *L, int index, vconfig &vcfg)
 Gets an optional vconfig from either a table or a userdata. More...
 
vconfig luaW_checkvconfig (lua_State *L, int index, bool allow_missing=false)
 Gets an optional vconfig from either a table or a userdata. More...
 
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. More...
 
template<typename... T>
bool luaW_getglobal (lua_State *L, T...path)
 Pushes the value found by following the variadic names (char *), if the value is not nil. More...
 
bool luaW_toboolean (lua_State *L, int n)
 
bool luaW_pushvariable (lua_State *L, variable_access_const &v)
 
bool luaW_checkvariable (lua_State *L, variable_access_create &v, int n)
 

Variables

const char * tstringKey
 

Macro Definition Documentation

#define modify_bool_attrib (   name,
  accessor 
)
Value:
if (strcmp(m, name) == 0) { \
bool value = luaW_toboolean(L, 3); \
accessor; \
return 0; \
}
GLsizei const GLfloat * value
Definition: glew.h:1817
bool luaW_toboolean(lua_State *L, int n)
Definition: lua_common.cpp:811
GLuint const GLchar * name
Definition: glew.h:1782
const GLdouble * m
Definition: glew.h:6968

Definition at line 252 of file lua_common.hpp.

Referenced by impl_side_set(), and impl_unit_set().

#define modify_int_attrib (   name,
  accessor 
)
Value:
if (strcmp(m, name) == 0) { \
accessor; \
return 0; \
}
LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int narg)
Definition: lauxlib.cpp:391
GLsizei const GLfloat * value
Definition: glew.h:1817
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
GLuint const GLchar * name
Definition: glew.h:1782
const GLdouble * m
Definition: glew.h:6968

Definition at line 237 of file lua_common.hpp.

Referenced by game_lua_kernel::impl_game_config_set(), impl_side_set(), impl_unit_attack_set(), and impl_unit_set().

#define modify_int_attrib_check_range (   name,
  accessor,
  allowed_min,
  allowed_max 
)
Value:
if (strcmp(m, name) == 0) { \
if (value < allowed_min || allowed_max < value) return luaL_argerror(L, 3, "out of bounds"); \
accessor; \
return 0; \
}
LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int narg)
Definition: lauxlib.cpp:391
GLsizei const GLfloat * value
Definition: glew.h:1817
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
LUALIB_API int luaL_argerror(lua_State *L, int narg, const char *extramsg)
Definition: lauxlib.cpp:152
GLuint const GLchar * name
Definition: glew.h:1782
const GLdouble * m
Definition: glew.h:6968
CALLABLE_WRAPPER_INPUT_END if(key=="terrain")

Definition at line 244 of file lua_common.hpp.

#define modify_string_attrib (   name,
  accessor 
)
Value:
if (strcmp(m, name) == 0) { \
const char *value = luaL_checkstring(L, 3); \
accessor; \
return 0; \
}
GLsizei const GLfloat * value
Definition: glew.h:1817
GLuint const GLchar * name
Definition: glew.h:1782
const GLdouble * m
Definition: glew.h:6968
#define luaL_checkstring(L, n)
Definition: lauxlib.h:115

Definition at line 230 of file lua_common.hpp.

Referenced by impl_side_set(), impl_unit_attack_set(), and impl_unit_set().

#define modify_tstring_attrib (   name,
  accessor 
)
Value:
if (strcmp(m, name) == 0) { \
t_string value = luaW_checktstring(L, 3); \
accessor; \
return 0; \
}
GLsizei const GLfloat * value
Definition: glew.h:1817
GLuint const GLchar * name
Definition: glew.h:1782
const GLdouble * m
Definition: glew.h:6968
t_string luaW_checktstring(lua_State *L, int index)
Converts a scalar to a translatable string.
Definition: lua_common.cpp:562

Definition at line 223 of file lua_common.hpp.

Referenced by impl_side_set(), impl_unit_attack_set(), and impl_unit_set().

#define modify_vector_string_attrib (   name,
  accessor 
)
Value:
if (strcmp(m, name) == 0) { \
std::vector<std::string> vector; \
char const* message = "table with unnamed indices holding strings expected"; \
if (!lua_istable(L, 3)) return luaL_argerror(L, 3, message); \
unsigned length = lua_rawlen(L, 3); \
for (unsigned i = 1; i <= length; ++i) { \
lua_rawgeti(L, 3, i); \
char const* string = lua_tostring(L, 4); \
if(!string) return luaL_argerror(L, 2 + i, message); \
vector.push_back(string); \
lua_pop(L, 1); \
} \
accessor; \
return 0; \
}
LUA_API void lua_rawgeti(lua_State *L, int idx, int n)
Definition: lapi.cpp:643
#define lua_pop(L, n)
Definition: lua.h:322
GLuint GLsizei GLsizei * length
Definition: glew.h:1793
LUALIB_API int luaL_argerror(lua_State *L, int narg, const char *extramsg)
Definition: lauxlib.cpp:152
size_t i
Definition: function.cpp:1057
#define lua_tostring(L, i)
Definition: lua.h:345
GLuint const GLchar * name
Definition: glew.h:1782
LUA_API size_t lua_rawlen(lua_State *L, int idx)
Definition: lapi.cpp:401
const GLdouble * m
Definition: glew.h:6968
#define lua_istable(L, n)
Definition: lua.h:331
CALLABLE_WRAPPER_INPUT_END if(key=="terrain")
GLsizei GLenum GLuint GLuint GLsizei char * message
Definition: glew.h:2499

Definition at line 259 of file lua_common.hpp.

Referenced by impl_unit_set().

#define return_bool_attrib (   name,
  accessor 
)
Value:
if (strcmp(m, name) == 0) { \
lua_pushboolean(L, accessor); \
return 1; \
}
LUA_API void lua_pushboolean(lua_State *L, int b)
Definition: lapi.cpp:571
GLuint const GLchar * name
Definition: glew.h:1782
const GLdouble * m
Definition: glew.h:6968

Definition at line 190 of file lua_common.hpp.

Referenced by game_lua_kernel::impl_game_config_get(), impl_race_get(), impl_side_get(), and impl_unit_get().

#define return_cfg_attrib (   name,
  accessor 
)
Value:
if (strcmp(m, name) == 0) { \
config cfg; \
accessor; \
luaW_pushconfig(L, cfg); \
return 1; \
}
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
GLuint const GLchar * name
Definition: glew.h:1782
const GLdouble * m
Definition: glew.h:6968

Definition at line 196 of file lua_common.hpp.

Referenced by impl_side_get(), and impl_unit_get().

#define return_cfgref_attrib (   name,
  accessor 
)
Value:
if (strcmp(m, name) == 0) { \
luaW_pushconfig(L, accessor); \
return 1; \
}
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
GLuint const GLchar * name
Definition: glew.h:1782
const GLdouble * m
Definition: glew.h:6968

Definition at line 204 of file lua_common.hpp.

Referenced by game_lua_kernel::impl_game_config_get(), impl_race_get(), impl_unit_attack_get(), impl_unit_attack_set(), impl_unit_type_get(), and impl_unit_variables_get().

#define return_cstring_attrib (   name,
  accessor 
)
Value:
if (strcmp(m, name) == 0) { \
lua_pushstring(L, accessor); \
return 1; \
}
GLuint const GLchar * name
Definition: glew.h:1782
const GLdouble * m
Definition: glew.h:6968
LUA_API const char * lua_pushstring(lua_State *L, const char *s)
Definition: lapi.cpp:507

Definition at line 169 of file lua_common.hpp.

Referenced by impl_side_get().

#define return_float_attrib (   name,
  accessor 
)
Value:
if (strcmp(m, name) == 0) { \
lua_pushnumber(L, accessor); \
return 1; \
}
LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
Definition: lapi.cpp:467
GLuint const GLchar * name
Definition: glew.h:1782
const GLdouble * m
Definition: glew.h:6968

Definition at line 184 of file lua_common.hpp.

Referenced by impl_side_get().

#define return_int_attrib (   name,
  accessor 
)
Value:
if (strcmp(m, name) == 0) { \
lua_pushinteger(L, accessor); \
return 1; \
}
GLuint const GLchar * name
Definition: glew.h:1782
const GLdouble * m
Definition: glew.h:6968
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
Definition: lapi.cpp:477

Definition at line 178 of file lua_common.hpp.

Referenced by game_lua_kernel::impl_current_get(), game_lua_kernel::impl_game_config_get(), impl_race_get(), impl_side_get(), impl_unit_attack_get(), impl_unit_get(), and impl_unit_type_get().

#define return_string_attrib (   name,
  accessor 
)    return_cstring_attrib(name, (accessor).c_str())
#define return_tstring_attrib (   name,
  accessor 
)
Value:
if (strcmp(m, name) == 0) { \
luaW_pushtstring(L, accessor); \
return 1; \
}
GLuint const GLchar * name
Definition: glew.h:1782
const GLdouble * m
Definition: glew.h:6968
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

Definition at line 163 of file lua_common.hpp.

Referenced by impl_race_get(), impl_side_get(), impl_unit_get(), and impl_unit_type_get().

#define return_vector_string_attrib (   name,
  accessor 
)
Value:
if (strcmp(m, name) == 0) { \
const std::vector<std::string>& vector = accessor; \
lua_createtable(L, vector.size(), 0); \
int i = 1; \
for (const std::string& s : vector) { \
lua_pushstring(L, s.c_str()); \
lua_rawseti(L, -2, i); \
++i; \
} \
return 1; \
}
LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
Definition: lapi.cpp:667
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
size_t i
Definition: function.cpp:1057
LUA_API void lua_rawseti(lua_State *L, int idx, int n)
Definition: lapi.cpp:778
GLuint const GLchar * name
Definition: glew.h:1782
const GLdouble * m
Definition: glew.h:6968
GLdouble s
Definition: glew.h:1358
GLsizei const GLcharARB ** string
Definition: glew.h:4503
LUA_API const char * lua_pushstring(lua_State *L, const char *s)
Definition: lapi.cpp:507

Definition at line 210 of file lua_common.hpp.

Referenced by impl_unit_get().

Function Documentation

config luaW_checkconfig ( lua_State L,
int  index 
)
map_location luaW_checklocation ( lua_State L,
int  index 
)
t_string luaW_checktstring ( lua_State L,
int  index 
)
bool luaW_checkvariable ( lua_State L,
variable_access_create v,
int  n 
)
vconfig luaW_checkvconfig ( lua_State L,
int  index,
bool  allow_missing = false 
)
void luaW_filltable ( lua_State L,
config const &  cfg 
)

Converts a config object to a Lua table.

The destination table should be at the top of the stack on entry. It is still at the top on exit.

Definition at line 570 of file lua_common.cpp.

References config::all_children_range(), config::attribute_range(), lua_checkstack(), lua_createtable(), LUA_MINSTACK, lua_newtable, lua_pushstring(), lua_rawseti(), lua_setfield(), luaW_filltable(), and luaW_pushscalar().

Referenced by luaW_filltable(), luaW_pushconfig(), and luaW_pushvariable().

bool luaW_getglobal ( lua_State L,
const std::vector< std::string > &  path 
)
template<typename... T>
bool luaW_getglobal ( lua_State L,
T...  path 
)

Pushes the value found by following the variadic names (char *), if the value is not nil.

Returns
true if an element was pushed.

Definition at line 151 of file lua_common.hpp.

References luaW_getglobal().

bool luaW_hasmetatable ( lua_State L,
int  index,
luatypekey  key 
)
void luaW_pushconfig ( lua_State L,
config const &  cfg 
)
void luaW_pushlocation ( lua_State L,
map_location const &  loc 
)
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 at line 492 of file lua_common.cpp.

References config::attribute_value::apply_visitor().

Referenced by lua_common::impl_vconfig_get(), lua_common::impl_vconfig_pairs_iter(), luaW_filltable(), and luaW_pushvariable().

void luaW_pushtstring ( lua_State L,
t_string const &  v 
)
bool luaW_pushvariable ( lua_State L,
variable_access_const v 
)
void luaW_pushvconfig ( lua_State L,
vconfig const &  cfg 
)
bool luaW_toboolean ( lua_State L,
int  n 
)
bool luaW_toconfig ( lua_State L,
int  index,
config cfg 
)
bool luaW_tolocation ( lua_State L,
int  index,
map_location loc 
)
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 at line 497 of file lua_common.cpp.

References LUA_TBOOLEAN, LUA_TNUMBER, lua_tonumber, lua_tostring, LUA_TSTRING, LUA_TUSERDATA, lua_type(), luaL_testudata(), luaW_toboolean(), and tstringKey.

Referenced by luaW_toconfig().

bool luaW_totstring ( lua_State L,
int  index,
t_string str 
)
bool luaW_tovconfig ( lua_State L,
int  index,
vconfig vcfg 
)

Gets an optional vconfig from either a table or a userdata.

Returns
false in case of failure.

Definition at line 757 of file lua_common.cpp.

References LUA_TNIL, LUA_TNONE, LUA_TTABLE, LUA_TUSERDATA, lua_type(), luaL_testudata(), luaW_toconfig(), and vconfigKey.

Referenced by game_lua_kernel::intf_find_cost_map(), luaW_checkvconfig(), luaW_tofaivariant(), luaW_tolocation(), and ai::lua_object< T >::to_type().

Variable Documentation

const char* tstringKey