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

Allow map and unordered_map with any template parameters. #37

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

verri
Copy link

@verri verri commented Oct 7, 2017

Similar to #12, but it works for the other template parameters as well.
I also provided some test cases and did the same thing for std::map.

@Habbie
Copy link
Collaborator

Habbie commented Oct 7, 2017

Thank you for this!

When I add tests/map.cpp to add_executable(tests and rebuild, this happens:

In file included from /Users/peter/projects/powerdns/luawrapper/tests/maps.cpp:1:
In file included from /Users/peter/projects/powerdns/luawrapper/include/LuaContext.hpp:31:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/algorithm:637:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/type_traits:1464:38: error: implicit instantiation of undefined template 'std::__1::hash<boost::variant<std::__1::basic_string<char>, int> >'
    : public integral_constant<bool, __is_empty(_Tp)> {};
                                     ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/unordered_map:383:18: note: in instantiation of template class 'std::__1::is_empty<std::__1::hash<boost::variant<std::__1::basic_string<char>, int> > >' requested here
          bool = is_empty<_Hash>::value && !__libcpp_is_final<_Hash>::value
                 ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/unordered_map:765:13: note: in instantiation of default argument for '__unordered_map_hasher<boost::variant<std::__1::basic_string<char>, int>, std::__1::__hash_value_type<boost::variant<std::__1::basic_string<char>, int>, int>, std::__1::hash<boost::variant<std::__1::basic_string<char>, int> > >' required here
    typedef __unordered_map_hasher<key_type, __value_type, hasher>   __hasher;
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/peter/projects/powerdns/luawrapper/tests/maps.cpp:54:28: note: in instantiation of template class 'std::__1::unordered_map<boost::variant<std::__1::basic_string<char>, int>, int, std::__1::hash<boost::variant<std::__1::basic_string<char>, int> >, std::__1::equal_to<boost::variant<std::__1::basic_string<char>, int> >, std::__1::allocator<std::__1::pair<const boost::variant<std::__1::basic_string<char>, int>, int> > >' requested here
    const auto b = context.readVariable<std::unordered_map<boost::variant<std::string, int>, int>>("a");
                           ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/type_traits:397:50: note: template is declared here
template <class _Tp> struct _LIBCPP_TEMPLATE_VIS hash;
                                                 ^
In file included from /Users/peter/projects/powerdns/luawrapper/tests/maps.cpp:1:
In file included from /Users/peter/projects/powerdns/luawrapper/include/LuaContext.hpp:48:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/unordered_map:369:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/__hash_table:1952:21: error: called object type 'hasher' (aka 'int') is not a function or function pointer
    size_t __hash = hash_function()(__k);
                    ^~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/__hash_table:1079:14: note: in instantiation of function template specialization 'std::__1::__hash_table<std::__1::__hash_value_type<boost::variant<std::__1::basic_string<char>, int>, int>, int, std::__1::__unordered_map_equal<boost::variant<std::__1::basic_string<char>, int>, std::__1::__hash_value_type<boost::variant<std::__1::basic_string<char>, int>, int>, std::__1::equal_to<boost::variant<std::__1::basic_string<char>, int> >, true>, std::__1::allocator<std::__1::__hash_value_type<boost::variant<std::__1::basic_string<char>, int>, int> > >::__emplace_unique_key_args<boost::variant<std::__1::basic_string<char>, int>, std::__1::pair<const boost::variant<std::__1::basic_string<char>, int>, int> >' requested here
      return __emplace_unique_key_args(_NodeTypes::__get_key(__x), _VSTD::move(__x));
             ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/unordered_map:944:26: note: in instantiation of member function 'std::__1::__hash_table<std::__1::__hash_value_type<boost::variant<std::__1::basic_string<char>, int>, int>, int, std::__1::__unordered_map_equal<boost::variant<std::__1::basic_string<char>, int>, std::__1::__hash_value_type<boost::variant<std::__1::basic_string<char>, int>, int>, std::__1::equal_to<boost::variant<std::__1::basic_string<char>, int> >, true>, std::__1::allocator<std::__1::__hash_value_type<boost::variant<std::__1::basic_string<char>, int>, int> > >::__insert_unique' requested here
        {return __table_.__insert_unique(_VSTD::move(__x));}
                         ^
/Users/peter/projects/powerdns/luawrapper/include/LuaContext.hpp:2753:24: note: in instantiation of member function 'std::__1::unordered_map<boost::variant<std::__1::basic_string<char>, int>, int, std::__1::hash<boost::variant<std::__1::basic_string<char>, int> >, std::__1::equal_to<boost::variant<std::__1::basic_string<char>, int> >, std::__1::allocator<std::__1::pair<const boost::variant<std::__1::basic_string<char>, int>, int> > >::insert' requested here
                result.insert({ key.get(), value.get() });
                       ^
/Users/peter/projects/powerdns/luawrapper/include/LuaContext.hpp:1069:68: note: in instantiation of member function 'LuaContext::Reader<std::__1::unordered_map<boost::variant<std::__1::basic_string<char>, int>, int, std::__1::hash<boost::variant<std::__1::basic_string<char>, int> >, std::__1::equal_to<boost::variant<std::__1::basic_string<char>, int> >, std::__1::allocator<std::__1::pair<const boost::variant<std::__1::basic_string<char>, int>, int> > >, void>::read' requested here
        auto val = Reader<typename std::decay<TReturnType>::type>::read(state, -object.getNum());
                                                                   ^
/Users/peter/projects/powerdns/luawrapper/include/LuaContext.hpp:672:16: note: in instantiation of function template specialization 'LuaContext::readTopAndPop<std::__1::unordered_map<boost::variant<std::__1::basic_string<char>, int>, int, std::__1::hash<boost::variant<std::__1::basic_string<char>, int> >, std::__1::equal_to<boost::variant<std::__1::basic_string<char>, int> >, std::__1::allocator<std::__1::pair<const boost::variant<std::__1::basic_string<char>, int>, int> > > >' requested here
        return readTopAndPop<TType>(mState, PushedObject{mState, 1});
               ^
/Users/peter/projects/powerdns/luawrapper/tests/maps.cpp:54:28: note: in instantiation of function template specialization 'LuaContext::readVariable<std::__1::unordered_map<boost::variant<std::__1::basic_string<char>, int>, int, std::__1::hash<boost::variant<std::__1::basic_string<char>, int> >, std::__1::equal_to<boost::variant<std::__1::basic_string<char>, int> >, std::__1::allocator<std::__1::pair<const boost::variant<std::__1::basic_string<char>, int>, int> > >>' requested here
    const auto b = context.readVariable<std::unordered_map<boost::variant<std::string, int>, int>>("a");
                           ^
In file included from /Users/peter/projects/powerdns/luawrapper/tests/maps.cpp:1:
In file included from /Users/peter/projects/powerdns/luawrapper/include/LuaContext.hpp:48:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/unordered_map:369:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/__hash_table:1952:21: error: called object type 'hasher' (aka 'int') is not a function or function pointer
    size_t __hash = hash_function()(__k);
                    ^~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/__hash_table:1113:16: note: in instantiation of function template specialization 'std::__1::__hash_table<std::__1::__hash_value_type<boost::variant<std::__1::basic_string<char>, int>, int>, int, std::__1::__unordered_map_equal<boost::variant<std::__1::basic_string<char>, int>, std::__1::__hash_value_type<boost::variant<std::__1::basic_string<char>, int>, int>, std::__1::equal_to<boost::variant<std::__1::basic_string<char>, int> >, true>, std::__1::allocator<std::__1::__hash_value_type<boost::variant<std::__1::basic_string<char>, int>, int> > >::__emplace_unique_key_args<boost::variant<std::__1::basic_string<char>, int>, const std::__1::pair<const boost::variant<std::__1::basic_string<char>, int>, int> &>' requested here
        return __emplace_unique_key_args(_NodeTypes::__get_key(__x), __x);
               ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/unordered_map:1414:18: note: in instantiation of member function 'std::__1::__hash_table<std::__1::__hash_value_type<boost::variant<std::__1::basic_string<char>, int>, int>, int, std::__1::__unordered_map_equal<boost::variant<std::__1::basic_string<char>, int>, std::__1::__hash_value_type<boost::variant<std::__1::basic_string<char>, int>, int>, std::__1::equal_to<boost::variant<std::__1::basic_string<char>, int> >, true>, std::__1::allocator<std::__1::__hash_value_type<boost::variant<std::__1::basic_string<char>, int>, int> > >::__insert_unique' requested here
        __table_.__insert_unique(*__first);
                 ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/unordered_map:1269:5: note: in instantiation of function template specialization 'std::__1::unordered_map<boost::variant<std::__1::basic_string<char>, int>, int, std::__1::hash<boost::variant<std::__1::basic_string<char>, int> >, std::__1::equal_to<boost::variant<std::__1::basic_string<char>, int> >, std::__1::allocator<std::__1::pair<const boost::variant<std::__1::basic_string<char>, int>, int> > >::insert<std::__1::__hash_map_const_iterator<std::__1::__hash_const_iterator<std::__1::__hash_node<std::__1::__hash_value_type<boost::variant<std::__1::basic_string<char>, int>, int>, void *> *> > >' requested here
    insert(__u.begin(), __u.end());
    ^
/Users/peter/projects/powerdns/luawrapper/include/LuaContext.hpp:1072:16: note: in instantiation of member function 'std::__1::unordered_map<boost::variant<std::__1::basic_string<char>, int>, int, std::__1::hash<boost::variant<std::__1::basic_string<char>, int> >, std::__1::equal_to<boost::variant<std::__1::basic_string<char>, int> >, std::__1::allocator<std::__1::pair<const boost::variant<std::__1::basic_string<char>, int>, int> > >::unordered_map' requested here
        return val.get();
               ^
/Users/peter/projects/powerdns/luawrapper/include/LuaContext.hpp:672:16: note: in instantiation of function template specialization 'LuaContext::readTopAndPop<std::__1::unordered_map<boost::variant<std::__1::basic_string<char>, int>, int, std::__1::hash<boost::variant<std::__1::basic_string<char>, int> >, std::__1::equal_to<boost::variant<std::__1::basic_string<char>, int> >, std::__1::allocator<std::__1::pair<const boost::variant<std::__1::basic_string<char>, int>, int> > > >' requested here
        return readTopAndPop<TType>(mState, PushedObject{mState, 1});
               ^
/Users/peter/projects/powerdns/luawrapper/tests/maps.cpp:54:28: note: in instantiation of function template specialization 'LuaContext::readVariable<std::__1::unordered_map<boost::variant<std::__1::basic_string<char>, int>, int, std::__1::hash<boost::variant<std::__1::basic_string<char>, int> >, std::__1::equal_to<boost::variant<std::__1::basic_string<char>, int> >, std::__1::allocator<std::__1::pair<const boost::variant<std::__1::basic_string<char>, int>, int> > >>' requested here
    const auto b = context.readVariable<std::unordered_map<boost::variant<std::string, int>, int>>("a");
                           ^
3 errors generated.
make[2]: *** [CMakeFiles/tests.dir/tests/maps.cpp.o] Error 1
make[1]: *** [CMakeFiles/tests.dir/all] Error 2
make: *** [all] Error 2

@verri
Copy link
Author

verri commented Oct 7, 2017

I'll check that.

@verri
Copy link
Author

verri commented Oct 7, 2017

The problem was an unrelated limitation. The code

context.writeVariable("a", std::map<boost::variant<std::string, int>, int>{ /* ... */ });
context.readVariable<std::map<boost::variant<std::string, int>, int>>("a");

never works. Do you have any idea why that happens?

@verri
Copy link
Author

verri commented Oct 7, 2017

In fact, the compilation error was because I forgot the boost::hash<...> stuff.

Then, after fixing that, I realized that writeVariable works when keys are boost::variant, but lua_atpanic is triggered if I try to read the variable back with readVariable.

@Habbie
Copy link
Collaborator

Habbie commented Mar 11, 2021

Hello! Assuming you still care, do you want to rebase this now that I finally merged #38?

@verri
Copy link
Author

verri commented Mar 11, 2021

Hello @Habbie!
Done.

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

Successfully merging this pull request may close these issues.

2 participants