From 334accd382cc33dfbfd45a793633351ebaa508dc Mon Sep 17 00:00:00 2001 From: Possseidon Date: Fri, 3 Sep 2021 16:04:40 +0200 Subject: [PATCH] Use SFINAE for Convert template and added another todo. --- dang-lua/include/dang-lua/Convert.h | 57 +++++++++-------------------- 1 file changed, 18 insertions(+), 39 deletions(-) diff --git a/dang-lua/include/dang-lua/Convert.h b/dang-lua/include/dang-lua/Convert.h index ae7c1cb8..f86f55fc 100644 --- a/dang-lua/include/dang-lua/Convert.h +++ b/dang-lua/include/dang-lua/Convert.h @@ -208,7 +208,7 @@ struct UniqueClassInfo { } // namespace detail /// @brief Converts instances of classes and enums to and from Lua as either value or reference. -template +template struct Convert { static_assert(enum_values[std::size(enum_values) - 1] == nullptr, "enum_values is not null-terminated"); static_assert(!std::is_enum_v || std::size(enum_values) > 1, "enum_values is empty"); @@ -657,9 +657,21 @@ struct Convert { static constexpr bool allow_nesting = false; }; +template +struct is_nil : std::false_type {}; + +template +inline constexpr auto is_nil_v = is_nil::value; + +template <> +struct is_nil : std::true_type {}; + +template <> +struct is_nil : std::true_type {}; + /// @brief Converts nil values. template -struct ConvertNil { +struct Convert>> { static constexpr std::optional push_count = 1; static constexpr bool allow_nesting = true; @@ -695,13 +707,6 @@ struct ConvertNil { static void push(lua_State* state, TNil = {}) { lua_pushnil(state); } }; -template <> -struct Convert : ConvertNil {}; -template <> -struct Convert : ConvertNil {}; -template <> -struct Convert : ConvertNil {}; - /// @brief Tag struct for Lua's `fail` value. struct Fail {}; @@ -747,9 +752,7 @@ struct Convert { /// @brief Allows for conversion between Lua numbers and C++ floating point types. template -struct ConvertFloatingPoint { - static_assert(std::is_floating_point_v, "T must be floating point"); - +struct Convert>> { static constexpr std::optional push_count = 1; static constexpr bool allow_nesting = true; @@ -782,21 +785,14 @@ struct ConvertFloatingPoint { static void push(lua_State* state, T value) { lua_pushnumber(state, static_cast(value)); } }; -template <> -struct Convert : ConvertFloatingPoint {}; -template <> -struct Convert : ConvertFloatingPoint {}; -template <> -struct Convert : ConvertFloatingPoint {}; - /// @brief Allows for conversion between Lua integers and C++ integral types. template -struct ConvertIntegral { - static_assert(std::is_integral_v, "T must be integral"); - +struct Convert>> { static constexpr std::optional push_count = 1; static constexpr bool allow_nesting = true; + // TODO: Some special case to allow for conversion between uint64 -> lua_Integer + // TODO: C++20 replace with std::in_range <3 /// @brief Checks, whether the given Lua integer fits into the range of the C++ integral type. static constexpr bool checkRange([[maybe_unused]] lua_Integer value) @@ -872,23 +868,6 @@ struct ConvertIntegral { static void push(lua_State* state, T value) { lua_pushinteger(state, static_cast(value)); } }; -template <> -struct Convert : ConvertIntegral {}; -template <> -struct Convert : ConvertIntegral {}; -template <> -struct Convert : ConvertIntegral {}; -template <> -struct Convert : ConvertIntegral {}; -template <> -struct Convert : ConvertIntegral {}; -template <> -struct Convert : ConvertIntegral {}; -template <> -struct Convert : ConvertIntegral {}; -template <> -struct Convert : ConvertIntegral {}; - /// @brief Allows for conversion between Lua strings and std::string. template <> struct Convert {