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

Is it possible to add support for enum key based std::map? #27

Closed
Zvicii opened this issue Jul 6, 2022 · 2 comments
Closed

Is it possible to add support for enum key based std::map? #27

Zvicii opened this issue Jul 6, 2022 · 2 comments

Comments

@Zvicii
Copy link

Zvicii commented Jul 6, 2022

Currently Im using the following workaround.

// std::map<enum, T>
template <typename TK, typename TV>
struct is_xpack_xtype<std::map<TK, TV>> {
    static bool const value = true;
};

template <class OBJ, typename TK, typename TV>
bool xpack_xtype_decode(OBJ& obj, const char* key, std::map<TK, TV>& val, const Extend* ext) {
    if constexpr (std::is_enum_v<TK>) {
        std::map<int64_t, TV> tmp;
        if (!obj.decode(key, tmp, ext)) {
            return false;
        }
        val.clear();
        for (auto&& item : tmp) {
            val[item.first] = item.second;
        }
        return true;
    } else {
        static_assert(false, "std::map with this key type is not supported to decode");
        return false;
    }
}

template <class OBJ, typename TK, typename TV>
bool xpack_xtype_encode(OBJ& obj, const char* key, const std::map<TK, TV>& val, const Extend* ext) {
    if constexpr (std::is_enum_v<TK>) {
        std::map<TK, TV> tmp;
        for (auto&& item : val) {
            tmp[item.first] = item.second;
        }
        return obj.encode(key, tmp, ext);
    } else {
        static_assert(false, "std::map with this key type is not supported to encode");
        return false;
    }
}
@Zvicii Zvicii changed the title Is it possible to add support for enum key based std::map Is it possible to add support for enum key based std::map? Jul 6, 2022
@xyz347
Copy link
Owner

xyz347 commented Jul 9, 2022

@Zvicii
Copy link
Author

Zvicii commented Jul 12, 2022

try this branch https://github.com/xyz347/xpack/tree/map-enum-key

works like a charm, thx!

@Zvicii Zvicii closed this as completed Jul 12, 2022
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

2 participants