Lua/LuaClass: Add pointer objects support

Former-commit-id: b4cf6fb6674278ed341a27d2a8e432845531a333
This commit is contained in:
Lynix 2015-12-13 16:35:42 +01:00
parent 2c79e5f4e0
commit 4c72e27784
6 changed files with 76 additions and 6 deletions

View File

@ -25,6 +25,12 @@ namespace Nz
template<typename T> ByteArray ComputeHash(AbstractHash* hash, const T& v);
template<typename T> void HashCombine(std::size_t& seed, const T& v);
template<typename T>
struct PointedType
{
using type = void; //< FIXME: I can't make SFINAE work
};
template<typename T>
struct TypeTag {};

View File

@ -79,6 +79,11 @@ namespace Nz
seed = static_cast<std::size_t>(b * kMul);
}
template<typename T> struct PointedType<T*> {typedef T type;};
template<typename T> struct PointedType<T* const> {typedef T type;};
template<typename T> struct PointedType<T* volatile> {typedef T type;};
template<typename T> struct PointedType<T* const volatile> {typedef T type;};
inline bool Serialize(SerializationContext& context, bool value)
{
if (context.currentBitPos == 8)

View File

@ -8,6 +8,7 @@
#define NAZARA_RESOURCEREF_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Algorithm.hpp>
#include <Nazara/Core/RefCounted.hpp>
#include <type_traits>
@ -44,6 +45,9 @@ namespace Nz
private:
T* m_object;
};
template<typename T> struct PointedType<ObjectRef<T>> {typedef T type;};
template<typename T> struct PointedType<ObjectRef<T> const> {typedef T type;};
}
#include <Nazara/Core/ObjectRef.inl>

View File

@ -8,6 +8,7 @@
#define NAZARA_LUACLASS_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Algorithm.hpp>
#include <Nazara/Core/String.hpp>
#include <Nazara/Lua/LuaInstance.hpp>
#include <functional>
@ -47,6 +48,8 @@ namespace Nz
void SetMethod(const String& name, ClassFunc method);
template<typename R, typename P, typename... Args, typename... DefArgs> std::enable_if_t<std::is_base_of<P, T>::value> SetMethod(const String& name, R(P::*func)(Args...), DefArgs... defArgs);
template<typename R, typename P, typename... Args, typename... DefArgs> std::enable_if_t<std::is_base_of<P, T>::value> SetMethod(const String& name, R(P::*func)(Args...) const, DefArgs... defArgs);
template<typename R, typename P, typename... Args, typename... DefArgs> std::enable_if_t<std::is_base_of<P, typename PointedType<T>::type>::value> SetMethod(const String& name, R(P::*func)(Args...), DefArgs... defArgs);
template<typename R, typename P, typename... Args, typename... DefArgs> std::enable_if_t<std::is_base_of<P, typename PointedType<T>::type>::value> SetMethod(const String& name, R(P::*func)(Args...) const, DefArgs... defArgs);
void SetSetter(ClassIndexFunc setter);
void SetStaticGetter(StaticIndexFunc getter);
void SetStaticMethod(const String& name, StaticFunc func);
@ -63,12 +66,12 @@ namespace Nz
std::vector<ParentFunc> parentGetters;
std::vector<StaticFunc> staticMethods;
std::unordered_map<String, InstanceGetter> instanceGetters;
ClassIndexFunc getter = nullptr;
ClassIndexFunc setter = nullptr;
ConstructorFunc constructor = nullptr;
FinalizerFunc finalizer = nullptr;
StaticIndexFunc staticGetter = nullptr;
StaticIndexFunc staticSetter = nullptr;
ClassIndexFunc getter;
ClassIndexFunc setter;
ConstructorFunc constructor;
FinalizerFunc finalizer;
StaticIndexFunc staticGetter;
StaticIndexFunc staticSetter;
String name;
int globalTableRef = -1;
};

View File

@ -225,6 +225,32 @@ namespace Nz
});
}
template<class T>
template<typename R, typename P, typename... Args, typename... DefArgs>
std::enable_if_t<std::is_base_of<P, typename PointedType<T>::type>::value> LuaClass<T>::SetMethod(const String& name, R(P::*func)(Args...), DefArgs... defArgs)
{
SetMethod(name, [func, defArgs...](LuaInstance& instance, T& object) -> int
{
typename LuaImplMethodProxy<T, Args...>::template Impl<DefArgs...> handler(instance, object, defArgs...);
handler.ProcessArgs();
return handler.Invoke(func);
});
}
template<class T>
template<typename R, typename P, typename... Args, typename... DefArgs>
std::enable_if_t<std::is_base_of<P, typename PointedType<T>::type>::value> LuaClass<T>::SetMethod(const String& name, R(P::*func)(Args...) const, DefArgs... defArgs)
{
SetMethod(name, [func, defArgs...](LuaInstance& instance, T& object) -> int
{
typename LuaImplMethodProxy<T, Args...>::template Impl<DefArgs...> handler(instance, object, defArgs...);
handler.ProcessArgs();
return handler.Invoke(func);
});
}
template<class T>
void LuaClass<T>::SetSetter(ClassIndexFunc setter)
{

View File

@ -344,6 +344,32 @@ namespace Nz
return LuaImplReplyVal(m_instance, std::move(Apply(m_object, func, m_args)), TypeTag<decltype(Apply(m_object, func, m_args))>());
}
template<typename P>
std::enable_if_t<std::is_base_of<P, typename PointedType<T>::type>::value, int> Invoke(void(P::*func)(Args...))
{
Apply(*m_object, func, m_args);
return 0;
}
template<typename P, typename Ret>
std::enable_if_t<std::is_base_of<P, typename PointedType<T>::type>::value, int> Invoke(Ret(P::*func)(Args...))
{
return LuaImplReplyVal(m_instance, std::move(Apply(*m_object, func, m_args)), TypeTag<decltype(Apply(*m_object, func, m_args))>());
}
template<typename P>
std::enable_if_t<std::is_base_of<P, typename PointedType<T>::type>::value, int> Invoke(void(P::*func)(Args...) const)
{
Apply(*m_object, func, m_args);
return 0;
}
template<typename P, typename Ret>
std::enable_if_t<std::is_base_of<P, typename PointedType<T>::type>::value, int> Invoke(Ret(P::*func)(Args...) const)
{
return LuaImplReplyVal(m_instance, std::move(Apply(*m_object, func, m_args)), TypeTag<decltype(Apply(*m_object, func, m_args))>());
}
private:
using ArgContainer = std::tuple<std::remove_cv_t<std::remove_reference_t<Args>>...>;
using DefArgContainer = std::tuple<std::remove_cv_t<std::remove_reference_t<DefArgs>>...>;