Freeciv-3.1
Loading...
Searching...
No Matches
api_game_specenum.c
Go to the documentation of this file.
1/*****************************************************************************
2 Freeciv - Copyright (C) 2010 - The Freeciv Project
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12*****************************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18#include <string.h>
19
20/* dependencies/lua */
21#include "lua.h"
22#include "lualib.h"
23#include "lauxlib.h"
24
25/* common */
26#include "events.h"
27
28/* utility */
29#include "support.h"
30
31#include "api_game_specenum.h"
32
33
34#define API_SPECENUM_INDEX_NAME(type) api_specenum_##type##_index
35#define API_SPECENUM_CREATE_TABLE(L, type, name) \
36 api_specenum_create_table((L), (name), API_SPECENUM_INDEX_NAME(type))
37
38/**********************************************************************/
44#define API_SPECENUM_DEFINE_INDEX(type_name, prefix) \
45 static int (API_SPECENUM_INDEX_NAME(type_name))(lua_State *L) \
46 { \
47 static char _buf[128]; \
48 const char *_key; \
49 enum type_name _value; \
50 luaL_checktype(L, 1, LUA_TTABLE); \
51 _key = luaL_checkstring(L, 2); \
52 fc_snprintf(_buf, sizeof(_buf), prefix "%s", _key); \
53 _value = type_name##_by_name(_buf, strcmp); \
54 if (_value != type_name##_invalid()) { \
55 /* T[_key] = _value */ \
56 lua_pushstring(L, _key); \
57 lua_pushinteger(L, _value); \
58 lua_rawset(L, 1); \
59 lua_pushinteger(L, _value); \
60 } else { \
61 lua_pushnil(L); \
62 } \
63 return 1; \
64 }
65
66/**********************************************************************/
69static void api_specenum_create_table(lua_State *L, const char *name,
70 lua_CFunction findex)
71{
72 /* Insert a module table in the global environment,
73 * or reuse any preexisting table */
74 lua_getglobal(L, name);
75 if (lua_isnil(L, -1)) {
76 lua_newtable(L);
77 lua_pushvalue(L, -1);
78 lua_setglobal(L, name);
79 }
80 fc_assert_ret(lua_istable(L, -1));
81 /* Create a metatable */
82 lua_newtable(L); /* stack: module mt */
83 lua_pushliteral(L, "__index");
84 lua_pushcfunction(L, findex); /* stack: module mt '__index' index */
85 lua_rawset(L, -3); /* stack: module mt */
86 lua_setmetatable(L, -2); /* stack: module */
87 lua_pop(L, 1);
88}
89
90/**********************************************************************/
93API_SPECENUM_DEFINE_INDEX(event_type, "E_")
94
95/**********************************************************************/
98int api_specenum_open(lua_State *L)
99{
100 API_SPECENUM_CREATE_TABLE(L, event_type, "E");
101 return 0;
102}
#define API_SPECENUM_CREATE_TABLE(L, type, name)
static void api_specenum_create_table(lua_State *L, const char *name, lua_CFunction findex)
#define API_SPECENUM_DEFINE_INDEX(type_name, prefix)
int api_specenum_open(lua_State *L)
const char * name
Definition inputfile.c:127
#define fc_assert_ret(condition)
Definition log.h:191