mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
Merge branch 'develop' into sol3
# Conflicts: # include/single/sol/sol.hpp # include/single/sol/sol_forward.hpp
This commit is contained in:
commit
7e5f2837a8
|
@ -31,8 +31,9 @@
|
|||
#include <functional>
|
||||
#include <utility>
|
||||
#include <cmath>
|
||||
#ifdef SOL_CXX17_FEATURES
|
||||
#ifdef SOL_STD_VARIANT
|
||||
#if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
|
||||
#include <optional>
|
||||
#if defined(SOL_STD_VARIANT) && SOL_STD_VARIANT
|
||||
#include <variant>
|
||||
#endif // SOL_STD_VARIANT
|
||||
#endif // SOL_CXX17_FEATURES
|
||||
|
@ -609,7 +610,26 @@ namespace stack {
|
|||
};
|
||||
|
||||
#if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
|
||||
|
||||
template <typename T, typename C>
|
||||
struct checker<std::optional<T>, type::poly, C> {
|
||||
template <typename Handler>
|
||||
static bool check(lua_State* L, int index, Handler&&, record& tracking) {
|
||||
type t = type_of(L, index);
|
||||
if (t == type::none) {
|
||||
tracking.use(0);
|
||||
return true;
|
||||
}
|
||||
if (t == type::lua_nil) {
|
||||
tracking.use(1);
|
||||
return true;
|
||||
}
|
||||
return stack::check<T>(L, index, no_panic, tracking);
|
||||
}
|
||||
};
|
||||
|
||||
#if defined(SOL_STD_VARIANT) && SOL_STD_VARIANT
|
||||
|
||||
template <typename... Tn, typename C>
|
||||
struct checker<std::variant<Tn...>, type::poly, C> {
|
||||
typedef std::variant<Tn...> V;
|
||||
|
@ -640,7 +660,9 @@ namespace stack {
|
|||
return is_one(std::integral_constant<std::size_t, V_size::value>(), L, index, std::forward<Handler>(handler), tracking);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // SOL_STD_VARIANT
|
||||
|
||||
#endif // SOL_CXX17_FEATURES
|
||||
}
|
||||
} // namespace sol::stack
|
||||
|
|
|
@ -39,6 +39,7 @@
|
|||
#include <string>
|
||||
#if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
|
||||
#include <string_view>
|
||||
#include <optional>
|
||||
#ifdef SOL_STD_VARIANT
|
||||
#include <variant>
|
||||
#endif
|
||||
|
@ -981,6 +982,9 @@ namespace sol {
|
|||
template <typename T>
|
||||
struct lua_type_of<optional<T>> : std::integral_constant<type, type::poly> {};
|
||||
|
||||
template <typename T>
|
||||
struct lua_type_of<std::optional<T>> : std::integral_constant<type, type::poly> {};
|
||||
|
||||
template <>
|
||||
struct lua_type_of<variadic_args> : std::integral_constant<type, type::poly> {};
|
||||
|
||||
|
|
|
@ -36,7 +36,6 @@
|
|||
std::mutex basic_init_require_mutex;
|
||||
|
||||
void basic_initialization_and_lib_open() {
|
||||
|
||||
sol::state lua;
|
||||
try {
|
||||
lua.open_libraries();
|
||||
|
@ -149,6 +148,25 @@ TEST_CASE("utility/optional", "test that shit optional can be round-tripped") {
|
|||
REQUIRE_FALSE(result.valid());
|
||||
};
|
||||
}
|
||||
SECTION("in classes") {
|
||||
sol::state lua;
|
||||
lua.open_libraries(sol::lib::base);
|
||||
|
||||
struct opt_c {
|
||||
std::optional<int> member;
|
||||
};
|
||||
|
||||
auto uto = lua.new_usertype<opt_c>("opt_c",
|
||||
"value", &opt_c::member);
|
||||
|
||||
opt_c obj;
|
||||
lua["obj"] = std::ref(obj);
|
||||
|
||||
lua.safe_script("print(obj.value) obj.value = 20 print(obj.value)");
|
||||
REQUIRE(obj.member == 20);
|
||||
lua.safe_script("print(obj.value) obj.value = nil print(obj.value)");
|
||||
REQUIRE(obj.member == std::nullopt);
|
||||
}
|
||||
#else
|
||||
REQUIRE(true);
|
||||
#endif // C++17
|
||||
|
|
Loading…
Reference in New Issue
Block a user