util/IntrusiveList: add struct IntrusiveListMemberHookTraits
This commit is contained in:

committed by
Max Kellermann

parent
3945a3add9
commit
1fb858e2d7
@@ -33,6 +33,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Cast.hxx"
|
#include "Cast.hxx"
|
||||||
|
#include "MemberPointer.hxx"
|
||||||
|
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
@@ -44,6 +45,7 @@ struct IntrusiveListNode {
|
|||||||
|
|
||||||
class IntrusiveListHook {
|
class IntrusiveListHook {
|
||||||
template<typename T> friend struct IntrusiveListBaseHookTraits;
|
template<typename T> friend struct IntrusiveListBaseHookTraits;
|
||||||
|
template<auto member> friend struct IntrusiveListMemberHookTraits;
|
||||||
template<typename T, typename HookTraits> friend class IntrusiveList;
|
template<typename T, typename HookTraits> friend class IntrusiveList;
|
||||||
|
|
||||||
protected:
|
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>>
|
template<typename T, typename HookTraits=IntrusiveListBaseHookTraits<T>>
|
||||||
class IntrusiveList {
|
class IntrusiveList {
|
||||||
template<typename U>
|
template<typename U>
|
||||||
|
Reference in New Issue
Block a user