+class LuaRefWithDeleter {
+public:
+ LuaRefWithDeleter(mutex *m, lua_State *L, int ref) : m(m), L(L), ref(ref) {}
+ ~LuaRefWithDeleter() {
+ unique_lock<mutex> lock(*m);
+ luaL_unref(L, LUA_REGISTRYINDEX, ref);
+ }
+ int get() const { return ref; }
+
+private:
+ LuaRefWithDeleter(const LuaRefWithDeleter &) = delete;
+
+ mutex *m;
+ lua_State *L;
+ int ref;
+};
+