Freeciv21
Develop your civilization from humble roots to a global empire
api_game_specenum.cpp File Reference
#include <cstring>
#include "lauxlib.h"
#include "lua.h"
#include "lualib.h"
#include "log.h"
#include "support.h"
#include "events.h"
#include "api_game_specenum.h"
+ Include dependency graph for api_game_specenum.cpp:

Go to the source code of this file.

Macros

#define API_SPECENUM_INDEX_NAME(type)   api_specenum_##type##_index
 
#define API_SPECENUM_CREATE_TABLE(L, type, name)    api_specenum_create_table((L), (name), API_SPECENUM_INDEX_NAME(type))
 
#define API_SPECENUM_DEFINE_INDEX(type_name, prefix)
 Define a the __index (table, key) -> value metamethod Return the enum value whose name is the concatenation of prefix and key. More...
 

Functions

static void api_specenum_create_table (lua_State *L, const char *name, lua_CFunction findex)
 Create a module table and set the member lookup function. More...
 
int api_specenum_open (lua_State *L)
 Define the __index function for each exported specenum type. More...
 

Macro Definition Documentation

◆ API_SPECENUM_CREATE_TABLE

#define API_SPECENUM_CREATE_TABLE (   L,
  type,
  name 
)     api_specenum_create_table((L), (name), API_SPECENUM_INDEX_NAME(type))

Definition at line 31 of file api_game_specenum.cpp.

◆ API_SPECENUM_DEFINE_INDEX

#define API_SPECENUM_DEFINE_INDEX (   type_name,
  prefix 
)
Value:
static int(API_SPECENUM_INDEX_NAME(type_name))(lua_State * L) \
{ \
static char _buf[128]; \
const char *_key; \
enum type_name _value; \
luaL_checktype(L, 1, LUA_TTABLE); \
_key = luaL_checkstring(L, 2); \
fc_snprintf(_buf, sizeof(_buf), prefix "%s", _key); \
_value = type_name##_by_name(_buf, strcmp); \
if (_value != type_name##_invalid()) { \
/* T[_key] = _value */ \
lua_pushstring(L, _key); \
lua_pushinteger(L, _value); \
lua_rawset(L, 1); \
lua_pushinteger(L, _value); \
} else { \
lua_pushnil(L); \
} \
return 1; \
}
#define API_SPECENUM_INDEX_NAME(type)

Define a the __index (table, key) -> value metamethod Return the enum value whose name is the concatenation of prefix and key.

The fetched value is written back to the lua table, and further accesses will resolve there instead of this function.

Definition at line 40 of file api_game_specenum.cpp.

◆ API_SPECENUM_INDEX_NAME

#define API_SPECENUM_INDEX_NAME (   type)    api_specenum_##type##_index

Definition at line 30 of file api_game_specenum.cpp.

Function Documentation

◆ api_specenum_create_table()

static void api_specenum_create_table ( lua_State *  L,
const char *  name,
lua_CFunction  findex 
)
static

Create a module table and set the member lookup function.

Definition at line 65 of file api_game_specenum.cpp.

◆ api_specenum_open()

int api_specenum_open ( lua_State *  L)

Define the __index function for each exported specenum type.

Load the specenum modules into Lua state L.

Definition at line 94 of file api_game_specenum.cpp.

Referenced by script_client_init(), and script_server_init().