// https://github.com/kunitoki/LuaBridge3 // Copyright 2026, kunitoki // SPDX-License-Identifier: MIT #pragma once #include "detail/Stack.h" #include #include namespace luabridge { //================================================================================================= /** * @brief Stack specialization for `std::variant`. */ template struct Stack> { using Type = std::variant; [[nodiscard]] static Result push(lua_State* L, const Type& variant) { return std::visit([L](const auto& value) { return Stack>::push(L, value); }, variant); } [[nodiscard]] static TypeResult get(lua_State* L, int index) { return tryGet(L, index); } [[nodiscard]] static bool isInstance(lua_State* L, int index) { return (Stack::isInstance(L, index) || ...); } private: template [[nodiscard]] static TypeResult tryGet(lua_State* L, int index) { if (auto value = Stack::get(L, index)) { #if defined(__GNUC__) && !defined(__clang__) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" #endif return Type{ std::in_place_type, std::move(*value) }; #if defined(__GNUC__) && !defined(__clang__) #pragma GCC diagnostic pop #endif } if constexpr (sizeof...(Rest) > 0) return tryGet(L, index); return makeErrorCode(ErrorCode::InvalidTypeCast); } }; /** * @brief Stack specialization for `std::monostate`. */ template <> struct Stack { using Type = std::monostate; [[nodiscard]] static Result push(lua_State* L, const Type&) { #if LUABRIDGE_SAFE_STACK_CHECKS if (! lua_checkstack(L, 1)) return makeErrorCode(ErrorCode::LuaStackOverflow); #endif lua_pushnil(L); return {}; } [[nodiscard]] static TypeResult get(lua_State* L, int index) { if (lua_isnoneornil(L, index)) return Type{}; return makeErrorCode(ErrorCode::InvalidTypeCast); } [[nodiscard]] static bool isInstance(lua_State* L, int index) { return lua_isnoneornil(L, index); } }; } // namespace luabridge