Merge branch 'NDK' of https://github.com/DigitalPulseSoftware/NazaraEngine into NDK
Former-commit-id: 3cd50359629d58a4e61744b94f3b7fc47ad551e4
This commit is contained in:
commit
32829863aa
|
|
@ -12,8 +12,12 @@
|
|||
#include <tuple>
|
||||
|
||||
template<typename F, typename Tuple> auto NzApply(F&& fn, Tuple&& t);
|
||||
template<typename O, typename F, typename Tuple> auto NzApply(O& object, F&& fn, Tuple&& t);
|
||||
template<typename T> void NzHashCombine(std::size_t& seed, const T& v);
|
||||
|
||||
template<typename T>
|
||||
struct NzTypeTag {};
|
||||
|
||||
#include <Nazara/Core/Algorithm.inl>
|
||||
|
||||
#endif // NAZARA_ALGORITHM_CORE_HPP
|
||||
|
|
|
|||
|
|
@ -9,8 +9,8 @@
|
|||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
// http://www.cppsamples.com/common-tasks/apply-tuple-to-function.html
|
||||
template<typename F, typename Tuple, size_t ...S >
|
||||
auto NzApplyImpl(F&& fn, Tuple&& t, std::index_sequence<S...>)
|
||||
template<typename F, typename Tuple, size_t... S>
|
||||
auto NzApplyImplFunc(F&& fn, Tuple&& t, std::index_sequence<S...>)
|
||||
{
|
||||
return std::forward<F>(fn)(std::get<S>(std::forward<Tuple>(t))...);
|
||||
}
|
||||
|
|
@ -18,11 +18,24 @@ auto NzApplyImpl(F&& fn, Tuple&& t, std::index_sequence<S...>)
|
|||
template<typename F, typename Tuple>
|
||||
auto NzApply(F&& fn, Tuple&& t)
|
||||
{
|
||||
std::size_t constexpr tSize = std::tuple_size<typename std::remove_reference<Tuple>::type>::value;
|
||||
constexpr std::size_t tSize = std::tuple_size<typename std::remove_reference<Tuple>::type>::value;
|
||||
|
||||
return NzApplyImpl(std::forward<F>(fn), std::forward<Tuple>(t), std::make_index_sequence<tSize>());
|
||||
return NzApplyImplFunc(std::forward<F>(fn), std::forward<Tuple>(t), std::make_index_sequence<tSize>());
|
||||
}
|
||||
|
||||
template<typename O, typename F, typename Tuple, size_t... S>
|
||||
auto NzApplyImplMethod(O& object, F&& fn, Tuple&& t, std::index_sequence<S...>)
|
||||
{
|
||||
return (object .* std::forward<F>(fn))(std::get<S>(std::forward<Tuple>(t))...);
|
||||
}
|
||||
|
||||
template<typename O, typename F, typename Tuple>
|
||||
auto NzApply(O& object, F&& fn, Tuple&& t)
|
||||
{
|
||||
constexpr std::size_t tSize = std::tuple_size<typename std::remove_reference<Tuple>::type>::value;
|
||||
|
||||
return NzApplyImplMethod(object, std::forward<F>(fn), std::forward<Tuple>(t), std::make_index_sequence<tSize>());
|
||||
}
|
||||
// Algorithme venant de CityHash par Google
|
||||
// http://stackoverflow.com/questions/8513911/how-to-create-a-good-hash-combine-with-64-bit-output-inspired-by-boosthash-co
|
||||
template<typename T>
|
||||
|
|
|
|||
|
|
@ -108,8 +108,9 @@ class NAZARA_LUA_API NzLuaInstance : NzNonCopyable
|
|||
void Pop(unsigned int n = 1U);
|
||||
|
||||
void PushBoolean(bool value);
|
||||
void PushCFunction(NzLuaCFunction func, int upvalueCount = 0);
|
||||
void PushCFunction(NzLuaCFunction func, unsigned int upvalueCount = 0);
|
||||
void PushFunction(NzLuaFunction func);
|
||||
template<typename R, typename... Args> void PushFunction(R(*func)(Args...));
|
||||
void PushInteger(long long value);
|
||||
void PushLightUserdata(void* value);
|
||||
void PushMetatable(const char* str);
|
||||
|
|
@ -118,6 +119,7 @@ class NAZARA_LUA_API NzLuaInstance : NzNonCopyable
|
|||
void PushNumber(double value);
|
||||
void PushReference(int ref);
|
||||
void PushString(const char* str);
|
||||
void PushString(const char* str, unsigned int size);
|
||||
void PushString(const NzString& str);
|
||||
void PushTable(unsigned int sequenceElementCount = 0, unsigned int arrayElementCount = 0);
|
||||
void* PushUserdata(unsigned int size);
|
||||
|
|
@ -165,4 +167,6 @@ class NAZARA_LUA_API NzLuaInstance : NzNonCopyable
|
|||
unsigned int m_level;
|
||||
};
|
||||
|
||||
#include <Nazara/Lua/LuaInstance.inl>
|
||||
|
||||
#endif // NAZARA_LUASTATE_HPP
|
||||
|
|
|
|||
|
|
@ -0,0 +1,161 @@
|
|||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Lua scripting module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Core/Algorithm.hpp>
|
||||
#include <string>
|
||||
|
||||
// Functions args
|
||||
bool NzLuaImplQueryArg(NzLuaInstance& instance, unsigned int index, NzTypeTag<bool>)
|
||||
{
|
||||
return instance.CheckBoolean(index);
|
||||
}
|
||||
|
||||
double NzLuaImplQueryArg(NzLuaInstance& instance, unsigned int index, NzTypeTag<double>)
|
||||
{
|
||||
return instance.CheckNumber(index);
|
||||
}
|
||||
|
||||
float NzLuaImplQueryArg(NzLuaInstance& instance, unsigned int index, NzTypeTag<float>)
|
||||
{
|
||||
return instance.CheckNumber(index);
|
||||
}
|
||||
|
||||
int NzLuaImplQueryArg(NzLuaInstance& instance, unsigned int index, NzTypeTag<int>)
|
||||
{
|
||||
return instance.CheckInteger(index);
|
||||
}
|
||||
|
||||
std::string NzLuaImplQueryArg(NzLuaInstance& instance, unsigned int index, NzTypeTag<std::string>)
|
||||
{
|
||||
return instance.CheckString(index);
|
||||
}
|
||||
|
||||
NzString NzLuaImplQueryArg(NzLuaInstance& instance, unsigned int index, NzTypeTag<NzString>)
|
||||
{
|
||||
return instance.CheckString(index);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T NzLuaImplQueryArg(NzLuaInstance& instance, unsigned int index, NzTypeTag<const T&>)
|
||||
{
|
||||
return NzLuaImplQueryArg(instance, index, NzTypeTag<T>());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::enable_if_t<std::is_unsigned<T>::value, T> NzLuaImplQueryArg(NzLuaInstance& instance, unsigned int index, NzTypeTag<T>)
|
||||
{
|
||||
return static_cast<T>(NzLuaImplQueryArg(instance, index, NzTypeTag<typename std::make_signed<T>::type>()));
|
||||
}
|
||||
|
||||
// Function returns
|
||||
int NzLuaImplReplyVal(NzLuaInstance& instance, bool&& val, NzTypeTag<bool>)
|
||||
{
|
||||
instance.PushBoolean(val);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int NzLuaImplReplyVal(NzLuaInstance& instance, double&& val, NzTypeTag<double>)
|
||||
{
|
||||
instance.PushNumber(val);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int NzLuaImplReplyVal(NzLuaInstance& instance, float&& val, NzTypeTag<float>)
|
||||
{
|
||||
instance.PushNumber(val);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int NzLuaImplReplyVal(NzLuaInstance& instance, int&& val, NzTypeTag<int>)
|
||||
{
|
||||
instance.PushInteger(val);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int NzLuaImplReplyVal(NzLuaInstance& instance, std::string&& val, NzTypeTag<std::string>)
|
||||
{
|
||||
instance.PushString(val.c_str(), val.size());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int NzLuaImplReplyVal(NzLuaInstance& instance, NzString&& val, NzTypeTag<NzString>)
|
||||
{
|
||||
instance.PushString(std::move(val));
|
||||
return 1;
|
||||
}
|
||||
|
||||
template<typename T1, typename T2>
|
||||
int NzLuaImplReplyVal(NzLuaInstance& instance, std::pair<T1, T2>&& val, NzTypeTag<std::pair<T1, T2>>)
|
||||
{
|
||||
int retVal = 0;
|
||||
|
||||
retVal += NzLuaImplReplyVal(instance, std::move(val.first), NzTypeTag<T1>());
|
||||
retVal += NzLuaImplReplyVal(instance, std::move(val.second), NzTypeTag<T2>());
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::enable_if_t<std::is_unsigned<T>::value, int> NzLuaImplReplyVal(NzLuaInstance& instance, T&& val, NzTypeTag<T>)
|
||||
{
|
||||
using SignedT = typename std::make_signed<T>::type;
|
||||
|
||||
return NzLuaImplReplyVal(instance, val, NzTypeTag<SignedT>());
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
class NzLuaImplFunctionProxy
|
||||
{
|
||||
public:
|
||||
NzLuaImplFunctionProxy(NzLuaInstance& instance) :
|
||||
m_instance(instance)
|
||||
{
|
||||
}
|
||||
|
||||
template<unsigned int N, typename ArgType>
|
||||
void ProcessArgs()
|
||||
{
|
||||
std::get<N>(m_args) = std::move(NzLuaImplQueryArg(m_instance, N+1, NzTypeTag<ArgType>()));
|
||||
}
|
||||
|
||||
template<int N, typename ArgType1, typename ArgType2, typename... Rest>
|
||||
void ProcessArgs()
|
||||
{
|
||||
ProcessArgs<N, ArgType1>();
|
||||
ProcessArgs<N+1, ArgType2, Rest...>();
|
||||
}
|
||||
|
||||
void ProcessArgs()
|
||||
{
|
||||
ProcessArgs<0, Args...>();
|
||||
}
|
||||
|
||||
int Invoke(void (*func)(Args...))
|
||||
{
|
||||
NzApply(func, m_args);
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<typename Ret>
|
||||
int Invoke(Ret (*func)(Args...))
|
||||
{
|
||||
return NzLuaImplReplyVal(m_instance, std::move(NzApply(func, m_args)), NzTypeTag<decltype(NzApply(func, m_args))>());
|
||||
}
|
||||
|
||||
private:
|
||||
NzLuaInstance& m_instance;
|
||||
std::tuple<Args...> m_args;
|
||||
};
|
||||
|
||||
template<typename R, typename... Args>
|
||||
void NzLuaInstance::PushFunction(R(*func)(Args...))
|
||||
{
|
||||
PushFunction([func](NzLuaInstance& instance) -> int
|
||||
{
|
||||
NzLuaImplFunctionProxy<Args...> handler(instance);
|
||||
handler.ProcessArgs();
|
||||
|
||||
return handler.Invoke(func);
|
||||
});
|
||||
}
|
||||
|
|
@ -624,7 +624,7 @@ void NzLuaInstance::PushBoolean(bool value)
|
|||
lua_pushboolean(m_state, (value) ? 1 : 0);
|
||||
}
|
||||
|
||||
void NzLuaInstance::PushCFunction(NzLuaCFunction func, int upvalueCount)
|
||||
void NzLuaInstance::PushCFunction(NzLuaCFunction func, unsigned int upvalueCount)
|
||||
{
|
||||
lua_pushcclosure(m_state, func, upvalueCount);
|
||||
}
|
||||
|
|
@ -677,6 +677,11 @@ void NzLuaInstance::PushString(const char* str)
|
|||
lua_pushstring(m_state, str);
|
||||
}
|
||||
|
||||
void NzLuaInstance::PushString(const char* str, unsigned int size)
|
||||
{
|
||||
lua_pushlstring(m_state, str, size);
|
||||
}
|
||||
|
||||
void NzLuaInstance::PushString(const NzString& str)
|
||||
{
|
||||
lua_pushlstring(m_state, str.GetConstBuffer(), str.GetSize());
|
||||
|
|
|
|||
Loading…
Reference in New Issue