mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 18:07:34 +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:
parent
cfad6606f0
commit
fb814ee720
1 changed files with 8 additions and 1 deletions
|
@ -27,6 +27,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/Assertions.h>
|
||||
#include <AK/BitCast.h>
|
||||
|
||||
namespace AK {
|
||||
|
||||
|
@ -269,7 +270,13 @@ inline typename IntrusiveList<T, member>::ConstIterator IntrusiveList<T, member>
|
|||
template<class T, IntrusiveListNode T::*member>
|
||||
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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue