static E from_string( const std::string &value ) { const auto iter = BINDINGS.find( value ); if( iter == BINDINGS.end() ) { // This point shall not be reached. Always call this with valid input. return BINDINGS.begin()->second; } return iter->second; }
static int index( lua_State * const L ) { // -1 is the key (funtion to call) const char * const key = lua_tostring( L, -1 ); if( key == nullptr ) { luaL_error( L, "Invalid input to __index: key is not a string." ); } const auto iter = BINDINGS.find( key ); if( iter == BINDINGS.end() ) { return luaL_error( L, "Invalid enum value." ); } lua_remove( L, -1 ); // remove key // Push the enum as string, it will be converted back to the enum later. This way, it can // be specified both ways in Lua code: either as string or via an entry here. lua_pushlstring( L, iter->first.c_str(), iter->first.length() ); return 1; }