BARE2D
LuaScriptEngine_impl.tcc
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 
5 #include "BAREErrors.hpp"
6 
7 namespace BARE2D {
8 
9  template<class T>
10  void LuaScriptEngine::addValueToRegistry(lua_State* L, T* value, std::string key) {
11  // Push the key
12  lua_pushstring(L, key.c_str());
13  // Push the data (as a void*)
14  lua_pushlightuserdata(L, static_cast<void*>(value));
15  // Combine name and key in the registry.
16  lua_settable(L, LUA_REGISTRYINDEX);
17  }
18 
19  template<class T>
20  T* LuaScriptEngine::getValueFromRegistry(lua_State* L, std::string key) {
21  // Put the key on the stack
22  lua_pushstring(L, key.c_str());
23  // Get the element at key in tge registry table
24  lua_gettable(L, LUA_REGISTRYINDEX);
25  // Turn the element into a void*
26  void* ret = lua_touserdata(L, -1);
27  lua_pop(L, 1);
28  // convert to whatever type
29  return static_cast<T*>(ret);
30  }
31 
32 }