1 #ifndef SAKI_APP_LUA_MANAGED_REF_H 2 #define SAKI_APP_LUA_MANAGED_REF_H 4 #include "lua_user_error_handler.h" 5 #include "../3rd/sol.hpp" 6 #include "../util/misc.h" 43 , mIsOwning(that.mIsOwning)
44 , mIsConst(that.mIsConst)
54 auto data()
const ->
const T *
64 template<
typename Ret,
typename ... Args>
65 static auto makeConstMethod(Ret (T::*method)(Args...)
const) -> std::function<Ret(
LuaManagedRef *, Args...)>
68 if (thiz->isBadReference(
false))
71 return (thiz->mData->*method)(args...);
75 template<
typename Ret,
typename ... Args>
76 static auto makeMutableMethod(Ret (T::*method)(Args...)) -> std::function<Ret(
LuaManagedRef *, Args...)>
79 if (thiz->isBadReference(
true))
82 return (thiz->mData->*method)(args...);
86 template<
typename Ret,
typename ... Args>
87 static auto makeConstMethodAsTable(Ret (T::*method)(Args...)
const)
88 -> std::function<sol::as_table_t<sol::meta::unqualified_t<Ret>>(
LuaManagedRef *, Args...)>
90 return [method](LuaManagedRef *thiz, Args ... args) {
91 if (thiz->isBadReference(
false))
94 return sol::as_table((thiz->mData->*method)(args...));
98 template<
typename Ret,
typename ... Args>
99 static auto makeMutableMethodAsTable(Ret (T::*method)(Args...))
100 -> std::function<sol::as_table_t<sol::meta::unqualified_t<Ret>>(LuaManagedRef *, Args...)>
102 return [method](LuaManagedRef *thiz, Args ... args) {
103 if (thiz->isBadReference(
true))
106 return sol::as_table((thiz->mData->*method)(args...));
110 template<
typename Ret,
typename ... Args>
111 static auto makeConstMethodAsConstRef(
const Ret &(T::*method)(Args...)
const)
112 -> std::function<std::unique_ptr<LuaManagedRef<Ret>>(LuaManagedRef *, Args...)>
114 return [method](LuaManagedRef *thiz, Args ... args) -> std::unique_ptr<
LuaManagedRef<Ret>> {
115 if (thiz->isBadReference(
false))
118 const Ret &r = (thiz->mData->*method)(args...);
119 return std::make_unique<LuaManagedRef<Ret>>(&r, thiz->mError,
false);
123 template<
typename Ret,
typename ... Args>
124 static auto makeConstFunction(Ret (*method)(
const T &, Args...))
125 -> std::function<Ret(LuaManagedRef *, Args...)>
127 return [method](LuaManagedRef *thiz, Args ... args) {
128 if (thiz->isBadReference(
false))
131 return method(*thiz->mData, args...);
135 template<
typename Ret,
typename ... Args>
136 static auto makeMutableFunction(Ret (*method)(T &, Args...))
137 -> std::function<Ret(LuaManagedRef *, Args...)>
139 return [method](LuaManagedRef *thiz, Args ... args) {
140 if (thiz->isBadReference(
true))
143 return method(*thiz->mData, args...);
147 template<
typename Ret,
typename ... Args>
148 static auto makeConstFunctionError(Ret (*method)(
LuaUserErrorHandler &,
const T &, Args...))
149 -> std::function<Ret(LuaManagedRef *, Args...)>
151 return [method](LuaManagedRef *thiz, Args ... args) {
152 if (thiz->isBadReference(
false))
155 return method(thiz->mError, *thiz->mData, args...);
159 template<
typename Ret,
typename ... Args>
161 -> std::function<Ret(LuaManagedRef *, Args...)>
163 return [method](LuaManagedRef *thiz, Args ... args) {
164 if (thiz->isBadReference(
true))
167 return method(thiz->mError, *thiz->mData, args...);
172 auto isBadReference(
bool modifying) ->
bool 174 if (mData ==
nullptr) {
175 mError.handleUserError(
"ERefNil");
179 if (modifying && mIsConst) {
180 mError.handleUserError(
"ERefCon");
190 bool mIsOwning =
false;
191 bool mIsConst =
false;
194 template<
typename ...Ts>
200 mPointers = std::make_tuple(pointers.get() ...);
205 std::apply([](
auto ...p) { (p->dispose(), ...); }, mPointers);
209 std::tuple<LuaManagedRef<Ts> * ...> mPointers;
218 #endif // SAKI_APP_LUA_MANAGED_REF_H Definition: lua_managed_ref.h:195
Definition: lua_user_error_handler.h:11
Definition: lua_managed_ref.h:18