Last active
April 5, 2021 19:30
-
-
Save Vbif/68c07bc02c70318f767b7320b37db056 to your computer and use it in GitHub Desktop.
Defold - auto register enums
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// test compile | |
typedef double lua_Number; | |
typedef struct {} lua_State; | |
void lua_pushnumber (lua_State *L, lua_Number n) {} | |
void lua_setfield (lua_State *L, int index, const char *k) {} | |
// test | |
#include <stdlib.h> | |
#include <string.h> | |
#define DEFOLD_ENUM(name, ...) \ | |
enum name { __VA_ARGS__ };\ | |
const char* DEFOLD_ENUM_##name##_items = #__VA_ARGS__; | |
#define DEFOLD_REGISTER_ENUM(lua, name) \ | |
register_enum(lua, DEFOLD_ENUM_##name##_items) | |
void register_enum(lua_State* L, const char* items) { | |
char *copy, *token, *temp; | |
int index = 0; | |
temp = copy = strdup(items); | |
while ((token = strsep(&temp, ", ")) != NULL) { | |
lua_pushnumber(L, (lua_Number) index); | |
lua_setfield(L, -2, token); | |
index++; | |
} | |
free(copy); | |
} | |
DEFOLD_ENUM(CONST, | |
FIRST, | |
SECOND, | |
THIRD | |
); | |
int main() | |
{ | |
lua_State* L; | |
DEFOLD_REGISTER_ENUM(L, CONST); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment