1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 12:07:45 +00:00

AK: Avoid the unnecessarily confusing bunch of casts in IntrusiveList

And use bit_cast instead.
Also explain what it does, because it's not at all trivial
to understand :^)
This commit is contained in:
AnotherTest 2021-04-16 16:30:20 +04:30 committed by Andreas Kling
parent cfad6606f0
commit fb814ee720

View file

@ -27,6 +27,7 @@
#pragma once #pragma once
#include <AK/Assertions.h> #include <AK/Assertions.h>
#include <AK/BitCast.h>
namespace AK { namespace AK {
@ -269,7 +270,13 @@ inline typename IntrusiveList<T, member>::ConstIterator IntrusiveList<T, member>
template<class T, IntrusiveListNode T::*member> template<class T, IntrusiveListNode T::*member>
inline T* IntrusiveList<T, member>::node_to_value(IntrusiveListNode& node) inline T* IntrusiveList<T, member>::node_to_value(IntrusiveListNode& node)
{ {
return (T*)((char*)&node - ((char*)&(((T*)nullptr)->*member) - (char*)nullptr)); // Note: Since this might seem odd, here's an explanation on what this function actually does:
// `node` is a reference that resides in some part of the actual value (of type T), the
// placement (i.e. offset) of which is described by the pointer-to-data-member parameter
// named `member`.
// This function effectively takes in the address of the data member, and returns the address
// of the value (of type T) holding that member.
return bit_cast<T*>(bit_cast<unsigned char*>(&node) - bit_cast<unsigned char*>(member));
} }
inline IntrusiveListNode::~IntrusiveListNode() inline IntrusiveListNode::~IntrusiveListNode()