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

Custom optional customization #1318

Open
deadlocklogic opened this issue Feb 27, 2022 · 0 comments
Open

Custom optional customization #1318

deadlocklogic opened this issue Feb 27, 2022 · 0 comments

Comments

@deadlocklogic
Copy link
Contributor

I have a custom optional class optional_ref.
I want the std::string & co™️ to be pushed as sol::string, I tried using a specialization with std::enable_if but the complier gives duplicate definitions.
I am customizing it as following:

template <typename Handler, typename T>
inline bool sol_lua_check(sol::types<optional_ref<T>>, lua_State* L, int index, Handler&& handler,
                          sol::stack::record& tracking) {
    int absolute_index = lua_absindex(L, index);
    bool success = sol::stack::check<sol::optional<std::reference_wrapper<T>>>(L, absolute_index, handler);
    tracking.use(1);
    return success;
}

template <typename T>
optional_ref<T> sol_lua_get(sol::types<optional_ref<T>>, lua_State* L, int index, sol::stack::record& tracking) {
    int absolute_index = lua_absindex(L, index);
    sol::optional<std::reference_wrapper<T>>& a =
        sol::stack::get<sol::optional<std::reference_wrapper<T>>&>(L, absolute_index);
    tracking.use(1);
    if (a.has_value()) {
        return optional_ref<T>(a.value().get());
    } else {
        return nullopt;
    }
}

template <typename T>
int sol_lua_push(sol::types<optional_ref<T>>, lua_State* L, const optional_ref<T>& obj) {
    int amount = 0;
    if (obj.has_value()) {
        amount = sol::stack::push(L, sol::optional<std::reference_wrapper<T>>(std::ref(obj.value())));
    } else {
        amount = sol::stack::push(L, sol::nullopt);
    }
    return amount;
}

Imagine pushing an optional_ref<std::string> to lua and trying print on it! =>failure
Maybe a specialization for all copyable types is fine too.
@ThePhD Maybe this is not sol related? But any help would be appreciated!

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