Composite keys and IxModel

You find a bug using QxOrm library and you know how to fix it : submit a patch on this forum

Composite keys and IxModel

Postby jimmytaker » Thu Jan 26, 2017 11:54 pm

Using composite keys (tuple as a key) in models does not work. It complains on not missing a qHash function. Here is what I put at the top of IxModel.h to fix the issue (found the solution on stackoverflow for std, and refitted it to qHash). I am sure Lio will find a better place for this snippet than IxModel.h for the next version.
Code: Select all
namespace
{

    // Code from boost
    // Reciprocal of the golden ratio helps spread entropy
    //     and handles duplicates.
    // See Mike Seymour in magic-numbers-in-boosthash-combine:
    //     http://stackoverflow.com/questions/4948780

    template <class T>
    inline void hash_combine(uint& seed, T const& v)
    {
        seed ^= qHash(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
    }

    // Recursive template code derived from Matthieu M.
    template <class Tuple, uint Index = std::tuple_size<Tuple>::value - 1>
    struct HashValueImpl
    {
        static void apply(uint& seed, Tuple const& tuple)
        {
            HashValueImpl<Tuple, Index - 1>::apply(seed, tuple);
            hash_combine(seed, std::get<Index>(tuple));
        }
    };

    template <class Tuple>
    struct HashValueImpl<Tuple, 0>
    {
        static void apply(uint& seed, Tuple const& tuple)
        {
            hash_combine(seed, std::get<0>(tuple));
        }
    };
}

template <typename ... TT>
inline uint qHash(std::tuple<TT...> const& tt)
{
    uint seed = 0;
    HashValueImpl<std::tuple<TT...> >::apply(seed, tt);
    return seed;
}
jimmytaker
 
Posts: 19
Joined: Fri Dec 23, 2016 11:04 pm

Re: Composite keys and IxModel

Postby qxorm » Mon Jan 30, 2017 8:16 am

Hello,

Using composite keys (tuple as a key) in models does not work. It complains on not missing a qHash function.

I don't understand what is the problem exactly : could you please provide a small example to reproduce it ?

found the solution on stackoverflow for std, and refitted it to qHash

What is the link please to your stackoverflow solution ?
Maybe it could help to understand what is the problem exactly.

Thx !
qxorm
Site Admin
 
Posts: 481
Joined: Mon Apr 12, 2010 7:45 am


Return to QxOrm - Submit a patch

Who is online

Users browsing this forum: No registered users and 2 guests