Freeciv-3.2
Loading...
Searching...
No Matches
api_specenum.h
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#ifndef FC__API_SPECENUM_H
14#define FC__API_SPECENUM_H
15
16#ifdef __cplusplus
17extern "C" {
18#endif /* __cplusplus */
19
20#define API_SPECENUM_INDEX_NAME(type) api_specenum_##type##_index
21#define API_SPECENUM_NAME_NAME(type) api_specenum_##type##_name
22
23/**********************************************************************/
29#define API_SPECENUM_DEFINE_INDEX(type_name, prefix) \
30 static int (API_SPECENUM_INDEX_NAME(type_name))(lua_State *L) \
31 { \
32 static char _buf[128]; \
33 const char *_key; \
34 enum type_name _value; \
35 luaL_checktype(L, 1, LUA_TTABLE); \
36 _key = luaL_checkstring(L, 2); \
37 fc_snprintf(_buf, sizeof(_buf), prefix "%s", _key); \
38 _value = type_name##_by_name(_buf, strcmp); \
39 if (_value != type_name##_invalid()) { \
40 /* T[_key] = _value */ \
41 lua_pushstring(L, _key); \
42 lua_pushinteger(L, _value); \
43 lua_rawset(L, 1); \
44 lua_pushinteger(L, _value); \
45 } else { \
46 lua_pushnil(L); \
47 } \
48 return 1; \
49 }
50
51/**********************************************************************/
58#define API_SPECENUM_DEFINE_INDEX_REV(type_name) \
59 static int (API_SPECENUM_NAME_NAME(type_name))(lua_State *L) \
60 { \
61 enum type_name _key; \
62 const char *_value; \
63 luaL_checktype(L, 1, LUA_TTABLE); \
64 _key = luaL_checkinteger(L, 2); \
65 if (type_name##_is_valid(_key)) { \
66 _value = type_name##_name(_key); \
67 /* T[_key] = _value */ \
68 lua_pushinteger(L, _key); \
69 lua_pushstring(L, _value); \
70 lua_rawset(L, 1); \
71 lua_pushstring(L, _value); \
72 } else { \
73 lua_pushnil(L); \
74 } \
75 return 1; \
76 }
77
78void api_specenum_create_table(lua_State *L, const char *name,
80
81#define API_SPECENUM_CREATE_TABLE(L, type, name) \
82 api_specenum_create_table((L), (name), API_SPECENUM_INDEX_NAME(type))
83#define API_SPECENUM_CREATE_TABLE_REV(L, type, name) \
84 api_specenum_create_table((L), (name), API_SPECENUM_NAME_NAME(type))
85
86
87
88#ifdef __cplusplus
89}
90#endif /* __cplusplus */
91
92#endif /* FC__API_SPECENUM_H */
void api_specenum_create_table(lua_State *L, const char *name, lua_CFunction findex)
char * incite_cost
Definition comments.c:75
const char * name
Definition inputfile.c:127