diff options
author | AnotherTest <ali.mpfard@gmail.com> | 2021-04-16 16:30:20 +0430 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-04-16 22:26:52 +0200 |
commit | fb814ee7202281e2fc623c686cc15a9ac6117152 (patch) | |
tree | 2b8898dcd8da365665fc75b7bb8a9813a3b655c3 | |
parent | cfad6606f02f5e24763567629799b4f0e9f75058 (diff) | |
download | serenity-fb814ee7202281e2fc623c686cc15a9ac6117152.zip |
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 :^)
-rw-r--r-- | AK/IntrusiveList.h | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/AK/IntrusiveList.h b/AK/IntrusiveList.h index fb183a77f9..b5fd221bd7 100644 --- a/AK/IntrusiveList.h +++ b/AK/IntrusiveList.h @@ -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() |