Freeciv-3.1
Loading...
Searching...
No Matches
Macros | Functions
api_game_specenum.c File Reference
#include <string.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include "events.h"
#include "support.h"
#include "api_game_specenum.h"

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)
 

Functions

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

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 35 of file api_game_specenum.c.

◆ 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)
static enum SPECENUM_NAME SPECENUM_FOO() _by_name(const char *name, int(*strcmp_func)(const char *, const char *))

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 44 of file api_game_specenum.c.

◆ API_SPECENUM_INDEX_NAME

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

Definition at line 34 of file api_game_specenum.c.

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 69 of file api_game_specenum.c.

◆ 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 98 of file api_game_specenum.c.

Referenced by script_client_init(), and script_server_init().