Skip to content

Commit

Permalink
pointers just wanna be included in the fun, man
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePhD committed May 29, 2019
1 parent 46a2b01 commit 5a4d7dc
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 15 deletions.
6 changes: 5 additions & 1 deletion include/sol/stack_core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,14 +530,18 @@ namespace sol {
}

struct properties_enrollment_allowed {
int& times_through;
std::bitset<64>& properties;
automagic_enrollments& enrollments;

properties_enrollment_allowed(std::bitset<64>& props, automagic_enrollments& enroll) : properties(props), enrollments(enroll) {
properties_enrollment_allowed(int& times, std::bitset<64>& props, automagic_enrollments& enroll) : times_through(times), properties(props), enrollments(enroll) {
}

bool operator()(meta_function mf) const {
bool p = properties[static_cast<int>(mf)];
if (times_through > 0) {
return p;
}
switch (mf) {
case meta_function::length:
return enrollments.length_operator && !p;
Expand Down
7 changes: 4 additions & 3 deletions include/sol/usertype_storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,7 @@ namespace sol { namespace u_detail {
// add intrinsics
// this one is the actual meta-handling table,
// the next one will be the one for

int for_each_backing_metatable_calls = 0;
auto for_each_backing_metatable = [&](lua_State* L, submetatable_type smt, reference& fast_index_table) {
// Pointer types, AKA "references" from C++
const char* metakey = nullptr;
Expand Down Expand Up @@ -1039,8 +1039,8 @@ namespace sol { namespace u_detail {
stack::set_field<false, true>(L, detail::base_class_check_key(), reinterpret_cast<void*>(&detail::inheritance<T>::type_check), t.stack_index());
stack::set_field<false, true>(L, detail::base_class_cast_key(), reinterpret_cast<void*>(&detail::inheritance<T>::type_cast), t.stack_index());

auto prop_fx = detail::properties_enrollment_allowed(storage.properties, enrollments);
auto insert_fx = [&L, &t, &storage](meta_function mf, lua_CFunction reg) {
auto prop_fx = detail::properties_enrollment_allowed(for_each_backing_metatable_calls, storage.properties, enrollments);
auto insert_fx = [&L, &t, &storage, &smt](meta_function mf, lua_CFunction reg) {
stack::set_field<false, true>(L, mf, reg, t.stack_index());
storage.properties[static_cast<int>(mf)] = true;
};
Expand Down Expand Up @@ -1082,6 +1082,7 @@ namespace sol { namespace u_detail {
storage.is_using_new_index = true;
}

++for_each_backing_metatable_calls;
fast_index_table = reference(L, t);
t.pop();
};
Expand Down
4 changes: 2 additions & 2 deletions single/include/sol/forward.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 2019-05-27 05:35:30.258702 UTC
// This header was generated with sol v3.0.2 (revision e814868)
// Generated 2019-05-29 20:53:05.788092 UTC
// This header was generated with sol v3.0.2 (revision 46a2b01)
// https://github.com/ThePhD/sol2

#ifndef SOL_SINGLE_INCLUDE_FORWARD_HPP
Expand Down
20 changes: 12 additions & 8 deletions single/include/sol/sol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 2019-05-27 05:35:29.780976 UTC
// This header was generated with sol v3.0.2 (revision e814868)
// Generated 2019-05-29 20:53:04.799735 UTC
// This header was generated with sol v3.0.2 (revision 46a2b01)
// https://github.com/ThePhD/sol2

#ifndef SOL_SINGLE_INCLUDE_HPP
Expand Down Expand Up @@ -9682,14 +9682,18 @@ namespace sol {
}

struct properties_enrollment_allowed {
int& times_through;
std::bitset<64>& properties;
automagic_enrollments& enrollments;

properties_enrollment_allowed(std::bitset<64>& props, automagic_enrollments& enroll) : properties(props), enrollments(enroll) {
properties_enrollment_allowed(int& times, std::bitset<64>& props, automagic_enrollments& enroll) : times_through(times), properties(props), enrollments(enroll) {
}

bool operator()(meta_function mf) const {
bool p = properties[static_cast<int>(mf)];
if (times_through > 0) {
return p;
}
switch (mf) {
case meta_function::length:
return enrollments.length_operator && !p;
Expand Down Expand Up @@ -11686,8 +11690,7 @@ namespace sol { namespace stack {
static decltype(auto) get(lua_State* L, int index, record& tracking) {
using Tu = meta::unqualified_t<X>;
static constexpr bool is_userdata_of_some_kind
= !std::is_reference_v<
X> && is_container_v<Tu> && std::is_default_constructible_v<Tu> && !is_lua_primitive_v<Tu> && !is_transparent_argument_v<Tu>;
= !std::is_reference_v<X> && is_container_v<Tu> && std::is_default_constructible_v<Tu> && !is_lua_primitive_v<Tu> && !is_transparent_argument_v<Tu>;
if constexpr (is_userdata_of_some_kind) {
if (type_of(L, index) == type::userdata) {
return static_cast<Tu>(stack_detail::unchecked_unqualified_get<Tu>(L, index, tracking));
Expand Down Expand Up @@ -21676,7 +21679,7 @@ namespace sol { namespace u_detail {
// add intrinsics
// this one is the actual meta-handling table,
// the next one will be the one for

int for_each_backing_metatable_calls = 0;
auto for_each_backing_metatable = [&](lua_State* L, submetatable_type smt, reference& fast_index_table) {
// Pointer types, AKA "references" from C++
const char* metakey = nullptr;
Expand Down Expand Up @@ -21750,8 +21753,8 @@ namespace sol { namespace u_detail {
stack::set_field<false, true>(L, detail::base_class_check_key(), reinterpret_cast<void*>(&detail::inheritance<T>::type_check), t.stack_index());
stack::set_field<false, true>(L, detail::base_class_cast_key(), reinterpret_cast<void*>(&detail::inheritance<T>::type_cast), t.stack_index());

auto prop_fx = detail::properties_enrollment_allowed(storage.properties, enrollments);
auto insert_fx = [&L, &t, &storage](meta_function mf, lua_CFunction reg) {
auto prop_fx = detail::properties_enrollment_allowed(for_each_backing_metatable_calls, storage.properties, enrollments);
auto insert_fx = [&L, &t, &storage, &smt](meta_function mf, lua_CFunction reg) {
stack::set_field<false, true>(L, mf, reg, t.stack_index());
storage.properties[static_cast<int>(mf)] = true;
};
Expand Down Expand Up @@ -21793,6 +21796,7 @@ namespace sol { namespace u_detail {
storage.is_using_new_index = true;
}

++for_each_backing_metatable_calls;
fast_index_table = reference(L, t);
t.pop();
};
Expand Down
28 changes: 27 additions & 1 deletion tests/runtime_tests/source/operators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
#include <numeric>
#include <iostream>

struct T {};

TEST_CASE("operators/default", "test that generic equality operators and all sorts of equality tests can be used") {
struct T {};
struct U {
int a;
U(int x = 20) : a(x) {
Expand Down Expand Up @@ -186,6 +187,31 @@ TEST_CASE("operators/default", "test that generic equality operators and all sor
}
}

TEST_CASE("operators/default with pointers", "test that default operations still work when working with reference (pointer) types") {
sol::state lua;
lua.open_libraries(sol::lib::base);

lua.new_usertype<T>("T");

T test;

lua["t1"] = &test;
lua["t2"] = &test;
lua["t3"] = std::unique_ptr<T, no_delete>(&test);
lua["t4"] = std::unique_ptr<T, no_delete>(&test);

lua.script("ptr_test = t1 == t2");
lua.script("ptr_unique_test = t1 == t3");
lua.script("unique_test = t3 == t4");

bool ptr_test = lua["ptr_test"];
bool ptr_unique_test = lua["ptr_unique_test"];
bool unique_test = lua["unique_test"];
REQUIRE(ptr_test);
REQUIRE(ptr_unique_test);
REQUIRE(unique_test);
}

TEST_CASE("operators/call", "test call operator generation") {
struct callable {
int operator()(int a, std::string b) {
Expand Down
7 changes: 7 additions & 0 deletions tests/runtime_tests/source/sol_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,11 @@ struct test_stack_guard {
}
};

struct no_delete {
template <typename P>
void operator()(P) const noexcept {

}
};

#endif // SOL_TESTS_SOL_TEST_HPP

0 comments on commit 5a4d7dc

Please sign in to comment.