Ai
2 Star 4 Fork 3

Jally/fusion

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
LuaRef.h 3.67 KB
一键复制 编辑 原始数据 按行查看 历史
Jally 提交于 2018-02-28 22:37 +08:00 . 'merge'
#pragma once
#include "noncopyable.h"
#include "LuaBus.h"
class LuaRef : public noncopyable
{
public:
LuaRef(lua_State *L = nullptr, bool doref = false) : L(L) {
ref_ = doref ? luaL_ref(L, LUA_REGISTRYINDEX) : LUA_NOREF;
}
LuaRef(lua_State *L, int index) : L(L) {
lua_pushvalue(L, index);
ref_ = luaL_ref(L, LUA_REGISTRYINDEX);
}
LuaRef(lua_State *L, const char *name) : L(L) {
lua_getglobal(L, name);
ref_ = luaL_ref(L, LUA_REGISTRYINDEX);
}
~LuaRef() {
if (isref()) {
luaL_unref(L, LUA_REGISTRYINDEX, ref_);
}
}
LuaRef(LuaRef &&other) {
L = other.L, ref_ = other.ref_;
other.ref_ = LUA_NOREF;
}
LuaRef &operator=(LuaRef &&other) {
this->~LuaRef(), new(this) LuaRef(std::move(other));
return *this;
}
lua_State *getL() const { return L; }
int index() const { return ref_; }
bool isref() const {
return ref_ != LUA_NOREF && ref_ != LUA_REFNIL;
}
void getref() const {
lua_rawgeti(L, LUA_REGISTRYINDEX, ref_);
}
void unref() {
*this = LuaRef();
}
LuaRef Clone() const {
getref();
return LuaRef(L, true);
}
template<typename T>
void Set(T object) {
lua::push<T>::invoke(L, object);
*this = LuaRef(L, true);
}
template<typename T>
T Get() const {
getref();
return lua::pop<T>::invoke(L);
}
private:
lua_State *L;
int ref_;
};
namespace lua {
template<> struct pop<LuaRef> {
static LuaRef invoke(lua_State *L) {
return LuaRef(L, true);
}
};
template<> struct pop<const LuaRef> {
static LuaRef invoke(lua_State *L) {
return LuaRef(L, true);
}
};
template<> struct pop<const LuaRef &> {
static LuaRef invoke(lua_State *L) {
return LuaRef(L, true);
}
};
template<> struct pop<LuaRef &&> {
static LuaRef invoke(lua_State *L) {
return LuaRef(L, true);
}
};
template<> struct pop<const LuaRef &&> {
static LuaRef invoke(lua_State *L) {
return LuaRef(L, true);
}
};
template<> struct read<LuaRef> {
static LuaRef invoke(lua_State *L, int index) {
return LuaRef(L, index);
}
};
template<> struct read<const LuaRef> {
static LuaRef invoke(lua_State *L, int index) {
return LuaRef(L, index);
}
};
template<> struct read<const LuaRef &> {
static LuaRef invoke(lua_State *L, int index) {
return LuaRef(L, index);
}
};
template<> struct read<LuaRef &&> {
static LuaRef invoke(lua_State *L, int index) {
return LuaRef(L, index);
}
};
template<> struct read<const LuaRef &&> {
static LuaRef invoke(lua_State *L, int index) {
return LuaRef(L, index);
}
};
template<> struct push<LuaRef> {
static void invoke(lua_State *L, const LuaRef &val) {
val.isref() ? val.getref() : lua_pushnil(L);
}
};
template<> struct push<const LuaRef> {
static void invoke(lua_State *L, const LuaRef &val) {
val.isref() ? val.getref() : lua_pushnil(L);
}
};
template<> struct push<LuaRef &> {
static void invoke(lua_State *L, const LuaRef &val) {
val.isref() ? val.getref() : lua_pushnil(L);
}
};
template<> struct push<const LuaRef &> {
static void invoke(lua_State *L, const LuaRef &val) {
val.isref() ? val.getref() : lua_pushnil(L);
}
};
template<> struct push<LuaRef &&> {
static void invoke(lua_State *L, const LuaRef &val) {
val.isref() ? val.getref() : lua_pushnil(L);
}
};
template<> struct push<const LuaRef &&> {
static void invoke(lua_State *L, const LuaRef &val) {
val.isref() ? val.getref() : lua_pushnil(L);
}
};
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/jallyx/fusion.git
git@gitee.com:jallyx/fusion.git
jallyx
fusion
fusion
master

搜索帮助