Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using enum in function argument #30

Closed
eduardodoria opened this issue Sep 16, 2022 · 1 comment
Closed

Using enum in function argument #30

eduardodoria opened this issue Sep 16, 2022 · 1 comment

Comments

@eduardodoria
Copy link

I have this enum:

    enum class Scaling{
        FITWIDTH,
        FITHEIGHT,
        LETTERBOX,
        CROP,
        STRETCH
    };

And these C++ functions:

static void setScalingMode(Scaling scalingMode);
static Scaling getScalingMode();

Lua bindings:

    luabridge::getGlobalNamespace(L)
        .beginNamespace("Scaling")
        .addProperty("FITWIDTH", Scaling::FITWIDTH)
        .addProperty("FITHEIGHT", Scaling::FITHEIGHT)
        .addProperty("LETTERBOX", Scaling::LETTERBOX)
        .addProperty("CROP", Scaling::CROP)
        .addProperty("STRETCH", Scaling::STRETCH)
        .endNamespace();

    luabridge::getGlobalNamespace(L)
        .beginClass<Engine>("Engine")
        .addStaticFunction("setScalingMode", &Engine::setScalingMode)
        .addStaticProperty("scalingMode", &Engine::getScalingMode, &Engine::setScalingMode)
        .endClass();

Code in Lua:

Engine.setScalingMode(Scaling.CROP)   -- not working
Engine.scalingMode = Scaling.CROP  -- not working

None of them are working. Is there anyway to get these enum working in function argument?

@eduardodoria
Copy link
Author

Answering my own question. I did this just by specializing luabridge::Stack.

template <class T>
struct EnumWrapper
{
    static auto push(lua_State* L, T value, std::error_code& ec) -> std::enable_if_t<std::is_enum_v<T>, bool>
    {
        lua_pushnumber(L, static_cast<std::size_t>(value));
        return true;
    }

    static auto get(lua_State* L, int index) -> std::enable_if_t<std::is_enum_v<T>, T>
    {
        return static_cast<T>(lua_tointeger(L, index));
    }
};

namespace luabridge
{
    template<> struct Stack<Supernova::Scaling> : EnumWrapper<Supernova::Scaling>{};
}

And works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant