static properties work as intended, I guess

This commit is contained in:
ThePhD 2017-04-25 19:44:53 -04:00
parent 0db6d99d4b
commit fc3e7c40f3
5 changed files with 110 additions and 10 deletions

View File

@ -20,8 +20,8 @@
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// This file was generated with a script.
// Generated 2017-04-21 00:18:17.116630 UTC
// This header was generated with sol v2.17.1 (revision 3d5f80e)
// Generated 2017-04-25 23:43:53.892903 UTC
// This header was generated with sol v2.17.1 (revision 0db6d99)
// https://github.com/ThePhD/sol2
#ifndef SOL_SINGLE_INCLUDE_HPP
@ -8026,6 +8026,11 @@ namespace sol {
return property_detail::property(std::true_type(), std::forward<F>(f));
}
template <typename F>
inline decltype(auto) writeonly_property(F&& f) {
return property_detail::property(std::false_type(), std::forward<F>(f));
}
// Allow someone to make a member variable readonly (const)
template <typename R, typename T>
inline auto readonly(R T::* v) {
@ -8542,12 +8547,21 @@ namespace sol {
struct lua_call_wrapper<T, property_wrapper<R, W>, is_index, is_variable, checked, boost, C> {
typedef std::conditional_t<is_index, R, W> P;
typedef meta::unqualified_t<P> U;
typedef wrapper<U> wrap;
typedef lua_bind_traits<U> traits_type;
typedef meta::unqualified_t<typename traits_type::template arg_at<0>> object_type;
template <typename F>
static int self_call(lua_State* L, F&& f) {
typedef wrapper<U> wrap;
typedef meta::unqualified_t<typename traits_type::template arg_at<0>> object_type;
static int self_call(std::true_type, lua_State* L, F&& f) {
// The type being void means we don't have any arguments, so it might be a free functions?
typedef typename traits_type::free_args_list args_list;
typedef typename wrap::returns_list returns_list;
typedef typename wrap::caller caller;
return stack::call_into_lua<checked>(returns_list(), args_list(), L, boost + (is_variable ? 3 : 2), caller(), f);
}
template <typename F>
static int self_call(std::false_type, lua_State* L, F&& f) {
typedef meta::pop_front_type_t<typename traits_type::free_args_list> args_list;
typedef T Ta;
#ifdef SOL_SAFE_USERTYPE
@ -8569,7 +8583,7 @@ namespace sol {
template <typename F, typename... Args>
static int defer_call(std::false_type, lua_State* L, F&& f, Args&&... args) {
return self_call(L, pick(meta::boolean<is_index>(), f), std::forward<Args>(args)...);
return self_call(meta::any<std::is_void<object_type>, meta::boolean<lua_type_of<meta::unwrap_unqualified_t<object_type>>::value != type::userdata>>(), L, pick(meta::boolean<is_index>(), f), std::forward<Args>(args)...);
}
template <typename F, typename... Args>

View File

@ -515,12 +515,21 @@ namespace sol {
struct lua_call_wrapper<T, property_wrapper<R, W>, is_index, is_variable, checked, boost, C> {
typedef std::conditional_t<is_index, R, W> P;
typedef meta::unqualified_t<P> U;
typedef wrapper<U> wrap;
typedef lua_bind_traits<U> traits_type;
typedef meta::unqualified_t<typename traits_type::template arg_at<0>> object_type;
template <typename F>
static int self_call(lua_State* L, F&& f) {
typedef wrapper<U> wrap;
typedef meta::unqualified_t<typename traits_type::template arg_at<0>> object_type;
static int self_call(std::true_type, lua_State* L, F&& f) {
// The type being void means we don't have any arguments, so it might be a free functions?
typedef typename traits_type::free_args_list args_list;
typedef typename wrap::returns_list returns_list;
typedef typename wrap::caller caller;
return stack::call_into_lua<checked>(returns_list(), args_list(), L, boost + (is_variable ? 3 : 2), caller(), f);
}
template <typename F>
static int self_call(std::false_type, lua_State* L, F&& f) {
typedef meta::pop_front_type_t<typename traits_type::free_args_list> args_list;
typedef T Ta;
#ifdef SOL_SAFE_USERTYPE
@ -542,7 +551,7 @@ namespace sol {
template <typename F, typename... Args>
static int defer_call(std::false_type, lua_State* L, F&& f, Args&&... args) {
return self_call(L, pick(meta::boolean<is_index>(), f), std::forward<Args>(args)...);
return self_call(meta::any<std::is_void<object_type>, meta::boolean<lua_type_of<meta::unwrap_unqualified_t<object_type>>::value != type::userdata>>(), L, pick(meta::boolean<is_index>(), f), std::forward<Args>(args)...);
}
template <typename F, typename... Args>

View File

@ -76,6 +76,11 @@ namespace sol {
return property_detail::property(std::true_type(), std::forward<F>(f));
}
template <typename F>
inline decltype(auto) writeonly_property(F&& f) {
return property_detail::property(std::false_type(), std::forward<F>(f));
}
// Allow someone to make a member variable readonly (const)
template <typename R, typename T>
inline auto readonly(R T::* v) {

View File

@ -840,3 +840,39 @@ TEST_CASE("simple_usertype/meta-key-retrievals", "allow for special meta keys (_
REQUIRE(keys[3] == "__call");
}
}
TEST_CASE("simple_usertype/static-properties", "allow for static functions to get and set things as a property") {
static int b = 50;
struct test_t {
static double s_func() {
return b + 0.5;
}
static void g_func(int v) {
b = v;
}
std::size_t func() {
return 24;
}
};
test_t manager;
sol::state lua;
lua.new_simple_usertype<test_t>("test",
"f", std::function<std::size_t()>(std::bind(std::mem_fn(&test_t::func), &manager)),
"g", sol::property(&test_t::s_func, &test_t::g_func)
);
lua.script("v1 = test.f()");
lua.script("v2 = test.g");
lua.script("test.g = 60");
lua.script("v2a = test.g");
int v1 = lua["v1"];
REQUIRE(v1 == 24);
double v2 = lua["v2"];
REQUIRE(v2 == 50.5);
double v2a = lua["v2a"];
REQUIRE(v2a == 60.5);
}

View File

@ -1353,6 +1353,42 @@ print(test.ref_global2)
REQUIRE(through_variable == 35);
}
TEST_CASE("usertype/static-properties", "allow for static functions to get and set things as a property") {
static int b = 50;
struct test_t {
static double s_func() {
return b + 0.5;
}
static void g_func(int v) {
b = v;
}
std::size_t func() {
return 24;
}
};
test_t manager;
sol::state lua;
lua.new_usertype<test_t>("test",
"f", std::function<std::size_t()>(std::bind(std::mem_fn(&test_t::func), &manager)),
"g", sol::property(&test_t::s_func, &test_t::g_func)
);
lua.script("v1 = test.f()");
lua.script("v2 = test.g");
lua.script("test.g = 60");
lua.script("v2a = test.g");
int v1 = lua["v1"];
REQUIRE(v1 == 24);
double v2 = lua["v2"];
REQUIRE(v2 == 50.5);
double v2a = lua["v2a"];
REQUIRE(v2a == 60.5);
}
TEST_CASE("usertype/var-and-property", "make sure const vars are readonly and properties can handle lambdas") {
const static int arf = 20;