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

EnumTags: Support non-monotonic enums (e.g. EventCommands) #470

Merged
merged 1 commit into from
Dec 1, 2023

Conversation

Ghabry
Copy link
Member

@Ghabry Ghabry commented Nov 22, 2023

Asked for by @jetrotal to allow a simple way for Event Command lookup by String without creating a seperate lookup in Player.

Please do not look at enum_tags.h. The template foo is difficult. Not an expert in this part of C++. I read various sources to figure some of the complex stuff, e.g. how to allow calling the constructor with "int, string, int string, ..." and storing it into the array.

No ChatGPT code involved, it isn't very good in advanced template programming 😅 (typical ChatGPT issue again: For the simple stuff I do not need it and for the hard stuff it is not helpful 😭 )

The hard part for me (Maybe should learn a bit about parameter packs ...args then this is likely easier to do :D)

template<typename... Args>
explicit constexpr EnumTags(Args const&...args) noexcept {
    AddTag<0>(args...);

    int_type i = 0;
    for (const auto& it : _tags) {
        if (it.value != i) {
            monotonic_from_zero = false;
        }
        ++i;
    }
}

template<std::size_t I, typename... Next>
constexpr void AddTag(E value, const char* name, Next const&...next) noexcept {
    _tags[I] = {int_type(value), name};

    if constexpr (sizeof...(Next) > 0) {
        AddTag<I + 1>(next...);
    }
}

Do not merge. breaks Player. Needs a patch first!

@Ghabry Ghabry added this to the 0.8.1 milestone Nov 22, 2023
@Ghabry Ghabry force-pushed the enum-tags branch 3 times, most recently from cdf8a39 to 0a2de2f Compare November 23, 2023 12:17
@fdelapena fdelapena merged commit 272daa5 into EasyRPG:master Dec 1, 2023
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

2 participants