Skip to content

Instantly share code, notes, and snippets.

@Vbif
Last active April 5, 2021 19:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vbif/68c07bc02c70318f767b7320b37db056 to your computer and use it in GitHub Desktop.
Save Vbif/68c07bc02c70318f767b7320b37db056 to your computer and use it in GitHub Desktop.
Defold - auto register enums
// 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