util/IntrusiveList: add struct IntrusiveListMemberHookTraits

This commit is contained in:
Max Kellermann 2022-06-08 18:01:18 +02:00 committed by Max Kellermann
parent 3945a3add9
commit 1fb858e2d7
1 changed files with 30 additions and 0 deletions

View File

@ -33,6 +33,7 @@
#pragma once
#include "Cast.hxx"
#include "MemberPointer.hxx"
#include <iterator>
#include <type_traits>
@ -44,6 +45,7 @@ struct IntrusiveListNode {
class IntrusiveListHook {
template<typename T> friend struct IntrusiveListBaseHookTraits;
template<auto member> friend struct IntrusiveListMemberHookTraits;
template<typename T, typename HookTraits> friend class IntrusiveList;
protected:
@ -145,6 +147,34 @@ struct IntrusiveListBaseHookTraits {
}
};
/**
* For classes which embed #IntrusiveListHook as member.
*/
template<auto member>
struct IntrusiveListMemberHookTraits {
using T = MemberPointerContainerType<decltype(member)>;
using _Hook = MemberPointerType<decltype(member)>;
using Hook = typename IntrusiveListHookDetection<_Hook>::type;
static constexpr T *Cast(IntrusiveListNode *node) noexcept {
auto &hook = Hook::Cast(*node);
return &ContainerCast(hook, member);
}
static constexpr const T *Cast(const IntrusiveListNode *node) noexcept {
const auto &hook = Hook::Cast(*node);
return &ContainerCast(hook, member);
}
static constexpr auto &ToHook(T &t) noexcept {
return t.*member;
}
static constexpr const auto &ToHook(const T &t) noexcept {
return t.*member;
}
};
template<typename T, typename HookTraits=IntrusiveListBaseHookTraits<T>>
class IntrusiveList {
template<typename U>