Lua/LuaInstance: Add [Check|Set][Field|Global] helpers
Former-commit-id: 4c6cb6097a28daa9099b3f00c201e49ec850c320
This commit is contained in:
parent
ee2626b928
commit
52599132a7
|
|
@ -47,8 +47,16 @@ namespace Nz
|
||||||
void CheckAny(int index) const;
|
void CheckAny(int index) const;
|
||||||
bool CheckBoolean(int index) const;
|
bool CheckBoolean(int index) const;
|
||||||
bool CheckBoolean(int index, bool defValue) const;
|
bool CheckBoolean(int index, bool defValue) const;
|
||||||
|
template<typename T> T CheckField(const char* fieldName, int tableIndex = -1);
|
||||||
|
template<typename T> T CheckField(const String& fieldName, int tableIndex = -1);
|
||||||
|
template<typename T> T CheckField(const char* fieldName, T defValue, int tableIndex = -1);
|
||||||
|
template<typename T> T CheckField(const String& fieldName, T defValue, int tableIndex = -1);
|
||||||
long long CheckInteger(int index) const;
|
long long CheckInteger(int index) const;
|
||||||
long long CheckInteger(int index, long long defValue) const;
|
long long CheckInteger(int index, long long defValue) const;
|
||||||
|
template<typename T> T CheckGlobal(const char* fieldName) const;
|
||||||
|
template<typename T> T CheckGlobal(const String& fieldName) const;
|
||||||
|
template<typename T> T CheckGlobal(const char* fieldName, T defValue) const;
|
||||||
|
template<typename T> T CheckGlobal(const String& fieldName, T defValue) const;
|
||||||
double CheckNumber(int index) const;
|
double CheckNumber(int index) const;
|
||||||
double CheckNumber(int index, double defValue) const;
|
double CheckNumber(int index, double defValue) const;
|
||||||
void CheckStack(int space, const char* error = nullptr) const;
|
void CheckStack(int space, const char* error = nullptr) const;
|
||||||
|
|
@ -78,8 +86,8 @@ namespace Nz
|
||||||
bool ExecuteFromStream(Stream& stream);
|
bool ExecuteFromStream(Stream& stream);
|
||||||
|
|
||||||
int GetAbsIndex(int index) const;
|
int GetAbsIndex(int index) const;
|
||||||
LuaType GetField(const char* fieldName, int index = -1) const;
|
LuaType GetField(const char* fieldName, int tableIndex = -1) const;
|
||||||
LuaType GetField(const String& fieldName, int index = -1) const;
|
LuaType GetField(const String& fieldName, int tableIndex = -1) const;
|
||||||
LuaType GetGlobal(const char* name) const;
|
LuaType GetGlobal(const char* name) const;
|
||||||
LuaType GetGlobal(const String& name) const;
|
LuaType GetGlobal(const String& name) const;
|
||||||
lua_State* GetInternalState() const;
|
lua_State* GetInternalState() const;
|
||||||
|
|
@ -136,8 +144,12 @@ namespace Nz
|
||||||
void Remove(int index);
|
void Remove(int index);
|
||||||
void Replace(int index);
|
void Replace(int index);
|
||||||
|
|
||||||
|
template<typename T> void SetField(const char* name, T&& arg, int tableIndex = -2);
|
||||||
|
template<typename T> void SetField(const String& name, T&& arg, int tableIndex = -2);
|
||||||
void SetField(const char* name, int tableIndex = -2);
|
void SetField(const char* name, int tableIndex = -2);
|
||||||
void SetField(const String& name, int tableIndex = -2);
|
void SetField(const String& name, int tableIndex = -2);
|
||||||
|
template<typename T> void SetGlobal(const char* name, T&& arg);
|
||||||
|
template<typename T> void SetGlobal(const String& name, T&& arg);
|
||||||
void SetGlobal(const char* name);
|
void SetGlobal(const char* name);
|
||||||
void SetGlobal(const String& name);
|
void SetGlobal(const String& name);
|
||||||
void SetMetatable(const char* tname);
|
void SetMetatable(const char* tname);
|
||||||
|
|
|
||||||
|
|
@ -78,7 +78,7 @@ namespace Nz
|
||||||
std::enable_if_t<std::is_unsigned<T>::value, unsigned int> LuaImplQueryArg(LuaInstance& instance, int index, T* arg, T defValue, TypeTag<T>)
|
std::enable_if_t<std::is_unsigned<T>::value, unsigned int> LuaImplQueryArg(LuaInstance& instance, int index, T* arg, T defValue, TypeTag<T>)
|
||||||
{
|
{
|
||||||
using SignedT = std::make_signed_t<T>;
|
using SignedT = std::make_signed_t<T>;
|
||||||
|
|
||||||
return LuaImplQueryArg(instance, index, reinterpret_cast<SignedT*>(arg), static_cast<SignedT>(defValue), TypeTag<SignedT>());
|
return LuaImplQueryArg(instance, index, reinterpret_cast<SignedT*>(arg), static_cast<SignedT>(defValue), TypeTag<SignedT>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -242,7 +242,7 @@ namespace Nz
|
||||||
ProcessArgs<0, Args...>(instance);
|
ProcessArgs<0, Args...>(instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
int Invoke(LuaInstance& instance, void (*func)(Args...)) const
|
int Invoke(LuaInstance& instance, void(*func)(Args...)) const
|
||||||
{
|
{
|
||||||
NazaraUnused(instance);
|
NazaraUnused(instance);
|
||||||
|
|
||||||
|
|
@ -251,7 +251,7 @@ namespace Nz
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Ret>
|
template<typename Ret>
|
||||||
int Invoke(LuaInstance& instance, Ret (*func)(Args...)) const
|
int Invoke(LuaInstance& instance, Ret(*func)(Args...)) const
|
||||||
{
|
{
|
||||||
return LuaImplReplyVal(instance, std::move(Apply(func, m_args)), TypeTag<decltype(Apply(func, m_args))>());
|
return LuaImplReplyVal(instance, std::move(Apply(func, m_args)), TypeTag<decltype(Apply(func, m_args))>());
|
||||||
}
|
}
|
||||||
|
|
@ -425,7 +425,79 @@ namespace Nz
|
||||||
|
|
||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T LuaInstance::CheckField(const char* fieldName, int tableIndex)
|
||||||
|
{
|
||||||
|
T object;
|
||||||
|
|
||||||
|
GetField(fieldName, tableIndex);
|
||||||
|
tableIndex += LuaImplQueryArg(*this, -1, &object, TypeTag<T>());
|
||||||
|
Pop();
|
||||||
|
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T LuaInstance::CheckField(const String& fieldName, int tableIndex)
|
||||||
|
{
|
||||||
|
return CheckField(fieldName.GetConstBuffer(), tableIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T LuaInstance::CheckField(const char* fieldName, T defValue, int tableIndex)
|
||||||
|
{
|
||||||
|
T object;
|
||||||
|
|
||||||
|
GetField(fieldName, tableIndex);
|
||||||
|
tableIndex += LuaImplQueryArg(*this, -1, &object, defValue, TypeTag<T>());
|
||||||
|
Pop();
|
||||||
|
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T LuaInstance::CheckField(const String& fieldName, T defValue, int tableIndex)
|
||||||
|
{
|
||||||
|
return CheckField(fieldName.GetConstBuffer(), defValue, tableIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T LuaInstance::CheckGlobal(const char* fieldName)
|
||||||
|
{
|
||||||
|
T object;
|
||||||
|
|
||||||
|
GetGlobal(fieldName);
|
||||||
|
tableIndex += LuaImplQueryArg(*this, -1, &object, TypeTag<T>());
|
||||||
|
Pop();
|
||||||
|
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T LuaInstance::CheckGlobal(const String& fieldName)
|
||||||
|
{
|
||||||
|
return CheckGlobal(fieldName.GetConstBuffer());
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T LuaInstance::CheckGlobal(const char* fieldName, T defValue)
|
||||||
|
{
|
||||||
|
T object;
|
||||||
|
|
||||||
|
GetGlobal(fieldName);
|
||||||
|
tableIndex += LuaImplQueryArg(*this, -1, &object, defValue, TypeTag<T>());
|
||||||
|
Pop();
|
||||||
|
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T LuaInstance::CheckGlobal(const String& fieldName, T defValue)
|
||||||
|
{
|
||||||
|
return CheckGlobal(fieldName.GetConstBuffer(), defValue);
|
||||||
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
int LuaInstance::Push(T arg)
|
int LuaInstance::Push(T arg)
|
||||||
{
|
{
|
||||||
|
|
@ -433,11 +505,11 @@ namespace Nz
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename R, typename... Args, typename... DefArgs>
|
template<typename R, typename... Args, typename... DefArgs>
|
||||||
void LuaInstance::PushFunction(R (*func)(Args...), DefArgs&&... defArgs)
|
void LuaInstance::PushFunction(R(*func)(Args...), DefArgs&&... defArgs)
|
||||||
{
|
{
|
||||||
typename LuaImplFunctionProxy<Args...>::template Impl<DefArgs...> handler(std::forward<DefArgs>(defArgs)...);
|
typename LuaImplFunctionProxy<Args...>::template Impl<DefArgs...> handler(std::forward<DefArgs>(defArgs)...);
|
||||||
|
|
||||||
PushFunction([func, handler](LuaInstance& lua) -> int
|
PushFunction([func, handler] (LuaInstance& lua) -> int
|
||||||
{
|
{
|
||||||
handler.ProcessArgs(lua);
|
handler.ProcessArgs(lua);
|
||||||
|
|
||||||
|
|
@ -458,4 +530,30 @@ namespace Nz
|
||||||
{
|
{
|
||||||
PushInstance(tname, new T(std::forward<Args>(args)...));
|
PushInstance(tname, new T(std::forward<Args>(args)...));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void LuaInstance::SetField(const char* name, T&& arg, int tableIndex)
|
||||||
|
{
|
||||||
|
Push<T>(std::forward<T>(arg));
|
||||||
|
SetField(name, tableIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void LuaInstance::SetField(const String& name, T&& arg, int tableIndex)
|
||||||
|
{
|
||||||
|
SetField(name.GetConstBuffer(), std::forward<T>(arg), tableIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void LuaInstance::SetGlobal(const char* name, T&& arg)
|
||||||
|
{
|
||||||
|
Push<T>(std::forward<T>(arg));
|
||||||
|
SetGlobal(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void LuaInstance::SetGlobal(const String& name, T&& arg)
|
||||||
|
{
|
||||||
|
SetGlobal(name.GetConstBuffer(), std::forward<T>(arg));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -443,14 +443,14 @@ namespace Nz
|
||||||
return lua_absindex(m_state, index);
|
return lua_absindex(m_state, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
LuaType LuaInstance::GetField(const char* fieldName, int index) const
|
LuaType LuaInstance::GetField(const char* fieldName, int tableIndex) const
|
||||||
{
|
{
|
||||||
return FromLuaType(lua_getfield(m_state, index, fieldName));
|
return FromLuaType(lua_getfield(m_state, tableIndex, fieldName));
|
||||||
}
|
}
|
||||||
|
|
||||||
LuaType LuaInstance::GetField(const String& fieldName, int index) const
|
LuaType LuaInstance::GetField(const String& fieldName, int tableIndex) const
|
||||||
{
|
{
|
||||||
return FromLuaType(lua_getfield(m_state, index, fieldName.GetConstBuffer()));
|
return FromLuaType(lua_getfield(m_state, tableIndex, fieldName.GetConstBuffer()));
|
||||||
}
|
}
|
||||||
|
|
||||||
LuaType LuaInstance::GetGlobal(const char* name) const
|
LuaType LuaInstance::GetGlobal(const char* name) const
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue