mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
Move type traits to its own header and clean up code using the traits
This commit is contained in:
parent
5eeaedec2f
commit
7cc8c93289
|
@ -24,18 +24,12 @@
|
||||||
|
|
||||||
#include "reference.hpp"
|
#include "reference.hpp"
|
||||||
#include "tuple.hpp"
|
#include "tuple.hpp"
|
||||||
|
#include "traits.hpp"
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <type_traits>
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
namespace sol {
|
namespace sol {
|
||||||
template<typename T, typename R = void>
|
|
||||||
using EnableIf = typename std::enable_if<T::value, R>::type;
|
|
||||||
|
|
||||||
template<typename T, typename R = void>
|
|
||||||
using DisableIf = typename std::enable_if<!T::value, R>::type;
|
|
||||||
|
|
||||||
namespace stack {
|
namespace stack {
|
||||||
namespace detail {
|
namespace detail {
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
|
|
@ -29,15 +29,15 @@
|
||||||
|
|
||||||
namespace sol {
|
namespace sol {
|
||||||
namespace detail {
|
namespace detail {
|
||||||
template<typename T>
|
template<typename T>
|
||||||
T* get_ptr(T& val) {
|
T* get_ptr(T& val) {
|
||||||
return std::addressof(val);
|
return std::addressof(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
T* get_ptr(T* val) {
|
T* get_ptr(T* val) {
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
} // detail
|
} // detail
|
||||||
|
|
||||||
class table : virtual public reference {
|
class table : virtual public reference {
|
||||||
|
@ -71,13 +71,13 @@ public:
|
||||||
|
|
||||||
template<typename T, typename TFx>
|
template<typename T, typename TFx>
|
||||||
table& set_function(T&& key, TFx&& fx) {
|
table& set_function(T&& key, TFx&& fx) {
|
||||||
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type clean_fx;
|
typedef typename std::remove_pointer<Decay<TFx>>::type clean_fx;
|
||||||
return set_isfunction_fx(std::is_function<clean_fx>(), std::forward<T>(key), std::forward<TFx>(fx));
|
return set_isfunction_fx(std::is_function<clean_fx>(), std::forward<T>(key), std::forward<TFx>(fx));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, typename TFx, typename TObj>
|
template<typename T, typename TFx, typename TObj>
|
||||||
table& set_function(T&& key, TFx&& fx, TObj&& obj) {
|
table& set_function(T&& key, TFx&& fx, TObj&& obj) {
|
||||||
return set_lvalue_fx(std::integral_constant<bool, std::is_lvalue_reference<TObj>::value || std::is_pointer<TObj>::value>(),
|
return set_lvalue_fx(Bool<std::is_lvalue_reference<TObj>::value || std::is_pointer<TObj>::value>(),
|
||||||
std::forward<T>(key), std::forward<TFx>(fx), std::forward<TObj>(obj));
|
std::forward<T>(key), std::forward<TFx>(fx), std::forward<TObj>(obj));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,7 +98,7 @@ private:
|
||||||
|
|
||||||
template<typename T, typename TFx>
|
template<typename T, typename TFx>
|
||||||
table& set_isfunction_fx(std::false_type, T&& key, TFx&& fx) {
|
table& set_isfunction_fx(std::false_type, T&& key, TFx&& fx) {
|
||||||
typedef typename std::decay<TFx>::type clean_lambda;
|
typedef Decay<TFx> clean_lambda;
|
||||||
typedef typename detail::function_traits<decltype(&clean_lambda::operator())>::free_function_pointer_type raw_func_t;
|
typedef typename detail::function_traits<decltype(&clean_lambda::operator())>::free_function_pointer_type raw_func_t;
|
||||||
typedef std::is_convertible<clean_lambda, raw_func_t> isconvertible;
|
typedef std::is_convertible<clean_lambda, raw_func_t> isconvertible;
|
||||||
return set_isconvertible_fx(isconvertible(), std::forward<T>(key), std::forward<TFx>(fx));
|
return set_isconvertible_fx(isconvertible(), std::forward<T>(key), std::forward<TFx>(fx));
|
||||||
|
@ -106,14 +106,14 @@ private:
|
||||||
|
|
||||||
template<typename T, typename TFx>
|
template<typename T, typename TFx>
|
||||||
table& set_isconvertible_fx(std::true_type, T&& key, TFx&& fx) {
|
table& set_isconvertible_fx(std::true_type, T&& key, TFx&& fx) {
|
||||||
typedef typename std::decay<TFx>::type clean_lambda;
|
typedef Decay<TFx> clean_lambda;
|
||||||
typedef typename detail::function_traits<decltype(&clean_lambda::operator())>::free_function_pointer_type raw_func_t;
|
typedef typename detail::function_traits<decltype(&clean_lambda::operator())>::free_function_pointer_type raw_func_t;
|
||||||
return set_isfunction_fx(std::true_type(), std::forward<T>(key), raw_func_t(std::forward<TFx>(fx)));
|
return set_isfunction_fx(std::true_type(), std::forward<T>(key), raw_func_t(std::forward<TFx>(fx)));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, typename TFx>
|
template<typename T, typename TFx>
|
||||||
table& set_isconvertible_fx(std::false_type, T&& key, TFx&& fx) {
|
table& set_isconvertible_fx(std::false_type, T&& key, TFx&& fx) {
|
||||||
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type clean_fx;
|
typedef typename std::remove_pointer<Decay<TFx>>::type clean_fx;
|
||||||
std::unique_ptr<lua_func> sptr(new lambda_lua_func<clean_fx>(std::forward<TFx>(fx)));
|
std::unique_ptr<lua_func> sptr(new lambda_lua_func<clean_fx>(std::forward<TFx>(fx)));
|
||||||
return set_fx(std::forward<T>(key), std::move(sptr));
|
return set_fx(std::forward<T>(key), std::move(sptr));
|
||||||
}
|
}
|
||||||
|
@ -125,16 +125,13 @@ private:
|
||||||
|
|
||||||
template<typename T, typename TFx, typename TObj>
|
template<typename T, typename TFx, typename TObj>
|
||||||
table& set_lvalue_fx(std::false_type, T&& key, TFx&& fx, TObj&& obj) {
|
table& set_lvalue_fx(std::false_type, T&& key, TFx&& fx, TObj&& obj) {
|
||||||
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type clean_fx;
|
typedef typename std::remove_pointer<Decay<TFx>>::type clean_fx;
|
||||||
std::unique_ptr<lua_func> sptr(new explicit_lua_func<clean_fx, TObj>(std::forward<TObj>(obj), std::forward<TFx>(fx)));
|
std::unique_ptr<lua_func> sptr(new explicit_lua_func<clean_fx, TObj>(std::forward<TObj>(obj), std::forward<TFx>(fx)));
|
||||||
return set_fx(std::forward<T>(key), std::move(sptr));
|
return set_fx(std::forward<T>(key), std::move(sptr));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, typename TFx, typename TObj>
|
template<typename T, typename TFx, typename TObj>
|
||||||
table& set_fx(std::true_type, T&& key, TFx&& fx, TObj&& obj) {
|
table& set_fx(std::true_type, T&& key, TFx&& fx, TObj&& obj) {
|
||||||
typedef typename std::decay<TObj>::type decay_of_to;
|
|
||||||
typedef typename std::decay<TFx>::type decay_of_tfx;
|
|
||||||
|
|
||||||
std::string fkey(key);
|
std::string fkey(key);
|
||||||
|
|
||||||
// Layout:
|
// Layout:
|
||||||
|
@ -142,9 +139,9 @@ private:
|
||||||
// idx n + 1: is the object's void pointer
|
// idx n + 1: is the object's void pointer
|
||||||
// We don't need to store the size, because the other side is templated
|
// We don't need to store the size, because the other side is templated
|
||||||
// with the same member function pointer type
|
// with the same member function pointer type
|
||||||
decay_of_tfx fxptr(std::forward<TFx>(fx));
|
Decay<TFx> fxptr(std::forward<TFx>(fx));
|
||||||
void* userobjdata = static_cast<void*>(detail::get_ptr(obj));
|
void* userobjdata = static_cast<void*>(detail::get_ptr(obj));
|
||||||
lua_CFunction freefunc = &static_object_lua_func<decay_of_to, TFx>::call;
|
lua_CFunction freefunc = &static_object_lua_func<Decay<TObj>, TFx>::call;
|
||||||
const char* freefuncname = fkey.c_str();
|
const char* freefuncname = fkey.c_str();
|
||||||
const luaL_Reg funcreg[ 2 ] = {
|
const luaL_Reg funcreg[ 2 ] = {
|
||||||
{ freefuncname, freefunc },
|
{ freefuncname, freefunc },
|
||||||
|
@ -164,9 +161,8 @@ private:
|
||||||
|
|
||||||
template<typename T, typename TFx>
|
template<typename T, typename TFx>
|
||||||
table& set_fx(std::false_type, T&& key, TFx&& fx) {
|
table& set_fx(std::false_type, T&& key, TFx&& fx) {
|
||||||
typedef typename std::decay<TFx>::type ptr_fx;
|
|
||||||
std::string fkey(key);
|
std::string fkey(key);
|
||||||
ptr_fx target(std::forward<TFx>(fx));
|
Decay<TFx> target(std::forward<TFx>(fx));
|
||||||
lua_CFunction freefunc = &static_lua_func<TFx>::call;
|
lua_CFunction freefunc = &static_lua_func<TFx>::call;
|
||||||
const char* freefuncname = fkey.c_str();
|
const char* freefuncname = fkey.c_str();
|
||||||
const luaL_Reg funcreg[ 2 ] = {
|
const luaL_Reg funcreg[ 2 ] = {
|
||||||
|
@ -176,7 +172,7 @@ private:
|
||||||
|
|
||||||
push();
|
push();
|
||||||
|
|
||||||
int upvalues = stack::push_user(state(), target);
|
int upvalues = stack::push_user(state(), target);
|
||||||
luaL_setfuncs(state(), funcreg, upvalues);
|
luaL_setfuncs(state(), funcreg, upvalues);
|
||||||
|
|
||||||
lua_pop(state(), 1);
|
lua_pop(state(), 1);
|
||||||
|
@ -205,10 +201,9 @@ private:
|
||||||
lua_settable(state(), -3);
|
lua_settable(state(), -3);
|
||||||
}
|
}
|
||||||
|
|
||||||
push();
|
push();
|
||||||
stack::push_user(state(), userdata, metatablename);
|
stack::push_user(state(), userdata, metatablename);
|
||||||
luaL_setfuncs(state(), funcreg, 1);
|
luaL_setfuncs(state(), funcreg, 1);
|
||||||
|
|
||||||
lua_pop(state(), 1);
|
lua_pop(state(), 1);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
72
sol/traits.hpp
Normal file
72
sol/traits.hpp
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
// The MIT License (MIT)
|
||||||
|
|
||||||
|
// Copyright (c) 2013 Danny Y., Rapptz
|
||||||
|
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
|
// this software and associated documentation files (the "Software"), to deal in
|
||||||
|
// the Software without restriction, including without limitation the rights to
|
||||||
|
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||||
|
// the Software, and to permit persons to whom the Software is furnished to do so,
|
||||||
|
// subject to the following conditions:
|
||||||
|
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||||
|
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||||
|
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||||
|
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||||
|
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
#ifndef SOL_TRAITS_HPP
|
||||||
|
#define SOL_TRAITS_HPP
|
||||||
|
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
|
namespace sol {
|
||||||
|
template<typename T, typename R = void>
|
||||||
|
using EnableIf = typename std::enable_if<T::value, R>::type;
|
||||||
|
|
||||||
|
template<typename T, typename R = void>
|
||||||
|
using DisableIf = typename std::enable_if<!T::value, R>::type;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
using Unqualified = typename std::remove_reference<typename std::remove_cv<T>::type>::type;
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
|
// code borrowed from Gears
|
||||||
|
// https://github.com/Rapptz/Gears/
|
||||||
|
template<typename T, typename = void>
|
||||||
|
struct is_function_impl : std::is_function<T> {};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct is_function_impl<T, EnableIf<std::is_class<Unqualified<T>>>> {
|
||||||
|
using yes = char;
|
||||||
|
using no = struct { char s[2]; };
|
||||||
|
|
||||||
|
struct F { void operator()(); };
|
||||||
|
struct Derived : T, F { };
|
||||||
|
template<typename U, U> struct Check;
|
||||||
|
|
||||||
|
template<typename V>
|
||||||
|
static no test(Check<void (F::*)(), &V::operator()>*);
|
||||||
|
|
||||||
|
template<typename>
|
||||||
|
static yes test(...);
|
||||||
|
|
||||||
|
static const bool value = sizeof(test<Derived>(0)) == sizeof(yes);
|
||||||
|
};
|
||||||
|
} // detail
|
||||||
|
|
||||||
|
template<bool B>
|
||||||
|
using Bool = std::integral_constant<bool, B>;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct Function : Bool<detail::is_function_impl<T>::value> {};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
using Decay = typename std::decay<T>::type;
|
||||||
|
} // sol
|
||||||
|
|
||||||
|
#endif // SOL_TRAITS_HPP
|
Loading…
Reference in New Issue
Block a user